LaravelConfigFile::addService()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
1
<?php
2
3
namespace Acacha\Llum\Traits;
4
5
use Illuminate\Contracts\Filesystem\FileNotFoundException;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * Class LaravelConfigFile.
11
 *
12
 * @property string $laravel_config_file
13
 * @property string $laravel_services_file
14
 * @property OutputInterface $output
15
 */
16
trait LaravelConfigFile
17
{
18
    /**
19
     * Avoids using bash using stubs instead to modify config/app.php file.
20
     *
21
     * @var bool
22
     */
23
    protected $noBash = false;
24
25
    /**
26
     * @param InputInterface  $input
27
     * @param OutputInterface $output
28
     */
29 View Code Duplication
    protected function initialize(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        parent::initialize($input, $output);
32
        if ($input->hasOption('no-bash')) {
33
            $this->noBash = $input->getOption('no-bash');
34
        }
35
    }
36
37
    /**
38
     * Check is --no-bash option is active.
39
     *
40
     * @return bool
41
     */
42
    private function isNoBashActive()
43
    {
44
        return $this->noBash;
45
    }
46
47
    /**
48
     * Add Laravel IDE Helper provider to config/app.php file.
49
     *
50
     * @return int|null
51
     */
52
    protected function addLaravelIdeHelperProvider()
53
    {
54
        return $this->addProvider('Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class');
55
    }
56
57
    /**
58
     *  Add provider to config/app.php file.
59
     *
60
     * @param $provider
61
     *
62
     * @return int|null
63
     */
64
    private function addProvider($provider)
65
    {
66
        return $this->addTextIntoMountPoint('#llum_providers', $provider);
67
    }
68
69
    /**
70
     *  Add service from file to config/services.php file.
71
     *
72
     * @param $file
73
     *
74
     * @return int|null
75
     */
76
    private function addService($file, $outputFile = null)
77
    {
78
        $result = $this->addFileIntoMountPoint('#llum_services', $file, $outputFile);
79
80
        if ($result == 0) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $result of type integer|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
81
            $txtFile = ($outputFile == null) ? $this->laravel_services_file : $outputFile;
82
            $this->output->writeln('<info>File '.$txtFile.' updated.</info>');
83
        }
84
85
        return $result;
86
    }
87
88
    /**
89
     * Add alias to config/app.php file.
90
     *
91
     * @param string $alias
92
     *
93
     * @return int|null
94
     */
95
    private function addAlias($alias)
96
    {
97
        return $this->addTextIntoMountPoint('#llum_aliases', $alias);
98
    }
99
100
    /**
101
     * Insert text into file using mountpoint. Mountpoint is maintained at file.
102
     *
103
     * @param string $mountpoint
104
     * @param $textToAdd
105
     *
106
     * @return int|null
107
     */
108
    private function addTextIntoMountPoint($mountpoint, $textToAdd)
109
    {
110
        passthru(
111
            'sed -i \'s/.*'.$mountpoint.'.*/ \ \ \ \ \ \ \ '.$this->scapeSingleQuotes(preg_quote($textToAdd)).',\n \ \ \ \ \ \ \ '.$mountpoint.'/\' '.str_replace(" ", "\ ", $this->laravel_config_file), $error);
112
113
        return $error;
114
    }
115
116
    /**
117
     * Insert file into file using mountpoint.
118
     *
119
     * @param $mountpoint
120
     * @param $fileToInsert
121
     *
122
     * @return mixed
123
     */
124
    private function addFileIntoMountPoint($mountpoint, $fileToInsert, $outputFile = null)
125
    {
126
        if ($outputFile != null) {
127
            passthru(
128
                'sed -e \'/'.$mountpoint.'/r'.$fileToInsert.'\' '.
129
                    str_replace(" ", "\ ", $this->laravel_services_file).' > '.$outputFile, $error);
130
        } else {
131
            passthru(
132
                'sed -i \'/'.$mountpoint.'/r'.$fileToInsert.'\' '.str_replace(" ", "\ ", $this->laravel_services_file), $error);
133
        }
134
135
        return $error;
136
    }
137
138
    /**
139
     * scape single quotes for sed using \x27.
140
     *
141
     * @param string $str
142
     *
143
     * @return string
144
     */
145
    private function scapeSingleQuotes($str)
146
    {
147
        return str_replace("'", '\\x27', $str);
148
    }
149
150
    /**
151
     * Installs provider in laravel config/app.php file.
152
     *
153
     * @param $provider
154
     */
155
    protected function provider($provider)
156
    {
157
        if ($this->installConfigFile() == -1) {
158
            return;
159
        }
160
        $this->addProvider($provider);
161
    }
162
163
    /**
164
     * Add service/s from file to Laravel config/services.php.
165
     *
166
     * @param $file
167
     * @param null $outputFile
168
     * @throws FileNotFoundException
169
     */
