aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Darricau <github@nicofuma.fr>2017-07-04 16:39:18 +0200
committerTristan Darricau <github@nicofuma.fr>2017-07-26 13:16:51 +0200
commit29f48475aa1492eea709fabddb5091b4da3c43aa (patch)
tree48bd1e4910fa2954adda4a54c173a50546959823 /phpBB/phpbb/di
parentMerge branch '3.1.x' into 3.2.x (diff)
downloadphpbb-29f48475aa1492eea709fabddb5091b4da3c43aa.tar.gz
phpbb-29f48475aa1492eea709fabddb5091b4da3c43aa.tar.bz2
phpbb-29f48475aa1492eea709fabddb5091b4da3c43aa.zip
[ticket/15258] Adds a method to get a service by class in service_collection
PHPBB3-15258
Diffstat (limited to 'phpBB/phpbb/di')
-rw-r--r--phpBB/phpbb/di/service_collection.php31
1 files changed, 31 insertions, 0 deletions
diff --git a/phpBB/phpbb/di/service_collection.php b/phpBB/phpbb/di/service_collection.php
index 8e9175e204..8c1c172e36 100644
--- a/phpBB/phpbb/di/service_collection.php
+++ b/phpBB/phpbb/di/service_collection.php
@@ -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);
+ }
}