Skip to content

Instantly share code, notes, and snippets.

@sminnee
Created February 8, 2010 23:03
Show Gist options
  • Select an option

  • Save sminnee/298698 to your computer and use it in GitHub Desktop.

Select an option

Save sminnee/298698 to your computer and use it in GitHub Desktop.
Index: sapphire/core/Object.php
===================================================================
--- sapphire/core/Object.php (revision 98430)
+++ sapphire/core/Object.php (working copy)
@@ -538,11 +538,7 @@
if($extensions = self::uninherited_static($class, 'extensions')) {
foreach($extensions as $extension) {
- // an $extension value can contain parameters as a string,
- // e.g. "Versioned('Stage','Live')"
- if(strpos($extension,'(') === false) $instance = new $extension();
- else $instance = eval("return new $extension;");
-
+ $instance = self::extension_inst($extension);
$instance->setOwner(null, $class);
$this->extension_instances[$instance->class] = $instance;
}
@@ -555,6 +551,41 @@
}
}
+ private static $_cache_inst_args = array();
+ static function extension_inst($extension) {
+ if(!isset(self::$_cache_inst_args[$extension])) {
+ // an $extension value can contain parameters as a string,
+ // e.g. "Versioned('Stage','Live')"
+ if(strpos($extension,'(') === false) {
+ self::$_cache_inst_args[$extension] = new $extension();
+
+ } else {
+ // Break down into class and args
+ preg_match('/([^(]+)\((.*)\)/', $extension, $matches);
+ $class = $matches[1];
+
+ // This is simplistic - allows for args containing commas
+ $args = explode(',', $matches[2]);
+ foreach($args as $i => $arg) {
+ $arg = ltrim($arg);
+ $lookahead=1;
+ while(($arg[0] == "'" && substr_count($arg, "'")==1)
+ || ($arg[0] == '"' && substr_count($arg, '"')==1)) {
+ $args[$i] .= ',' . $args[$i+$lookahead];
+ unset($args[$i+$lookahead]);
+ $lookahead++;
+ }
+ $args[$i] = preg_replace('/(^["\'])|(["\']$)/','',trim($args[$i]));
+ }
+
+ $reflector = new ReflectionClass($class);
+ self::$_cache_inst_args[$extension] = $reflector->newInstanceArgs($args);
+ }
+ }
+
+ return clone self::$_cache_inst_args[$extension];
+ }
+
/**
* Attemps to locate and call a method dynamically added to a class at runtime if a default cannot be located
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment