Completed
Push — master ( e0759a...dc0dc9 )
by Sergi Tur
04:57
created

InstallCommand   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 7
Bugs 0 Features 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 0
loc 157
rs 10
c 7
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
B initialize() 0 16 5
A isNoLlumActive() 0 4 1
A configure() 0 7 1
A execute() 0 11 2
A executeWithoutLlum() 0 18 2
A findComposer() 0 8 2
A getDevSuffix() 0 4 2
A publish() 0 5 1
A publishWithVendor() 0 5 1
1
<?php
2
3
namespace Acacha\AdminLTETemplateLaravel\Console;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Process\Process;
8
9
/**
10
 * Class InstallCommand.
11
 */
12
class InstallCommand extends BaseCommand
13
{
14
15
    /**
16
     * Avoids using llum to install package.
17
     *
18
     * @var bool
19
     */
20
    protected $noLlum = false;
21
22
    /**
23
     * Install using php artisan vendor:publish.
24
     *
25
     * @var bool
26
     */
27
    protected $useVendorPublish = false;
28
29
    /**
30
     * Install asking before overwrite files.
31
     *
32
     * @var bool
33
     */
34
    protected $askBeforeOverwrite = false;
35
36
    /**
37
     * Initialize command.
38
     *
39
     * @param InputInterface  $input
40
     * @param OutputInterface $output
41
     */
42
    protected function initialize(InputInterface $input, OutputInterface $output)
43
    {
44
        parent::initialize($input, $output);
45
        if ($input->hasOption('no-llum')) {
46
            $this->noLlum = $input->getOption('no-llum');
47
        }
48
        if ($input->hasOption('dev')) {
49
            $this->installDev = $input->getOption('dev');
50
        }
51
        if ($input->hasOption('use-vendor-publish')) {
52
            $this->useVendorPublish = $input->getOption('use-vendor-publish');
53
        }
54
        if ($input->hasOption('dontforce')) {
55
            $this->askBeforeOverwrite = $input->getOption('dontforce');
56
        }
57
    }
58
59
    /**
60
     * Check is --no-llum option is active.
61
     *
62
     * @return bool
63
     */
64
    private function isNoLlumActive()
65
    {
66
        return $this->noLlum;
67
    }
68
69
    /**
70
     * Configure the command options.
71
     */
72
    protected function configure()
73
    {
74
        $this->ignoreValidationErrors();
75
76
        $this->setName('install')
77
            ->setDescription('Install Acacha AdminLTE package into the current project.');
78
    }
79
80
    /**
81
     * Execute the command.
82
     *
83
     * @param InputInterface $input
84
     * @param OutputInterface $output
85
     * @return void
86
     */
87
    protected function execute(InputInterface $input, OutputInterface $output)
88
    {
89
        if ($this->isNoLlumActive()) {
90
            $this->executeWithoutLlum($output);
91
        } else {
92
            $llum = $this->findLlum();
93
            $package = $this->getPackageName();
94
            $output->writeln('<info>'.$llum.' package '.$this->getDevOption()." $package".'</info>');
95
            passthru($llum.' package '.$this->getDevOption().' '.$package);
96
        }
97
    }
98
99
    /**
100
     * Execute command with option --no-llum.
101
     *
102
     * @param OutputInterface $output
103
     */
104
    protected function executeWithoutLlum(OutputInterface $output)
105
    {
106
        $composer = $this->findComposer();
107
108
        $process = new Process($composer.' require acacha/admin-lte-template-laravel'.$this->getDevSuffix(),
109
            null, null, null, null);
110
111
        $output->writeln(
112
            '<info>Running composer require acacha/admin-lte-template-laravel'.$this->getDevSuffix().'</info>');
113
        $process->run(function ($type, $line) use ($output) {
114
            $output->write($line);
115
        });
116
117
        $output->writeln('<info>Copying file '.__DIR__.'/stubs/app.php'.' into '.getcwd().'/config/app.php</info>');
118
        copy(__DIR__.'/stubs/app.php', getcwd().'/config/app.php');
119
120
        $this->useVendorPublish ? $this->publishWithVendor($output) : $this->publish($output);
121
    }
122
123
    /**
124
     * Get the composer command for the environment.
125
     *
126
     * @return string
127
     */
128
    private function findComposer()
129
    {
130
        if (file_exists(getcwd().'/composer.phar')) {
131
            return '"'.PHP_BINARY.'" composer.phar"';
132
        }
133
134
        return 'composer';
135
    }
136
137
    /**
138
     * Gets dev suffix.
139
     *
140
     * @return string
141
     */
142
    private function getDevSuffix()
143
    {
144
        return $this->installDev ? ':dev-master' : '';
145
    }
146
147
    /**
148
     * Manually publishes files to project.
149
     *
150
     * @param OutputInterface $output
151
     */
152
    protected function publish(OutputInterface $output)
153
    {
154
        $output->writeln('<info>php artisan adminlte:publish</info>');
155
        passthru('php artisan adminlte-laravel:publish');
156
    }
157
158
    /**
159
     * Publishes files with artisan publish command.
160
     *
161
     * @param OutputInterface $output
162
     */
163
    protected function publishWithVendor(OutputInterface $output)
164
    {
165
        $output->writeln('<info>php artisan vendor:publish --tag=adminlte --force</info>');
166
        passthru('php artisan vendor:publish --tag=adminlte --force');
167
    }
168
}
169