Skip to content
This repository has been archived by the owner on Feb 6, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'kokx/hotfix/reduced-canonicalize-calls'
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanDotPro committed Jul 3, 2012
2 parents 32f4be5 + df33739 commit 00cfdb8
Showing 1 changed file with 43 additions and 36 deletions.
79 changes: 43 additions & 36 deletions src/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class ServiceManager implements ServiceLocatorInterface
const SCOPE_CHILD = 'child';
/**@#-*/

/**
* Lookup for canonicalized names.
*
* @var array
*/
protected $canonicalNames = array();

/**
* @var bool
*/
Expand Down Expand Up @@ -185,16 +192,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;
}

Expand All @@ -205,23 +213,24 @@ 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(
'Provided abstract factory must be the class name of an abstract factory or an instance of an AbstractFactoryInterface.'
);
}

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;
}

Expand Down Expand Up @@ -289,9 +298,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__,
Expand All @@ -303,8 +313,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;
}

Expand Down Expand Up @@ -347,7 +357,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
Expand Down Expand Up @@ -400,24 +410,22 @@ public function get($name, $usePeeringServiceManagers = true)
}

/**
* @param $cName
* @param string|array $name
* @return false|object
* @throws Exception\ServiceNotCreatedException
* @throws Exception\InvalidServiceNameException
*/
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);
}
Expand Down Expand Up @@ -458,17 +466,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])
Expand Down Expand Up @@ -496,16 +500,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))) {
Expand Down Expand Up @@ -566,18 +570,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;
}

Expand Down Expand Up @@ -631,7 +635,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];
}

/**
Expand Down

0 comments on commit 00cfdb8

Please sign in to comment.