Skip to content

Commit

Permalink
Container: optimized getByType() performance
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik authored and dg committed Feb 5, 2015
1 parent 994fa92 commit 9ae99c7
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/DI/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,14 @@ public function createService($name, array $args = array())
*/
public function getByType($class, $need = TRUE)
{
$names = $this->findByType($class);
if (!$names) {
if ($need) {
throw new MissingServiceException("Service of type $class not found.");
}
$class = ltrim($class, '\\');
$names = & $this->meta[self::TYPES][$class][TRUE];
if (count($names) === 1) {
return $this->getService($names[0]);
} elseif (count($names) > 1) {
throw new MissingServiceException("Multiple services of type $class found: " . implode(', ', $names) . '.');
} else {
return $this->getService($names[0]);
} elseif ($need) {
throw new MissingServiceException("Service of type $class not found.");
}
}

Expand Down

0 comments on commit 9ae99c7

Please sign in to comment.