Completed
Push — master ( c84e1c...f32b00 )
by Sergi Tur
03:36
created

LaravelConfigFile::provider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Acacha\Llum\Traits;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
/**
9
 * Class LaravelConfigFile.
10
 * @property string $laravel_config_file
11
 * @property OutputInterface $output
12
 */
13
trait LaravelConfigFile
14
{
15
    /**
16
     * Avoids using bash using stubs instead to modify config/app.php file.
17
     *
18
     * @var bool
19
     */
20
    protected $noBash = false;
21
22
    /**
23
     * @param InputInterface  $input
24
     * @param OutputInterface $output
25
     */
26
    protected function initialize(InputInterface $input, OutputInterface $output)
27
    {
28
        parent::initialize($input, $output);
29
        if ($input->hasOption('no-bash')) {
30
            $this->noBash = $input->getOption('no-bash');
31
        }
32
    }
33
34
    /**
35
     * Check is --no-bash option is active.
36
     *
37
     * @return bool
38
     */
39
    private function isNoBashActive()
40
    {
41
        return $this->noBash;
42
    }
43
44
    /**
45
     * Add Laravel IDE Helper provider to config/app.php file.
46
     *
47
     * @return int|null
48
     */
49
    protected function addLaravelIdeHelperProvider()
50
    {
51
        return $this->addProvider('Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class');
52
    }
53
54
    /**
55
     *  Add provider to config/app.php file.
56
     *
57
     * @param $provider
58
     *
59
     * @return int|null
60
     */
61
    private function addProvider($provider)
62
    {
63
        return $this->addTextIntoMountPoint('#llum_providers', $provider);
64
    }
65
66
    /**
67
     * Add alias to config/app.php file.
68
     *
69
     * @param string $alias
70
     *
71
     * @return int|null
72
     */
73
    private function addAlias($alias)
74
    {
75
        return $this->addTextIntoMountPoint('#llum_aliases', $alias);
76
    }
77
78
    /**
79
     * Insert text into file using mountpoint. Mountpoint is maintained at file.
80
     *
81
     * @param string $mountpoint
82
     * @param $textToAdd
83
     *
84
     * @return int|null
85
     */
86
    private function addTextIntoMountPoint($mountpoint, $textToAdd)
87
    {
88
        passthru(
89
            'sed -i \'s/.*'.$mountpoint.'.*/ \ \ \ \ \ \ \ '.$this->scapeSingleQuotes(preg_quote($textToAdd)).',\n \ \ \ \ \ \ \ '.$mountpoint.'/\' '.$this->laravel_config_file, $error);
90
91
        return $error;
92
    }
93
94
    /**
95
     * scape single quotes for sed using \x27.
96
     *
97
     * @param string $str
98
     *
99
     * @return string
100
     */
101
    private function scapeSingleQuotes($str)
102
    {
103
        return str_replace("'", '\\x27', $str);
104
    }
105
106
    /**
107
     * Installs provider in laravel config/app.php file.
108
     *
109
     * @param $provider
110
     */
111
    protected function provider($provider)
112
    {
113
        if ($this->installConfigAppFile() == -1) {
114
            return;
115
        }
116
        $this->addProvider($provider);
117
    }
118
119
    /**
120
     * Setup laravel config file adding providers and aliases.
121
     *
122
     * @param $providers
123
     * @param $aliases
124
     * @return int
125
     */
126
    private function setupLaravelConfigFile($providers, $aliases)
127
    {
128
        if ($this->installConfigAppFile() == -1) {
129
            return -1;
130
        }
131
132
        $this->addProviders($providers);
133
134
        $this->addAliases($aliases);
135
    }
136
137
    /**
138
     * Installs alias/facade in laravel config/app.php file.
139
     *
140
     * @param $aliasName
141
     * @param $aliasClass
142
     */
143
    protected function alias($aliasName, $aliasClass)
144
    {
145
        if ($this->installConfigAppFile() == -1) {
146
            return;
147
        }
148
        $this->addAlias("'".$aliasName."' => ".$aliasClass);
149
    }
150
151
    /**
152
     * Install /config/app.php file using bash script.
153
     */
154
    protected function installConfigAppFileWithBash()
155
    {
156
        passthru(__DIR__.'/../bash_scripts/iluminar.sh '.$this->laravel_config_file);
157
    }
158
159
    /**
160
     * Install /stubs/app.php into /config/app.php.
161
     */
162
    protected function installConfigAppFileWithStubs()
163
    {
164
        copy(__DIR__.'/stubs/app.php', $this->laravel_config_file);
165
    }
166
167
    /**
168
     * Check if Laravel config file exists.
169
     *
170
     * @return bool
171
     */
172
    protected function checkIfLaravelConfigFileExists()
173
    {
174
        return file_exists($this->laravel_config_file);
175
    }
176
177
    /**
178
     * Install llum custom config/app.php file.
179
     *
180
     * @return int
181
     */
182
    protected function installConfigAppFile()
183
    {
184
        $this->testLaravelConfigFileExists();
185
186
        $this->showWarningIfLaravelConfigAlreadySupportsLlum();
187
188
        if ($this->isNoBashActive()) {
189
            $this->installConfigAppFileWithStubs();
190
            $this->output->writeln('<info>File '.$this->laravel_config_file.' overwrited correctly with and stub.</info>');
191
        } else {
192
            $this->installConfigAppFileWithBash();
193
        }
194
195
        return 0;
196
    }
197
198
    /**
199
     * Test Laravel config file exists.
200
     *
201
     * @return int
202
     */
203
    private function testLaravelConfigFileExists()
204
    {
205
        if (! $this->checkIfLaravelConfigFileExists()) {
206
            $this->output->writeln('<error>File '.$this->laravel_config_file.' doesn\'t exists');
207
208
            return -1;
209
        }
210
    }
211
212
    /**
213
     * Show warning if Laravel config file already supports llum.
214
     *
215
     * @return int
216
     */
217
    private function showWarningIfLaravelConfigAlreadySupportsLlum()
218
    {
219
        if ($this->configAppFileAlreadyInstalled()) {
220
            $this->output->writeln('<info>File '.$this->laravel_config_file.' already supports llum.</info>');
221
222
            return 0;
223
        }
224
    }
225
226
    /**
227
     * Add providers to Laravel config file.
228
     *
229
     * @param $providers
230
     */
231
    protected function addProviders($providers)
232
    {
233
        foreach ($providers as $provider) {
234
            $this->output->writeln('<info>Adding '.$provider.' to Laravel config app.php file</info>');
235
            $this->addProvider($provider);
236
        }
237
    }
238
239
    /**
240
     * Add aliases to Laravel config file.
241
     *
242
     * @param $aliases
243
     */
244
    protected function addAliases($aliases)
245
    {
246
        foreach ($aliases as $alias => $aliasClass) {
247
            $this->output->writeln('<info>Adding '.$aliasClass.' to Laravel config app.php file</info>');
248
            $this->addAlias("'$alias' => ".$aliasClass);
249
        }
250
    }
251
252
    /**
253
     * Check if config/app.php stub file is already installed.
254
     *
255
     * @return bool
256
     */
257
    protected function configAppFileAlreadyInstalled()
258
    {
259
        if (strpos(file_get_contents($this->laravel_config_file), '#llum_providers') !== false) {
260
            return true;
261
        }
262
263
        return false;
264
    }
265
}
266