mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #1836 from simonwelsh/remove-dup
Remove duplicate parse_class_spec method.
This commit is contained in:
commit
fe2663a140
@ -671,78 +671,9 @@ class SSObjectCreator extends InjectionCreator {
|
||||
if (strpos($class, '(') === false) {
|
||||
return parent::create($class, $params);
|
||||
} else {
|
||||
list($class, $params) = self::parse_class_spec($class);
|
||||
list($class, $params) = Object::parse_class_spec($class);
|
||||
$params = $this->injector->convertServiceProperty($params);
|
||||
return parent::create($class, $params);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a class-spec, such as "Versioned('Stage','Live')", as passed to create_from_string().
|
||||
* Returns a 2-elemnent array, with classname and arguments
|
||||
*/
|
||||
public static function parse_class_spec($classSpec) {
|
||||
$tokens = token_get_all("<?php $classSpec");
|
||||
$class = null;
|
||||
$args = array();
|
||||
$passedBracket = false;
|
||||
|
||||
// Keep track of the current bucket that we're putting data into
|
||||
$bucket = &$args;
|
||||
$bucketStack = array();
|
||||
|
||||
foreach($tokens as $token) {
|
||||
$tName = is_array($token) ? $token[0] : $token;
|
||||
// Get the class naem
|
||||
if($class == null && is_array($token) && $token[0] == T_STRING) {
|
||||
$class = $token[1];
|
||||
// Get arguments
|
||||
} else if(is_array($token)) {
|
||||
switch($token[0]) {
|
||||
case T_CONSTANT_ENCAPSED_STRING:
|
||||
$argString = $token[1];
|
||||
switch($argString[0]) {
|
||||
case '"': $argString = stripcslashes(substr($argString,1,-1)); break;
|
||||
case "'":
|
||||
$argString = str_replace(array("\\\\", "\\'"),array("\\", "'"), substr($argString,1,-1));
|
||||
break;
|
||||
default: throw new Exception("Bad T_CONSTANT_ENCAPSED_STRING arg $argString");
|
||||
}
|
||||
$bucket[] = $argString;
|
||||
break;
|
||||
|
||||
case T_DNUMBER:
|
||||
$bucket[] = (double)$token[1];
|
||||
break;
|
||||
|
||||
case T_LNUMBER:
|
||||
$bucket[] = (int)$token[1];
|
||||
break;
|
||||
|
||||
case T_STRING:
|
||||
switch($token[1]) {
|
||||
case 'true': $args[] = true; break;
|
||||
case 'false': $args[] = false; break;
|
||||
default: throw new Exception("Bad T_STRING arg '{$token[1]}'");
|
||||
}
|
||||
|
||||
case T_ARRAY:
|
||||
// Add an empty array to the bucket
|
||||
$bucket[] = array();
|
||||
$bucketStack[] = &$bucket;
|
||||
$bucket = &$bucket[sizeof($bucket)-1];
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
if($tName == ')') {
|
||||
// Pop-by-reference
|
||||
$bucket = &$bucketStack[sizeof($bucketStack)-1];
|
||||
array_pop($bucketStack);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return array($class, $args);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user