Passed
Pull Request — master (#20)
by
unknown
02:47
created

TranslationExtension::loadConfiguration()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 16
c 2
b 0
f 0
dl 0
loc 28
ccs 0
cts 17
cp 0
rs 8.8333
cc 7
nc 64
nop 0
crap 56
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Efabrica\Translatte\Bridge\Nette;
6
7
use Efabrica\Translatte\Latte\TranslateMacros;
8
use Efabrica\Translatte\Resolver\ChainResolver;
9
use Efabrica\Translatte\Resource\NeonDirectoryResource;
10
use Efabrica\Translatte\Translator;
11
use Latte\Engine;
12
use Nette\Application\UI\ITemplateFactory;
13
use Nette\DI\CompilerExtension;
14
use Nette\DI\Definitions\FactoryDefinition;
15
use Nette\DI\Definitions\Statement;
16
use Nette\DI\DynamicParameter;
17
use Nette\DI\ServiceDefinition;
18
use Nette\PhpGenerator\PhpLiteral;
19
use Nette\Schema\Expect;
20
use Nette\Schema\Schema;
21
22
class TranslationExtension extends CompilerExtension
23
{
24
    public function getConfigSchema(): Schema
25
    {
26
        return Expect::structure([
27
            'default' => Expect::string()->required(),
28
            'fallback' => Expect::arrayOf('string'),
29
            'dirs' => Expect::arrayOf('string'),
30
            'cache' => Expect::anyOf(Expect::type(Statement::class), Expect::type(DynamicParameter::class)),
31
            'resolvers' => Expect::arrayOf(Statement::class),
32
            'resources' => Expect::arrayOf(Statement::class),
33
            'recordTranslate' => Expect::anyOf(Expect::type(Statement::class), Expect::type(DynamicParameter::class)),
34
        ]);
35
    }
36
37
    public function loadConfiguration(): void
38
    {
39
        $builder = $this->getContainerBuilder();
40
41
        // Prepare params for translator
42
        $params = ['defaultLang' => $this->config->default];
43
        if (!empty($this->config->resolvers)) {
44
            $params['resolver'] = new Statement(ChainResolver::class, [$this->config->resolvers]);
45
        }
46
        if ($this->config->cache) {
47
            $params['cache'] = $this->config->cache;
48
        }
49
        if ($this->config->recordTranslate) {
50
            $params['recordTranslate'] = $this->config->recordTranslate;
51
        }
52
53
        $translator = $builder->addDefinition($this->prefix('translator'))
54
            ->setFactory(Translator::class, $params);
55
56
        // Configure translator
57
        foreach ($this->config->resources as $resource) {
58
            $translator->addSetup('addResource', [$resource]);
59
        }
60
        if (!empty($this->config->fallback)) {
61
            $translator->addSetup('setFallbackLanguages', [$this->config->fallback]);
62
        }
63
        if (!empty($this->config->dirs)) {
64
            $translator->addSetup('addResource', [new Statement(NeonDirectoryResource::class, [$this->config->dirs])]);
65
        }
66
    }
67
68
    public function beforeCompile(): void
69
    {
70
        $builder = $this->getContainerBuilder();
71
72
        /** @var ServiceDefinition $translator */
73
        $translator = $builder->getDefinition($this->prefix('translator'));
74
75
        $templateFactoryName = $builder->getByType(ITemplateFactory::class);
76
        if ($templateFactoryName !== null) {
77
            /** @var ServiceDefinition $templateFactory */
78
            $templateFactory = $builder->getDefinition($templateFactoryName);
79
            $templateFactory->addSetup('
0 ignored issues
show
Bug introduced by
The method addSetup() does not exist on Nette\DI\ServiceDefinition. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
            $templateFactory->/** @scrutinizer ignore-call */ 
80
                              addSetup('

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
80
					$service->onCreate[] = function (Nette\\Bridges\\ApplicationLatte\\Template $template): void {
81
						$template->setTranslator(?);
82
					};', [$translator]);
83
        }
84
85
        if ($builder->hasDefinition('latte.latteFactory')) {
86
            /** @var FactoryDefinition $latteFactory */
87
            $latteFactory = $builder->getDefinition('latte.latteFactory');
88
            $latteFactory->getResultDefinition()
89
                ->addSetup('addProvider', ['translator', $builder->getDefinition($this->prefix('translator'))]);
90
91
            if (version_compare(Engine::VERSION, '3', '<')) {
92
                $latteFactory->getResultDefinition()->addSetup('?->onCompile[] = function($engine) { ?::install($engine->getCompiler()); }', ['@self', new PhpLiteral(TranslateMacros::class)]);
93
            }
94
        }
95
    }
96
}
97