Created
February 21, 2013 15:35
-
-
Save stanistan/5005514 to your computer and use it in GitHub Desktop.
A closure for "sandboxing" settings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// usage | |
withSetting::ini('display_errors', 'off', function() { | |
//.. do things | |
}); | |
withSetting::timezone('America/New_York', function() { | |
//.. do things | |
}); | |
// or if there is a class somewhere | |
// you can pass args to the method | |
$class = new SomeClass; | |
withSetting::timezone($timezone, [$class, $method], $arg1, $arg2, $arg3); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class withSetting { | |
/** | |
* Returns a closure bound to the getter and setter of the setting. | |
* | |
* @param callable $getter | |
* @param callable $setter -- function that accepts one argument | |
* | |
* @return callable | |
*/ | |
public static function withFns($getter, $setter) | |
{ | |
return function($value, $fn) use($getter, $setter) { | |
// store and set | |
$old_val = $getter(); | |
$setter($value); | |
// reset fn | |
$reset = function() use($setter, $value) { | |
$setter($value); | |
}; | |
try { | |
$args = array_slice(func_get_args(), 2); | |
$response = call_user_func_array($fn, $args); | |
} catch (Exception $e) { | |
$reset(); | |
throw $e; | |
} | |
$reset(); | |
return $response; | |
}; | |
} | |
/** | |
* Execute a function in a specfiic timezone, it gets set as the default. | |
* | |
* @param string $value the timezone, example UTC | |
* @param callable $fn the continuation | |
* @param mixed ... arguments to pass to the continuation | |
* | |
* @return mixed whatever $fn returns | |
*/ | |
public static function timezone($value, $fn /*...args*/) | |
{ | |
$withSetting = self::withFns( | |
'date_default_timezone_get', | |
'date_default_timezone_set' | |
); | |
$args = func_get_args(); | |
return call_user_func_array($withSetting, $args); | |
} | |
/** | |
* Execute a functino in with a given ini setting | |
* | |
* Usage: | |
* withSetting::ini('display_errors', 'off', function() { ... }); | |
* | |
* @param string $name name of setting for ini_get/ini_set | |
* @param mixed $value the value you want to set it to | |
* @param callable $fn | |
* @param mixed ... | |
* | |
* @return mixed | |
*/ | |
public static function ini($name, $value, $fn /*...args*/) | |
{ | |
list($get, $set) = static::iniGetSet($name); | |
$withSetting = self::withFns($get, $set); | |
// getters and setters bound to name | |
$args = array_slice(func_get_args(), 1); | |
return call_user_func_array($withSetting, $args); | |
} | |
/** | |
* Usage: list($get, $set) = iniGetSet('display_errors'); | |
* @param string $name | |
* | |
* @return array of closures a getter and setter | |
*/ | |
protected static function iniGetSet($name) | |
{ | |
return array( | |
function() use($name) { | |
return ini_get($name); | |
}, | |
function($v) use($name) { | |
ini_set($name, $v); | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment