Skip to content

Loading…

Feature/config #1244

Merged
merged 10 commits into from

5 participants

@hafriedlander
SilverStripe Ltd. member

Some changes to the Config system

  • Merged versions are cached in an intra-process in-mem LRU cache
  • Statics are extracted pre-execution via code parsing a-la the class manifest

This is a slight API change. Statics used as config value defaults are now considered immutable after declaration, and any assignment to them will be ignored.

@hafriedlander hafriedlander referenced this pull request in silverstripe/silverstripe-cms
Merged

FIX CMSMainTest to access batch_action config property properly #295

@wilr
SilverStripe Ltd. member

Slight API change enough to warrant going into 3.1 after 2 betas slight?

@sminnee
SilverStripe Ltd. member

The underlying issue is that the absence of this patch causes quite serious performance regressions when compared to 2.4.

@hafriedlander
SilverStripe Ltd. member

TLDR; It's pretty slight. I think it should be OK. We might want to do a Beta 3 - Sam?

The basic change is that before, some code might do this to update a config value:

Foo::$some_var = "new value";

That used to work or not work depending on when that code was run & what the config value was. For instance this:

Page::$db = array( .... );

would either work fine or cause errors (or maybe even database corruption) depending on when it's executed.

Now code like that will always be a no-op. You can do anything you like to any static and it won't matter because the static has been extracted before code execution happened.

In master I'll introduce a bigger API change which will deprecate having config values with statics that aren't marked private. That'll make attempts to change config statics throw an error instead of just work silently.

I'll also introduce a freeze method, so that the database layer can do Page::config()->freeze('db') and then any attempt to even do Page::config()->db = .... will cause an error

@chillu
SilverStripe Ltd. member

Can't really say much about the implementation, it's over my head ;)
Hamish, given this should get some exposure before we're getting close to 3.1 final,
I'll recommend that you merge yourself, or get somebody with more insight into the config system to have a look.
But it does need a mention in the upgrading docs first.

@sminnee
SilverStripe Ltd. member

Is the loss of caching here okay due to other changes?

SilverStripe Ltd. member

Answered my own question; looks good.

@sminnee
SilverStripe Ltd. member

I can't see any tests for this? It's the only part of the code that really makes my eyes glaze over; it seems like a pretty key candidate for some unit tests.

@simonwelsh

There's also a test that's currently being skipped (I think it's a MySQL-specific one), since it relies the db config being immutable. Would be good to get that test re-enabled :)

@sminnee
SilverStripe Ltd. member

Alright I'm going to merge this and update the upgrade docs.

@sminnee sminnee merged commit 362ca9b into silverstripe:3.1

1 check failed

Details default The Travis build failed
@chillu
SilverStripe Ltd. member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
245 core/Config.php
@@ -164,12 +164,43 @@ static public function set_instance($instance) {
}
/**
- * Empty construction, otherwise calling singleton('Config') (not the right way to get the current active config
- * instance, but people might) gives an error
+ * Make the newly active Config be a copy of the current active Config instance.
+ *
+ * You can then make changes to the configuration by calling update and remove on the new
+ * value returned by Config::inst(), and then discard those changes later by calling unnest
+ */
+ static public function nest() {
+ $current = self::$instance;
+
+ $new = clone $current;
+ $new->nestedFrom = $current;
+ self::set_instance($new);
+ }
+
+ /**
+ * Change the active Config back to the Config instance the current active Config object
+ * was copied from
+ */
+ static public function unnest() {
+ self::set_instance(self::$instance->nestedFrom);
+ }
+
+ protected $cache;
+
+ /**
+ * Each copy of the Config object need's it's own cache, so changes don't leak through to other instances
*/
public function __construct() {
+ $this->cache = new Config_LRU();
+ }
+
+ public function __clone() {
+ $this->cache = clone $this->cache;
}
+ /** @var Config - The config instance this one was copied from when Config::nest() was called */
+ protected $nestedFrom = null;
+
/** @var [array] - Array of arrays. Each member is an nested array keyed as $class => $name => $value,
* where value is a config value to treat as the highest priority item */
protected $overrides = array();
@@ -178,6 +209,13 @@ public function __construct() {
* where value is a config value suppress from any lower priority item */
protected $suppresses = array();
+ protected $staticManifests = array();
+
+ public function pushConfigStaticManifest(SS_ConfigStaticManifest $manifest) {
+ array_unshift($this->staticManifests, $manifest);
+ $this->cache->clean();
+ }
+
/** @var [array] - The list of settings pulled from config files to search through */
protected $manifests = array();
@@ -187,8 +225,9 @@ public function __construct() {
* WARNING: Config manifests to not merge entries, and do not solve before/after rules inter-manifest -
* instead, the last manifest to be added always wins
*/
- public function pushConfigManifest(SS_ConfigManifest $manifest) {
+ public function pushConfigYamlManifest(SS_ConfigManifest $manifest) {
array_unshift($this->manifests, $manifest->yamlConfig);
+ $this->cache->clean();
// @todo: Do anything with these. They're for caching after config.php has executed
$this->collectConfigPHPSettings = true;
@@ -342,34 +381,17 @@ static protected function filter_array_by_suppress_array($array, $suppress) {
return $res;
}
- /**
- * 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 $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 $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)
- */
- public function get($class, $name, $sourceOptions = 0, &$result = null, $suppress = null) {
+ protected $extraConfigSources = array();
+
+ public function extraConfigSourcesChanged($class) {
+ unset($this->extraConfigSources[$class]);
+ $this->cache->clean("__{$class}");
+ }
+
+ protected function getUncached($class, $name, $sourceOptions, &$result, $suppress, &$tags) {
+ $tags[] = "__{$class}";
+ $tags[] = "__{$class}__{$name}";
+
// If result is already not something to merge into, just return it
if ($result !== null && !is_array($result)) return $result;
@@ -397,19 +419,32 @@ public function get($class, $name, $sourceOptions = 0, &$result = null, $suppres
}
}
- // Then look at the static variables
- $nothing = new stdClass();
-
$sources = 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) {
- $extraSources = Object::get_extra_config_sources($class);
+ // If we don't have a fresh list of extra sources, get it from the class itself
+ if (!array_key_exists($class, $this->extraConfigSources)) {
+ $this->extraConfigSources[$class] = Object::get_extra_config_sources($class);
+ }
+
+ // Update $sources with any extra sources
+ $extraSources = $this->extraConfigSources[$class];
if ($extraSources) $sources = array_merge($sources, $extraSources);
}
+ $value = $nothing = null;
+
foreach ($sources as $staticSource) {
- if (is_array($staticSource)) $value = isset($staticSource[$name]) ? $staticSource[$name] : $nothing;
- else $value = Object::static_lookup($staticSource, $name, $nothing);
+ if (is_array($staticSource)) {
+ $value = isset($staticSource[$name]) ? $staticSource[$name] : $nothing;
+ }
+ else {
+ foreach ($this->staticManifests as $i => $statics) {
+ $value = $statics->get($staticSource, $name, $nothing);
+ if ($value !== $nothing) break;
+ }
+ }
if ($value !== $nothing) {
self::merge_low_into_high($result, $value, $suppress);
@@ -418,14 +453,53 @@ public function get($class, $name, $sourceOptions = 0, &$result = null, $suppres
}
// Finally, merge in the values from the parent class
- if (($sourceOptions & self::UNINHERITED) != self::UNINHERITED
- && (($sourceOptions & self::FIRST_SET) != self::FIRST_SET || $result === null)) {
+ 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 ($parent) $this->getUncached($parent, $name, $sourceOptions, $result, $suppress, $tags);
}
- if ($name == 'routes') {
- print_r($result); die;
+ return $result;
+ }
+
+ /**
+ * 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 $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 $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)
+ */
+ public function get($class, $name, $sourceOptions = 0, &$result = null, $suppress = null) {
+ // Have we got a cached value? Use it if so
+ $key = $class.$name.$sourceOptions;
+
+ if (($result = $this->cache->get($key)) === false) {
+ $tags = array();
+ $result = null;
+ $this->getUncached($class, $name, $sourceOptions, $result, $suppress, $tags);
+ $this->cache->set($key, $result, $tags);
}
return $result;
@@ -452,6 +526,8 @@ public function update($class, $name, $val) {
if (!isset($this->overrides[0][$class][$name])) $this->overrides[0][$class][$name] = $val;
else self::merge_high_into_low($this->overrides[0][$class][$name], $val);
+
+ $this->cache->clean("__{$class}__{$name}");
}
/**
@@ -512,6 +588,91 @@ public function remove($class, $name) {
}
+class Config_LRU {
+ const SIZE = 1000;
+
+ protected $cache;
+ protected $indexing;
+
+ protected $i = 0;
+ protected $c = 0;
+
+ public function __construct() {
+ $this->cache = new SplFixedArray(self::SIZE);
+
+ // Pre-fill with stdClass instances. By reusing we avoid object-thrashing
+ for ($i = 0; $i < self::SIZE; $i++) {
+ $this->cache[$i] = new stdClass();
+ $this->cache[$i]->key = null;
+ }
+
+ $this->indexing = array();
+ }
+
+ public function set($key, $val, $tags = array()) {
+ // Find an index to set at
+ $replacing = null;
+
+ // Target count - not always the lowest, but guaranteed to exist (or hit an empty item)
+ $target = $this->c - self::SIZE + 1;
+ $i = $stop = $this->i;
+
+ do {
+ if (!($i--)) $i = self::SIZE-1;
+ $item = $this->cache[$i];
+
+ if ($item->key === null) { $replacing = null; break; }
+ else if ($item->c <= $target) { $replacing = $item; break; }
+ }
+ while ($i != $stop);
+
+ if ($replacing) unset($this->indexing[$replacing->key]);
+
+ $this->indexing[$key] = $this->i = $i;
+
+ $obj = $this->cache[$i];
+ $obj->key = $key;
+ $obj->value = $val;
+ $obj->tags = $tags;
+ $obj->c = ++$this->c;
+ }
+
+ private $hit = 0;
+ private $miss = 0;
+
+ public function stats() {
+ return $this->miss ? ($this->hit / $this->miss) : 0;
+ }
+
+ public function get($key) {
+ if (isset($this->indexing[$key])) {
+ $this->hit++;
+
+ $res = $this->cache[$this->indexing[$key]];
+ $res->c = ++$this->c;
+ return $res->value;
+ }
+
+ $this->miss++;
+ return false;
+ }
+
+ public function clean($tag = null) {
+ if ($tag) {
+ foreach ($this->cache as $i => $v) {
+ if ($v->key !== null && in_array($tag, $v->tags)) {
+ unset($this->indexing[$v->key]);
+ $this->cache[$i]->key = null;
+ }
+ }
+ }
+ else {
+ for ($i = 0; $i < self::SIZE; $i++) $this->cache[$i]->key = null;
+ $this->indexing = array();
+ }
+ }
+}
+
class Config_ForClass {
protected $class;
View
6 core/Core.php
@@ -289,8 +289,12 @@
}
// Now that the class manifest is up, load the configuration
+$configManifest = new SS_ConfigStaticManifest(BASE_PATH, false, $flush);
+Config::inst()->pushConfigStaticManifest($configManifest);
+
+// Now that the class manifest is up, load the configuration
$configManifest = new SS_ConfigManifest(BASE_PATH, false, $flush);
-Config::inst()->pushConfigManifest($configManifest);
+Config::inst()->pushConfigYamlManifest($configManifest);
SS_TemplateLoader::instance()->pushManifest(new SS_TemplateManifest(
BASE_PATH, project(), false, isset($_GET['flush'])
View
28 core/Object.php
@@ -52,23 +52,12 @@
*/
public $class;
-
- /**
- * @todo Set this via dependancy injection? Can't call it $config, because too many clashes with form elements etc
- * @var Config_ForClass
- */
- private $_config_forclass = null;
-
/**
* Get a configuration accessor for this class. Short hand for Config::inst()->get($this->class, .....).
* @return Config_ForClass|null
*/
- public function config() {
- if (!$this->_config_forclass) {
- $this->_config_forclass = Config::inst()->forClass($this->class);
- }
-
- return $this->_config_forclass;
+ static public function config() {
+ return Config::inst()->forClass(get_called_class());
}
/**
@@ -494,10 +483,11 @@ public static function add_extension($extension) {
if($subclasses) foreach($subclasses as $subclass) {
unset(self::$classes_constructed[$subclass]);
unset(self::$extra_methods[$subclass]);
- unset(self::$extension_sources[$subclass]);
}
Config::inst()->update($class, 'extensions', array($extension));
+ Config::inst()->extraConfigSourcesChanged($class);
+
Injector::inst()->unregisterAllObjects();
// load statics now for DataObject classes
@@ -534,6 +524,7 @@ public static function remove_extension($extension) {
}
Config::inst()->remove($class, 'extensions', Config::anything(), $extension);
+ Config::inst()->extraConfigSourcesChanged($class);
// unset singletons to avoid side-effects
Injector::inst()->unregisterAllObjects();
@@ -544,7 +535,6 @@ public static function remove_extension($extension) {
if($subclasses) foreach($subclasses as $subclass) {
unset(self::$classes_constructed[$subclass]);
unset(self::$extra_methods[$subclass]);
- unset(self::$extension_sources[$subclass]);
}
}
@@ -571,9 +561,6 @@ public static function get_extensions($class, $includeArgumentString = false) {
// --------------------------------------------------------------------------------------------------------------
- private static $extension_sources = array();
-
- // Don't bother checking some classes that should never be extended
private static $unextendable_classes = array('Object', 'ViewableData', 'RequestHandler');
static public function get_extra_config_sources($class = null) {
@@ -582,9 +569,6 @@ static public function get_extra_config_sources($class = null) {
// If this class is unextendable, NOP
if(in_array($class, self::$unextendable_classes)) return;
- // If we have a pre-cached version, use that
- if(array_key_exists($class, self::$extension_sources)) return self::$extension_sources[$class];
-
// Variable to hold sources in
$sources = null;
@@ -615,7 +599,7 @@ static public function get_extra_config_sources($class = null) {
}
}
- return self::$extension_sources[$class] = $sources;
+ return $sources;
}
public function __construct() {
View
325 core/manifest/ConfigStaticManifest.php
@@ -0,0 +1,325 @@
+<?php
+/**
+ * A utility class which builds a manifest of the statics defined in all classes, along with their
+ * access levels and values
+ *
+ * We use this to make the statics that the Config system uses as default values be truely immutable.
+ *
+ * It has the side effect of allowing Config to avoid private-level access restrictions, so we can
+ * optionally catch attempts to modify the config statics (otherwise the modification will appear
+ * to work, but won't actually have any effect - the equvilent of failing silently)
+ *
+ * @subpackage manifest
+ */
+class SS_ConfigStaticManifest {
+
+ protected $base;
+ protected $tests;
+
+ protected $cache;
+ protected $key;
+
+ protected $index;
+ protected $statics;
+
+ static protected $initial_classes = array(
+ 'Object', 'ViewableData', 'Injector', 'Director'
+ );
+
+ /**
+ * Constructs and initialises a new config static manifest, either loading the data
+ * from the cache or re-scanning for classes.
+ *
+ * @param string $base The manifest base path.
+ * @param bool $includeTests Include the contents of "tests" directories.
+ * @param bool $forceRegen Force the manifest to be regenerated.
+ * @param bool $cache If the manifest is regenerated, cache it.
+ */
+ public function __construct($base, $includeTests = false, $forceRegen = false, $cache = true) {
+ $this->base = $base;
+ $this->tests = $includeTests;
+
+ $this->cache = SS_Cache::factory('SS_ConfigStatics', 'Core', array(
+ 'automatic_serialization' => true,
+ 'lifetime' => null
+ ));
+
+ $this->key = 'sc_'.sha1($base . ($includeTests ? '!tests' : ''));
+
+ if(!$forceRegen) {
+ $this->index = $this->cache->load($this->key);
+ }
+
+ if($this->index) {
+ $this->statics = $this->index['$statics'];
+ }
+ else {
+ $this->regenerate($cache);
+ }
+ }
+
+ public function get($class, $name, $default) {
+ if (!isset($this->statics[$class])) {
+ if (isset($this->index[$class])) {
+ $info = $this->index[$class];
+
+ if (isset($info['key']) && $details = $this->cache->load($this->key.'_'.$info['key'])) {
+ $this->statics += $details;
+ }
+
+ if (!isset($this->statics[$class])) {
+ $this->handleFile(null, $info['path'], null);
+ }
+ }
+ else {
+ $this->statics[$class] = false;
+ }
+ }
+
+ if (isset($this->statics[$class][$name])) {
+ $static = $this->statics[$class][$name];
+
+ if ($static['access'] != T_PRIVATE) {
+ Deprecation::notice('3.1.0', "Config static $class::\$$name must be marked as private", Deprecation::SCOPE_GLOBAL);
+ // Don't warn more than once per static
+ $static['access'] = T_PRIVATE;
+ }
+
+ return $static['value'];
+ }
+
+ return $default;
+ }
+
+ /**
+ * Completely regenerates the manifest file.
+ */
+ public function regenerate($cache = true) {
+ $this->index = array('$statics' => array());
+ $this->statics = array();
+
+ $finder = new ManifestFileFinder();
+ $finder->setOptions(array(
+ 'name_regex' => '/^([^_].*\.php)$/',
+ 'ignore_files' => array('index.php', 'main.php', 'cli-script.php'),
+ 'ignore_tests' => !$this->tests,
+ 'file_callback' => array($this, 'handleFile')
+ ));
+
+ $finder->find($this->base);
+
+ if($cache) {
+ $keysets = array();
+
+ foreach ($this->statics as $class => $details) {
+ if (in_array($class, self::$initial_classes)) {
+ $this->index['$statics'][$class] = $details;
+ }
+ else {
+ $key = sha1($class);
+ $this->index[$class]['key'] = $key;
+
+ $keysets[$key][$class] = $details;
+ }
+ }
+
+ foreach ($keysets as $key => $details) {
+ $this->cache->save($details, $this->key.'_'.$key);
+ }
+
+ $this->cache->save($this->index, $this->key);
+ }
+ }
+
+ public function handleFile($basename, $pathname, $depth) {
+ $parser = new SS_ConfigStaticManifest_Parser($pathname);
+ $parser->parse();
+
+ $this->index = array_merge($this->index, $parser->getInfo());
+ $this->statics = array_merge($this->statics, $parser->getStatics());
+ }
+
+ public function getStatics() {
+ return $this->statics;
+ }
+}
+
+/**
+ * A parser that processes a PHP file, using PHP's built in parser to get a string of tokens,
+ * then processing them to find the static class variables, their access levels & values
+ *
+ * We can't do this using TokenisedRegularExpression because we need to keep track of state
+ * as we process the token list (when we enter and leave a namespace or class, when we see
+ * an access level keyword, etc)
+ */
+class SS_ConfigStaticManifest_Parser {
+
+ protected $info = array();
+ protected $statics = array();
+
+ protected $path;
+ protected $tokens;
+ protected $length;
+ protected $pos;
+
+ function __construct($path) {
+ $this->path = $path;
+ $file = file_get_contents($path);
+
+ $this->tokens = token_get_all($file);
+ $this->length = count($this->tokens);
+ $this->pos = 0;
+ }
+
+ function getInfo() {
+ return $this->info;
+ }
+
+ function getStatics() {
+ return $this->statics;
+ }
+
+ /**
+ * Get the next token to process, incrementing the pointer
+ *
+ * @param bool $ignoreWhitespace - if true will skip any whitespace tokens & only return non-whitespace ones
+ * @return null | int - Either the next token or null if there isn't one
+ */
+ protected function next($ignoreWhitespace = true) {
+ do {
+ if($this->pos >= $this->length) return null;
+ $next = $this->tokens[$this->pos++];
+ }
+ while($ignoreWhitespace && is_array($next) && $next[0] == T_WHITESPACE);
+
+ return $next;
+ }
+
+ /**
+ * Parse the given file to find the static variables declared in it, along with their access & values
+ */
+ function parse() {
+ $depth = 0; $namespace = null; $class = null; $clsdepth = null; $access = 0;
+
+ while($token = $this->next()) {
+ $type = is_array($token) ? $token[0] : $token;
+
+ if($type == T_CLASS) {
+ $next = $this->next();
+ if($next[0] != T_STRING) {
+ user_error("Couldn\'t parse {$this->path} when building config static manifest", E_USER_ERROR);
+ }
+
+ $class = $next[1];
+ }
+ else if($type == T_NAMESPACE) {
+ $next = $this->next();
+
+ if($next[0] != T_STRING) {
+ user_error("Couldn\'t parse {$this->path} when building config static manifest", E_USER_ERROR);
+ }
+
+ $namespace = $next[1];
+ }
+ else if($type == '{' || $type == T_CURLY_OPEN || $type == T_DOLLAR_OPEN_CURLY_BRACES){
+ $depth += 1;
+ if($class && !$clsdepth) $clsdepth = $depth;
+ }
+ else if($type == '}') {
+ $depth -= 1;
+ if($depth < $clsdepth) $class = $clsdepth = null;
+ if($depth < 0) user_error("Hmm - depth calc wrong, hit negatives", E_USER_ERROR);
+ }
+ else if($type == T_PUBLIC || $type == T_PRIVATE || $type == T_PROTECTED) {
+ $access = $type;
+ }
+ else if($type == T_STATIC) {
+ if($class && $depth == $clsdepth) $this->parseStatic($access, $namespace ? $namespace.'\\'.$class : $class);
+ }
+ else {
+ $access = '';
+ }
+ }
+ }
+
+ /**
+ * During parsing we've found a "static" keyword. Parse out the variable names and value
+ * assignments that follow.
+ *
+ * Seperated out from parse partially so that we can recurse if there are multiple statics
+ * being declared in a comma seperated list
+ */
+ function parseStatic($access, $class) {
+ $variable = null;
+ $value = '';
+
+ while($token = $this->next()) {
+ $type = is_array($token) ? $token[0] : $token;
+
+ if($type == T_PUBLIC || $type == T_PRIVATE || $type == T_PROTECTED) {
+ $access = $type;
+ }
+ else if($type == T_FUNCTION) {
+ return;
+ }
+ else if($type == T_VARIABLE) {
+ $variable = substr($token[1], 1); // Cut off initial "$"
+ }
+ else if($type == ';' || $type == ',' || $type == '=') {
+ break;
+ }
+ else {
+ user_error('Unexpected token when building static manifest: '.print_r($token, true), E_USER_ERROR);
+ }
+ }
+
+ if($token == '=') {
+ $depth = 0;
+
+ while($token = $this->next(false)){
+ $type = is_array($token) ? $token[0] : $token;
+
+ // Track array nesting depth
+ if($type == T_ARRAY) {
+ $depth += 1;
+ }
+ else if($type == ')') {
+ $depth -= 1;
+ }
+
+ // Parse out the assignment side of a static declaration, ending on either a ';' or a ',' outside an array
+ if($type == T_WHITESPACE) {
+ $value .= ' ';
+ }
+ else if($type == ';' || ($type == ',' && !$depth)) {
+ break;
+ }
+ // Statics can reference class constants with self:: (and that won't work in eval)
+ else if($type == T_STRING && $token[1] == 'self') {
+ $value .= $class;
+ }
+ else {
+ $value .= is_array($token) ? $token[1] : $token;
+ }
+ }
+ }
+
+ if (!isset($this->info[$class])) {
+ $this->info[$class] = array(
+ 'path' => $this->path,
+ 'mtime' => filemtime($this->path),
+ );
+ }
+
+ if(!isset($this->statics[$class])) {
+ $this->statics[$class] = array();
+ }
+
+ $this->statics[$class][$variable] = array(
+ 'access' => $access,
+ 'value' => eval('return '.$value.';')
+ );
+
+ if($token == ',') $this->parseStatic($access, $class);
+ }
+}
View
4 dev/TestRunner.php
@@ -88,6 +88,10 @@ public static function use_test_manifest() {
SS_TemplateLoader::instance()->pushManifest(new SS_TemplateManifest(
BASE_PATH, project(), true, isset($_GET['flush'])
));
+
+ Config::inst()->pushConfigStaticManifest(new SS_ConfigStaticManifest(
+ BASE_PATH, true, isset($_GET['flush'])
+ ));
}
public function init() {
View
52 tests/core/ConfigTest.php
@@ -163,5 +163,55 @@ public function testStaticLookup() {
public function testFragmentOrder() {
$this->markTestIncomplete();
}
-
+
+ public function testLRUDiscarding() {
+ $cache = new ConfigTest_Config_LRU();
+
+ for ($i = 0; $i < Config_LRU::SIZE*2; $i++) $cache->set($i, $i);
+ $this->assertEquals(
+ Config_LRU::SIZE, count($cache->indexing),
+ 'Homogenous usage gives exact discarding'
+ );
+
+ $cache = new ConfigTest_Config_LRU();
+
+ for ($i = 0; $i < Config_LRU::SIZE; $i++) $cache->set($i, $i);
+ for ($i = 0; $i < Config_LRU::SIZE; $i++) $cache->set(-1, -1);
+ $this->assertLessThan(
+ Config_LRU::SIZE, count($cache->indexing),
+ 'Heterogenous usage gives sufficient discarding'
+ );
+ }
+
+ public function testLRUCleaning() {
+ $cache = new ConfigTest_Config_LRU();
+
+ for ($i = 0; $i < Config_LRU::SIZE; $i++) $cache->set($i, $i);
+ $this->assertEquals(Config_LRU::SIZE, count($cache->indexing));
+
+ $cache->clean();
+ $this->assertEquals(0, count($cache->indexing), 'Clean clears all items');
+ $this->assertFalse($cache->get(1), 'Clean clears all items');
+
+ $cache->set(1, 1, array('Foo'));
+ $this->assertEquals(1, count($cache->indexing));
+
+ $cache->clean('Foo');
+ $this->assertEquals(0, count($cache->indexing), 'Clean items with matching tag');
+ $this->assertFalse($cache->get(1), 'Clean items with matching tag');
+
+ $cache->set(1, 1, array('Foo', 'Bar'));
+ $this->assertEquals(1, count($cache->indexing));
+
+ $cache->clean('Bar');
+ $this->assertEquals(0, count($cache->indexing), 'Clean items with any single matching tag');
+ $this->assertFalse($cache->get(1), 'Clean items with any single matching tag');
+ }
+}
+
+class ConfigTest_Config_LRU extends Config_LRU {
+
+ public $cache;
+ public $indexing;
+
}
View
123 tests/core/manifest/ConfigStaticManifestTest.php
@@ -0,0 +1,123 @@
+<?php
+
+class ConfigStaticManifestTest extends SapphireTest {
+
+ /* Example statics */
+
+ // Different access levels
+ static $nolevel;
+ public static $public;
+ protected static $protected;
+ private static $private;
+ static public $public2;
+ static protected $protected2;
+ static private $private2;
+
+ // Assigning values
+ static $snone;
+ static $snull = null;
+ static $sint = 1;
+ static $sfloat = 2.5;
+ static $sstring = 'string';
+ static $sarray = array(1, 2, array(3, 4), 5);
+
+ // Assigning multiple values
+ static $onone, $onull = null, $oint = 1, $ofloat = 2.5, $ostring = 'string', $oarray = array(1, 2, array(3, 4), 5);
+
+ static
+ $mnone,
+ $mnull = null,
+ $mint = 1,
+ $mfloat = 2.5,
+ $mstring = 'string',
+ $marray = array(
+ 1, 2,
+ array(3, 4),
+ 5
+ );
+
+ // Should ignore static methpds
+ static function static_method() {}
+
+ // Should ignore method statics
+ function instanceMethod() {
+ static $method_static;
+ }
+
+ /* The tests */
+
+ protected function parseSelf() {
+ static $statics = null;
+
+ if ($statics === null) {
+ $parser = new SS_ConfigStaticManifest_Parser(__FILE__);
+ $parser->parse();
+ }
+
+ return $parser;
+ }
+
+ public function testParsingAccessLevels() {
+ $statics = $this->parseSelf()->getStatics();
+
+ $levels = array(
+ 'nolevel' => null,
+ 'public' => T_PUBLIC,
+ 'public2' => T_PUBLIC,
+ 'protected' => T_PROTECTED,
+ 'protected2' => T_PROTECTED,
+ 'private' => T_PRIVATE,
+ 'private2' => T_PRIVATE
+ );
+
+ foreach($levels as $var => $level) {
+ $this->assertEquals(
+ $level,
+ $statics[__CLASS__][$var]['access'],
+ 'Variable '.$var.' has '.($level ? token_name($level) : 'no').' access level'
+ );
+ }
+ }
+
+ public function testParsingValues() {
+ $statics = $this->parseSelf()->getStatics();
+
+ // Check assigning values
+ $values = array(
+ 'none',
+ 'null',
+ 'int',
+ 'float',
+ 'string',
+ 'array',
+ );
+
+ $prepends = array(
+ 's', // Each on it's own
+ 'o', // All on one line
+ 'm' // All in on static statement, but each on own line
+ );
+
+ foreach ($values as $value) {
+ foreach ($prepends as $prepend) {
+ $var = "$prepend$value";
+
+ $this->assertEquals(
+ self::$$var,
+ $statics[__CLASS__][$var]['value'],
+ 'Variable '.$var.' value is extracted properly'
+ );
+ }
+ }
+ }
+
+ public function testIgnoresMethodStatics() {
+ $statics = $this->parseSelf()->getStatics();
+ $this->assertNull(@$statics[__CLASS__]['method_static']);
+ }
+
+ public function testIgnoresStaticMethods() {
+ $statics = $this->parseSelf()->getStatics();
+ $this->assertNull(@$statics[__CLASS__]['static_method']);
+ }
+}
View
33 tests/model/DataObjectSchemaGenerationTest.php
@@ -43,9 +43,11 @@ public function testFieldsRequestChanges() {
// Table will have been initially created by the $extraDataObjects setting
// Let's insert a new field here
- $oldDB = DataObjectSchemaGenerationTest_DO::$db;
- DataObjectSchemaGenerationTest_DO::$db['SecretField'] = 'Varchar(100)';
-
+ Config::nest();
+ Config::inst()->update('DataObjectSchemaGenerationTest_DO', 'db', array(
+ 'SecretField' => 'Varchar(100)'
+ ));
+
// Verify that the above extra field triggered a schema update
$db->beginSchemaUpdate();
$obj = new DataObjectSchemaGenerationTest_DO();
@@ -55,7 +57,7 @@ public function testFieldsRequestChanges() {
$this->assertTrue($needsUpdating);
// Restore db configuration
- DataObjectSchemaGenerationTest_DO::$db = $oldDB;
+ Config::unnest();
}
/**
@@ -76,9 +78,12 @@ public function testIndexesDontRerequestChanges() {
$this->assertFalse($needsUpdating);
// Test with alternate index format, although these indexes are the same
- $oldIndexes = DataObjectSchemaGenerationTest_IndexDO::$indexes;
- DataObjectSchemaGenerationTest_IndexDO::$indexes = DataObjectSchemaGenerationTest_IndexDO::$indexes_alt;
-
+ Config::nest();
+ Config::inst()->remove('DataObjectSchemaGenerationTest_IndexDO', 'indexes');
+ Config::inst()->update('DataObjectSchemaGenerationTest_IndexDO', 'indexes',
+ Config::inst()->get('DataObjectSchemaGenerationTest_IndexDO', 'indexes_alt')
+ );
+
// Verify that it still doesn't need to be recreated
$db->beginSchemaUpdate();
$obj2 = new DataObjectSchemaGenerationTest_IndexDO();
@@ -88,7 +93,7 @@ public function testIndexesDontRerequestChanges() {
$this->assertFalse($needsUpdating);
// Restore old index format
- DataObjectSchemaGenerationTest_IndexDO::$indexes = $oldIndexes;
+ Config::unnest();
}
/**
@@ -101,9 +106,13 @@ public function testIndexesRerequestChanges() {
// Table will have been initially created by the $extraDataObjects setting
// Update the SearchFields index here
- $oldIndexes = DataObjectSchemaGenerationTest_IndexDO::$indexes;
- DataObjectSchemaGenerationTest_IndexDO::$indexes['SearchFields']['value'] = '"Title"';
-
+ Config::nest();
+ Config::inst()->update('DataObjectSchemaGenerationTest_IndexDO', 'indexes', array(
+ 'SearchFields' => array(
+ 'value' => 'Title'
+ )
+ ));
+
// Verify that the above index change triggered a schema update
$db->beginSchemaUpdate();
$obj = new DataObjectSchemaGenerationTest_IndexDO();
@@ -113,7 +122,7 @@ public function testIndexesRerequestChanges() {
$this->assertTrue($needsUpdating);
// Restore old indexes
- DataObjectSchemaGenerationTest_IndexDO::$indexes = $oldIndexes;
+ Config::unnest();
}
}
Something went wrong with that request. Please try again.