static function load($filename) { self::$stream = IO_FS::FileStream($filename); while ($line = self::get()) { $line = trim($line); if ($m = Core_Regexps::match_with_results('{^!DUMP\\s+([^\\s]+)(.*)$}', $line)) { $module = trim($m[1]); $parms = trim($m[2]); $dumper = trim(self::$dumpers[$module]); if ($dumper == '') { throw new CMS_Dumps_UnknownDumperException("Unknown dumper: {$module}"); } $dumper_class_name = str_replace('.', '_', $dumper); if (!class_exists($dumper_class_name)) { Core::load($dumper); } $class = new ReflectionClass($dumper_class_name); $class->setStaticPropertyValue('stream', self::$stream); $method = $class->getMethod('load'); $rc = $method->invokeArgs(null, array($parms)); if (trim($rc) != '') { return $rc; } } } return true; }
public function __construct() { // TODO: caching of parsed ll-config // TODO: check if file is readable // read config stuff $reflectedClass = new \ReflectionClass('\\Slimpd\\modules\\localization\\Localization'); $reflectedClass->setStaticPropertyValue('lang', parse_ini_file(APP_ROOT . "config/i18n.ini", FALSE)); }
private function setValue(&$val) { yTest_debugCC('setValue ' . $this->className . '::' . $this->propertyName . ' = ' . var_export($val, true)); if ($this->isPublic) { $refl = new ReflectionClass($this->className); $refl->setStaticPropertyValue($this->propertyName, $val); } else { call_user_func(array($this->className, yTest_AddPublicAccessors::getSetterName($this->className, $this->propertyName)), $val); } }
/** * Add each provided value, as long as it exists as a property - i.e ignore others! * @param array $arr_values */ private function fill_values(array $arr_values) { $reflection_class = new \ReflectionClass(get_called_class()); foreach ($arr_values as $label => $value) { if (property_exists(get_called_class(), $label)) { if (isset($arr_values[$label])) { $reflection_class->setStaticPropertyValue($label, $arr_values[$label]); } } } }
public static function enumerate() { if (PHP_VERSION_ID < 50300) { $class = callerName(); } else { $class = get_called_class(); } $ref = new ReflectionClass($class); $statics = $ref->getStaticProperties(); foreach ($statics as $name => $value) { $ref->setStaticPropertyValue($name, new $class($value)); } }
static function testing() { $ref = new ReflectionClass('Test'); foreach (array('pub', 'pro', 'pri') as $name) { try { var_dump($ref->getStaticPropertyValue($name)); var_dump($ref->getStaticPropertyValue($name)); $ref->setStaticPropertyValue($name, 'updated'); var_dump($ref->getStaticPropertyValue($name)); } catch (Exception $e) { echo "EXCEPTION\n"; } } }
static function __static() { if (__CLASS__ === ($class = get_called_class())) { return; } // Automatically initialize this enum's public static members $i = 0; $c = new ReflectionClass($class); foreach ($c->getStaticProperties() as $name => $prop) { if (NULL !== $prop) { $i = $prop; } $c->setStaticPropertyValue($name, $c->newInstance($i++, $name)); } }
function initLogger() { $loggerName = "log"; // Iterate over all declared classes $classes = get_declared_classes(); foreach ($classes as $class) { $reflection = new ReflectionClass($class); // If the class is internally defined by PHP or has no property called "logger", skip it. if ($reflection->isInternal() || !$reflection->hasProperty($loggerName)) { continue; } // Get information regarding the "logger" property of this class. $property = new ReflectionProperty($class, $loggerName); // If the "logger" property is not static or not public, then it is not the one we are interested in. Skip this class. if (!$property->isStatic() || !$property->isPublic()) { continue; } // Initialize the logger for this class. $reflection->setStaticPropertyValue($loggerName, getLogger()); } }
public function each($cb) { $b = null; foreach ($this as $row) { if ($this->set_ref) { $ref = array(); foreach ($row as $i => $name) { $ref[$name] = $i; } $reflectedClass = new \ReflectionClass('Row'); $reflectedClass->setStaticPropertyValue('r', $ref); $this->set_ref = false; } else { $b = $cb(new Row($row)); if ($b === false) { break; } } } $this->set_ref = true; }
/** * Allows easy setting & backing up of enviroment config * * Option types are checked in the following order: * * * Server Var * * Static Variable * * Config option * * @param array $environment List of environment to set */ function setEnvironment(array $environment) { if (!count($environment)) { return FALSE; } foreach ($environment as $option => $value) { $backup_needed = !array_key_exists($option, $this->environmentBackup); // Handle changing superglobals if (in_array($option, array('_GET', '_POST', '_SERVER', '_FILES'))) { // For some reason we need to do this in order to change the superglobals global ${$option}; if ($backup_needed) { $this->environmentBackup[$option] = ${$option}; } // PHPUnit makes a backup of superglobals automatically ${$option} = $value; } elseif (strpos($option, '::$') !== FALSE) { list($class, $var) = explode('::$', $option, 2); $class = new ReflectionClass($class); if ($backup_needed) { $this->environmentBackup[$option] = $class->getStaticPropertyValue($var); } $class->setStaticPropertyValue($var, $value); } elseif (preg_match('/^[A-Z_-]+$/', $option) or isset($_SERVER[$option])) { if ($backup_needed) { $this->environmentBackup[$option] = isset($_SERVER[$option]) ? $_SERVER[$option] : ''; } $_SERVER[$option] = $value; } else { if ($backup_needed) { $this->environmentBackup[$option] = Kohana::config($option); } list($group, $var) = explode('.', $option, 2); Kohana::config($group)->set($var, $value); } } }
/** * @covers FOF30\Layout\LayoutFile::getPath * * @dataProvider FOF30\Tests\Layout\LayoutFileTestProvider::getTestGetPath * * @param string $layoutId The layout to load * @param array $platformSetup Platform setup (baseDirs, template, templateSuffixes) * @param string $expectedPath The expected path which should be returned * @param string $message Failure message */ public function testGetPath($layoutId, $platformSetup, $expectedPath, $message) { // Set up the platform $defaultPlatformSetup = array('baseDirs' => null, 'template' => null, 'templateSuffixes' => null); if (!is_array($platformSetup)) { $platformSetup = array(); } $platformSetup = array_merge($defaultPlatformSetup, $platformSetup); $reflector = new \ReflectionClass('FOF30\\Tests\\Helpers\\TestJoomlaPlatform'); foreach ($platformSetup as $k => $v) { $reflector->setStaticPropertyValue($k, $v); } unset($reflector); // Set up a fake options JRegistry object $fakeOptions = new \JRegistry(array('option' => 'com_foobar', 'client' => 0)); $fakeBase = realpath(__DIR__ . '/../_data/layout/base'); // Create the layout file object $layoutFile = new LayoutFile($layoutId, $fakeBase, $fakeOptions); $layoutFile->container = static::$container; // Call the protected method. Dirty, but that's what we have to test without loading and displaying an actual // PHP file. $actual = ReflectionHelper::invoke($layoutFile, 'getPath'); $this->assertEquals($expectedPath, $actual, $message); }
/** * {@inheritdoc} */ public function setStaticPropertyValue($propertyName, $value) { $this->reflectionClass->setStaticPropertyValue($propertyName, $value); }
/** * Set static variable on a class * * @param string $classname * @param string $name * @param mixed $value * @return Robo47_Application_Resource_Object *Provides Fluent Interface* */ protected function _setStaticVariable($classname, $name, $value) { $ref = new ReflectionClass($classname); $ref->setStaticPropertyValue($name, $value); return $this; }
<?php class C { public static $x; } $rc = new ReflectionClass('C'); try { var_dump($rc->setStaticPropertyValue("x", "default value", 'blah')); } catch (Exception $e) { echo $e->getMessage() . "\n"; } try { var_dump($rc->setStaticPropertyValue()); } catch (Exception $e) { echo $e->getMessage() . "\n"; } try { var_dump($rc->setStaticPropertyValue(null)); } catch (Exception $e) { echo $e->getMessage() . "\n"; } try { var_dump($rc->setStaticPropertyValue(null, null)); } catch (Exception $e) { echo $e->getMessage() . "\n"; } try { var_dump($rc->setStaticPropertyValue(1.5, 'def')); } catch (Exception $e) { echo $e->getMessage() . "\n";
/** * property factory set/get properties on passed class which can either be a instance of class * or class name as string. pass the property name in the second parameter and set the third * parameter to a the value to set to blank to read the property value. * * @error 10604 * @param string|object $class expects the class name as string or class instance * @param string $property expects the property name * @param null|mixed $value expects if set a value to set or none to get the value * @return mixed|null * @throws Xapp_Error */ public static final function propertyFactory($class, $property, $value = 'NIL') { $prop = null; $obj = null; $array = null; $return = null; try { $_class = is_object($class) ? get_class($class) : $class; $property = trim($property); $jailbreak = func_num_args() === 4 && (bool) func_get_arg(3) ? true : false; if (!is_object($class)) { $class = (string) $class; } $obj = new ReflectionClass($class); if ($obj->hasProperty($property)) { $prop = $obj->getProperty($property); if ($prop->isStatic()) { $props = $obj->getStaticProperties(); if (array_key_exists($property, $props)) { if ($value !== 'NIL') { if ($prop->isProtected() && $jailbreak) { $prop->setAccessible(true); $prop->setValue($class, $value); $prop->setAccessible(false); } else { $obj->setStaticPropertyValue($property, $value); } } else { return $props[$property]; } } else { throw new Xapp_Error(xapp_sprintf(_("static property: %s does not exist in class: %s"), $property, $_class), 1060404); } } else { if (is_object($class)) { if ($value !== 'NIL') { if ($prop->isProtected() && $jailbreak) { $prop->setAccessible(true); $prop->setValue($class, $value); $prop->setAccessible(false); } else { $prop->setValue($class, $value); } } else { if ($prop->isProtected() && $jailbreak) { $prop->setAccessible(true); $value = $prop->getValue($class); $prop->setAccessible(false); $return = $value; } else { $return = $prop->getValue($class); } } $obj = null; $prop = null; return $return; } else { throw new Xapp_Error(xapp_sprintf(_("unable to set/get non static property for class: %s passed as string"), $_class), 1060403); } } } else { throw new Xapp_Error(xapp_sprintf(_("class: %s or property: %s does not exist"), $_class, $property), 1060402); } } catch (ReflectionException $e) { throw new Xapp_Error($e, 1060401); } return null; }
public function onEnable() { $this->getLogger()->info(TextFormat::AQUA . "Loading Zombie Game..."); if (!file_exists($this->getDataFolder())) { $this->getLogger()->info(TextFormat::YELLOW . "Created Data Folder!"); mkdir($this->getDataFolder()); } if (!file_exists($this->getDataFolder() . "config.yml")) { (new Config($this->getDataFolder() . "config.yml", Config::YAML, yaml_parse(stream_get_contents($this->getResource("config.yml")))))->save(); $this->getLogger()->info(TextFormat::YELLOW . "Extracted config.yml!"); } if (!file_exists($this->getDataFolder() . "translation_en.yml")) { (new Config($this->getDataFolder() . "translation_en.yml", Config::YAML, yaml_parse(stream_get_contents($this->getResource("translation_en.yml")))))->save(); $this->getLogger()->info(TextFormat::YELLOW . "Extracted translation_en.yml!"); } if (!file_exists($this->getDataFolder() . "translation_ko.yml")) { (new Config($this->getDataFolder() . "translation_ko.yml", Config::YAML, yaml_parse(stream_get_contents($this->getResource("translation_ko.yml")))))->save(); $this->getLogger()->info(TextFormat::YELLOW . "Extracted translation_ko.yml!"); } if (!file_exists($this->getDataFolder() . "translation_ru.yml")) { (new Config($this->getDataFolder() . "translation_ru.yml", Config::YAML, yaml_parse(stream_get_contents($this->getResource("translation_ru.yml")))))->save(); $this->getLogger()->info(TextFormat::YELLOW . "Extracted translation_ru.yml!"); } $this->config = (new Config($this->getDataFolder() . "config.yml", Config::YAML))->getAll(); if (!isset($this->config["language"])) { $this->getLogger()->error(TextFormat::RED . "Language Not Found!"); $this->getServer()->getPluginManager()->disablePlugin($this); } if (!file_exists($this->getDataFolder() . "translation_" . $this->config["language"] . ".yml")) { $this->getLogger()->error(TextFormat::RED . "Language Not Found!"); $this->getServer()->getPluginManager()->disablePlugin($this); } $this->getLogger()->info(TextFormat::AQUA . "Loading Translation Pack : " . $this->config["language"]); $this->translation = (new Config($this->getDataFolder() . "translation_" . $this->config["language"] . ".yml", Config::YAML))->getAll(); $this->getLogger()->info(TextFormat::AQUA . $this->getTranslation("DONE_LOADING_TRANSLATION", $this->config["language"])); $managerClass = new \ReflectionClass(GameManager::class); $geniusClass = new \ReflectionClass(GameGenius::class); $this->getLogger()->info(TextFormat::AQUA . $this->getTranslation("LOADING_CONFIG")); foreach ($this->config as $key => $value) { if ($managerClass->hasProperty($key)) { $managerClass->setStaticPropertyValue($key, $value); $this->getLogger()->info($this->getTranslation("FOUND_CONFIG", $key, "GameManager")); } if ($geniusClass->hasProperty($key)) { $geniusClass->setStaticPropertyValue($key, $value); $this->getLogger()->info($this->getTranslation("FOUND_CONFIG", $key, "GameGenius")); } } $this->getLogger()->info(TextFormat::AQUA . $this->getTranslation("DONE_LOADING_CONFIG")); if (!isset($this->config["UPDATE_CHECK"])) { $this->config["UPDATE_CHECK"] = true; } if ($this->config["UPDATE_CHECK"]) { try { $recentVersion = yaml_parse(Utils::getURL("https://raw.githubusercontent.com/HelloWorld017/ZombieGame/master/plugin.yml"))["version"]; if ($recentVersion !== $this->getDescription()->getVersion()) { $this->getLogger()->info(TextFormat::RED . $this->getTranslation("VERSION_DIFFER")); } else { $this->getLogger()->info(TextFormat::AQUA . $this->getTranslation("RECENT_VERSION")); } } catch (\Exception $e) { $this->getLogger()->info(TextFormat::RED . $this->getTranslation("CHECK_VERSION_FAILED")); } } $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->getLogger()->info(TextFormat::AQUA . $this->getTranslation("DONE_LOADING_GAME")); $this->getServer()->getScheduler()->scheduleRepeatingTask(new GameTickTask($this), 1); $this->getServer()->getScheduler()->scheduleRepeatingTask(new SendPopupTask($this), 1); }
/** * @covers FOF30\Layout\LayoutHelper::render * * @dataProvider FOF30\Tests\Layout\LayoutHelperTestProvider::getTestRender * * @param string $layoutId The layout to load * @param array $platformSetup Platform setup (baseDirs, template, templateSuffixes) * @param string $expectedOutput The expected output which should be returned * @param string $message Failure message */ public function testRenderDefaultBase($layoutId, $platformSetup, $expectedOutput, $message) { // Set up the platform $defaultPlatformSetup = array('baseDirs' => null, 'template' => null, 'templateSuffixes' => null); if (!is_array($platformSetup)) { $platformSetup = array(); } $platformSetup = array_merge($defaultPlatformSetup, $platformSetup); $reflector = new \ReflectionClass('FOF30\\Tests\\Helpers\\TestJoomlaPlatform'); foreach ($platformSetup as $k => $v) { $reflector->setStaticPropertyValue($k, $v); } unset($reflector); // Set up a fake base $fakeBase = realpath(__DIR__ . '/../_data/layout/base'); // Create the layout file object LayoutHelper::$defaultBasePath = $fakeBase; $actual = LayoutHelper::render(self::$container, $layoutId); $this->assertEquals($expectedOutput, $actual, $message); }
} class B extends A { private static $privateOverridden = "changed private"; protected static $protectedOverridden = "changed protected"; public static $publicOverridden = "changed public"; } echo "Set static values in A:\n"; $rcA = new ReflectionClass('A'); $rcA->setStaticPropertyValue("AprivateOverridden", "new value 1"); $rcA->setStaticPropertyValue("*protectedOverridden", "new value 2"); $rcA->setStaticPropertyValue("publicOverridden", "new value 3"); print_r($rcA->getStaticProperties()); echo "\nSet static values in B:\n"; $rcB = new ReflectionClass('B'); $rcB->setStaticPropertyValue("AprivateOverridden", "new value 4"); $rcB->setStaticPropertyValue("BprivateOverridden", "new value 5"); $rcB->setStaticPropertyValue("*protectedOverridden", "new value 6"); $rcB->setStaticPropertyValue("publicOverridden", "new value 7"); print_r($rcA->getStaticProperties()); print_r($rcB->getStaticProperties()); echo "\nSet non-existent values from A with no default value:\n"; try { var_dump($rcA->setStaticPropertyValue("protectedOverridden", "new value 8")); echo "you should not see this"; } catch (Exception $e) { echo $e->getMessage() . "\n"; } try { var_dump($rcA->setStaticPropertyValue("privateOverridden", "new value 9")); echo "you should not see this";
/** * @param string $property * @param mixed $value * @return mixed */ public function __set($property, $value) { $class = new \ReflectionClass($this->_staticClassName); $class->setStaticPropertyValue($property, $value); return $value; }
/** * Sets an object class property * * @param type $property * @param type $value */ public static function set($property, $value = NULL) { $object = new \ReflectionClass(Object::class); $object->setStaticPropertyValue($property, $value); return true; }
/** * Plugin entry point. * @param string $file This should be __FILE__ from the main plugin file * @return bool true if the instance was generated for the first time */ public static function run($file, $name = '', $slug = '') { if (!defined('ABSPATH')) { die; } $data = get_file_data($file, array('Plugin Name', 'Version', 'Text Domain', 'Domain Path')); $plugin_name = array_shift($data); $version = array_shift($data); $text_domain = array_shift($data); $domain_path = array_shift($data); if ($name == '') { $name = $plugin_name; } if ($slug == '') { $slug = $text_domain; } $class = new ReflectionClass($name . '_Plugin'); if (!is_null($class->getStaticPropertyValue('instance', null))) { return false; } $instance = $class->newInstance($file, $name, $slug, $version, $text_domain, $domain_path); // Setup as a singleton for global access if ($class->hasProperty('instance')) { $class->setStaticPropertyValue('instance', $instance); } else { // If the child doesn't have an instance property, we'll create a global variable. $GLOBALS[$slug . '_plugin'] = $instance; } $instance->superPreinit(); return true; }
# Very verbose. #var_dump( $rb->getProperties(); print "\n"; print "--- getProperty() ---\n"; # Very verbose. #var_dump( $rb->getProperty('p0'); print "\n"; print "--- getStaticProperties() ---\n"; # Very verbose. #var_dump( $rb->getStaticProperties(); print "\n"; print "--- setStaticPropertyValue() ---\n"; var_dump($rb->setStaticPropertyValue('s0', 'new value for s0')); print "\n"; print "--- getStaticPropertyValue() ---\n"; var_dump($rb->getStaticPropertyValue('s0')); var_dump($rb->getStaticPropertyValue('s4')); print "\n"; print "--- hasConstant() ---\n"; var_dump($rb->hasConstant('C0')); var_dump($rb->hasConstant('C4')); print "\n"; print "--- hasMethod() ---\n"; var_dump($rb->hasMethod('methB')); var_dump($rb->hasMethod('methX')); print "\n"; print "--- hasProperty() ---\n"; var_dump($rb->hasProperty('p0'));
/** * Allows easy setting & backing up of enviroment config * * Option types are checked in the following order: * * * Server Var * * Static Variable * * Config option * * @param array $environment List of environment to set */ public function set_environment(array $environment) { if ( ! count($environment)) return FALSE; foreach ($environment as $option => $value) { $backup_needed = ! array_key_exists($option, $this->_environment_backup); // Handle changing superglobals if (in_array($option, array('_GET', '_POST', '_SERVER', '_FILES'))) { // For some reason we need to do this in order to change the superglobals global $$option; if ($backup_needed) { $this->_environment_backup[$option] = $$option; } // PHPUnit makes a backup of superglobals automatically $$option = $value; } // If this is a static property i.e. Html::$windowed_urls elseif (strpos($option, '::$') !== FALSE) { list($class, $var) = explode('::$', $option, 2); $class = new ReflectionClass($class); if ($backup_needed) { $this->_environment_backup[$option] = $class->getStaticPropertyValue($var); } $class->setStaticPropertyValue($var, $value); } // If this is an environment variable elseif (preg_match('/^[A-Z_-]+$/', $option) OR isset($_SERVER[$option])) { if ($backup_needed) { $this->_environment_backup[$option] = isset($_SERVER[$option]) ? $_SERVER[$option] : ''; } $_SERVER[$option] = $value; } // Else we assume this is a config option else { if ($backup_needed) { $this->_environment_backup[$option] = Kohana::config($option); } list($group, $var) = explode('.', $option, 2); Kohana::config($group)->set($var, $value); } } }
/** * set value to static property. * @param string $clsname * @param string $property * @param mixed $value */ protected function _setEntityConfig($clsname, $property, $value) { $ref = new ReflectionClass($clsname); $ref->setStaticPropertyValue($property, $value); }
/** * kick start to just fetch the config * @return array */ public static function resolveConfiguration() { $refl = new \ReflectionClass(get_called_class()); $refl->setStaticPropertyValue('init', true); $self = $refl->newInstance(); $refl->setStaticPropertyValue('init', false); $conf = array('table' => $self->getTable(), 'fieldConf' => $self->getFieldConfiguration(), 'db' => $self->db, 'fluid' => $self->fluid); unset($self); return $conf; }