Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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-2022 Yuuki Takezawa
16
 *
17
 */
18
 
19
declare(strict_types=1);
20
 
21
namespace Ytake\LaravelSmarty\Console;
22
 
23
use Illuminate\Console\Command;
24
use Illuminate\Contracts\Config\Repository as ConfigContract;
25
use Symfony\Component\Console\Input\InputOption;
26
use Ytake\LaravelSmarty\Smarty;
27
 
28
use function is_null;
29
use function ob_get_clean;
30
use function ob_get_contents;
31
use function ob_start;
32
use function str_replace;
33
use function trim;
34
 
35
/**
36
 * Class OptimizeCommand
37
 *
38
 * @author yuuki.takezawa<yuuki.takezawa@comnect.jp.net>
39
 * @license http://opensource.org/licenses/MIT MIT
40
 */
41
class OptimizeCommand extends Command
42
{
43
    /**
44
     * @param Smarty $smarty
45
     * @param ConfigContract $config
46
     */
47
    public function __construct(
48
        protected Smarty $smarty,
49
        protected ConfigContract $config
50
    ) {
51
        parent::__construct();
52
    }
53
 
54
    /**
55
     * The console command name.
56
     *
57
     * @var string
58
     */
59
    protected $name = 'ytake:smarty-optimize';
60
 
61
    /**
62
     * The console command description.
63
     *
64
     * @var string
65
     */
66
    protected $description = 'Compile all known templates.';
67
 
68
    /**
69
     * Execute the console command.
70
     *
71
     * @return int
72
     */
73
    public function handle(): int
74
    {
75
        $configureFileExtension = $this->config->get('ytake-laravel-smarty.extension', 'tpl');
76
        $fileExtension = (is_null($this->option('extension')))
77
            ? $configureFileExtension : $this->option('extension');
78
        ob_start();
79
        $compileFiles = $this->smarty->compileAllTemplates(
80
            $fileExtension,
81
            $this->forceCompile()
82
        );
83
        $contents = ob_get_contents();
84
        ob_get_clean();
85
        $this->comment(str_replace("<br>", "", trim($contents)));
86
        $this->info("{$compileFiles} template files recompiled");
87
        return 0;
88
    }
89
 
90
    /**
91
     * Get the console command options.
92
     *
93
     * @return array
94
     */
95
    protected function getOptions(): array
96
    {
97
        return [
98
            ['extension', 'e', InputOption::VALUE_OPTIONAL, 'Specified smarty file extension'],
99
            ['force', null, InputOption::VALUE_NONE, 'Compiles template files found in views directory'],
100
        ];
101
    }
102
 
103
    /**
104
     * @return bool
105
     */
106
    protected function forceCompile(): bool
107
    {
108
        if ($this->option('force')) {
109
            return true;
110
        }
111
        return false;
112
    }
113
}