Completed
Push — master ( fd6c7c...1f9fde )
by Matze
09:32
created

CompilerPass::process()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.6383

Importance

Changes 5
Bugs 1 Features 0
Metric Value
c 5
b 1
f 0
dl 0
loc 31
ccs 13
cts 17
cp 0.7647
rs 6.7272
cc 7
eloc 17
nc 9
nop 1
crap 7.6383
1
<?php
2
3
namespace BrainExe\Core\Translation;
4
5
use BrainExe\Core\Annotations\CompilerPass as CompilerPassAnnotation;
6
use BrainExe\Core\Traits\FileCacheTrait;
7
use Exception;
8
use ReflectionClass;
9
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10
use Symfony\Component\DependencyInjection\ContainerBuilder;
11
12
/**
13
 * @CompilerPassAnnotation("Core.Translation.CompilerPass")
14
 */
15
class CompilerPass implements CompilerPassInterface
16
{
17
18
    const CACHE_FILE = ROOT . 'cache/translation_token';
19
20
    use FileCacheTrait;
21
22
    const TAG = 'middleware';
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 2
    public function process(ContainerBuilder $container)
28
    {
29 2
        $serviceIds = $container->getServiceIds();
30
31 2
        $tokens = [];
32 2
        foreach ($serviceIds as $serviceId) {
33
            try {
34 2
                $class = $container->getDefinition($serviceId)->getClass();
35 2
                $reflection = new ReflectionClass($class);
36
            } catch (Exception $e) {
37
                continue;
38
            }
39
40 2
            if ($reflection->implementsInterface(ServiceTranslationProvider::class)) {
41
                /** @var ServiceTranslationProvider $class */
42 2
                $service = $container->get($serviceId);
43
44 2
                foreach ($service->getTokens() as $token) {
45
                    $tokens[] = $token;
46
                }
47
            }
48 2
            if ($reflection->implementsInterface(TranslationProvider::class)) {
49
                /** @var TranslationProvider $class */
50
                foreach ($class::getTokens() as $token) {
51 2
                    $tokens[] = $token;
52
                }
53
            }
54
        }
55
56 2
        $this->dumpTranslations($tokens);
57 2
    }
58
59
    /**
60
     * @param array $tokens
61
     */
62 2
    protected function dumpTranslations(array $tokens)
63
    {
64 2
        sort($tokens);
65
66
        $contentPhp = sprintf("return [\n    %s\n];\n", implode(",\n    ", array_map(function ($token) {
67
            return sprintf('_("%s")', addslashes($token));
68 2
        }, $tokens)));
69
70 2
        $contentHtml = sprintf("%s", implode("\n", array_map(function ($token) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal %s 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...
71
            return sprintf('<span translate>%s</span>', addslashes($token));
72 2
        }, $tokens)));
73
74 2
        $this->dumpCacheFile(self::CACHE_FILE, $contentPhp);
75 2
        file_put_contents(self::CACHE_FILE . '.html', $contentHtml);
76 2
        chmod(self::CACHE_FILE . '.html', 0777);
77 2
    }
78
}
79