1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of FSConsoleTools |
4
|
|
|
* Copyright (C) 2018 Francesc Pineda Segarra <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation, either version 3 of the |
9
|
|
|
* License, or (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Lesser General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace FacturaScriptsUtils\Console\Command; |
21
|
|
|
|
22
|
|
|
use FacturaScripts\Core\Base\FileManager; |
|
|
|
|
23
|
|
|
use FacturaScriptsUtils\Console\ConsoleAbstract; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class OrderJsonFiles |
27
|
|
|
* |
28
|
|
|
* @author Francesc Pineda Segarra <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class OrderJsonFiles extends ConsoleAbstract |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Constant values for return, to easy know how execution ends. |
34
|
|
|
*/ |
35
|
|
|
const RETURN_SUCCESS = 0; |
36
|
|
|
const RETURN_SRC_FOLDER_NOT_SET = 1; |
37
|
|
|
const RETURN_DST_FOLDER_NOT_SET = 2; |
38
|
|
|
const RETURN_CANT_CREATE_FOLDER = 3; |
39
|
|
|
const RETURN_FAIL_SAVING_FILE = 4; |
40
|
|
|
const RETURN_NO_FILES = 5; |
41
|
|
|
const RETURN_SRC_FOLDER_NOT_EXISTS = 6; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Folder source path. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $folderSrcPath; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Folder destiny path. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $folderDstPath; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Set default source folder. |
59
|
|
|
* |
60
|
|
|
* @param string $folderSrcPath |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function setFolderSrcPath(string $folderSrcPath): self |
65
|
|
|
{ |
66
|
|
|
$this->folderSrcPath = $folderSrcPath; |
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set default destiny folder. |
72
|
|
|
* |
73
|
|
|
* @param string $folderDstPath |
74
|
|
|
* |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
|
|
public function setFolderDstPath(string $folderDstPath): self |
78
|
|
|
{ |
79
|
|
|
$this->folderDstPath = $folderDstPath; |
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Start point to run the command. |
85
|
|
|
* |
86
|
|
|
* @param array $params |
87
|
|
|
* |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
|
|
public function run(...$params): int |
91
|
|
|
{ |
92
|
|
|
$status = $this->checkOptions($params); |
93
|
|
|
if ($status !== 0) { |
94
|
|
|
return $status; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->setFolderSrcPath($params[0] ?? \FS_FOLDER . 'Core/Translation/'); |
98
|
|
|
$this->setFolderDstPath($params[1] ?? \FS_FOLDER . 'Core/Translation/'); |
99
|
|
|
|
100
|
|
|
echo 'Options setted:' . \PHP_EOL; |
101
|
|
|
echo ' Source path: ' . $this->folderSrcPath . \PHP_EOL; |
102
|
|
|
echo ' Destiny path: ' . $this->folderDstPath . \PHP_EOL; |
103
|
|
|
if (!$this->areYouSure()) { |
104
|
|
|
echo ' Options [SRC] [DST] [TAG]' . \PHP_EOL; |
105
|
|
|
return self::RETURN_SUCCESS; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$status = $this->check(); |
109
|
|
|
if ($status !== 0) { |
110
|
|
|
return $status; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$files = FileManager::scanFolder($this->folderSrcPath); |
114
|
|
|
|
115
|
|
|
if (\count($files) === 0) { |
116
|
|
|
echo 'ERROR: No files on folder' . \PHP_EOL; |
117
|
|
|
return self::RETURN_NO_FILES; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->orderJson($files); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Return description about this class. |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getDescription(): string |
129
|
|
|
{ |
130
|
|
|
return 'Order JSON content files by key.'; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Print help information to the user. |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getHelpMsg(): string |
139
|
|
|
{ |
140
|
|
|
$array = \explode('\\', __CLASS__); |
141
|
|
|
$class = array_pop($array); |
142
|
|
|
return 'Use as: php vendor/bin/console ' . $class . ' [OPTIONS]' . \PHP_EOL |
143
|
|
|
. 'Available options:' . \PHP_EOL |
144
|
|
|
. ' -h, --help Show this help.' . \PHP_EOL |
145
|
|
|
. \PHP_EOL |
146
|
|
|
. ' OPTION1 Source path' . \PHP_EOL |
147
|
|
|
. ' OPTION2 Destiny path' . \PHP_EOL |
148
|
|
|
. \PHP_EOL; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Check if options are looking for help. |
153
|
|
|
* |
154
|
|
|
* @param array $params |
155
|
|
|
* |
156
|
|
|
* @return int |
157
|
|
|
*/ |
158
|
|
|
private function checkOptions(array $params = []): int |
159
|
|
|
{ |
160
|
|
|
if (isset($params[0])) { |
161
|
|
|
switch ($params[0]) { |
162
|
|
|
case '-h': |
163
|
|
|
case '--help': |
164
|
|
|
echo $this->getHelpMsg(); |
165
|
|
|
return -1; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
return 0; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Launch basic checks. |
173
|
|
|
* |
174
|
|
|
* @return int |
175
|
|
|
*/ |
176
|
|
|
private function check(): int |
177
|
|
|
{ |
178
|
|
|
if ($this->folderSrcPath === null) { |
179
|
|
|
echo 'ERROR: Source folder not setted.' . \PHP_EOL; |
180
|
|
|
return self::RETURN_SRC_FOLDER_NOT_SET; |
181
|
|
|
} |
182
|
|
|
if ($this->folderDstPath === null) { |
183
|
|
|
echo 'ERROR: Destiny folder not setted.' . \PHP_EOL; |
184
|
|
|
return self::RETURN_DST_FOLDER_NOT_SET; |
185
|
|
|
} |
186
|
|
|
if (!is_dir($this->folderSrcPath)) { |
187
|
|
|
echo 'ERROR: Source folder ' . $this->folderSrcPath . ' not exists.' . \PHP_EOL; |
188
|
|
|
return self::RETURN_SRC_FOLDER_NOT_EXISTS; |
189
|
|
|
} |
190
|
|
|
if (!is_file($this->folderDstPath) && !@mkdir($this->folderDstPath) && !is_dir($this->folderDstPath)) { |
191
|
|
|
echo "ERROR: Can't create folder " . $this->folderDstPath; |
192
|
|
|
return self::RETURN_CANT_CREATE_FOLDER; |
193
|
|
|
} |
194
|
|
|
return self::RETURN_SUCCESS; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Order JSON files |
199
|
|
|
* |
200
|
|
|
* @param array $files |
201
|
|
|
* |
202
|
|
|
* @return int |
203
|
|
|
*/ |
204
|
|
|
private function orderJson(array $files): int |
205
|
|
|
{ |
206
|
|
|
foreach ($files as $fileName) { |
207
|
|
|
$arrayContent = $this->readJSON($this->folderSrcPath . $fileName); |
208
|
|
|
\ksort($arrayContent); |
209
|
|
|
if (!$this->saveJSON($arrayContent, $this->folderDstPath . $fileName)) { |
210
|
|
|
echo "ERROR: Can't save file " . $fileName . \PHP_EOL; |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
echo 'Finished! Look at "' . $this->folderDstPath . '"' . \PHP_EOL; |
215
|
|
|
return self::RETURN_SUCCESS; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Reads a JSON from disc and return it content as array. |
220
|
|
|
* |
221
|
|
|
* @param string $pathName |
222
|
|
|
* |
223
|
|
|
* @return array |
224
|
|
|
*/ |
225
|
|
|
private function readJSON(string $pathName): array |
226
|
|
|
{ |
227
|
|
|
$data = json_decode(file_get_contents($pathName), true); |
228
|
|
|
return \is_array($data) ? (array) $data : []; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Write a JSON from an array to disc and return its result. |
233
|
|
|
* |
234
|
|
|
* @param array $data |
235
|
|
|
* @param string $pathName |
236
|
|
|
* |
237
|
|
|
* @return int |
238
|
|
|
*/ |
239
|
|
|
private function saveJSON(array $data, string $pathName): int |
240
|
|
|
{ |
241
|
|
|
$jsonData = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); |
242
|
|
|
return (int) file_put_contents($pathName, $jsonData); |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths