Completed
Push — master ( 3e25f8...e7bd0a )
by Sergi Tur
02:09
created

InstallCommand::getPackageName()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.2
cc 4
eloc 4
nc 4
nop 0
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
     * Avoids using llum to install package.
16
     *
17
     * @var bool
18
     */
19
    protected $noLlum = false;
20
21
    /**
22
     * Install using php artisan vendor:publish.
23
     *
24
     * @var bool
25
     */
26
    protected $useVendorPublish = false;
27
28
    /**
29
     * Install asking before overwrite files.
30
     *
31
     * @var bool
32
     */
33
    protected $askBeforeOverwrite = false;
34
35
    /**
36
     * Initialize command.
37
     *
38
     * @param InputInterface  $input
39
     * @param OutputInterface $output
40
     */
41
    protected function initialize(InputInterface $input, OutputInterface $output)
42
    {
43
        parent::initialize($input, $output);
44
        if ($input->hasOption('no-llum')) {
45
            $this->noLlum = $input->getOption('no-llum');
46
        }
47
        if ($input->hasOption('dev')) {
48
            $this->installDev = $input->getOption('dev');
49
        }
50
        if ($input->hasOption('use-vendor-publish')) {
51
            $this->useVendorPublish = $input->getOption('use-vendor-publish');
52
        }
53
        if ($input->hasOption('dontforce')) {
54
            $this->askBeforeOverwrite = $input->getOption('dontforce');
55
        }
56
    }
57
58
    /**
59
     * Check is --no-llum option is active.
60
     *
61
     * @return bool
62
     */
63
    private function isNoLlumActive()
64
    {
65
        return $this->noLlum;
66
    }
67
68
    /**
69
     * Configure the command options.
70
     */
71
    protected function configure()
72
    {
73
        $this->ignoreValidationErrors();
74
75
        $this->setName('install')
76
            ->setDescription('Install Acacha AdminLTE package into the current project.');
77
    }
78
79
    /**
80
     * Execute the command.
81
     *
82
     * @param InputInterface  $input
83
     * @param OutputInterface $output
84
     *
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
     * Get llum package name.
101
     */
102
    private function getPackageName()
103
    {
104
        if (!$this->askBeforeOverwrite) {
105
            return $this->useVendorPublish ? $package = 'AdminLTEVendorPublish' : $package = 'AdminLTE';
106
        }
107
108
        return $this->useVendorPublish ? $package = 'AdminLTEVendorPublishDontForce' : $package = 'AdminLTEDontForce';
109
    }
110
111
    /**
112
     * Execute command with option --no-llum.
113
     *
114
     * @param OutputInterface $output
115
     */
116
    protected function executeWithoutLlum(OutputInterface $output)
117
    {
118
        $composer = $this->findComposer();
119
120
        $process = new Process($composer.' require acacha/admin-lte-template-laravel'.$this->getDevSuffix(),
121
            null, null, null, null);
122
123
        $output->writeln(
124
            '<info>Running composer require acacha/admin-lte-template-laravel'.$this->getDevSuffix().'</info>');
125
        $process->run(function ($type, $line) use ($output) {
126
            $output->write($line);
127
        });
128
129
        $output->writeln('<info>Copying file '.__DIR__.'/stubs/app.php'.' into '.getcwd().'/config/app.php</info>');
130
        copy(__DIR__.'/stubs/app.php', getcwd().'/config/app.php');
131
132
        $this->useVendorPublish ? $this->publishWithVendor($output) : $this->publish($output);
133
    }
134
135
    /**
136
     * Get the composer command for the environment.
137
     *
138
     * @return string
139
     */
140
    private function findComposer()
141
    {
142
        if (file_exists(getcwd().'/composer.phar')) {
143
            return '"'.PHP_BINARY.'" composer.phar"';
144
        }
145
146
        return 'composer';
147
    }
148
149
    /**
150
     * Gets dev suffix.
151
     *
152
     * @return string
153
     */
154
    private function getDevSuffix()
155
    {
156
        return $this->installDev ? ':dev-master' : '';
157
    }
158
159
    /**
160
     * Manually publishes files to project.
161
     *
162
     * @param OutputInterface $output
163
     */
164
    protected function publish(OutputInterface $output)
165
    {
166
        $output->writeln('<info>php artisan adminlte:publish</info>');
167
        passthru('php artisan adminlte-laravel:publish');
168
    }
169
170
    /**
171
     * Publishes files with artisan publish command.
172
     *
173
     * @param OutputInterface $output
174
     */
175
    protected function publishWithVendor(OutputInterface $output)
176
    {
177
        $output->writeln('<info>php artisan vendor:publish --tag=adminlte --force</info>');
178
        passthru('php artisan vendor:publish --tag=adminlte --force');
179
    }
180
}
181