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:
committed by
Marc Alexander
parent
574749daeb
commit
deb556fbf0
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user