1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
5
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
6
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
7
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
8
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
9
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
10
|
|
|
* THE SOFTWARE. |
11
|
|
|
* |
12
|
|
|
* This software consists of voluntary contributions made by many individuals |
13
|
|
|
* and is licensed under the MIT license. |
14
|
|
|
* |
15
|
|
|
* Copyright (c) 2014-2017 Yuuki Takezawa |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Ytake\LaravelSmarty\Console; |
20
|
|
|
|
21
|
|
|
use Ytake\LaravelSmarty\Smarty; |
22
|
|
|
use Illuminate\Console\Command; |
23
|
|
|
use Symfony\Component\Console\Input\InputOption; |
24
|
|
|
use Illuminate\Contracts\Config\Repository as ConfigContract; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class OptimizeCommand |
28
|
|
|
* |
29
|
|
|
* @author yuuki.takezawa<[email protected]> |
30
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
31
|
|
|
*/ |
32
|
|
|
class OptimizeCommand extends Command |
33
|
|
|
{ |
34
|
|
|
/** @var Smarty */ |
35
|
|
|
protected $smarty; |
36
|
|
|
|
37
|
|
|
/** @var ConfigContract */ |
38
|
|
|
protected $config; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Smarty $smarty |
42
|
|
|
* @param ConfigContract $config |
43
|
|
|
*/ |
44
|
|
|
public function __construct(Smarty $smarty, ConfigContract $config) |
45
|
|
|
{ |
46
|
|
|
parent::__construct(); |
47
|
|
|
$this->smarty = $smarty; |
48
|
|
|
$this->config = $config; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The console command name. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $name = 'ytake:smarty-optimize'; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The console command description. |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $description = 'Compile all known templates.'; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Execute the console command. |
67
|
|
|
* |
68
|
|
|
*/ |
69
|
|
|
public function handle() |
70
|
|
|
{ |
71
|
|
|
$configureFileExtension = $this->config->get('ytake-laravel-smarty.extension', 'tpl'); |
72
|
|
|
$fileExtension = (is_null($this->option('extension'))) |
73
|
|
|
? $configureFileExtension : $this->option('extension'); |
74
|
|
|
ob_start(); |
75
|
|
|
$compileFiles = $this->smarty->compileAllTemplates( |
76
|
|
|
$fileExtension, $this->forceCompile() |
77
|
|
|
); |
78
|
|
|
$contents = ob_get_contents(); |
79
|
|
|
ob_get_clean(); |
80
|
|
|
$this->comment(str_replace("<br>", "", trim($contents))); |
81
|
|
|
$this->info("{$compileFiles} template files recompiled"); |
82
|
|
|
return 0; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the console command options. |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
protected function getOptions() |
91
|
|
|
{ |
92
|
|
|
return [ |
93
|
|
|
['extension', 'e', InputOption::VALUE_OPTIONAL, 'Specified smarty file extension'], |
94
|
|
|
['force', null, InputOption::VALUE_NONE, 'Compiles template files found in views directory'], |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
protected function forceCompile() |
102
|
|
|
{ |
103
|
|
|
if ($this->option('force')) { |
104
|
|
|
return true; |
105
|
|
|
} |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|