Passed
Push — master ( 1316c5...63993c )
by Matthew
05:18
created

GridSourceCompilerPass::addDoctrine()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dtc\GridBundle\DependencyInjection\Compiler;
4
5
use Dtc\GridBundle\Util\ColumnUtil;
6
use Symfony\Component\Config\Resource\DirectoryResource;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Config...ource\DirectoryResource was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Symfony\Component\Config\Resource\FileExistenceResource;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Config...e\FileExistenceResource was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\Config\Resource\FileResource;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Config\Resource\FileResource was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Component\DependencyInjection\Container;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\DependencyInjection\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...ection\ContainerBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Depend...r\CompilerPassInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Symfony\Component\DependencyInjection\Reference;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\DependencyInjection\Reference was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Symfony\Component\Finder\Finder;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Finder\Finder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
15
class GridSourceCompilerPass implements CompilerPassInterface
16
{
17
    /**
18
     * @param ContainerBuilder $container
19
     * @throws \ReflectionException
20
     * @throws \Exception
21
     */
22
    public function process(ContainerBuilder $container)
23
    {
24
        self::addDoctrine($container);
25
26
        if ($container->has('twig')) {
27
            $container->getDefinition('dtc_grid.renderer.factory')->addMethodCall('setTwigEnvironment', [new Reference('twig')]);
28
        }
29
30
        // Add each worker to workerManager, make sure each worker has instance to work
31
        foreach ($container->findTaggedServiceIds('dtc_grid.source') as $id => $attributes) {
32
            self::addGridSource($container, $id);
33
        }
34
35
        self::addGridFiles($container);
36
        self::addLocalCssJs($container, 'css');
37
        self::addLocalCssJs($container, 'js');
38
        self::legacyTwigExtension($container);
39
    }
40
41
    private static function legacyTwigExtension(ContainerBuilder $container) {
42
        if (!class_exists('Twig\Extension\AbstractExtension')) {
43
            $container->getDefinition('dtc_grid.twig.extension')->setClass('Dtc\GridBundle\Twig\Extension\TwigExtensionLegacy');
44
        }
45
    }
46
47
    private static function addDoctrine(ContainerBuilder $container)
48
    {
49
        $sourceManager = $container->getDefinition('dtc_grid.manager.source');
50
51
        if ($container->has('doctrine')) {
52
            $sourceManager->addMethodCall('setRegistry', [new Reference('doctrine')]);
53
        }
54
55
        if ($container->has('doctrine_mongodb')) {
56
            $sourceManager->addMethodCall('setMongodbRegistry', [new Reference('doctrine_mongodb')]);
57
        }
58
    }
59
60
    /**
61
     * @param ContainerBuilder $container
62
     * @param $id
63
     *
64
     * @throws \ReflectionException
65
     */
66
    public static function addGridSource(ContainerBuilder $container, $id)
67
    {
68
        $sourceManager = $container->getDefinition('dtc_grid.manager.source');
69
        $gridSourceDefinition = $container->getDefinition($id);
70
        $class = $gridSourceDefinition->getClass();
71
72
        $refClass = new \ReflectionClass($class);
73
        $interface = 'Dtc\GridBundle\Grid\Source\GridSourceInterface';
74
75
        if (!$refClass->implementsInterface($interface)) {
76
            throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
77
        }
78
79
        $gridSourceDefinition->addMethodCall('setId', array($id));
80
        $sourceManager->addMethodCall('add', [$id, new Reference($id)]);
81
    }
82
83
    /**
84
     * @param ContainerBuilder $container
85
     *
86
     * @throws \Exception
87
     */
88
    private static function addGridFiles(ContainerBuilder $container)
89
    {
90
        $cacheDir = $container->getParameter('kernel.cache_dir');
91
        if ($container->hasParameter('kernel.project_dir')) {
92
            $directory = $container->getParameter('kernel.project_dir') . \DIRECTORY_SEPARATOR . 'config' . \DIRECTORY_SEPARATOR . 'dtc_grid';
93
            if (is_dir($directory)) {
94
                $finder = new Finder();
95
                $finder->files()->in(str_replace(\DIRECTORY_SEPARATOR, '/', $directory));
96
                self::cacheAllFiles($cacheDir, $finder);
97
                $container->addResource(new DirectoryResource($directory));
98
            }
99
            $container->addResource(new FileExistenceResource($directory));
100
        } elseif ($container->hasParameter('kernel.root_dir')) {
101
            // Typically Symfony versions < 4 using the older directory layout.
102
            $directory = str_replace(\DIRECTORY_SEPARATOR, '/', $container->getParameter('kernel.root_dir')).'/../src';
103
            $finder = new Finder();
104
            $finder->files()->in($directory)->name('dtc_grid.yaml')->name('dtc_grid.yml')->path('Resources/config');
105
            self::cacheAllFiles($cacheDir, $finder);
106
            if (class_exists('Symfony\Component\Config\Resource\GlobResource')) {
107
                $container->addResource(new \Symfony\Component\Config\Resource\GlobResource(str_replace('/', \DIRECTORY_SEPARATOR, $directory),str_replace('/', \DIRECTORY_SEPARATOR, '/**/Resources/config/dtc_grid.yaml'), false));
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Config\Resource\GlobResource was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
108
                $container->addResource(new \Symfony\Component\Config\Resource\GlobResource(str_replace('/', \DIRECTORY_SEPARATOR, $directory),str_replace('/', \DIRECTORY_SEPARATOR, '/**/Resources/config/dtc_grid.yml'), false));
109
            }
110
            // TODO: To cover symfony versions that don't support GlobResource, such as 2.x, it would probably be necessary to add a recursive set of FileExistenceResources here.
111
        }
112
    }
113
114
    private static function addLocalCssJs(ContainerBuilder $container, $type) {
115
        $parameter = 'dtc_grid.datatables.local.files.'.$type;
116
        if ($container->hasParameter($parameter)) {
117
            foreach($container->getParameter($parameter) as $filepath) {
118
                $container->addResource(new FileResource($filepath));
119
            }
120
        }
121
    }
122
123
    /**
124
     * @param $cacheDir
125
     * @param Finder $finder
126
     * @throws \Exception
127
     */
128
    private static function cacheAllFiles($cacheDir, Finder $finder) {
129
        foreach ($finder as $file) {
130
            ColumnUtil::cacheClassesFromFile($cacheDir, $file->getRealPath());
131
        }
132
    }
133
}
134