Completed
Push — master ( 28eb0b...2d1f2b )
by Vitaly
03:31
created

Container::loadFromClasses()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by PhpStorm.
4
 * User: root
5
 * Date: 02.08.16
6
 * Time: 0:46.
7
 */
8
9
namespace samsonframework\container;
10
11
use samsonframework\container\metadata\ClassMetadata;
12
use samsonframework\container\resolver\Resolver;
13
use samsonframework\filemanager\FileManagerInterface;
14
15
/**
16
 * Class Container.
17
 */
18
class Container
19
{
20
    /** Controller classes scope name */
21
    const SCOPE_CONTROLLER = 'controllers';
22
23
    /** Service classes scope name */
24
    const SCOPE_SERVICES = 'services';
25
26
    /** @var string[] Collection of available container scopes */
27
    protected $scopes = [
28
        self::SCOPE_CONTROLLER => [],
29
        self::SCOPE_SERVICES => []
30
    ];
31
32
    /** @var ClassMetadata[string] Collection of classes metadata */
33
    protected $classMetadata = [];
34
35
    /** @var FileManagerInterface */
36
    protected $fileManger;
37
38
    /** @var Resolver */
39
    protected $classResolver;
40
41
    /**
42
     * Container constructor.
43
     *
44
     * @param FileManagerInterface $fileManger
45
     */
46 1
    public function __construct(FileManagerInterface $fileManger, Resolver $classResolver)
47
    {
48 1
        $this->fileManger = $fileManger;
49 1
        $this->resolver = $classResolver;
0 ignored issues
show
Bug introduced by
The property resolver does not seem to exist. Did you mean classResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
50 1
    }
51
52
    /**
53
     * Load classes from paths.
54
     *
55
     * @param array $paths Paths for importing
56
     *
57
     * @return $this
58
     */
59 1
    public function loadFromPaths(array $paths)
60
    {
61
        // Iterate all paths and get files
62 1
        foreach ($this->fileManger->scan($paths, ['php']) as $phpFile) {
63
            // Read all classes in given file
64 1
            $this->loadFromClasses($this->getDefinedClasses(require_once($phpFile)));
65
        }
66
67 1
        return $this;
68
    }
69
70
    /**
71
     * Load classes from class names collection.
72
     *
73
     * @param string[] $classes Collection of class names for resolving
74
     *
75
     * @return $this
76
     */
77 1
    public function loadFromClasses(array $classes)
78
    {
79
        // Read all classes in given file
80 1
        foreach ($classes as $className) {
81
            // Resolve class metadata
82 1
            $this->classMetadata[$className] = $this->resolver->resolve(new \ReflectionClass($className));
0 ignored issues
show
Bug introduced by
The property resolver does not seem to exist. Did you mean classResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
83
            // Store class in defined scopes
84 1
            foreach ($this->classMetadata[$className]->scopes as $scope) {
85 1
                $this->scopes[$scope][] = $className;
86
            }
87
        }
88
89 1
        return $this;
90
    }
91
92
    /**
93
     * Find class names defined in PHP code.
94
     *
95
     * @param string $php PHP code for scanning
96
     *
97
     * @return string[] Collection of found class names in php code
98
     */
99 1
    protected function getDefinedClasses($php) : array
100
    {
101 1
        $classes = array();
102 1
        $tokens = token_get_all(is_string($php) ? $php : '');
103
104 1
        for ($i = 2, $count = count($tokens); $i < $count; $i++) {
105
            if ($tokens[$i - 2][0] === T_CLASS
106
                && $tokens[$i - 1][0] === T_WHITESPACE
107
                && $tokens[$i][0] === T_STRING
108
            ) {
109
                $classes[] = $tokens[$i][1];
110
            }
111
        }
112
113 1
        return $classes;
114
    }
115
}
116