public function __construct() { $this->options = array(); $class = get_class($this); // We build our options array ourselves, because possibly no class or config manifest exists at this point do { $this->options = array_merge(Object::static_lookup($class, 'default_options'), $this->options); } while ($class = get_parent_class($class)); }
public function testStaticLookup() { $this->assertEquals(Object::static_lookup('ConfigTest_DefinesFoo', 'foo'), 1); $this->assertEquals(Object::static_lookup('ConfigTest_DefinesFoo', 'bar'), null); $this->assertEquals(Object::static_lookup('ConfigTest_DefinesBar', 'foo'), null); $this->assertEquals(Object::static_lookup('ConfigTest_DefinesBar', 'bar'), 2); $this->assertEquals(Object::static_lookup('ConfigTest_DefinesFooAndBar', 'foo'), 3); $this->assertEquals(Object::static_lookup('ConfigTest_DefinesFooAndBar', 'bar'), 3); $this->assertEquals(Object::static_lookup('ConfigTest_DefinesFooDoesntExtendObject', 'foo'), 4); $this->assertEquals(Object::static_lookup('ConfigTest_DefinesFooDoesntExtendObject', 'bar'), null); }
/** * Get the config value associated for a given class and property * * This merges all current sources and overrides together to give final value * todo: Currently this is done every time. This function is an inner loop function, so we really need to be caching heavily here. * * @param $class string - The name of the class to get the value for * @param $name string - The property to get the value for * @param int $sourceOptions - Bitmask which can be set to some combintain of Config::UNINHERITED, Config::FIRST_SET, and Config::EXCLUDE_EXTENSIONS. * Config::UNINHERITED does not include parent classes when merging configuration fragments * Config::FIRST_SET stops inheriting once the first class that sets a value (even an empty value) is encoutered * Config::EXCLUDE_EXTRA_SOURCES does not include any additional static sources (such as extensions) * * Config::INHERITED is a utility constant that can be used to mean "none of the above", equvilient to 0 * Setting both Config::UNINHERITED and Config::FIRST_SET behaves the same as just Config::UNINHERITED * * should the parent classes value be merged in as the lowest priority source? * @param null $result array|scalar - Reference to a variable to put the result in. Also returned, so this can be left as null safely. If you do pass a value, it will be treated as the highest priority value in the result chain * @param null $suppress array - Internal use when called by child classes. Array of mask pairs to filter value by * @return array|scalar - The value of the config item, or null if no value set. Could be an associative array, sequential array or scalar depending on value (see class docblock) */ function get($class, $name, $sourceOptions = 0, &$result = null, $suppress = null) { // If result is already not something to merge into, just return it if ($result !== null && !is_array($result)) { return $result; } // First, look through the override values foreach ($this->overrides as $k => $overrides) { if (isset($overrides[$class][$name])) { $value = $overrides[$class][$name]; self::merge_low_into_high($result, $value, $suppress); if ($result !== null && !is_array($result)) { return $result; } } if (isset($this->suppresses[$k][$class][$name])) { $suppress = $suppress ? array_merge($suppress, $this->suppresses[$k][$class][$name]) : $this->suppresses[$k][$class][$name]; } } // Then the manifest values foreach ($this->manifests as $manifest) { if (isset($manifest[$class][$name])) { self::merge_low_into_high($result, $manifest[$class][$name], $suppress); if ($result !== null && !is_array($result)) { return $result; } } } // Then look at the static variables $nothing = new stdClass(); $classes = array($class); // Include extensions only if not flagged not to, and some have been set if (($sourceOptions & self::EXCLUDE_EXTRA_SOURCES) != self::EXCLUDE_EXTRA_SOURCES && isset(self::$extra_static_sources[$class])) { $classes = array_merge($classes, self::$extra_static_sources[$class]); } foreach ($classes as $staticSource) { $value = Object::static_lookup($staticSource, $name, $nothing); if ($value !== $nothing) { self::merge_low_into_high($result, $value, $suppress); if ($result !== null && !is_array($result)) { return $result; } } } // Finally, merge in the values from the parent class if (($sourceOptions & self::UNINHERITED) != self::UNINHERITED && (($sourceOptions & self::FIRST_SET) != self::FIRST_SET || $result === null)) { $parent = get_parent_class($class); if ($parent) { $this->get($parent, $name, $sourceOptions, $result, $suppress); } } if ($name == 'routes') { print_r($result); die; } return $result; }