1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-06 00:37:42 +02:00

Merge pull request #4866 from Nicofuma/ticket/15258

[ticket/15258] Adds a method to get a service by class in service_collection
This commit is contained in:
Máté Bartus
2017-07-26 17:42:53 +02:00
committed by GitHub
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);
}
}