Completed
Push — master ( 689521...8ed57c )
by Matze
09:40
created

CompilerPass::process()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 8.5806
cc 4
eloc 20
nc 5
nop 1
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 ReflectionException;
10
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
13
/**
14
 * @CompilerPassAnnotation
15
 */
16
class CompilerPass implements CompilerPassInterface
17
{
18
19
    const CACHE_FILE = ROOT . 'cache/translation_token';
20
21
    use FileCacheTrait;
22
23
    const TAG = 'middleware';
24
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function process(ContainerBuilder $container)
29
    {
30
        $serviceIds = $container->getServiceIds();
31
32
        $tokens = [];
33
        foreach ($serviceIds as $serviceId) {
34
            try {
35
                $class = $container->getDefinition($serviceId)->getClass();
36
                $reflection = new ReflectionClass($class);
37
            } catch (Exception $e) {
38
                continue;
39
            }
40
41
            if ($reflection->implementsInterface(TranslationProvider::class)) {
42
                /** @var TranslationProvider $class */
43
                $tokens = array_merge($tokens, $class::getTokens());
44
            }
45
        }
46
47
        sort($tokens);
48
49
        $contentPhp = sprintf("return [\n    %s\n];\n", implode(",\n    ", array_map(function ($token) {
50
            return sprintf('_("%s")', addslashes($token));
51
        }, $tokens)));
52
53
        $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...
54
            return sprintf('<span translate>%s</span>', addslashes($token));
55
        }, $tokens)));
56
57
        $this->dumpCacheFile(self::CACHE_FILE, $contentPhp);
58
        file_put_contents(self::CACHE_FILE . '.html', $contentHtml);
59
    }
60
}
61