From b686b6f94c52785a6bf602ebc60bf442cdfe3c3c Mon Sep 17 00:00:00 2001 From: Pieter Kokx Date: Tue, 3 Jul 2012 10:29:33 +0200 Subject: [PATCH 1/2] Reduced canonicalize calls. --- src/ServiceManager.php | 67 ++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index 2a78b723..e503a42d 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -180,16 +180,17 @@ public function retrieveFromPeeringManagerFirst() */ public function setInvokableClass($name, $invokableClass, $shared = true) { - $name = $this->canonicalizeName($name); + $cName = $this->canonicalizeName($name); + $rName = $name; - if ($this->allowOverride === false && $this->has($name)) { + if ($this->allowOverride === false && $this->has(array($cName, $rName))) { throw new Exception\InvalidServiceNameException(sprintf( 'A service by the name or alias "%s" already exists and cannot be overridden; please use an alternate name', - $name + $cName )); } - $this->invokableClasses[$name] = $invokableClass; - $this->shared[$name] = $shared; + $this->invokableClasses[$cName] = $invokableClass; + $this->shared[$cName] = $shared; return $this; } @@ -200,7 +201,8 @@ public function setInvokableClass($name, $invokableClass, $shared = true) */ public function setFactory($name, $factory, $shared = true) { - $name = $this->canonicalizeName($name); + $cName = $this->canonicalizeName($name); + $rName = $name; if (!is_string($factory) && !$factory instanceof FactoryInterface && !is_callable($factory)) { throw new Exception\InvalidArgumentException( @@ -208,15 +210,15 @@ public function setFactory($name, $factory, $shared = true) ); } - if ($this->allowOverride === false && $this->has($name)) { + if ($this->allowOverride === false && $this->has(array($cName, $rName))) { throw new Exception\InvalidServiceNameException(sprintf( 'A service by the name or alias "%s" already exists and cannot be overridden, please use an alternate name', - $name + $cName )); } - $this->factories[$name] = $factory; - $this->shared[$name] = $shared; + $this->factories[$cName] = $factory; + $this->shared[$cName] = $shared; return $this; } @@ -284,9 +286,10 @@ public function addInitializer($initializer, $topOfStack = true) */ public function setService($name, $service, $shared = true) { - $name = $this->canonicalizeName($name); + $cName = $this->canonicalizeName($name); + $rName = $name; - if ($this->allowOverride === false && $this->has($name)) { + if ($this->allowOverride === false && $this->has($cName)) { throw new Exception\InvalidServiceNameException(sprintf( '%s: A service by the name "%s" or alias already exists and cannot be overridden, please use an alternate name.', __METHOD__, @@ -298,8 +301,8 @@ public function setService($name, $service, $shared = true) * @todo If a service is being overwritten, destroy all previous aliases */ - $this->instances[$name] = $service; - $this->shared[$name] = (bool) $shared; + $this->instances[$cName] = $service; + $this->shared[$cName] = (bool) $shared; return $this; } @@ -342,7 +345,7 @@ public function get($name, $usePeeringServiceManagers = true) $cName = $this->aliases[$cName]; } while ($this->hasAlias($cName)); - if (!$this->has($cName)) { + if (!$this->has(array($cName, $rName))) { throw new Exception\ServiceNotFoundException(sprintf( 'An alias "%s" was requested but no service could be found.', $name @@ -395,7 +398,7 @@ public function get($name, $usePeeringServiceManagers = true) } /** - * @param $cName + * @param string|array $name * @return false|object * @throws Exception\ServiceNotCreatedException * @throws Exception\InvalidServiceNameException @@ -403,16 +406,14 @@ public function get($name, $usePeeringServiceManagers = true) public function create($name) { $instance = false; - $rName = null; if (is_array($name)) { list($cName, $rName) = $name; } else { - $cName = $name; + $rName = $name; + $cName = $this->canonicalizeName($rName); } - $cName = $this->canonicalizeName($cName); - if (isset($this->factories[$cName])) { $instance = $this->createFromFactory($cName, $rName); } @@ -453,17 +454,13 @@ public function create($name) */ public function canCreate($name) { - $instance = false; - $rName = null; - if (is_array($name)) { list($cName, $rName) = $name; } else { - $cName = $name; + $rName = $name; + $cName = $this->canonicalizeName($rName); } - $cName = $this->canonicalizeName($cName); - $has = ( isset($this->invokableClasses[$cName]) || isset($this->factories[$cName]) @@ -491,16 +488,16 @@ public function canCreate($name) } /** - * @param $nameOrAlias + * @param $name * @return bool */ - public function has($nameOrAlias, $usePeeringServiceManagers = true) + public function has($name, $usePeeringServiceManagers = true) { - if (is_array($nameOrAlias)) { - list($cName, $rName) = $nameOrAlias; + if (is_array($name)) { + list($cName, $rName) = $name; } else { - $cName = $this->canonicalizeName($nameOrAlias); - $rName = $nameOrAlias; + $rName = $name; + $cName = $this->canonicalizeName($rName); } if ($this->canCreate(array($cName, $rName))) { @@ -554,18 +551,18 @@ public function setAlias($alias, $nameOrAlias) throw new Exception\InvalidServiceNameException('Service or alias names must be strings.'); } - $alias = $this->canonicalizeName($alias); + $cAlias = $this->canonicalizeName($alias); $nameOrAlias = $this->canonicalizeName($nameOrAlias); if ($alias == '' || $nameOrAlias == '') { throw new Exception\InvalidServiceNameException('Invalid service name alias'); } - if ($this->allowOverride === false && $this->has($alias)) { + if ($this->allowOverride === false && $this->has(array($cAlias, $alias))) { throw new Exception\InvalidServiceNameException('An alias by this name already exists'); } - $this->aliases[$alias] = $nameOrAlias; + $this->aliases[$cAlias] = $nameOrAlias; return $this; } From df33739ef70f5f6ada1124a7220479934fa682ce Mon Sep 17 00:00:00 2001 From: Pieter Kokx Date: Tue, 3 Jul 2012 11:06:11 +0200 Subject: [PATCH 2/2] Attempt to make the canonicalize calls faster. --- src/ServiceManager.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/ServiceManager.php b/src/ServiceManager.php index e503a42d..93509d03 100644 --- a/src/ServiceManager.php +++ b/src/ServiceManager.php @@ -12,6 +12,13 @@ class ServiceManager implements ServiceLocatorInterface const SCOPE_CHILD = 'child'; /**@#-*/ + /** + * Lookup for canonicalized names. + * + * @var array + */ + protected $canonicalNames = array(); + /** * @var bool */ @@ -616,7 +623,10 @@ public function addPeeringServiceManager(ServiceManager $manager, $peering = sel */ protected function canonicalizeName($name) { - return strtolower(str_replace(array('-', '_', ' ', '\\', '/'), '', $name)); + if (!isset($this->canonicalNames[$name])) { + $this->canonicalNames[$name] = strtolower(str_replace(array('-', '_', ' ', '\\', '/'), '', $name)); + } + return $this->canonicalNames[$name]; } /**