Completed
Push — master ( a35231...0dbf21 )
by Iulian
02:54
created

ElixirMixCommand::getRootDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Iulyanp\ElixirMixBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
9
use Symfony\Component\Filesystem\Filesystem;
10
11
/**
12
 * Class ElixirMixCommand
13
 * @package Iulyanp\ElixirMixCommand\Command
14
 */
15
class ElixirMixCommand extends ContainerAwareCommand
16
{
17
    /**
18
     * Configure.
19
     */
20
    public function configure()
21
    {
22
        $this->setName('mix:init')
23
            ->setDescription('Init the bootstrap package.json and webpack.mix.js files.');
24
    }
25
26
    /**
27
     * @param InputInterface  $input
28
     * @param OutputInterface $output
29
     *
30
     * @return void
31
     */
32
    public function execute(InputInterface $input, OutputInterface $output)
33
    {
34
        $this->writeInfo($output, 'Installing laravel mix...');
35
36
        $container = $this->getContainer();
37
        $appDir = $container->getParameter('kernel.root_dir');
38
        $rootDir = $this->getRootDir($appDir);
39
40
        try {
41
            $fs = new Filesystem();
42
43
            $packageContent = realpath(dirname(__FILE__)).'/../package.json';
44
            $packagePath = sprintf('%s%s', $rootDir, 'package.json');
45
            $fs->copy($packageContent, $packagePath);
46
47
            $webpackMixContent = realpath(dirname(__FILE__)).'/../webpack.mix.js.dist';
48
            $webpackMixPath = sprintf('%s%s', $rootDir, 'webpack.mix.js');
49
            $fs->copy($webpackMixContent, $webpackMixPath);
50
        } catch (IOExceptionInterface $e) {
51
            $this->writeError($output, $e->getMessage());
52
        }
53
54
        $this->writeInfo($output, "You're all set. Go and build something amazing.");
55
    }
56
57
    /**
58
     * @param OutputInterface $output
59
     * @param string          $error
60
     *
61
     * @return mixed
62
     */
63
    private function writeError(OutputInterface $output, $error)
64
    {
65
        return $output->writeln('<error>'.$error.'</error>');
66
    }
67
68
    /**
69
     * @param OutputInterface $output
70
     * @param string          $message
71
     *
72
     * @return mixed
73
     */
74
    private function writeInfo(OutputInterface $output, $message)
75
    {
76
        return $output->writeln(sprintf('<info>%s</info>', $message));
77
    }
78
79
    /**
80
     * @param $appDir
81
     *
82
     * @return string
83
     */
84
    private function getRootDir($appDir)
85
    {
86
        return sprintf('%s%s%s%s', $appDir, DIRECTORY_SEPARATOR, '..', DIRECTORY_SEPARATOR);
87
    }
88
}
89