Completed
Push — master ( 030030...90a95a )
by Matze
10:56 queued 07:21
created

Rebuild::dumpContainer()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 23
ccs 16
cts 16
cp 1
rs 9.0857
cc 2
eloc 16
nc 1
nop 1
crap 2
1
<?php
2
3
namespace BrainExe\Core\DependencyInjection;
4
5
use BrainExe\Annotations\Annotations\Service;
6
use BrainExe\Annotations\Loader;
7
use BrainExe\Core\Core;
8
use BrainExe\Core\DependencyInjection\CompilerPass\GlobalCompilerPass;
9
10
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper;
11
use Symfony\Component\DependencyInjection\Container;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
14
15
use Symfony\Component\Finder\Finder;
16
use Symfony\Component\Finder\SplFileInfo;
17
18
/**
19
 * @Service("Core.Rebuild", public=false)
20
 */
21
class Rebuild
22
{
23
24
    /**
25
     * @param bool $boot
26
     * @return Container|ContainerBuilder
27
     */
28 2
    public function rebuildDIC($boot = true)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
29
    {
30 2
        $containerBuilder = new ContainerBuilder();
31
32 2
        $this->readAnnotations($containerBuilder);
33
34 2
        $containerBuilder->addCompilerPass(new GlobalCompilerPass());
35 2
        $containerBuilder->compile();
36
37 2
        $this->dumpContainer($containerBuilder);
38
39 2
        if ($boot) {
40 1
            $core = new Core();
41 1
            return $core->boot();
42
        }
43
44 1
        return $containerBuilder;
45
    }
46
47
    /**
48
     * @param ContainerBuilder $container
49
     */
50 2
    protected function readAnnotations(ContainerBuilder $container)
51
    {
52 2
        $annotationLoader = new Loader($container);
53 2
        $appFinder = new Finder();
54
55 2
        $appFinder->directories()
56 2
            ->in([ROOT, CORE_ROOT, BRAINEXE_VENDOR_ROOT])
57 2
            ->depth("<=1")
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal <=1 does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
58 2
            ->name('src');
59
60 2
        $annotationLoader->load('src/');
61 2
        $annotationLoader->load(CORE_ROOT);
62
63 2
        foreach ($appFinder as $dir) {
64
            /** @var SplFileInfo $dir */
65 2
            $dirName = $dir->getPathname();
66 2
            $annotationLoader->load($dirName);
67
        }
68 2
    }
69
70
    /**
71
     * @param ContainerBuilder $container
72
     */
73 2
    protected function dumpContainer(ContainerBuilder $container)
74
    {
75 2
        $randomId      = mt_rand();
76 2
        $className     = sprintf('dic_%d', $randomId);
77 2
        $containerFile = 'cache/dic.php';
78
79 2
        $dumper = new PhpDumper($container);
80 2
        $dumper->setProxyDumper(new ProxyDumper());
81
82 2
        $containerContent = $dumper->dump(['class' => $className]);
83 2
        file_put_contents('cache/dic.php', $containerContent);
84 2
        file_put_contents('cache/dic.txt', $className);
85 2
        @chmod($containerFile, 0777);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
86
87 2
        $debug = $container->getParameter('debug');
88 2
        file_put_contents(
89 2
            ROOT . 'cache/config.json',
90
            json_encode(
91 2
                $container->getParameterBag()->all(),
92 2
                $debug ? JSON_PRETTY_PRINT : null
93
            )
94
        );
95 2
    }
96
}
97