Completed
Pull Request — master (#58)
by
unknown
05:54
created

CompileShell   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 52.08%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 5
dl 0
loc 132
ccs 25
cts 48
cp 0.5208
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setTwigview() 0 4 1
A all() 0 9 2
A plugin() 0 5 1
A getOptionParser() 0 22 1
A walkIterator() 0 6 2
A compileTemplate() 0 13 2
A file() 0 5 1
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of TwigView.
4
 *
5
 ** (c) 2014 Cees-Jan Kiewiet
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace WyriHaximus\TwigView\Shell;
12
13
use Cake\Console\ConsoleIo;
14
use Cake\Console\Shell;
15
use Cake\Core\Plugin;
16
use WyriHaximus\TwigView\Lib\Scanner;
17
use WyriHaximus\TwigView\View\TwigView;
18
19
/**
20
 * Class CompileTemplatesShell.
21
 * @package WyriHaximus\TwigView\Console\Command
22
 */
23
class CompileShell extends Shell
24
{
25
26
    /**
27
     * Instance of TwigView to be used to compile templates.
28
     *
29
     * @var TwigView
30
     */
31
    protected $twigView;
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param ConsoleIo $consoleIo An IO instance.
37
     */
38 1
    public function __construct(ConsoleIo $consoleIo = null)
39
    {
40 1
        parent::__construct($consoleIo);
41
42 1
        $this->twigView = new TwigView();
43 1
    }
44
45
    /**
46
     * Set TwigView.
47
     *
48
     * @param TwigView $twigView TwigView instance.
49
     *
50
     */
51
    public function setTwigview(TwigView $twigView)
52
    {
53
        $this->twigView = $twigView;
54
    }
55
56
    /**
57
     * Compile all templates.
58
     *
59
     */
60
    public function all()
61
    {
62
        $this->out('<info>Compiling all templates</info>');
63
64
        foreach (Scanner::all() as $section => $templates) {
65
            $this->out('<info>Compiling ' . $section . '\'s templates</info>');
66
            $this->walkIterator($templates);
67
        }
68
    }
69
70
    /**
71
     * Compile only this plugin.
72
     *
73
     * @param string $plugin Plugin name.
74
     *
75
     */
76 1
    public function plugin($plugin)
77
    {
78 1
        $this->out('<info>Compiling one ' . $plugin . '\'s templates</info>');
79 1
        $this->walkIterator(Scanner::plugin($plugin));
80
    }
81
82
    /**
83
     * Only compile one file.
84
     *
85
     * @param string $fileName File to compile.
86
     *
87
     */
88 1
    public function file($fileName)
89
    {
90 1
        $this->out('<info>Compiling one template</info>');
91 1
        $this->compileTemplate($fileName);
92 1
    }
93
94
    /**
95
     * Set options for this console.
96
     *
97
     * @return \Cake\Console\ConsoleOptionParser
98
     */
99 1
    public function getOptionParser()
100
    {
101 1
        return parent::getOptionParser()->addSubcommand(
0 ignored issues
show
Bug Compatibility introduced by
The expression parent::getOptionParser(...mplates precompiler')); of type Cake\Console\ConsoleOptionParser|string adds the type string to the return on line 101 which is incompatible with the return type documented by WyriHaximus\TwigView\She...eShell::getOptionParser of type Cake\Console\ConsoleOptionParser.
Loading history...
Deprecated Code introduced by
The method Cake\Console\ConsoleOptionParser::description() has been deprecated with message: 3.4.0 Use setDescription()/getDescription() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
102 1
            'all',
103
            [
104 1
                'short' => 'a',
105 1
                'help' => __('Searches and precompiles all twig templates it finds.'),
106
            ]
107 1
        )->addSubcommand(
108 1
            'plugin',
109
            [
110 1
                'short' => 'p',
111 1
                'help' => __('Searches and precompiles all twig templates for a specific plugin.'),
112
            ]
113 1
        )->addSubcommand(
114 1
            'file',
115
            [
116 1
                'short' => 'f',
117 1
                'help' => __('Precompile a specific file.'),
118
            ]
119 1
        )->description(__('TwigView templates precompiler'));
120
    }
121
122
    /**
123
     * Walk over $iterator and compile all templates in it.
124
     *
125
     * @param mixed $iterator Iterator to walk over.
126
     *
127
     */
128
    protected function walkIterator($iterator)
129
    {
130
        foreach ($iterator as $template) {
131
            $this->compileTemplate($template);
132
        }
133
    }
134
135
    /**
136
     * Compile a template.
137
     *
138
     * @param string $fileName Template to compile.
139
     *
140
     */
141
    protected function compileTemplate($fileName)
142
    {
143
        try {
144
            $this->
145
                twigView->
146
                getTwig()->
147
                loadTemplate($fileName);
148
            $this->out('<success>' . $fileName . '</success>');
149
        } catch (\Exception $exception) {
150
            $this->out('<error>' . $fileName . '</error>');
151
            $this->out('<error>' . $exception->getMessage() . '</error>');
152
        }
153
    }
154
}
155