Skip to content

Commit 9ae99c7

Browse files
JanTvrdikdg
authored andcommitted
Container: optimized getByType() performance
1 parent 994fa92 commit 9ae99c7

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/DI/Container.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,14 @@ public function createService($name, array $args = array())
177177
*/
178178
public function getByType($class, $need = TRUE)
179179
{
180-
$names = $this->findByType($class);
181-
if (!$names) {
182-
if ($need) {
183-
throw new MissingServiceException("Service of type $class not found.");
184-
}
180+
$class = ltrim($class, '\\');
181+
$names = & $this->meta[self::TYPES][$class][TRUE];
182+
if (count($names) === 1) {
183+
return $this->getService($names[0]);
185184
} elseif (count($names) > 1) {
186185
throw new MissingServiceException("Multiple services of type $class found: " . implode(', ', $names) . '.');
187-
} else {
188-
return $this->getService($names[0]);
186+
} elseif ($need) {
187+
throw new MissingServiceException("Service of type $class not found.");
189188
}
190189
}
191190

0 commit comments

Comments
 (0)