Passed
Push — nested-sections ( 2d40b2...7dbf34 )
by Arnaud
12:03 queued 05:37
created

UtilTemplatesExtract   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 50
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 12 1
A execute() 0 26 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
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\InputDefinition;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\Finder\Finder;
24
25
/**
26
 * Extracts built-in templates.
27
 */
28
class UtilTemplatesExtract extends AbstractCommand
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    protected function configure()
34
    {
35
        $this
36
            ->setName('util:templates:extract')
37
            ->setDescription('Extracts built-in templates')
38
            ->setDefinition(
39
                new InputDefinition([
40
                    new InputArgument('path', InputArgument::OPTIONAL, 'Use the given path as working directory'),
41
                    new InputOption('force', 'f', InputOption::VALUE_NONE, 'Override files if they already exist'),
42
                ])
43
            )
44
            ->setHelp('Extracts built-in templates in the "layouts" directory.');
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     *
50
     * @throws RuntimeException
51
     */
52
    protected function execute(InputInterface $input, OutputInterface $output)
53
    {
54
        $force = $input->getOption('force');
55
56
        try {
57
            $phar = new \Phar(Util\Plateform::getPharPath());
58
59
            $templatesList = [];
60
            $templates = Finder::create()
61
                ->files()
62
                ->in($this->getBuilder()->getConfig()->getLayoutsInternalPath());
63
            foreach ($templates as $template) {
64
                $templatesList[] = Util::joinPath((string) $this->getBuilder()->getConfig()->get('layouts.internal.dir'), Util\File::getFS()->makePathRelative($template->getPathname(), $this->getBuilder()->getConfig()->getLayoutsInternalPath()));
65
            }
66
67
            $force = ($force !== false) ?: $this->io->confirm('Do you want to override existing files?', false);
68
69
            $phar->extractTo($this->getBuilder()->getConfig()->getLayoutsPath(), $templatesList, $force);
70
            Util\File::getFS()->mirror(Util::joinPath($this->getBuilder()->getConfig()->getLayoutsPath(), (string) $this->getBuilder()->getConfig()->get('layouts.internal.dir')), $this->getBuilder()->getConfig()->getLayoutsPath());
71
            Util\File::getFS()->remove(Util::joinPath($this->getBuilder()->getConfig()->getLayoutsPath(), 'resources'));
72
            $output->writeln(sprintf('<info>Built-in templates extracted to "%s".</info>', (string) $this->getBuilder()->getConfig()->get('layouts.dir')));
73
        } catch (\Exception $e) {
74
            throw new RuntimeException(sprintf($e->getMessage()));
75
        }
76
77
        return 0;
78
    }
79
}
80