Completed
Pull Request — develop (#563)
by Narcotic
63:42
created

GeneratorHashCompilerPass::process()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 45
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 45
rs 8.439
cc 5
eloc 31
nc 4
nop 1
1
<?php
2
/**
3
 * a compilerpass that computes a single hash of the current generated bundles
4
 */
5
6
namespace Graviton\GeneratorBundle\DependencyInjection\Compiler;
7
8
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\Finder\Finder;
11
12
/**
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class GeneratorHashCompilerPass implements CompilerPassInterface
18
{
19
20
    /**
21
     * generate the hashes
22
     *
23
     * @param ContainerBuilder $container container builder
24
     *
25
     * @return void
26
     */
27
    public function process(ContainerBuilder $container)
28
    {
29
        // first type of hash: the genhash'es of all GravitonDyn bundles
30
        $dir = $container->getParameter('graviton.generator.dynamicbundle.dir');
31
32
        $dynHash = '';
33
        if (is_dir($dir)) {
34
            $finder = (new Finder())
35
                ->in($dir)
36
                ->files()
37
                ->sortByName()
38
                ->name('genhash');
39
40
            foreach ($finder as $file) {
41
                $dynHash .= DIRECTORY_SEPARATOR . $file->getContents();
42
            }
43
        }
44
45
        // 2nd hash: configuration of our own graviton things (static stuff)
46
        $staticHash = '';
47
        if (is_dir(__DIR__.'/../../../')) {
48
            $finder = (new Finder())
49
                ->in(__DIR__ . '/../../../')
50
                ->path('/serializer/')
51
                ->path('/doctrine/')
52
                ->path('/schema/')
53
                ->notPath('/Tests/')
54
                ->files()
55
                ->sortByName()
56
                ->name('*.xml')
57
                ->name('*.json');
58
59
            foreach ($finder as $file) {
60
                $staticHash .= DIRECTORY_SEPARATOR . sha1_file($file->getPathname());
61
            }
62
        }
63
64
        $dynHash = sha1($dynHash);
65
        $staticHash = sha1($staticHash);
66
        $allHash = sha1($dynHash . DIRECTORY_SEPARATOR . $staticHash);
67
68
        $container->setParameter('graviton.generator.hash.dyn', $dynHash);
69
        $container->setParameter('graviton.generator.hash.static', $staticHash);
70
        $container->setParameter('graviton.generator.hash.all', $allHash);
71
    }
72
}
73