CompileShell::setTwigview()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of TwigView.
6
 *
7
 ** (c) 2014 Cees-Jan Kiewiet
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace WyriHaximus\TwigView\Shell;
14
15
use Cake\Console\ConsoleIo;
16
use Cake\Console\ConsoleOptionParser;
17
use Cake\Console\Shell;
18
use Cake\ORM\Locator\LocatorInterface;
19
use WyriHaximus\TwigView\Lib\Scanner;
20
use WyriHaximus\TwigView\View\TwigView;
21
22
/**
23
 * Class CompileTemplatesShell.
24
 * @package WyriHaximus\TwigView\Console\Command
25
 */
26
class CompileShell extends Shell
0 ignored issues
show
Deprecated Code introduced by
The class Cake\Console\Shell has been deprecated with message: 3.6.0 ShellDispatcher and Shell will be removed in 5.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

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