Completed
Push — feature/try-update-symfony-32 ( f32603...80dbe1 )
by Narcotic
149:00 queued 83:23
created

GeneratorHashCompilerPass   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
c 1
b 0
f 1
lcom 0
cbo 2
dl 0
loc 52
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 41 3
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
        $finder = (new Finder())
33
            ->in($dir)
34
            ->files()
35
            ->sortByName()
36
            ->name('genhash');
37
38
        $dynHash = '';
39
        foreach ($finder as $file) {
40
            $dynHash .= DIRECTORY_SEPARATOR . $file->getContents();
41
        }
42
43
        // 2nd hash: configuration of our own graviton things (static stuff)
44
        $finder = (new Finder())
45
            ->in(__DIR__.'/../../../')
46
            ->path('/serializer/')
47
            ->path('/doctrine/')
48
            ->path('/schema/')
49
            ->notPath('/Tests/')
50
            ->files()
51
            ->sortByName()
52
            ->name('*.xml')
53
            ->name('*.json');
54
55
        $staticHash = '';
56
        foreach ($finder as $file) {
57
            $staticHash .= DIRECTORY_SEPARATOR . sha1_file($file->getPathname());
58
        }
59
60
        $dynHash = sha1($dynHash);
61
        $staticHash = sha1($staticHash);
62
        $allHash = sha1($dynHash . DIRECTORY_SEPARATOR . $staticHash);
63
64
        $container->setParameter('graviton.generator.hash.dyn', $dynHash);
65
        $container->setParameter('graviton.generator.hash.static', $staticHash);
66
        $container->setParameter('graviton.generator.hash.all', $allHash);
67
    }
68
}
69