Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
52.38% covered (warning)
52.38%
11 / 21
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
navigation_provider
52.38% covered (warning)
52.38%
11 / 21
60.00% covered (warning)
60.00%
3 / 5
24.07
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
2
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 register
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 set_nav_property
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 merge
62.50% covered (warning)
62.50%
5 / 8
0.00% covered (danger)
0.00%
0 / 1
6.32
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\install\helper\navigation;
15
16use phpbb\di\service_collection;
17
18/**
19 * Installers navigation provider
20 */
21class navigation_provider
22{
23    /**
24     * @var array
25     */
26    private $menu_collection;
27
28    /**
29     * Constructor
30     *
31     * @param service_collection        $plugins
32     */
33    public function __construct(service_collection $plugins)
34    {
35        $this->menu_collection    = array();
36
37        foreach ($plugins as $plugin => $plugin_instance)
38        {
39            $this->register($plugin_instance);
40        }
41    }
42
43    /**
44     * Returns navigation array
45     *
46     * @return array
47     */
48    public function get()
49    {
50        return $this->menu_collection;
51    }
52
53    /**
54     * Registers a navigation provider's navigation items
55     *
56     * @param navigation_interface    $navigation
57     */
58    public function register(navigation_interface $navigation)
59    {
60        $nav_arry = $navigation->get();
61        $this->menu_collection = $this->merge($nav_arry, $this->menu_collection);
62    }
63
64    /**
65     * Set a property in the navigation array
66     *
67     * @param array    $nav_element    Array to the navigation elem
68     * @param array    $property_array    Array with the properties to set
69     */
70    public function set_nav_property($nav_element, $property_array)
71    {
72        $array_pointer = array();
73        $array_root_pointer = &$array_pointer;
74        foreach ($nav_element as $array_path)
75        {
76            $array_pointer[$array_path] = array();
77            $array_pointer = &$array_pointer[$array_path];
78        }
79
80        $array_pointer = $property_array;
81
82        $this->menu_collection = $this->merge($array_root_pointer, $this->menu_collection);
83    }
84
85    /**
86     * Recursive array merge
87     *
88     * This function is necessary to be able to replace the options of
89     * already set navigation items.
90     *
91     * @param array    $array_to_merge
92     * @param array    $array_to_merge_into
93     *
94     * @return array    Merged array
95     */
96    private function merge($array_to_merge, $array_to_merge_into)
97    {
98        $merged_array = $array_to_merge_into;
99
100        foreach ($array_to_merge as $key => $value)
101        {
102            if (isset($array_to_merge_into[$key]))
103            {
104                if (is_array($array_to_merge_into[$key]) && is_array($value))
105                {
106                    $merged_array[$key] = $this->merge($value, $array_to_merge_into[$key]);
107                }
108                else
109                {
110                    $merged_array[$key] = $value;
111                }
112            }
113            else
114            {
115                $merged_array[$key] = $value;
116            }
117        }
118
119        return $merged_array;
120    }
121}