170
    protected function service($file, $outputFile = null)
171
    {
172
        if (!file_exists($file)) throw new FileNotFoundException($file);
173
        if ($this->installConfigFile() == -1) {
174
            return;
175
        }
176
        $this->addService($file, $outputFile);
177
    }
178
179
    /**
180
     * Setup Laravel config file adding providers and aliases.
181
     *
182
     * @param $providers
183
     * @param $aliases
184
     *
185
     * @return int
186
     */
187
    private function setupLaravelConfigFile($providers, $aliases)
188
    {
189
        if ($this->installConfigFile() == -1) {
190
            return -1;
191
        }
192
193
        $this->addProviders($providers);
194
195
        $this->addAliases($aliases);
196
    }
197
198
    /**
199
     * Installs alias/facade in laravel config/app.php file.
200
     *
201
     * @param $aliasName
202
     * @param $aliasClass
203
     */
204
    protected function alias($aliasName, $aliasClass)
205
    {
206
        if ($this->installConfigFile() == -1) {
207
            return;
208
        }
209
        $this->addAlias("'".$aliasName."' => ".$aliasClass);
210
    }
211
212
    /**
213
     * Install /config/app.php file using bash script.
214
     */
215
    protected function installConfigFileWithBash()
216
    {
217
        passthru(str_replace(" ", "\ ", __DIR__).'/../bash_scripts/iluminar.sh '. str_replace(" ", "\ ", $this->laravel_config_file) .' '
218
            . str_replace(" ", "\ ", $this->laravel_services_file));
219
    }
220
221
    /**
222
     * Install /stubs/app.php into /config/app.php.
223
     */
224
    protected function installConfigFileWithStubs()
225
    {
226
        copy(__DIR__.'/stubs/app.php', $this->laravel_config_file);
227
        copy(__DIR__.'/stubs/services.php', $this->laravel_services_file);
228
    }
229
230
    /**
231
     * Check if Laravel config file exists.
232
     *
233
     * @return bool
234
     */
235
    protected function checkIfLaravelConfigFileExists()
236
    {
237
        return file_exists($this->laravel_config_file);
238
    }
239
240
    /**
241
     * Install llum custom config/app.php file.
242
     *
243
     * @return int
244
     */
245
    protected function installConfigFile()
246
    {
247
        if ($this->testLaravelConfigFileExists() == -1) {
248
            return;
249
        }
250
251
        $this->showWarningIfLaravelConfigAlreadySupportsLlum();
252
253
        if ($this->isNoBashActive()) {
254
            $this->installConfigFileWithStubs();
255
            $this->output->writeln('<info>File '.$this->laravel_config_file.' overwrited correctly with and stub.</info>');
256
        } else {
257
            $this->installConfigFileWithBash();
258
        }
259
260
        return 0;
261
    }
262
263
    /**
264
     * Test Laravel config file exists.
265
     *
266
     * @return int
267
     */
268
    private function testLaravelConfigFileExists()
269
    {
270
        if (!$this->checkIfLaravelConfigFileExists()) {
271
            $this->output->writeln('<error>File '.$this->laravel_config_file.' doesn\'t exists');
272
273
            return -1;
274
        }
275
    }
276
277
    /**
278
     * Show warning if Laravel config file already supports llum.
279
     *
280
     * @return int
281
     */
282
    private function showWarningIfLaravelConfigAlreadySupportsLlum()
283
    {
284
        if ($this->configAppFileAlreadyInstalled()) {
285
            $this->output->writeln('<info>File '.$this->laravel_config_file.' already supports llum.</info>');
286
287
            return 0;
288
        }
289
    }
290
291
    /**
292
     * Add providers to Laravel config file.
293
     *
294
     * @param $providers
295
     */
296
    protected function addProviders($providers)
297
    {
298
        foreach ($providers as $provider) {
299
            $this->output->writeln('<info>Adding '.$provider.' to Laravel config app.php file</info>');
300
            $this->addProvider($provider);
301
        }
302
    }
303
304
    /**
305
     * Add aliases to Laravel config file.
306
     *
307
     * @param $aliases
308
     */
309
    protected function addAliases($aliases)
310
    {
311
        if ($aliases == null) {
312
            return;
313
        }
314
        foreach ($aliases as $alias => $aliasClass) {
315
            $this->output->writeln('<info>Adding '.$aliasClass.' to Laravel config app.php file</info>');
316
            $this->addAlias("'$alias' => ".$aliasClass);
317
        }
318
    }
319
320
    /**
321
     * Check if config/app.php stub file is already installed.
322
     *
323
     * @return bool
324
     */
325
    protected function configAppFileAlreadyInstalled()
326
    {
327
        if (strpos(file_get_contents($this->laravel_config_file), '#llum_providers') !== false) {
328
            return true;
329
        }
330
331
        return false;
332
    }
333
}
334