Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.76% covered (warning)
82.76%
24 / 29
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
extension_base
82.76% covered (warning)
82.76%
24 / 29
80.00% covered (warning)
80.00%
4 / 5
15.00
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 load
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 load_services
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
8
 getConfiguration
50.00% covered (danger)
50.00%
5 / 10
0.00% covered (danger)
0.00%
0 / 1
4.12
 getAlias
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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\extension\di;
15
16use Symfony\Component\Config\FileLocator;
17use Symfony\Component\Config\Resource\FileResource;
18use Symfony\Component\DependencyInjection\ContainerBuilder;
19use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
20use Symfony\Component\HttpKernel\DependencyInjection\Extension;
21use phpbb\filesystem\helper as filesystem_helper;
22
23/**
24 * Container core extension
25 */
26class extension_base extends Extension
27{
28    /**
29     * Name of the extension (vendor/name)
30     *
31     * @var string
32     */
33    protected $extension_name;
34
35    /**
36     * Path to the extension.
37     *
38     * @var string
39     */
40    protected $ext_path;
41
42    /**
43     * Constructor
44     *
45     * @param string $extension_name Name of the extension (vendor/name)
46     * @param string $ext_path       Path to the extension
47     */
48    public function __construct($extension_name, $ext_path)
49    {
50        $this->extension_name = $extension_name;
51        $this->ext_path = $ext_path;
52    }
53
54    /**
55     * Loads a specific configuration.
56     *
57     * @param array            $configs   An array of configuration values
58     * @param ContainerBuilder $container A ContainerBuilder instance
59     *
60     * @throws \InvalidArgumentException When provided tag is not defined in this extension
61     */
62    public function load(array $configs, ContainerBuilder $container)
63    {
64        $this->load_services($container);
65    }
66
67    /**
68     * Loads the services.yml file.
69     *
70     * @param ContainerBuilder $container A ContainerBuilder instance
71     */
72    protected function load_services(ContainerBuilder $container)
73    {
74        $services_directory = false;
75        $services_file = false;
76
77        if (file_exists($this->ext_path . 'config/' . $container->getParameter('core.environment') . '/container/environment.yml'))
78        {
79            $services_directory = $this->ext_path . 'config/' . $container->getParameter('core.environment') . '/container/';
80            $services_file = 'environment.yml';
81        }
82        else if (!is_dir($this->ext_path . 'config/' . $container->getParameter('core.environment')))
83        {
84            if (file_exists($this->ext_path . 'config/default/container/environment.yml'))
85            {
86                $services_directory = $this->ext_path . 'config/default/container/';
87                $services_file = 'environment.yml';
88            }
89            else if (!is_dir($this->ext_path . 'config/default') && file_exists($this->ext_path . '/config/services.yml'))
90            {
91                $services_directory = $this->ext_path . 'config';
92                $services_file = 'services.yml';
93            }
94        }
95
96        if ($services_directory && $services_file)
97        {
98            $loader = new YamlFileLoader($container, new FileLocator(filesystem_helper::realpath($services_directory)));
99            $loader->load($services_file);
100        }
101    }
102
103    /**
104     * {@inheritdoc}
105     */
106    public function getConfiguration(array $config, ContainerBuilder $container)
107    {
108        $reflected = new \ReflectionClass($this);
109        $namespace = $reflected->getNamespaceName();
110
111        $class = $namespace . '\\di\configuration';
112        if (class_exists($class))
113        {
114            $r = new \ReflectionClass($class);
115            $container->addResource(new FileResource($r->getFileName()));
116
117            if (!method_exists($class, '__construct'))
118            {
119                $configuration = new $class();
120
121                return $configuration;
122            }
123        }
124
125        return null;
126    }
127
128    /**
129     * Returns the recommended alias to use in XML.
130     *
131     * This alias is also the mandatory prefix to use when using YAML.
132     *
133     * @return string The alias
134     */
135    public function getAlias(): string
136    {
137        return str_replace('/', '_', $this->extension_name);
138    }
139}