1
|
|
|
<?php namespace Limoncello\Templates\Commands; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Limoncello\Contracts\Commands\CommandInterface; |
20
|
|
|
use Limoncello\Contracts\Commands\IoInterface; |
21
|
|
|
use Limoncello\Contracts\FileSystem\FileSystemInterface; |
22
|
|
|
use Limoncello\Contracts\Settings\SettingsProviderInterface; |
23
|
|
|
use Limoncello\Templates\Contracts\TemplatesCacheInterface; |
24
|
|
|
use Limoncello\Templates\Package\TemplatesSettings; |
25
|
|
|
use Psr\Container\ContainerInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @package Limoncello\Templates |
29
|
|
|
*/ |
30
|
|
|
class TemplatesCommand implements CommandInterface |
31
|
|
|
{ |
32
|
|
|
/** Argument name */ |
33
|
|
|
const ARG_ACTION = 'action'; |
34
|
|
|
|
35
|
|
|
/** Command action */ |
36
|
|
|
const ACTION_CLEAR_CACHE = 'clear-cache'; |
37
|
|
|
|
38
|
|
|
/** Command action */ |
39
|
|
|
const ACTION_CREATE_CACHE = 'cache'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
1 |
|
public static function getName(): string |
45
|
|
|
{ |
46
|
1 |
|
return 'l:templates'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
1 |
|
public static function getDescription(): string |
53
|
|
|
{ |
54
|
1 |
|
return 'Creates and cleans templates cache.'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritdoc |
59
|
|
|
*/ |
60
|
1 |
|
public static function getHelp(): string |
61
|
|
|
{ |
62
|
1 |
|
return 'This command creates and cleans cache for HTML templates.'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
1 |
|
public static function getArguments(): array |
69
|
|
|
{ |
70
|
1 |
|
$cache = static::ACTION_CREATE_CACHE; |
71
|
1 |
|
$clear = static::ACTION_CLEAR_CACHE; |
72
|
|
|
|
73
|
|
|
return [ |
74
|
|
|
[ |
75
|
1 |
|
static::ARGUMENT_NAME => static::ARG_ACTION, |
76
|
1 |
|
static::ARGUMENT_DESCRIPTION => "Action such as `$cache` or `$clear`.", |
77
|
1 |
|
static::ARGUMENT_MODE => static::ARGUMENT_MODE__REQUIRED, |
78
|
|
|
], |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @inheritdoc |
84
|
|
|
*/ |
85
|
1 |
|
public static function getOptions(): array |
86
|
|
|
{ |
87
|
1 |
|
return []; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
* |
93
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
94
|
|
|
*/ |
95
|
3 |
|
public static function execute(ContainerInterface $container, IoInterface $inOut): void |
96
|
|
|
{ |
97
|
3 |
|
$action = $inOut->getArgument(static::ARG_ACTION); |
98
|
|
|
switch ($action) { |
99
|
3 |
|
case static::ACTION_CREATE_CACHE: |
100
|
1 |
|
(new self())->executeCache($container); |
101
|
1 |
|
break; |
102
|
2 |
|
case static::ACTION_CLEAR_CACHE: |
103
|
1 |
|
(new self())->executeClear($container); |
104
|
1 |
|
break; |
105
|
|
|
default: |
106
|
1 |
|
$inOut->writeError("Unsupported action `$action`." . PHP_EOL); |
107
|
1 |
|
break; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param ContainerInterface $container |
113
|
|
|
* |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
1 |
|
protected function executeClear(ContainerInterface $container): void |
117
|
|
|
{ |
118
|
1 |
|
$settings = $this->getTemplatesSettings($container); |
119
|
1 |
|
$cacheFolder = $settings[TemplatesSettings::KEY_CACHE_FOLDER]; |
120
|
|
|
|
121
|
|
|
/** @var FileSystemInterface $fileSystem */ |
122
|
1 |
|
$fileSystem = $container->get(FileSystemInterface::class); |
123
|
1 |
|
foreach ($fileSystem->scanFolder($cacheFolder) as $fileOrFolder) { |
124
|
1 |
|
$fileSystem->isFolder($fileOrFolder) === false ?: $fileSystem->deleteFolderRecursive($fileOrFolder); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param ContainerInterface $container |
130
|
|
|
* |
131
|
|
|
* @return void |
132
|
|
|
* |
133
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
134
|
|
|
*/ |
135
|
1 |
|
protected function executeCache(ContainerInterface $container): void |
136
|
|
|
{ |
137
|
|
|
/** @var TemplatesCacheInterface $cache */ |
138
|
1 |
|
$cache = $container->get(TemplatesCacheInterface::class); |
139
|
|
|
|
140
|
1 |
|
$settings = $this->getTemplatesSettings($container); |
141
|
1 |
|
$templates = $settings[TemplatesSettings::KEY_TEMPLATES_LIST]; |
142
|
|
|
|
143
|
1 |
|
foreach ($templates as $templateName) { |
144
|
|
|
// it will write template to cache |
145
|
1 |
|
$cache->cache($templateName); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param ContainerInterface $container |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
2 |
|
protected function getTemplatesSettings(ContainerInterface $container): array |
155
|
|
|
{ |
156
|
2 |
|
$tplConfig = $container->get(SettingsProviderInterface::class)->get(TemplatesSettings::class); |
157
|
|
|
|
158
|
2 |
|
return $tplConfig; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|