https://github.com/icio/scope
Python's with statement for PHP
https://github.com/icio/scope
Last synced: about 1 year ago
JSON representation
Python's with statement for PHP
- Host: GitHub
- URL: https://github.com/icio/scope
- Owner: icio
- Created: 2014-08-26T08:39:30.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2014-09-25T20:55:43.000Z (over 11 years ago)
- Last Synced: 2025-01-28T00:43:00.232Z (over 1 year ago)
- Language: PHP
- Size: 203 KB
- Stars: 1
- Watchers: 4
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Scope [](https://travis-ci.org/icio/scope)
Scope offers a mechanism for preparing global environments and containers for the duration of a callback, keeping state mutation and clean-up to only declaration in your code. Inspired by Python's native [with](https://docs.python.org/2/reference/datamodel.html#with-statement-context-managers).
For example, instead of doing:
```php
global $x;
$xBackup = $x;
$x = $xNew;
$done = do_it();
$x = $xBackup;
```
with `KeyState` you can set an Array key state:
```php
(new KeyState($GLOBALS, array("x" => $newX)))->call('do_it');
```
Currently available scopes help with:
* Collecting multiple scopes together, with `Scope`;
* Setting temporary values on arrays, with `KeyState`;
* Capturing function output, with `Buffer`;
* Changing into a new directory, with `WorkingDirectory`; and
* Worrying about different error levels, with `ErrorReporting`.
Sometimes you'll want to hop in and out of a given scope, providing default context by following the changes incurred by your actions within. The scopes can optionally account for this. The `Buffer` can collect the output across multiple sessions; the `WorkingDirectory` can go back to the directory it left upon re-entry; the `KetState` can track the value of keys, should they be changed in-scope; and the `ErrorReporting` can track any changes to the reporting. See the spec tests for details.
All entered scopes are passed into the callback as function arguments. For example, when working with output buffers you can capture and return the output as so:
```php
$output = (new Scope(
new Buffer(Buffer::CLEAN),
new WorkingDirectory("./old")
))->call(function(Buffer $buffer) {
require "old-script.php";
return $buffer->getContents();
});
```
If exceptions are thrown during the callback then we close the scopes and re-throw the exception. If exceptions are thrown by the scopes then we clean up any already entered and re-throw the exception. Exceptions thrown whilst trying to leave a scope prevent the clean-up of others (which should perhaps change).