1
|
|
|
<?php |
2
|
|
|
/*************************************************************************** |
3
|
|
|
* for license information see LICENSE.md |
4
|
|
|
***************************************************************************/ |
5
|
|
|
|
6
|
|
|
namespace Oc\Command; |
7
|
|
|
|
8
|
|
|
use Exception; |
9
|
|
|
use Leafo\ScssPhp\Compiler; |
10
|
|
|
use RecursiveDirectoryIterator; |
11
|
|
|
use RecursiveIteratorIterator; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
13
|
|
|
use Symfony\Component\Console\Exception\InvalidArgumentException; |
14
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
17
|
|
|
use Symfony\Component\Finder\Finder; |
18
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class CreateWebCacheCommand |
22
|
|
|
*/ |
23
|
|
|
class CreateWebCacheCommand extends ContainerAwareCommand |
24
|
|
|
{ |
25
|
|
|
const COMMAND_NAME = 'cache:web:create'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var OutputInterface |
29
|
|
|
*/ |
30
|
|
|
private $output; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Configures the command. |
34
|
|
|
* |
35
|
|
|
* |
36
|
|
|
* @throws InvalidArgumentException |
37
|
|
|
*/ |
38
|
|
|
protected function configure() |
39
|
|
|
{ |
40
|
|
|
parent::configure(); |
41
|
|
|
|
42
|
|
|
$this |
43
|
|
|
->setName(self::COMMAND_NAME) |
44
|
|
|
->setDescription('create legacy web caches'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Executes the command. |
49
|
|
|
* |
50
|
|
|
* @param InputInterface $input |
51
|
|
|
* @param OutputInterface $output |
52
|
|
|
* |
53
|
|
|
* @return int|null |
54
|
|
|
*/ |
55
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
56
|
|
|
{ |
57
|
|
|
$this->output = $output; |
58
|
|
|
|
59
|
|
|
$projectDir = $this->getContainer()->getParameter('kernel.project_dir'); |
60
|
|
|
|
61
|
|
|
$output->writeln('Generating WebCache'); |
62
|
|
|
|
63
|
|
|
$paths = [ |
64
|
|
|
$projectDir . '/web/assets', |
65
|
|
|
$projectDir . '/web/assets/css', |
66
|
|
|
$projectDir . '/web/assets/js', |
67
|
|
|
$projectDir . '/web/assets/images', |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
foreach ($paths as $path) { |
71
|
|
|
if (!file_exists($path)) { |
72
|
|
|
mkdir($path, 0755, true); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
try { |
77
|
|
|
$this->compileCss($projectDir); |
78
|
|
|
$this->compileJs($projectDir); |
79
|
|
|
$this->copyImages($projectDir); |
80
|
|
|
} catch (Exception $e) { |
81
|
|
|
$this->output->writeln('<error>An exception occurred!</error>'); |
82
|
|
|
$this->output->writeln('<error>' . $e->getMessage() . '</error>'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$output->writeln('WebCache generated'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Compiles js to one file. |
90
|
|
|
* |
91
|
|
|
* @param string $projectDir |
92
|
|
|
*/ |
93
|
|
|
private function compileJs($projectDir) |
94
|
|
|
{ |
95
|
|
|
$this->output->writeln('Generating javascript'); |
96
|
|
|
|
97
|
|
|
$applicationJsPath = $projectDir . '/theme/frontend/js/'; |
98
|
|
|
|
99
|
|
|
if (!file_exists($applicationJsPath)) { |
100
|
|
|
$this->output->writeln('<comment>- Javascript directory not found!</comment>'); |
101
|
|
|
|
102
|
|
|
return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$vendorFiles = [ |
106
|
|
|
$projectDir . '/vendor/components/jquery/jquery.min.js' |
107
|
|
|
]; |
108
|
|
|
|
109
|
|
|
$filesJson = json_decode(file_get_contents($projectDir . '/theme/frontend/js/files.json'), true); |
110
|
|
|
$ownFiles = $filesJson['files']; |
111
|
|
|
|
112
|
|
|
$ownFiles = array_map(function($file) use ($applicationJsPath) { |
113
|
|
|
if (strpos($file, '**') !== false) { |
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $applicationJsPath . $file; |
118
|
|
|
}, $ownFiles); |
119
|
|
|
|
120
|
|
|
$ownFiles = array_filter($ownFiles, 'is_string'); |
121
|
|
|
|
122
|
|
|
$finder = new Finder(); |
123
|
|
|
$finder->in($applicationJsPath . '/plugins/')->name('*.js'); |
124
|
|
|
|
125
|
|
|
$jsPlugins = array_map(function($file) { |
126
|
|
|
return $file->getRealPath(); |
127
|
|
|
}, iterator_to_array($finder->files())); |
128
|
|
|
|
129
|
|
|
$jsFiles = array_merge($vendorFiles, $ownFiles, array_values($jsPlugins)); |
130
|
|
|
|
131
|
|
|
$js = ''; |
132
|
|
|
|
133
|
|
|
foreach ($jsFiles as $file) { |
134
|
|
|
$js .= file_get_contents($file) . PHP_EOL; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
file_put_contents($projectDir . '/web/assets/js/main.js', $js); |
138
|
|
|
|
139
|
|
|
$this->output->writeln('<info>- Javascript generated</info>'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Compiles scss to one file. |
144
|
|
|
* |
145
|
|
|
* @param string $projectDir |
146
|
|
|
*/ |
147
|
|
|
private function compileCss($projectDir) |
148
|
|
|
{ |
149
|
|
|
$this->output->writeln('Generating stylesheets'); |
150
|
|
|
|
151
|
|
|
$applicationScssPath = $projectDir . '/theme/frontend/scss/'; |
152
|
|
|
|
153
|
|
|
$scss = new Compiler(); |
154
|
|
|
$scss->setIgnoreErrors(true); |
155
|
|
|
$scss->addImportPath($applicationScssPath); |
156
|
|
|
$scss->addImportPath(function ($path) use ($projectDir) { |
|
|
|
|
157
|
|
|
//Check for tilde as this refers to the node_modules dir |
158
|
|
|
if (strpos($path, '~') === 0) { |
159
|
|
|
$path = str_replace( |
160
|
|
|
['~', 'bootstrap'], |
161
|
|
|
[$projectDir . '/vendor/', 'twbs/bootstrap'], |
162
|
|
|
$path |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
$path .= '.scss'; |
166
|
|
|
|
167
|
|
|
//if file does not exist, try with underscore before filename |
168
|
|
|
if (!file_exists($path)) { |
169
|
|
|
$chunks = explode('/', $path); |
170
|
|
|
|
171
|
|
|
end($chunks); |
172
|
|
|
$lastKey = key($chunks); |
173
|
|
|
reset($chunks); |
174
|
|
|
|
175
|
|
|
$chunks[$lastKey] = '_' . $chunks[$lastKey]; |
176
|
|
|
|
177
|
|
|
$path = implode('/', $chunks); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if (!file_exists($path)) { |
181
|
|
|
return null; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $path; |
186
|
|
|
}); |
187
|
|
|
|
188
|
|
|
file_put_contents( |
189
|
|
|
$projectDir . '/web/assets/css/style.min.css', |
190
|
|
|
$scss->compile(file_get_contents($applicationScssPath . '/all.scss')) |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
file_put_contents( |
194
|
|
|
$projectDir . '/web/assets/css/legacy.min.css', |
195
|
|
|
$scss->compile(file_get_contents($applicationScssPath . '/legacy.scss')) |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
$this->output->writeln('<info>- Stylesheets generated</info>'); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string $projectDir |
203
|
|
|
*/ |
204
|
|
|
private function copyImages($projectDir) |
205
|
|
|
{ |
206
|
|
|
$this->output->writeln('Copying images'); |
207
|
|
|
|
208
|
|
|
$source = $projectDir . '/theme/frontend/images'; |
209
|
|
|
$destination = $projectDir . '/web/assets/images'; |
210
|
|
|
|
211
|
|
|
$fs = new Filesystem(); |
212
|
|
|
$fs->mirror($source, $destination); |
213
|
|
|
|
214
|
|
|
$this->output->writeln('<info>- Images copied</info>'); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: