1 |
cb15593b
|
Cajova-Houba
|
<?php
|
2 |
|
|
|
3 |
|
|
namespace Illuminate\Filesystem;
|
4 |
|
|
|
5 |
|
|
use Symfony\Component\Finder\Finder;
|
6 |
|
|
|
7 |
|
|
class ClassFinder
|
8 |
|
|
{
|
9 |
|
|
/**
|
10 |
|
|
* Find all the class and interface names in a given directory.
|
11 |
|
|
*
|
12 |
|
|
* @param string $directory
|
13 |
|
|
* @return array
|
14 |
|
|
*/
|
15 |
|
|
public function findClasses($directory)
|
16 |
|
|
{
|
17 |
|
|
$classes = [];
|
18 |
|
|
|
19 |
|
|
foreach (Finder::create()->in($directory)->name('*.php') as $file) {
|
20 |
|
|
$classes[] = $this->findClass($file->getRealPath());
|
21 |
|
|
}
|
22 |
|
|
|
23 |
|
|
return array_filter($classes);
|
24 |
|
|
}
|
25 |
|
|
|
26 |
|
|
/**
|
27 |
|
|
* Extract the class name from the file at the given path.
|
28 |
|
|
*
|
29 |
|
|
* @param string $path
|
30 |
|
|
* @return string|null
|
31 |
|
|
*/
|
32 |
|
|
public function findClass($path)
|
33 |
|
|
{
|
34 |
|
|
$namespace = null;
|
35 |
|
|
|
36 |
|
|
$tokens = token_get_all(file_get_contents($path));
|
37 |
|
|
|
38 |
|
|
foreach ($tokens as $key => $token) {
|
39 |
|
|
if ($this->tokenIsNamespace($token)) {
|
40 |
|
|
$namespace = $this->getNamespace($key + 2, $tokens);
|
41 |
|
|
} elseif ($this->tokenIsClassOrInterface($token)) {
|
42 |
|
|
return ltrim($namespace.'\\'.$this->getClass($key + 2, $tokens), '\\');
|
43 |
|
|
}
|
44 |
|
|
}
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
/**
|
48 |
|
|
* Find the namespace in the tokens starting at a given key.
|
49 |
|
|
*
|
50 |
|
|
* @param int $key
|
51 |
|
|
* @param array $tokens
|
52 |
|
|
* @return string|null
|
53 |
|
|
*/
|
54 |
|
|
protected function getNamespace($key, array $tokens)
|
55 |
|
|
{
|
56 |
|
|
$namespace = null;
|
57 |
|
|
|
58 |
|
|
$tokenCount = count($tokens);
|
59 |
|
|
|
60 |
|
|
for ($i = $key; $i < $tokenCount; $i++) {
|
61 |
|
|
if ($this->isPartOfNamespace($tokens[$i])) {
|
62 |
|
|
$namespace .= $tokens[$i][1];
|
63 |
|
|
} elseif ($tokens[$i] == ';') {
|
64 |
|
|
return $namespace;
|
65 |
|
|
}
|
66 |
|
|
}
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
/**
|
70 |
|
|
* Find the class in the tokens starting at a given key.
|
71 |
|
|
*
|
72 |
|
|
* @param int $key
|
73 |
|
|
* @param array $tokens
|
74 |
|
|
* @return string|null
|
75 |
|
|
*/
|
76 |
|
|
protected function getClass($key, array $tokens)
|
77 |
|
|
{
|
78 |
|
|
$class = null;
|
79 |
|
|
|
80 |
|
|
$tokenCount = count($tokens);
|
81 |
|
|
|
82 |
|
|
for ($i = $key; $i < $tokenCount; $i++) {
|
83 |
|
|
if ($this->isPartOfClass($tokens[$i])) {
|
84 |
|
|
$class .= $tokens[$i][1];
|
85 |
|
|
} elseif ($this->isWhitespace($tokens[$i])) {
|
86 |
|
|
return $class;
|
87 |
|
|
}
|
88 |
|
|
}
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
/**
|
92 |
|
|
* Determine if the given token is a namespace keyword.
|
93 |
|
|
*
|
94 |
|
|
* @param array|string $token
|
95 |
|
|
* @return bool
|
96 |
|
|
*/
|
97 |
|
|
protected function tokenIsNamespace($token)
|
98 |
|
|
{
|
99 |
|
|
return is_array($token) && $token[0] == T_NAMESPACE;
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
/**
|
103 |
|
|
* Determine if the given token is a class or interface keyword.
|
104 |
|
|
*
|
105 |
|
|
* @param array|string $token
|
106 |
|
|
* @return bool
|
107 |
|
|
*/
|
108 |
|
|
protected function tokenIsClassOrInterface($token)
|
109 |
|
|
{
|
110 |
|
|
return is_array($token) && ($token[0] == T_CLASS || $token[0] == T_INTERFACE);
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
/**
|
114 |
|
|
* Determine if the given token is part of the namespace.
|
115 |
|
|
*
|
116 |
|
|
* @param array|string $token
|
117 |
|
|
* @return bool
|
118 |
|
|
*/
|
119 |
|
|
protected function isPartOfNamespace($token)
|
120 |
|
|
{
|
121 |
|
|
return is_array($token) && ($token[0] == T_STRING || $token[0] == T_NS_SEPARATOR);
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
/**
|
125 |
|
|
* Determine if the given token is part of the class.
|
126 |
|
|
*
|
127 |
|
|
* @param array|string $token
|
128 |
|
|
* @return bool
|
129 |
|
|
*/
|
130 |
|
|
protected function isPartOfClass($token)
|
131 |
|
|
{
|
132 |
|
|
return is_array($token) && $token[0] == T_STRING;
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
/**
|
136 |
|
|
* Determine if the given token is whitespace.
|
137 |
|
|
*
|
138 |
|
|
* @param array|string $token
|
139 |
|
|
* @return bool
|
140 |
|
|
*/
|
141 |
|
|
protected function isWhitespace($token)
|
142 |
|
|
{
|
143 |
|
|
return is_array($token) && $token[0] == T_WHITESPACE;
|
144 |
|
|
}
|
145 |
|
|
}
|