1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cecil. |
5
|
|
|
* |
6
|
|
|
* (c) Arnaud Ligny <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Cecil\Command; |
15
|
|
|
|
16
|
|
|
use Cecil\Exception\RuntimeException; |
17
|
|
|
use Cecil\Util; |
18
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
use Symfony\Component\Finder\Finder; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* UtilTemplatesExtract command. |
26
|
|
|
* |
27
|
|
|
* This command extracts built-in templates from the Phar archive to the specified layouts directory. |
28
|
|
|
* It can override existing files if the --force option is provided. |
29
|
|
|
* If no path is provided, it uses the default layouts directory defined in the configuration. |
30
|
|
|
*/ |
31
|
|
|
class UtilTemplatesExtract extends AbstractCommand |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
protected function configure() |
37
|
|
|
{ |
38
|
|
|
$this |
39
|
|
|
->setName('util:templates:extract') |
40
|
|
|
->setDescription('Extracts built-in templates') |
41
|
|
|
->setDefinition([ |
42
|
|
|
new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'), |
43
|
|
|
new InputOption('force', 'f', InputOption::VALUE_NONE, 'Override files if they already exist'), |
44
|
|
|
]) |
45
|
|
|
->setHelp( |
46
|
|
|
<<<'EOF' |
47
|
|
|
The <info>%command.name%</> command extracts built-in templates in the "layouts" directory. |
48
|
|
|
|
49
|
|
|
<info>%command.full_name%</> |
50
|
|
|
<info>%command.full_name% path/to/the/working/directory</> |
51
|
|
|
|
52
|
|
|
To <comment>override</comment> existing files, run: |
53
|
|
|
|
54
|
|
|
<info>%command.full_name% --force</> |
55
|
|
|
EOF |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
* |
62
|
|
|
* @throws RuntimeException |
63
|
|
|
*/ |
64
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
65
|
|
|
{ |
66
|
|
|
$force = $input->getOption('force'); |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$phar = new \Phar(Util\Platform::getPharPath()); |
70
|
|
|
|
71
|
|
|
$templatesList = []; |
72
|
|
|
$templates = Finder::create() |
73
|
|
|
->files() |
74
|
|
|
->in($this->getBuilder()->getConfig()->getLayoutsInternalPath()); |
75
|
|
|
foreach ($templates as $template) { |
76
|
|
|
$relativePath = rtrim(Util\File::getFS()->makePathRelative($template->getPathname(), $this->getBuilder()->getConfig()->getLayoutsInternalPath()), '/'); |
77
|
|
|
if (!isset($phar["resources/layouts/$relativePath"])) { |
78
|
|
|
throw new RuntimeException(\sprintf('Internal template `%s` doesn\'t exist.', $relativePath)); |
79
|
|
|
} |
80
|
|
|
$templatesList[] = "resources/layouts/$relativePath"; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$force = ($force !== false) ?: $this->io->confirm('Do you want to override existing files?', false); |
84
|
|
|
|
85
|
|
|
$phar->extractTo($this->getBuilder()->getConfig()->getLayoutsPath(), $templatesList, $force); |
86
|
|
|
Util\File::getFS()->mirror(Util::joinPath($this->getBuilder()->getConfig()->getLayoutsPath(), 'resources/layouts/'), $this->getBuilder()->getConfig()->getLayoutsPath()); |
87
|
|
|
Util\File::getFS()->remove(Util::joinPath($this->getBuilder()->getConfig()->getLayoutsPath(), 'resources')); |
88
|
|
|
$output->writeln(\sprintf('<info>Built-in templates extracted to "%s".</info>', (string) $this->getBuilder()->getConfig()->get('layouts.dir'))); |
89
|
|
|
} catch (\Exception $e) { |
90
|
|
|
throw new RuntimeException($e->getMessage()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return 0; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|