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\Finder\SplFileInfo; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class CreateWebCacheCommand |
20
|
|
|
*/ |
21
|
|
|
class CreateWebCacheCommand extends ContainerAwareCommand |
22
|
|
|
{ |
23
|
|
|
const COMMAND_NAME = 'cache:web:create'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var OutputInterface |
27
|
|
|
*/ |
28
|
|
|
private $output; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Configures the command. |
32
|
|
|
* |
33
|
|
|
* |
34
|
|
|
* @throws InvalidArgumentException |
35
|
|
|
*/ |
36
|
|
|
protected function configure() |
37
|
|
|
{ |
38
|
|
|
parent::configure(); |
39
|
|
|
|
40
|
|
|
$this |
41
|
|
|
->setName(self::COMMAND_NAME) |
42
|
|
|
->setDescription('create legacy web caches'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Executes the command. |
47
|
|
|
* |
48
|
|
|
* @param InputInterface $input |
49
|
|
|
* @param OutputInterface $output |
50
|
|
|
* |
51
|
|
|
* @return int|null |
52
|
|
|
*/ |
53
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
54
|
|
|
{ |
55
|
|
|
$this->output = $output; |
56
|
|
|
|
57
|
|
|
$projectDir = $this->getContainer()->getParameter('kernel.project_dir'); |
58
|
|
|
|
59
|
|
|
$output->writeln('Generating WebCache'); |
60
|
|
|
|
61
|
|
|
$paths = [ |
62
|
|
|
$projectDir . '/web/assets', |
63
|
|
|
$projectDir . '/web/assets/css', |
64
|
|
|
$projectDir . '/web/assets/js', |
65
|
|
|
$projectDir . '/web/assets/images', |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
foreach ($paths as $path) { |
69
|
|
|
if (!file_exists($path)) { |
70
|
|
|
mkdir($path, 0755, true); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
try { |
75
|
|
|
$this->compileCss($projectDir); |
76
|
|
|
$this->compileJs($projectDir); |
77
|
|
|
$this->copyImages($projectDir); |
78
|
|
|
} catch (Exception $e) { |
79
|
|
|
$this->output->writeln('<error>An exception occurred!</error>'); |
80
|
|
|
$this->output->writeln('<error>' . $e->getMessage() . '</error>'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$output->writeln('WebCache generated'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Compiles js to one file. |
88
|
|
|
* |
89
|
|
|
* @param string $projectDir |
90
|
|
|
*/ |
91
|
|
|
private function compileJs($projectDir) |
92
|
|
|
{ |
93
|
|
|
$this->output->writeln('Generating javascript'); |
94
|
|
|
|
95
|
|
|
$applicationJsPath = $projectDir . '/app/Resources/assets/js/'; |
96
|
|
|
|
97
|
|
|
if (!file_exists($applicationJsPath)) { |
98
|
|
|
$this->output->writeln('<comment>- Javascript directory not found!</comment>'); |
99
|
|
|
|
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$rii = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($applicationJsPath)); |
104
|
|
|
|
105
|
|
|
$js = ''; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @var SplFileInfo |
109
|
|
|
*/ |
110
|
|
|
foreach ($rii as $file) { |
111
|
|
|
if ($file->isDir() || $file->getExtension() !== 'js') { |
112
|
|
|
continue; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$js .= file_get_contents($file->getRealPath()) . PHP_EOL; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
file_put_contents($projectDir . '/web/assets/js/main.js', $js); |
119
|
|
|
|
120
|
|
|
$this->output->writeln('<info>- Javascript generated</info>'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Compiles scss to one file. |
125
|
|
|
* |
126
|
|
|
* @param string $projectDir |
127
|
|
|
*/ |
128
|
|
|
private function compileCss($projectDir) |
129
|
|
|
{ |
130
|
|
|
$this->output->writeln('Generating stylesheets'); |
131
|
|
|
|
132
|
|
|
$applicationScssPath = $projectDir . '/app/Resources/assets/scss/'; |
133
|
|
|
|
134
|
|
|
$scss = new Compiler(); |
135
|
|
|
$scss->setIgnoreErrors(true); |
136
|
|
|
$scss->addImportPath($applicationScssPath); |
137
|
|
|
$scss->addImportPath(function ($path) use ($projectDir) { |
|
|
|
|
138
|
|
|
//Check for tilde as this refers to the node_modules dir |
139
|
|
|
if (strpos($path, '~') === 0) { |
140
|
|
|
$path = str_replace( |
141
|
|
|
['~', 'bootstrap'], |
142
|
|
|
[$projectDir . '/vendor/', 'twbs/bootstrap'], |
143
|
|
|
$path |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
$path .= '.scss'; |
147
|
|
|
|
148
|
|
|
//if file does not exist, try with underscore before filename |
149
|
|
|
if (!file_exists($path)) { |
150
|
|
|
$chunks = explode('/', $path); |
151
|
|
|
|
152
|
|
|
end($chunks); |
153
|
|
|
$lastKey = key($chunks); |
154
|
|
|
reset($chunks); |
155
|
|
|
|
156
|
|
|
$chunks[$lastKey] = '_' . $chunks[$lastKey]; |
157
|
|
|
|
158
|
|
|
$path = implode('/', $chunks); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if (!file_exists($path)) { |
162
|
|
|
return null; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $path; |
167
|
|
|
}); |
168
|
|
|
|
169
|
|
|
file_put_contents( |
170
|
|
|
$projectDir . '/web/assets/css/style.min.css', |
171
|
|
|
$scss->compile(file_get_contents($applicationScssPath . '/all.scss')) |
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
file_put_contents( |
175
|
|
|
$projectDir . '/web/assets/css/legacy.min.css', |
176
|
|
|
$scss->compile(file_get_contents($applicationScssPath . '/legacy.scss')) |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
$this->output->writeln('<info>- Stylesheets generated</info>'); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $projectDir |
184
|
|
|
*/ |
185
|
|
|
private function copyImages($projectDir) |
186
|
|
|
{ |
187
|
|
|
$this->output->writeln('Copying images'); |
188
|
|
|
|
189
|
|
|
$source = $projectDir . '/app/Resources/assets/images'; |
190
|
|
|
$destination = $projectDir . '/web/assets/images'; |
191
|
|
|
|
192
|
|
|
$iterator = new RecursiveIteratorIterator( |
193
|
|
|
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), |
194
|
|
|
RecursiveIteratorIterator::SELF_FIRST |
195
|
|
|
); |
196
|
|
|
|
197
|
|
|
foreach ($iterator as $item) { |
198
|
|
|
if ($item->isDir()) { |
199
|
|
|
mkdir($destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); |
200
|
|
|
} else { |
201
|
|
|
copy($item, $destination . DIRECTORY_SEPARATOR . $iterator->getSubPathName()); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$this->output->writeln('<info>- Images copied</info>'); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
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: