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( |
|
|
|
|
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
|
|
|
|