Total Complexity | 7 |
Total Lines | 44 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
13 | class ModelsFinder |
||
14 | { |
||
15 | /** |
||
16 | * Find the models in the given directory. |
||
17 | * |
||
18 | * @param string $directory |
||
19 | * @return array |
||
20 | */ |
||
21 | public function findInDirectory(string $directory) : array |
||
22 | { |
||
23 | $models = []; |
||
24 | $iterator = Finder::create()->files()->name('*.php')->in($directory)->depth(0)->sortByName(); |
||
25 | |||
26 | foreach ($iterator as $file) { |
||
27 | if (!class_exists($class = $this->qualifyClassFromFile($file))) { |
||
28 | continue; |
||
29 | } |
||
30 | |||
31 | $isAbstract = (new ReflectionClass($class))->isAbstract(); |
||
32 | $isModel = is_subclass_of($class, 'Illuminate\Database\Eloquent\Model'); |
||
33 | |||
34 | if ($isModel && !$isAbstract) { |
||
35 | $models[] = new $class; |
||
36 | } |
||
37 | } |
||
38 | |||
39 | return $models; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Retrieve the fully-qualified class name of the given file. |
||
44 | * |
||
45 | * @param SplFileInfo $file |
||
46 | * @return string|null |
||
47 | */ |
||
48 | protected function qualifyClassFromFile(SplFileInfo $file) : ?string |
||
57 | } |
||
58 | } |
||
59 |