1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-07-31 22:10:45 +02:00

[ticket/15258] Adds a method to get a service by class in service_collection

PHPBB3-15258
This commit is contained in:
Tristan Darricau
2017-07-04 16:39:18 +02:00
committed by Marc Alexander
parent 574749daeb
commit deb556fbf0
2 changed files with 54 additions and 0 deletions

View File

@@ -103,4 +103,35 @@ class service_collection extends \ArrayObject
{
return $this->service_classes;
}
/**
* Returns the service associated to a class
*
* @return mixed
* @throw \RuntimeException if the
*/
public function get_by_class($class)
{
$service_id = null;
foreach ($this->service_classes as $id => $service_class)
{
if ($service_class === $class)
{
if ($service_id !== null)
{
throw new \RuntimeException('More than one service definitions found for class "'.$class.'" in collection.');
}
$service_id = $id;
}
}
if ($service_id === null)
{
throw new \RuntimeException('No service found for class "'.$class.'" in collection.');
}
return $this->offsetGet($service_id);
}
}