Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.26% covered (warning)
78.26%
18 / 23
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ordered_service_collection
78.26% covered (warning)
78.26%
18 / 23
50.00% covered (danger)
50.00%
3 / 6
13.48
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getIterator
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 offsetExists
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 offsetGet
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 add
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 sort_services
87.50% covered (warning)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
4.03
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
14namespace phpbb\di;
15
16use Symfony\Component\DependencyInjection\ContainerInterface;
17
18/**
19 * Collection of services in a specified order
20 */
21class ordered_service_collection extends service_collection
22{
23    /**
24     * @var bool
25     */
26    protected $is_ordered;
27
28    /**
29     * @var array
30     */
31    protected $service_ids;
32
33    /**
34     * Constructor
35     *
36     * @param ContainerInterface $container Container object
37     */
38    public function __construct(ContainerInterface $container)
39    {
40        $this->is_ordered = false;
41        $this->service_ids = array();
42
43        parent::__construct($container);
44    }
45
46    /**
47     * {@inheritdoc}
48     */
49    public function getIterator(): \Iterator
50    {
51        if (!$this->is_ordered)
52        {
53            $this->sort_services();
54        }
55
56        return new service_collection_iterator($this);
57    }
58
59    /**
60     * {@inheritdoc}
61     */
62    public function offsetExists($key): bool
63    {
64        if (!$this->is_ordered)
65        {
66            $this->sort_services();
67        }
68
69        return parent::offsetExists($key);
70    }
71
72    /**
73     * {@inheritdoc}
74     */
75    public function offsetGet($key): mixed
76    {
77        if (!$this->is_ordered)
78        {
79            $this->sort_services();
80        }
81
82        return parent::offsetGet($key);
83    }
84
85    /**
86     * Adds a service ID to the collection
87     *
88     * @param string    $name
89     * @param int        $order
90     */
91    public function add($name, $order = 0)
92    {
93        $order = (int) $order;
94        $this->service_ids[$order][] = $name;
95        $this->is_ordered = false;
96    }
97
98    protected function sort_services()
99    {
100        if ($this->is_ordered)
101        {
102            return;
103        }
104
105        $this->exchangeArray(array());
106        ksort($this->service_ids);
107        foreach ($this->service_ids as $service_order_group)
108        {
109            foreach ($service_order_group as $service_id)
110            {
111                $this->offsetSet($service_id, null);
112            }
113        }
114
115        $this->is_ordered = true;
116    }
117}