ClassFinder.php 2.74 KB
Newer Older
1
<?php declare(strict_types=1);
2
/*
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 * This file is part of "irstea/plantuml-bundle".
 *
 * Copyright (C) 2016-2019 IRSTEA
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License and the GNU
 * Lesser General Public License along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
19
20
21
22
23
24
25
26
27
28
29
30
 */

namespace Irstea\PlantUmlBundle\Finder;

use ArrayIterator;
use Irstea\PlantUmlBundle\Model\Filter\DirectoryFilter;
use ReflectionClass;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

/**
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
31
 * Description of ClassFinder.
32
33
34
35
36
37
38
39
40
41
42
43
44
 */
class ClassFinder implements FinderInterface
{
    /**
     * @var string[]
     */
    private $directories;

    /**
     * @var \ReflectionClass
     */
    private $classes = null;

45
46
    /**
     * ClassFinder constructor.
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
47
     *
48
49
50
     * @param string[] $directories
     */
    public function __construct(array $directories)
51
52
53
54
    {
        $this->directories = $directories;
    }

55
    private function initialize(): void
56
57
58
59
60
61
62
    {
        $files = Finder::create()
            ->in($this->directories)
            ->files()
            ->name('*.php')
            ->getIterator();

Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
63
        foreach ($files as $file) {
64
65
66
67
            /* @var $file SplFileInfo */
            $path = $file->getPathname();
            try {
                irstea_plantmul_include($path);
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
68
            } catch (Exception $ex) {
69
70
71
72
73
74
                printf("%s: %s (%s)\n", $path, get_class($ex), $ex->getMessage());
            }
        }

        $filter = new DirectoryFilter($this->directories);
        $this->classes = [];
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
75
        foreach (get_declared_classes() as $className) {
76
77
78
79
80
81
82
            $class = new ReflectionClass($className);
            if ($filter->accept($class)) {
                $this->classes[$className] = $class;
            }
        }
    }

83
84
85
    /**
     * @return ArrayIterator|\Traversable
     */
86
87
88
89
90
    public function getIterator()
    {
        if ($this->classes === null) {
            $this->initialize();
        }
Guillaume Perréal's avatar
CS.    
Guillaume Perréal committed
91

92
93
        return new ArrayIterator($this->classes);
    }
94

95
96
97
    /**
     * @param array $conf
     */
98
99
100
101
102
    public function toConfig(array &$conf)
    {
        $conf['type'] = 'classes';
        $conf['directories'] = $this->directories;
    }
103
}