Passed
Push — master ( 96803c...b18ae0 )
by Divine Niiquaye
01:37
created

Extension::getFromConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\Loaders\RobotLoader;
23
use ReflectionClass;
24
25
/**
26
 * Configurator compiling extension.
27
 *
28
 * @author Divine Niiquaye Ibok <[email protected]>
29
 */
30
abstract class Extension extends CompilerExtension
31
{
32
    /**
33
     * @return Builder
34
     */
35
    public function getContainerBuilder(): ContainerBuilder
36
    {
37
        return $this->compiler->getContainerBuilder();
38
    }
39
40
    /**
41
     * Get a key from config using dots on string
42
     *
43
     * @return mixed
44
     */
45
    public function getFromConfig(string $key)
46
    {
47
        return Builder::arrayGet($this->config, $key);
48
    }
49
50
    /**
51
     * Returns the configuration array for the given extension.
52
     *
53
     * @param string $extension The extension class name
54
     * @param string $config    The config in dotted form
55
     *
56
     * @return mixed value from extension config or null if not found
57
     */
58
    protected function getExtensionConfig(string $extension, string $config)
59
    {
60
        $extensions = $this->compiler->getExtensions($extension);
61
62
        if (empty($extensions) || \count($extensions) !== 1) {
63
            return null;
64
        }
65
66
        return Builder::arrayGet(\current($extensions)->getConfig(), $config);
67
    }
68
69
    /**
70
     * @param string[] $scanDirs
71
     * @param string   $className
72
     *
73
     * @return string[]
74
     */
75
    protected function findClasses(array $scanDirs, string $className): array
76
    {
77
        $classes = [];
78
79
        if (!empty($scanDirs)) {
80
            $robot = new RobotLoader();
81
82
            // back compatibility to robot loader of version  < 3.0
83
            if (\method_exists($robot, 'setCacheStorage')) {
84
                $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...
85
            }
86
87
            $robot->addDirectory(...$scanDirs);
88
            $robot->acceptFiles = ['*.php'];
89
            $robot->rebuild();
90
            $classes = \array_keys($robot->getIndexedClasses());
91
        }
92
93
        $foundClasses = [];
94
95
        foreach (\array_unique($classes) as $class) {
96
            if (
97
                \class_exists($class)
98
                && ($rc = new ReflectionClass($class)) && $rc->isSubclassOf($className)
99
                && !$rc->isAbstract()
100
            ) {
101
                $foundClasses[] = $rc->getName();
102
            }
103
        }
104
105
        return $foundClasses;
106
    }
107
}
108