Completed
Pull Request — master (#171)
by Anton
02:55
created

IndexCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
B perform() 0 25 3
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Commands\Translator;
10
11
use Psr\Log\LoggerAwareInterface;
12
use Psr\Log\NullLogger;
13
use Spiral\Console\Command;
14
use Spiral\Tokenizer\ClassesInterface;
15
use Spiral\Tokenizer\InvocationsInterface;
16
use Spiral\Translator\Indexer;
17
use Spiral\Translator\Translator;
18
19
/**
20
 * Index available classes and function calls to fetch every used string translation. Can
21
 * understand l, p and translate (trait) function.
22
 *
23
 * @see Indexer
24
 */
25
class IndexCommand extends Command
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    const NAME = 'i18n:index';
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    const DESCRIPTION = 'Index all declared translation strings and usages';
36
37
    /**
38
     * @param Indexer              $indexer
39
     * @param Translator           $translator
40
     * @param InvocationsInterface $invocations
41
     * @param ClassesInterface     $classes
42
     */
43
    public function perform(
44
        Indexer $indexer,
45
        Translator $translator,
46
        InvocationsInterface $invocations,
47
        ClassesInterface $classes
48
    ) {
49
        if ($invocations instanceof LoggerAwareInterface) {
50
            //Way too much verbosity
51
            $invocations->setLogger(new NullLogger());
52
        }
53
54
        if ($classes instanceof LoggerAwareInterface) {
55
            //Way too much verbosity
56
            $classes->setLogger(new NullLogger());
57
        }
58
59
        $this->writeln("<info>Scanning translate function usages...</info>");
60
        $indexer->indexInvocations($invocations);
61
62
        $this->writeln("<info>Scanning Translatable classes...</info>");
63
        $indexer->indexClasses($classes);
64
65
        //Make sure that all located messages are properly registered
66
        $translator->syncLocales();
67
    }
68
}
69