Completed
Push — master ( 15f0d4...d2cbab )
by Sergi Tur
03:41
created

InstallCommand::getPackageName()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
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\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Process\Process;
9
10
/**
11
 * Class InstallCommand.
12
 */
13
class InstallCommand extends Command
14
{
15
    /**
16
     * Avoids using llum to install package.
17
     *
18
     * @var bool
19
     */
20
    protected $noLlum = false;
21
22
    /**
23
     * Install development version.
24
     *
25
     * @var bool
26
     */
27
    protected $installDev = false;
28
29
    /**
30
     * Install using php artisan vendor:publish.
31
     *
32
     * @var bool
33
     */
34
    protected $useVendorPublish = false;
35
36
    /**
37
     * Install asking before overwrite files.
38
     *
39
     * @var bool
40
     */
41
    protected $askBeforeOverwrite = false;
42
43
    /**
44
     * Initialize command.
45
     *
46
     * @param InputInterface  $input
47
     * @param OutputInterface $output
48
     */
49
    protected function initialize(InputInterface $input, OutputInterface $output)
50
    {
51
        parent::initialize($input, $output);
52
        if ($input->hasOption('no-llum')) {
53
            $this->noLlum = $input->getOption('no-llum');
54
        }
55
        if ($input->hasOption('dev')) {
56
            $this->installDev = $input->getOption('dev');
57
        }
58
        if ($input->hasOption('use-vendor-publish')) {
59
            $this->useVendorPublish = $input->getOption('use-vendor-publish');
60
        }
61
        if ($input->hasOption('dontforce')) {
62
            $this->askBeforeOverwrite = $input->getOption('dontforce');
63
        }
64
    }
65
66
    /**
67
     * Check is --no-llum option is active.
68
     *
69
     * @return bool
70
     */
71
    private function isNoLlumActive()
72
    {
73
        return $this->noLlum;
74
    }
75
76
    /**
77
     * Configure the command options.
78
     */
79
    protected function configure()
80
    {
81
        $this->ignoreValidationErrors();
82
83
        $this->setName('install')
84
            ->setDescription('Install Acacha AdminLTE package into the current project.');
85
    }
86
87
    /**
88
     * Execute the command.
89
     *
90
     * @param InputInterface  $input
91
     * @param OutputInterface $output
92
     */
93
    protected function execute(InputInterface $input, OutputInterface $output)
94
    {
95
        if ($this->isNoLlumActive()) {
96
            $this->executeWithoutLlum($output);
97
        } else {
98
            $llum = $this->findLlum();
99
            $package = $this->getPackageName();
100
            $output->writeln('<info>'.$llum.' package '.$this->getDevOption()." $package".'</info>');
101
            passthru($llum.' package '.$this->getDevOption(). ' ' . $package);
102
        }
103
    }
104
105
    /**
106
     * Get llum package name
107
     */
108
    private function getPackageName() {
109
        if (! $this->askBeforeOverwrite) {
110
            return $this->useVendorPublish ? $package = 'AdminLTEVendorPublish' : $package = 'AdminLTE';
111
        }
112
        return $this->useVendorPublish ? $package = 'AdminLTEVendorPublishDontForce' : $package = 'AdminLTEDontForce';
113
    }
114
115
    /**
116
     * Execute command wiht option --no-llum.
117
     *
118
     * @param OutputInterface $output
119
     */
120
    protected function executeWithoutLlum(OutputInterface $output)
121
    {
122
        $composer = $this->findComposer();
123
124
        $process = new Process($composer.' require acacha/admin-lte-template-laravel'.$this->getDevSuffix(),
125
            null, null, null, null);
126
127
        $output->writeln(
128
            '<info>Running composer require acacha/admin-lte-template-laravel'.$this->getDevSuffix().'</info>');
129
        $process->run(function ($type, $line) use ($output) {
130
            $output->write($line);
131
        });
132
133
        $output->writeln('<info>Copying file '.__DIR__.'/stubs/app.php'.' into '.getcwd().'/config/app.php</info>');
134
        copy(__DIR__.'/stubs/app.php', getcwd().'/config/app.php');
135
136
        $this->useVendorPublish ? $this->publishWithVendor($output) : $this->publish($output);
137
    }
138
139
    /**
140
     * Get the composer command for the environment.
141
     *
142
     * @return string
143
     */
144
    private function findComposer()
145
    {
146
        if (file_exists(getcwd().'/composer.phar')) {
147
            return '"'.PHP_BINARY.'" composer.phar"';
148
        }
149
150
        return 'composer';
151
    }
152
153
    /**
154
     * Get the llum command for the environment.
155
     *
156
     * @return string
157
     */
158
    private function findLlum()
159
    {
160
        $HOME = $this->getUserHomePath();
161
        if (is_executable($this->getRealPath("$HOME/.composer/vendor/bin/llum"))) {
162
            return "$HOME/.composer/vendor/bin/llum";
163
        }
164
        if (is_executable($this->getRealPath("$HOME/.config/composer/vendor/bin/llum"))) {
165
            return "$HOME/.config/composer/vendor/bin/llum";
166
        }
167
168
        return 'llum';
169
    }
170
171
    /**
172
     * Get the real path of a link or regular path if file is not a link.
173
     *
174
     * @param $file
175
     *
176
     * @return string
177
     */
178
    private function getRealPath($file)
179
    {
180
        if (is_link($file)) {
181
            return realpath($file);
182
        }
183
184
        return $file;
185
    }
186
187
    /**
188
     * @return string
189
     */
190
    public function getUserHomePath()
0 ignored issues
show
Coding Style introduced by
getUserHomePath uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
191
    {
192
        if (isset($_SERVER['HOME'])) {
193
            return $_SERVER['HOME'];
194
        }
195
196
        if (PHP_OS == 'WINNT') {
197
            return getenv('USERPROFILE');
198
        } else {
199
            return getenv('HOME');
200
        }
201
    }
202
203
    /*
204
     * Gets dev option
205
     *
206
     * @return string
207
     */
208
    /**
209
     * @return string
210
     */
211
    private function getDevOption()
212
    {
213
        return $this->installDev ? '--dev' : '';
214
    }
215
216
    /*
217
     * Gets dev suffix
218
     *
219
     * @return string
220
     */
221
    /**
222
     * @return string
223
     */
224
    private function getDevSuffix()
225
    {
226
        return $this->installDev ? ':dev-master' : '';
227
    }
228
229
    /**
230
     * Manually publishes files to project.
231
     *
232
     * @param OutputInterface $output
233
     */
234
    protected function publish(OutputInterface $output)
235
    {
236
        $output->writeln('<info>php artisan adminlte:publish</info>');
237
        passthru('php artisan adminlte-laravel:publish');
238
    }
239
240
    /**
241
     * Publishes files with artisan publish command.
242
     *
243
     * @param OutputInterface $output
244
     */
245
    protected function publishWithVendor(OutputInterface $output)
246
    {
247
        $output->writeln('<info>php artisan vendor:publish --tag=adminlte --force</info>');
248
        passthru('php artisan vendor:publish --tag=adminlte --force');
249
    }
250
}
251