TranslationExtension   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
eloc 35
c 3
b 0
f 0
dl 0
loc 67
ccs 0
cts 37
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfigSchema() 0 9 1
A loadConfiguration() 0 25 6
A beforeCompile() 0 25 4
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
        ]);
34
    }
35
36
    public function loadConfiguration(): void
37
    {
38
        $builder = $this->getContainerBuilder();
39
40
        // Prepare params for translator
41
        $params = ['defaultLang' => $this->config->default];
42
        if (!empty($this->config->resolvers)) {
43
            $params['resolver'] = new Statement(ChainResolver::class, [$this->config->resolvers]);
44
        }
45
        if ($this->config->cache) {
46
            $params['cache'] = $this->config->cache;
47
        }
48
49
        $translator = $builder->addDefinition($this->prefix('translator'))
50
            ->setFactory(Translator::class, $params);
51
52
        // Configure translator
53
        foreach ($this->config->resources as $resource) {
54
            $translator->addSetup('addResource', [$resource]);
55
        }
56
        if (!empty($this->config->fallback)) {
57
            $translator->addSetup('setFallbackLanguages', [$this->config->fallback]);
58
        }
59
        if (!empty($this->config->dirs)) {
60
            $translator->addSetup('addResource', [new Statement(NeonDirectoryResource::class, [$this->config->dirs])]);
61
        }
62
    }
63
64
    public function beforeCompile(): void
65
    {
66
        $builder = $this->getContainerBuilder();
67
68
        /** @var ServiceDefinition $translator */
69
        $translator = $builder->getDefinition($this->prefix('translator'));
70
71
        $templateFactoryName = $builder->getByType(ITemplateFactory::class);
72
        if ($templateFactoryName !== null) {
73
            /** @var ServiceDefinition $templateFactory */
74
            $templateFactory = $builder->getDefinition($templateFactoryName);
75
            $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

75
            $templateFactory->/** @scrutinizer ignore-call */ 
76
                              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...
76
					$service->onCreate[] = function (Nette\\Bridges\\ApplicationLatte\\Template $template): void {
77
						$template->setTranslator(?);
78
					};', [$translator]);
79
        }
80
81
        if ($builder->hasDefinition('latte.latteFactory')) {
82
            /** @var FactoryDefinition $latteFactory */
83
            $latteFactory = $builder->getDefinition('latte.latteFactory');
84
            $latteFactory->getResultDefinition()
85
                ->addSetup('addProvider', ['translator', $builder->getDefinition($this->prefix('translator'))]);
86
87
            if (version_compare(Engine::VERSION, '3', '<')) {
88
                $latteFactory->getResultDefinition()->addSetup('?->onCompile[] = function($engine) { ?::install($engine->getCompiler()); }', ['@self', new PhpLiteral(TranslateMacros::class)]);
89
            }
90
        }
91
    }
92
93
}
94