AdminAnnotatedElementPass::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminBundle\DependencyInjection\Compiler;
13
14
use Doctrine\Common\Annotations\AnnotationReader;
15
use FSi\Bundle\AdminBundle\Annotation\Element;
16
use FSi\Bundle\AdminBundle\Finder\AdminClassFinder;
17
use ReflectionClass;
18
use Symfony\Component\Config\Resource\DirectoryResource;
19
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Definition;
22
23
/**
24
 * @deprecated since 3.0
25
 */
26
class AdminAnnotatedElementPass implements CompilerPassInterface
27
{
28
    /**
29
     * @var AnnotationReader
30
     */
31
    private $annotationReader;
32
33
    /**
34
     * @var AdminClassFinder
35
     */
36
    private $adminClassFinder;
37
38
    public function __construct(AnnotationReader $annotationReader, AdminClassFinder $adminClassFinder)
39
    {
40
        $this->annotationReader = $annotationReader;
41
        $this->adminClassFinder = $adminClassFinder;
42
    }
43
44
    public function process(ContainerBuilder $container): void
45
    {
46
        $paths = $this->getBundlesAdminPaths($container);
47
48
        $annotatedAdminClasses = $this->findAnnotatedAdminClasses($paths);
49
        $adminElementsDefinitions = [];
50
        foreach ($annotatedAdminClasses as $adminClass) {
51
            $adminElementsDefinitions[] = $this->createAdminElementDefinition($adminClass);
52
        }
53
54
        $container->addDefinitions($adminElementsDefinitions);
55
    }
56
57
    private function getBundlesAdminPaths(ContainerBuilder $container): array
58
    {
59
        $bundleClasses = $container->getParameter('kernel.bundles');
60
        $paths = [];
61
        foreach ($bundleClasses as $bundleClass) {
62
            $bundleClassReflector = new ReflectionClass($bundleClass);
63
            $bundleAdminPath = dirname($bundleClassReflector->getFileName()) . '/Admin';
64
            if (is_dir($bundleAdminPath)) {
65
                $container->addResource(new DirectoryResource($bundleAdminPath, '/\.php$/'));
66
                $paths[] = $bundleAdminPath;
67
            }
68
        }
69
        return $paths;
70
    }
71
72
    private function createAdminElementDefinition(string $class): Definition
73
    {
74
        $definition = new Definition($class);
75
        $definition->addTag('admin.element');
76
77
        return $definition;
78
    }
79
80
    private function findAnnotatedAdminClasses(array $paths): array
81
    {
82
        $annotatedAdminClasses = [];
83
84
        foreach ($this->adminClassFinder->findClasses($paths) as $class) {
85
            $annotation = $this->annotationReader->getClassAnnotation(
86
                new ReflectionClass($class),
87
                Element::class
88
            );
89
90
            if ($annotation instanceof Element) {
91
                $annotatedAdminClasses[] = $class;
92
            }
93
        }
94
95
        return $annotatedAdminClasses;
96
    }
97
}
98