Completed
Push — master ( 9c1f83...0c1135 )
by Paulo Rodrigues
10:00
created

Command/InstallCommand.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Rj\FrontendBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Process\Process;
9
10
class InstallCommand extends Command
11
{
12 36
    protected function configure()
13
    {
14
        $this
15 36
            ->setName('rj_frontend:install')
16 36
            ->setDescription('Install npm and bower dependencies')
17
        ;
18 36
    }
19
20 18
    protected function execute(InputInterface $input, OutputInterface $output)
21
    {
22 18
        if (!$this->commandExists('npm')) {
23 14
            return $output->writeln(
24
'<error>npm is not installed</error>
25
26
node.js is probably not installed on your system. For node.js installation
27
instructions, refer to https://nodejs.org/en/download/package-manager
28 14
'
29
            );
30
        }
31
32 4
        if (!$this->commandExists('bower')) {
33 2
            return $output->writeln(
34
'<error>bower is not installed</error>
35
36
You can install bower using npm:
37
npm install -g bower
38 2
'
39
            );
40
        }
41
42 2
        $output->writeln('<info>Running `npm install`</info>');
43 2
        $this->runProcess($output, 'npm install');
44
45 2
        $output->writeln('<info>Running `bower install`</info>');
46 2
        $this->runProcess($output, 'bower install');
47 2
    }
48
49
    protected function runProcess($output, $command)
50
    {
51
        $process = new Process($command);
52
53
        $process->run(function ($type, $buffer) use ($output) {
54
            if (Process::ERR === $type) {
55
                $output->writeln("<error>$buffer</error>");
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $buffer instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
56
            } else {
57
                $output->writeln($buffer);
58
            }
59
        });
60
61
        if (!$process->isSuccessful()) {
62
            throw new \RuntimeException($process->getErrorOutput());
63
        }
64
    }
65
66
    protected function commandExists($command)
67
    {
68
        $process = new Process("$command -v");
1 ignored issue
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $command instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
69
        $process->run();
70
71
        if (!$process->isSuccessful()) {
72
            return !preg_match('/: not found/', $process->getErrorOutput());
73
        }
74
75
        return true;
76
    }
77
}
78