Completed
Pull Request — master (#244)
by Łukasz
11:47
created

AdminClassFinder::findClassesInPaths()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 14
nc 9
nop 1
1
<?php
2
3
namespace FSi\Bundle\AdminBundle\Finder;
4
5
use Symfony\Component\Finder\Finder;
6
7
class AdminClassFinder
8
{
9
    const ADMIN_ELEMENT_INTERFACE = 'FSi\\Bundle\\AdminBundle\\Admin\\Element';
10
11
    public function findClasses($paths = array())
12
    {
13
        $classes = $this->findClassesInPaths($paths);
14
15
        return $this->filterAdminClasses($classes);
16
    }
17
18
    /**
19
     * @param $classes
20
     * @return array
21
     */
22
    private function filterAdminClasses($classes)
23
    {
24
        $adminClasses = array();
25
        foreach ($classes as $className) {
26
            $classImplements = class_implements($className);
27
            if (in_array(self::ADMIN_ELEMENT_INTERFACE, $classImplements)) {
28
                $adminClasses[] = $className;
29
            }
30
        }
31
32
        return $adminClasses;
33
    }
34
35
    /**
36
     * @param $searchPaths
37
     * @return array
38
     */
39
    private function findClassesInPaths($searchPaths)
40
    {
41
        $finder = new Finder();
42
        $includedFiles = array();
43
        foreach ($searchPaths as $path) {
44
            foreach ($finder->files()->name('*.php')->in($path) as $file) {
45
                require_once $file->getRealpath();
46
                $includedFiles[] = $file->getRealpath();
47
            }
48
        }
49
50
        $declared = get_declared_classes();
51
52
        $classes = array();
53
        foreach ($declared as $className) {
54
            $reflection = new \ReflectionClass($className);
55
            if (in_array($reflection->getFileName(), $includedFiles)) {
56
                $classes[] = $className;
57
            }
58
        }
59
60
        return $classes;
61
    }
62
}
63