Completed
Push — master ( b68ec1...9e5bf9 )
by Cees-Jan
03:04
created

CompileShell::setTwigview()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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\ConsoleOptionParser;
15
use Cake\Console\Shell;
16
use Cake\Core\Plugin;
17
use WyriHaximus\TwigView\Lib\Scanner;
18
use WyriHaximus\TwigView\View\TwigView;
19
20
/**
21
 * Class CompileTemplatesShell.
22
 * @package WyriHaximus\TwigView\Console\Command
23
 */
24
class CompileShell extends Shell
25
{
26
27
    /**
28
     * Instance of TwigView to be used to compile templates.
29
     *
30
     * @var TwigView
31
     */
32
    protected $twigView;
33
34
    /**
35
     * Constructor.
36
     *
37
     * @param ConsoleIo $consoleIo An IO instance.
38
     */
39 1
    public function __construct(ConsoleIo $consoleIo = null)
40
    {
41 1
        parent::__construct($consoleIo);
42
43 1
        $this->twigView = new TwigView();
44 1
    }
45
46
    /**
47
     * Set TwigView.
48
     *
49
     * @param TwigView $twigView TwigView instance.
50
     *
51
     */
52
    public function setTwigview(TwigView $twigView)
53
    {
54
        $this->twigView = $twigView;
55
    }
56
57
    /**
58
     * Compile all templates.
59
     *
60
     */
61
    public function all()
62
    {
63
        $this->out('<info>Compiling all templates</info>');
64
65
        foreach (Scanner::all() as $section => $templates) {
66
            $this->out('<info>Compiling ' . $section . '\'s templates</info>');
67
            $this->walkIterator($templates);
68
        }
69
    }
70
71
    /**
72
     * Compile only this plugin.
73
     *
74
     * @param string $plugin Plugin name.
75
     *
76
     */
77 1
    public function plugin($plugin)
78
    {
79 1
        $this->out('<info>Compiling one ' . $plugin . '\'s templates</info>');
80 1
        $this->walkIterator(Scanner::plugin($plugin));
81
    }
82
83
    /**
84
     * Only compile one file.
85
     *
86
     * @param string $fileName File to compile.
87
     *
88
     */
89 1
    public function file($fileName)
90
    {
91 1
        $this->out('<info>Compiling one template</info>');
92 1
        $this->compileTemplate($fileName);
93 1
    }
94
95
    /**
96
     * Set options for this console.
97
     *
98
     * @return \Cake\Console\ConsoleOptionParser
99
     */
100 1
    public function getOptionParser(): ConsoleOptionParser
101
    {
102 1
        return parent::getOptionParser()->addSubcommand(
0 ignored issues
show
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...
103 1
            'all',
104
            [
105 1
                'short' => 'a',
106 1
                'help' => __('Searches and precompiles all twig templates it finds.'),
107
            ]
108 1
        )->addSubcommand(
109 1
            'plugin',
110
            [
111 1
                'short' => 'p',
112 1
                'help' => __('Searches and precompiles all twig templates for a specific plugin.'),
113
            ]
114 1
        )->addSubcommand(
115 1
            'file',
116
            [
117 1
                'short' => 'f',
118 1
                'help' => __('Precompile a specific file.'),
119
            ]
120 1
        )->description(__('TwigView templates precompiler'));
121
    }
122
123
    /**
124
     * Walk over $iterator and compile all templates in it.
125
     *
126
     * @param mixed $iterator Iterator to walk over.
127
     *
128
     */
129
    protected function walkIterator($iterator)
130
    {
131
        foreach ($iterator as $template) {
132
            $this->compileTemplate($template);
133
        }
134
    }
135
136
    /**
137
     * Compile a template.
138
     *
139
     * @param string $fileName Template to compile.
140
     *
141
     */
142
    protected function compileTemplate($fileName)
143
    {
144
        try {
145
            $this->
146
                twigView->
147
                getTwig()->
148
                loadTemplate($fileName);
149
            $this->out('<success>' . $fileName . '</success>');
150
        } catch (\Exception $exception) {
151
            $this->out('<error>' . $fileName . '</error>');
152
            $this->out('<error>' . $exception->getMessage() . '</error>');
153
        }
154
    }
155
}
156