AssetsInstallCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 3
nc 2
nop 2
1
<?php
2
3
namespace Admingenerator\GeneratorBundle\Command;
4
5
use Admingenerator\GeneratorBundle\Filesystem\RelativePathComputer;
6
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Process\Process;
11
12
/**
13
 * Class AssetsInstallCommand
14
 * @package Admingenerator\GeneratorBundle\Command
15
 * @author Stéphane Escandell
16
 *
17
 * Automatically call bower install to properly import bowers components.
18
 * Push them to the web root directory
19
 */
20
class AssetsInstallCommand extends ContainerAwareCommand
21
{
22
    /**
23
     * {@inheritdoc}
24
     */
25
    protected function configure()
26
    {
27
        $this
28
            ->setName('admin:assets-install')
29
            ->setDescription('Fetch bower declared dependencies and push them into web root directory')
30
            ->setHelp('The <info>admin:assets-install</info> command fetch bower dependencies (CSS and JS files) to the web root dir.')
31
            ->setDefinition(array(
32
                new InputOption('mode', 'm', InputOption::VALUE_OPTIONAL, 'Mode to fetch dependencies', 'install'),
33
                new InputOption('bower-bin', 'b', InputOption::VALUE_REQUIRED, 'Path to the bower binary', 'bower')
34
            ))
35
        ;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $bowerFileLocation = dirname(dirname(__FILE__));
44
        $targetDir = $this->computeTargetDirectory($bowerFileLocation);
45
        $formatter = $this->getHelperSet()->get('formatter');
46
47
        $cmd = sprintf(
48
            '%s %s --config.directory=%s',
49
            $input->getOption('bower-bin'),
50
            $input->getOption('mode'),
51
            $targetDir
52
        );
53
54
        if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
55
            $output->writeln($formatter->formatSection('Bower', sprintf('Running command %s', $cmd)));
56
        }
57
58
        $process = new Process($cmd);
59
        $process->setTimeout(300);
60
        $process->setWorkingDirectory($bowerFileLocation);
61
        $output->writeln($formatter->formatSection('Bower', sprintf('Fetching vendors using the <info>%s</info> mode.', $input->getOption('mode'))));
62
        $process->run(function ($type, $buffer) use ($output, $formatter) {
63
            if (Process::ERR == $type) {
64
                $output->write($formatter->formatBlock($buffer, 'error'));
65
            } else {
66
                $output->write($formatter->formatSection('Bower', $buffer, 'info' ));
67
            }
68
        });
69
        
70
        return $process->getExitCode();
71
    }
72
73
    /**
74
     * Compute relative path from $bowerFileDirectory to the web directory
75
     *
76
     * @param string $bowerFileDirectory
77
     * @return string
78
     */
79
    private function computeTargetDirectory($bowerFileDirectory)
80
    {
81
        $parentWebDir = dirname($this->getContainer()->getParameter('kernel.root_dir'));
82
        $relativePathComputer = new RelativePathComputer($bowerFileDirectory);
83
84
        return $relativePathComputer->computeToParent($parentWebDir) . 'web' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'components';
85
    }
86
}
87