GridSourceCompilerPass::cacheAllFiles()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 2
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\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...
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\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...
12
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...
13
14
class GridSourceCompilerPass implements CompilerPassInterface
15
{
16
    /**
17
     * @throws \ReflectionException
18
     * @throws \Exception
19
     */
20
    public function process(ContainerBuilder $container)
21
    {
22
        self::addDoctrine($container);
23
24
        if ($container->has('twig')) {
25
            $container->getDefinition('dtc_grid.renderer.factory')->addMethodCall('setTwigEnvironment', [new Reference('twig')]);
26
        }
27
28
        // Add each worker to workerManager, make sure each worker has instance to work
29
        foreach ($container->findTaggedServiceIds('dtc_grid.source') as $id => $attributes) {
30
            self::addGridSource($container, $id);
31
        }
32
33
        self::addGridFiles($container);
34
        self::addLocalCssJs($container, 'css');
35
        self::addLocalCssJs($container, 'js');
36
        self::legacyTwigExtension($container);
37
    }
38
39
    private static function legacyTwigExtension(ContainerBuilder $container)
40
    {
41
        if (!class_exists('\Twig\Extension\AbstractExtension')) {
42
            $container->getDefinition('dtc_grid.twig.extension')->setClass('Dtc\GridBundle\Twig\Extension\TwigExtensionLegacy');
43
        }
44
    }
45
46
    private static function addDoctrine(ContainerBuilder $container)
47
    {
48
        $sourceManager = $container->getDefinition('dtc_grid.manager.source');
49
50
        if ($container->has('doctrine')) {
51
            $sourceManager->addMethodCall('setRegistry', [new Reference('doctrine')]);
52
        }
53
54
        if ($container->has('doctrine_mongodb')) {
55
            $sourceManager->addMethodCall('setMongodbRegistry', [new Reference('doctrine_mongodb')]);
56
        }
57
    }
58
59
    /**
60
     * @param $id
61
     *
62
     * @throws \ReflectionException
63
     */
64
    public static function addGridSource(ContainerBuilder $container, $id)
65
    {
66
        $sourceManager = $container->getDefinition('dtc_grid.manager.source');
67
        $gridSourceDefinition = $container->getDefinition($id);
68
        $class = $gridSourceDefinition->getClass();
69
70
        $refClass = new \ReflectionClass($class);
71
        $interface = 'Dtc\GridBundle\Grid\Source\GridSourceInterface';
72
73
        if (!$refClass->implementsInterface($interface)) {
74
            throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
75
        }
76
77
        $gridSourceDefinition->addMethodCall('setId', [$id]);
78
        $sourceManager->addMethodCall('add', [$id, new Reference($id)]);
79
    }
80
81
    /**
82
     * @throws \Exception
83
     */
84
    private static function addGridFiles(ContainerBuilder $container)
85
    {
86
        $cacheDir = $container->getParameter('kernel.cache_dir');
87
        if ($container->hasParameter('kernel.project_dir')) {
88
            $directory = $container->getParameter('kernel.project_dir').\DIRECTORY_SEPARATOR.'config'.\DIRECTORY_SEPARATOR.'dtc_grid';
89
            if (is_dir($directory)) {
90
                $finder = new Finder();
91
                $finder->files()->in(str_replace(\DIRECTORY_SEPARATOR, '/', $directory));
92
                self::cacheAllFiles($cacheDir, $finder);
93
                $container->addResource(new DirectoryResource($directory));
94
            }
95
            $container->addResource(new FileExistenceResource($directory));
96
            $container->addResource(new DirectoryResource(dirname(dirname(__DIR__))));
97
        } elseif ($container->hasParameter('kernel.root_dir')) {
98
            // Typically Symfony versions < 4 using the older directory layout.
99
            $directory = str_replace(\DIRECTORY_SEPARATOR, '/', $container->getParameter('kernel.root_dir')).'/../src';
100
            $finder = new Finder();
101
            $finder->files()->in($directory)->name('dtc_grid.yaml')->name('dtc_grid.yml')->path('Resources/config');
102
            self::cacheAllFiles($cacheDir, $finder);
103
            if (class_exists('Symfony\Component\Config\Resource\GlobResource')) {
104
                $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...
105
                $container->addResource(new \Symfony\Component\Config\Resource\GlobResource(str_replace('/', \DIRECTORY_SEPARATOR, $directory), str_replace('/', \DIRECTORY_SEPARATOR, '/**/Resources/config/dtc_grid.yml'), false));
106
            }
107
            // 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.
108
        }
109
    }
110
111
    private static function addLocalCssJs(ContainerBuilder $container, $type)
112
    {
113
        $parameter = 'dtc_grid.datatables.local.files.'.$type;
114
        if ($container->hasParameter($parameter)) {
115
            foreach ($container->getParameter($parameter) as $filepath) {
116
                $container->addResource(new FileResource($filepath));
117
            }
118
        }
119
    }
120
121
    /**
122
     * @param $cacheDir
123
     *
124
     * @throws \Exception
125
     */
126
    private static function cacheAllFiles($cacheDir, Finder $finder)
127
    {
128
        foreach ($finder as $file) {
129
            ColumnUtil::cacheClassesFromFile($cacheDir, $file->getRealPath());
130
        }
131
    }
132
}
133