Extension   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 90
rs 10
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A createLoader() 0 9 1
A getContainerBuilder() 0 3 1
A getExtensionConfig() 0 9 3
A getFromConfig() 0 3 1
B findClasses() 0 31 8
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Biurad opensource projects.
7
 *
8
 * PHP version 7.2 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Biurad\DependencyInjection;
19
20
use Contributte\DI\Extension\CompilerExtension;
21
use Nette\DI\ContainerBuilder;
22
use Nette\DI\Config\Loader as NetteLoader;
23
use Nette\Loaders\RobotLoader;
24
use ReflectionClass;
25
26
/**
27
 * Configurator compiling extension.
28
 *
29
 * @author Divine Niiquaye Ibok <[email protected]>
30
 */
31
abstract class Extension extends CompilerExtension
32
{
33
    /**
34
     * @return Builder
35
     */
36
    public function getContainerBuilder(): ContainerBuilder
37
    {
38
        return $this->compiler->getContainerBuilder();
39
    }
40
41
    /**
42
     * Get a key from config using dots on string
43
     *
44
     * @return mixed
45
     */
46
    public function getFromConfig(string $key)
47
    {
48
        return Builder::arrayGet($this->config, $key);
49
    }
50
51
    /**
52
     * @return NetteLoader
53
     */
54
    protected function createLoader(): NetteLoader
55
    {
56
        $loader = parent::createLoader();
57
58
        $loader->addAdapter('yaml', Adapters\YamlAdapter::class);
59
        $loader->addAdapter('yml', Adapters\YamlAdapter::class);
60
        $loader->addAdapter('xml', Adapters\XmlAdapter::class);
61
62
        return $loader;
63
    }
64
65
    /**
66
     * Returns the configuration array for the given extension.
67
     *
68
     * @param string $extension The extension class name
69
     * @param string $config    The config in dotted form
70
     *
71
     * @return mixed value from extension config or null if not found
72
     */
73
    protected function getExtensionConfig(string $extension, string $config)
74
    {
75
        $extensions = $this->compiler->getExtensions($extension);
76
77
        if (empty($extensions) || \count($extensions) !== 1) {
78
            return null;
79
        }
80
81
        return Builder::arrayGet(\current($extensions)->getConfig(), $config);
82
    }
83
84
    /**
85
     * @param string[] $scanDirs
86
     * @param string   $className
87
     *
88
     * @return string[]
89
     */
90
    protected function findClasses(array $scanDirs, string $className): array
91
    {
92
        $classes = [];
93
94
        if (!empty($scanDirs)) {
95
            $robot = new RobotLoader();
96
97
            // back compatibility to robot loader of version  < 3.0
98
            if (\method_exists($robot, 'setCacheStorage')) {
99
                $robot->setCacheStorage(new \Nette\Caching\Storages\DevNullStorage());
0 ignored issues
show
Bug introduced by
The type Nette\Caching\Storages\DevNullStorage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
100
            }
101
102
            $robot->addDirectory(...$scanDirs);
103
            $robot->acceptFiles = ['*.php'];
104
            $robot->rebuild();
105
            $classes = \array_keys($robot->getIndexedClasses());
106
        }
107
108
        $foundClasses = [];
109
110
        foreach (\array_unique($classes) as $class) {
111
            if (
112
                \class_exists($class)
113
                && ($rc = new ReflectionClass($class)) && $rc->isSubclassOf($className)
114
                && !$rc->isAbstract()
115
            ) {
116
                $foundClasses[] = $rc->getName();
117
            }
118
        }
119
120
        return $foundClasses;
121
    }
122
}
123