Completed
Pull Request — master (#11)
by Petr
03:43
created

buildControllerIdFromClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of Symplify
5
 * Copyright (c) 2015 Tomas Votruba (http://tomasvotruba.cz).
6
 */
7
8
namespace Symplify\ControllerAutowire\DependencyInjection\Compiler;
9
10
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\DependencyInjection\Definition;
13
use Symfony\Component\DependencyInjection\Reference;
14
use Symplify\ControllerAutowire\Config\Definition\ConfigurationResolver;
15
use Symplify\ControllerAutowire\Contract\DependencyInjection\ControllerClassMapInterface;
16
use Symplify\ControllerAutowire\Contract\HttpKernel\ControllerFinderInterface;
17
18
final class InjectDependenciesPass implements CompilerPassInterface
19
{
20
    /**
21
     * @var ControllerClassMapInterface
22
     */
23
    private $controllerClassMap;
24
25
    /**
26
     * @var ControllerFinderInterface
27
     */
28
    private $controllerFinder;
29
30 2
    public function __construct(
31
        ControllerClassMapInterface $controllerClassMap,
32
        ControllerFinderInterface $controllerFinder
33
    ) {
34 2
        $this->controllerClassMap = $controllerClassMap;
35 2
        $this->controllerFinder = $controllerFinder;
36 2
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 2
    public function process(ContainerBuilder $containerBuilder)
42
    {
43 2
        $controllerDirs = $this->getControllerDirs($containerBuilder);
44 2
        $controllers = $this->controllerFinder->findControllersInDirs($controllerDirs);
45 2
        $this->injectDependenciesToController($controllers, $containerBuilder);
46 2
    }
47
48
    /**
49
     * @return string[]
50
     */
51 2
    private function getControllerDirs(ContainerBuilder $containerBuilder) : array
52
    {
53 2
        $config = (new ConfigurationResolver())->resolveFromContainerBuilder($containerBuilder);
54
55 2
        return $config['controller_dirs'];
56
    }
57
58 2
    private function injectDependenciesToController(array $controllers, ContainerBuilder $containerBuilder)
59
    {
60 2
        foreach ($controllers as $controller) {
61 2
            $id = $this->buildControllerIdFromClass($controller);
62 2
            $controllerDefinition = $containerBuilder->findDefinition($id);
63 2
            $this->injectDependencies($controllerDefinition);
64
        }
65 2
    }
66
67 2
    private function buildControllerIdFromClass(string $class) : string
68
    {
69 2
        return strtr(strtolower($class), ['\\' => '.']);
70
    }
71
72
    /**
73
     * @param Definition $definition
74
     *
75
     * @return Definition
76
     */
77 2
    private function injectDependencies(Definition $definition) : Definition
78
    {
79 2
        if (method_exists($definition->getClass(), 'setDoctrineRegistry')) {
80
            //        if (array_key_exists('Symplify\ControllerAutowire\DependencyInjection\ControllerAwareTrait',
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
81
//            class_uses($definition->getClass()))) {
82
            $definition->addMethodCall('setDoctrineRegistry', [new Reference('doctrine')]);
83
        }
84
85 2
        return $definition;
86
    }
87
}
88