Completed
Push — master ( e74ae2...768df3 )
by Cees-Jan
24s
created

CompileShell::compileTemplate()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.1481

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 9
cp 0.6667
rs 9.8333
c 0
b 0
f 0
cc 2
nc 3
nop 1
crap 2.1481
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 Cake\ORM\Locator\LocatorInterface;
18
use WyriHaximus\TwigView\Lib\Scanner;
19
use WyriHaximus\TwigView\View\TwigView;
20
21
/**
22
 * Class CompileTemplatesShell.
23
 * @package WyriHaximus\TwigView\Console\Command
24
 */
25
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...
26
{
27
28
    /**
29
     * Instance of TwigView to be used to compile templates.
30
     *
31
     * @var TwigView
32
     */
33
    protected $twigView;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param ConsoleIo $consoleIo An IO instance.
0 ignored issues
show
Bug introduced by
There is no parameter named $consoleIo. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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 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
                loadTemplate($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