OptimizeCommand::forceCompile()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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