Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
16 / 16 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
| service_collection | |
100.00% |
16 / 16 |
|
100.00% |
7 / 7 |
11 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getIterator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| offsetGet | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| add | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| add_service_class | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_service_classes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_by_class | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | namespace phpbb\di; |
| 15 | |
| 16 | use phpbb\di\exception\multiple_service_definitions_exception; |
| 17 | use phpbb\di\exception\service_not_found_exception; |
| 18 | use Symfony\Component\DependencyInjection\ContainerInterface; |
| 19 | |
| 20 | /** |
| 21 | * Collection of services to be configured at container compile time. |
| 22 | */ |
| 23 | class service_collection extends \ArrayObject |
| 24 | { |
| 25 | /** |
| 26 | * @var ContainerInterface |
| 27 | */ |
| 28 | protected $container; |
| 29 | |
| 30 | /** |
| 31 | * @var array |
| 32 | */ |
| 33 | protected $service_classes; |
| 34 | |
| 35 | /** |
| 36 | * Constructor |
| 37 | * |
| 38 | * @param ContainerInterface $container Container object |
| 39 | */ |
| 40 | public function __construct(ContainerInterface $container) |
| 41 | { |
| 42 | $this->container = $container; |
| 43 | $this->service_classes = array(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * {@inheritdoc} |
| 48 | */ |
| 49 | public function getIterator(): \Iterator |
| 50 | { |
| 51 | return new service_collection_iterator($this); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * {@inheritdoc} |
| 56 | */ |
| 57 | public function offsetGet($key): mixed |
| 58 | { |
| 59 | return $this->container->get($key); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Add a service to the collection |
| 64 | * |
| 65 | * @param string $name The service name |
| 66 | * @return void |
| 67 | */ |
| 68 | public function add($name) |
| 69 | { |
| 70 | $this->offsetSet($name, false); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Add a service's class to the collection |
| 75 | * |
| 76 | * @param string $service_id |
| 77 | * @param string $class |
| 78 | */ |
| 79 | public function add_service_class($service_id, $class) |
| 80 | { |
| 81 | $this->service_classes[$service_id] = $class; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get services' classes |
| 86 | * |
| 87 | * @return array |
| 88 | */ |
| 89 | public function get_service_classes() |
| 90 | { |
| 91 | return $this->service_classes; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns the service associated to a class |
| 96 | * |
| 97 | * @return mixed |
| 98 | * @throws \RuntimeException if the service isn't found |
| 99 | */ |
| 100 | public function get_by_class($class) |
| 101 | { |
| 102 | $service_id = null; |
| 103 | |
| 104 | foreach ($this->service_classes as $id => $service_class) |
| 105 | { |
| 106 | if ($service_class === $class) |
| 107 | { |
| 108 | if ($service_id !== null) |
| 109 | { |
| 110 | throw new multiple_service_definitions_exception('DI_MULTIPLE_SERVICE_DEFINITIONS', [$class]); |
| 111 | } |
| 112 | |
| 113 | $service_id = $id; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if ($service_id === null) |
| 118 | { |
| 119 | throw new service_not_found_exception('DI_SERVICE_NOT_FOUND', [$class]); |
| 120 | } |
| 121 | |
| 122 | return $this->offsetGet($service_id); |
| 123 | } |
| 124 | } |