Completed
Push — master ( b20807...1a2b32 )
by Mokhlas
01:20
created

MultiAuthPrepare::handle()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 21
nc 2
nop 0
1
<?php
2
3
namespace iMokhles\MultiAuthCommand\Command;
4
5
6
use Illuminate\Database\Console\Migrations\BaseCommand;
7
use Illuminate\Database\Migrations\MigrationCreator;
8
use Illuminate\Support\Composer;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Process\Exception\ProcessFailedException;
12
use Symfony\Component\Process\Process;
13
14
class MultiAuthPrepare extends BaseCommand
15
{
16
    /**
17
     * The name and signature of the console command.
18
     *
19
     * @var string
20
     */
21
    protected $signature = 'test-make:multi-auth {name} {--is_backpack= : Check if backpack or not to publish correct views}';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 126 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
22
23
    /**
24
     * The console command description.
25
     *
26
     * @var string
27
     */
28
    protected $description = 'Create MultiAuth for your project';
29
30
    /**
31
     * The migration creator instance.
32
     *
33
     * @var \Illuminate\Database\Migrations\MigrationCreator
34
     */
35
    protected $creator;
36
37
    /**
38
     * The Composer instance.
39
     *
40
     * @var \Illuminate\Support\Composer
41
     */
42
    protected $composer;
43
44
    /**
45
     * Create a new migration install command instance.
46
     *
47
     * @param  \Illuminate\Database\Migrations\MigrationCreator  $creator
48
     * @param  \Illuminate\Support\Composer  $composer
49
     */
50
    public function __construct(MigrationCreator $creator, Composer $composer)
51
    {
52
        parent::__construct();
53
        $this->creator = $creator;
54
        $this->composer = $composer;
55
    }
56
57
    /**
58
     * Execute the console command.
59
     *
60
     * @return boolean
61
     */
62
    public function handle()
63
    {
64
65
        $this->info("### Preparing For MultiAuth. Please wait...");
66
67
        $is_backpack = $this->option('is_backpack');
68
69
        $is_backpack_enabled = false;
70
        if ($is_backpack == 1) {
71
            $is_backpack_enabled = true;
72
        }
73
74
        $this->installMigration($is_backpack_enabled);
75
        $this->installModel($is_backpack_enabled);
76
        $this->installRouteMaps($is_backpack_enabled);
77
        $this->installRouteFiles($is_backpack_enabled);
78
        $this->installControllers($is_backpack_enabled);
79
        $this->installRequests($is_backpack_enabled);
80
        $this->installConfigs($is_backpack_enabled);
81
        $this->installMiddleware($is_backpack_enabled);
82
        $this->installUnauthenticated($is_backpack_enabled);
83
        $this->installView($is_backpack_enabled);
84
        $this->installPrologueAlert($is_backpack_enabled);
85
86
        $this->composer->dumpAutoloads();
87
88
        $this->info("### Finished MultiAuth.");
89
        $this->info("### Finished MultiAuth for Backpack.");
90
91
92
        return true;
93
    }
94
95
    /**
96
     * Publish Prologue Alert
97
     *
98
     * @return boolean
99
     */
100
    public function installPrologueAlert($is_backpack_enabled) {
0 ignored issues
show
Coding Style Naming introduced by
The parameter $is_backpack_enabled is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
101
        $alertsConfigFile = $this->getConfigsFolderPath().DIRECTORY_SEPARATOR."prologue/alerts.php";
102
        if (!file_exists($alertsConfigFile)) {
103
            $this->executeProcess('php artisan vendor:publish --provider="Prologue\Alerts\AlertsServiceProvider"',
104
                'publishing config for notifications - prologue/alerts');
105
        }
106
107
        return true;
108
    }
109
110
    /**
111
     * Install Migration.
112
     *
113
     * @return boolean
114
     */
115
    public function installMigration($is_backpack_enabled)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $is_backpack_enabled is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
116
    {
117
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
118
        $name = ucfirst($this->getParsedNameInput());
119
        $namePlural = str_plural($name);
120
121
122
123
        $modelTableContent = file_get_contents(__DIR__ . '/../Migration/modelTable.stub');
124
        $modelTableContentNew = str_replace([
125
            '{{$namePlural}}',
126
            '{{$nameSmallPlural}}',
127
        ], [
128
            $namePlural,
129
            $nameSmallPlural
130
        ], $modelTableContent);
131
132
133
        $modelResetPasswordTableContent = file_get_contents(__DIR__ . '/../Migration/passwordResetsTable.stub');
134
        $modelResetPasswordTableContentNew = str_replace([
135
            '{{$namePlural}}',
136
            '{{$nameSmallPlural}}',
137
        ], [
138
            $namePlural,
139
            $nameSmallPlural
140
        ], $modelResetPasswordTableContent);
141
142
143
        $migrationName = date('Y_m_d_His') . '_'.'create_' . str_plural(snake_case($name)) .'_table.php';
144
        $migrationModelPath = $this->getMigrationPath().DIRECTORY_SEPARATOR.$migrationName;
145
        file_put_contents($migrationModelPath, $modelTableContentNew);
146
147
        $migrationResetName = date('Y_m_d_His') . '_'
148
            .'create_' . str_plural(snake_case($name))
149
            .'_password_resets_table.php';
150
        $migrationResetModelPath = $this->getMigrationPath().DIRECTORY_SEPARATOR.$migrationResetName;
151
        file_put_contents($migrationResetModelPath, $modelResetPasswordTableContentNew);
152
153
        return true;
154
155
    }
156
157
158
    /**
159
     * Install Model.
160
     *
161
     * @return boolean
162
     */
163
    public function installModel($is_backpack_enabled)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $is_backpack_enabled is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
164
    {
165
        $nameSmall = snake_case($this->getParsedNameInput());
166
        $name = ucfirst($this->getParsedNameInput());
167
168
169
        $arrayToChange = [
170
            '{{$name}}',
171
        ];
172
173
        $newChanges = [
174
            $name,
175
        ];
176
        if ($is_backpack_enabled == true) {
177
            $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
178
            array_push($arrayToChange, '{{$nameSmallPlural}}');
179
            array_push($newChanges, $nameSmallPlural);
180
181
            $modelContent = file_get_contents(__DIR__ . '/../Backpack/Model/model.stub');
182
            $modelContentNew = str_replace($arrayToChange, $newChanges, $modelContent);
183
        } else {
184
            $modelContent = file_get_contents(__DIR__ . '/../Model/model.stub');
185
            $modelContentNew = str_replace($arrayToChange, $newChanges, $modelContent);
186
        }
187
188
        $createFolder = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Models";
189
        if (!file_exists($createFolder)) {
190
            mkdir($createFolder);
191
        }
192
193
        $modelPath = $createFolder.DIRECTORY_SEPARATOR.$name.".php";
194
        file_put_contents($modelPath, $modelContentNew);
195
196
197
198
        $resetNotificationContent = file_get_contents(__DIR__ . '/../Notification/resetPasswordNotification.stub');
199
        $resetNotificationContentNew = str_replace([
200
            '{{$name}}',
201
            '{{$nameSmall}}',
202
        ], [
203
            $name,
204
            $nameSmall
205
        ], $resetNotificationContent);
206
207
        $createFolder = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Notifications";
208
        if (!file_exists($createFolder)) {
209
            mkdir($createFolder);
210
        }
211
212
        $resetNotificationPath = $createFolder.DIRECTORY_SEPARATOR.$name."ResetPasswordNotification.php";
213
        file_put_contents($resetNotificationPath, $resetNotificationContentNew);
214
215
        return true;
216
217
    }
218
219
    /**
220
     * Install View.
221
     *
222
     * @return boolean
223
     */
224
    public function installView($is_backpack_enabled)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $is_backpack_enabled is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
225
    {
226
        $nameSmall = snake_case($this->getParsedNameInput());
227
        $name = ucfirst($this->getParsedNameInput());
228
229
        if ($is_backpack_enabled == true) {
230
            $appBlade = file_get_contents(__DIR__ . '/../Backpack/Views/layouts/layout.blade.stub');
0 ignored issues
show
Unused Code introduced by
$appBlade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
231
        } else {
232
            $appBlade = file_get_contents(__DIR__ . '/../Views/layouts/app.blade.stub');
0 ignored issues
show
Unused Code introduced by
$appBlade is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
233
        }
234
235
236
237
238
239
        if ($is_backpack_enabled == true) {
240
            $appBlade = file_get_contents(__DIR__ . '/../Backpack/Views/layouts/layout.blade.stub');
241
            $homeBlade = file_get_contents(__DIR__ . '/../Backpack/Views/home.blade.stub');
242
            $loginBlade = file_get_contents(__DIR__ . '/../Backpack/Views/auth/login.blade.stub');
243
            $registerBlade = file_get_contents(__DIR__ . '/../Backpack/Views/auth/register.blade.stub');
244
            $resetBlade = file_get_contents(__DIR__ . '/../Backpack/Views/auth/passwords/reset.blade.stub');
245
            $emailBlade = file_get_contents(__DIR__ . '/../Backpack/Views/auth/passwords/email.blade.stub');
246
247
            $update_infoBlade = file_get_contents(__DIR__
248
                . '/../Backpack/Views/auth/account/update_info.blade.stub');
249
            $change_passwordBlade = file_get_contents(__DIR__
250
                . '/../Backpack/Views/auth/account/change_password.blade.stub');
251
252
            $sidemenuBlade = file_get_contents(__DIR__
253
                . '/../Backpack/Views/auth/account/sidemenu.blade.stub');
254
            $main_headerBlade = file_get_contents(__DIR__
255
                . '/../Backpack/Views/inc/main_header.blade.stub');
256
            $menuBlade = file_get_contents(__DIR__
257
                . '/../Backpack/Views/inc/menu.blade.stub');
258
            $sidebarBlade = file_get_contents(__DIR__
259
                . '/../Backpack/Views/inc/sidebar.blade.stub');
260
            $sidebar_user_panelBlade = file_get_contents(__DIR__
261
                . '/../Backpack/Views/inc/sidebar_user_panel.blade.stub');
262
263
264
        } else {
265
            $welcomeBlade = file_get_contents(__DIR__ . '/../Views/welcome.blade.stub');
266
            $appBlade = file_get_contents(__DIR__ . '/../Views/layouts/app.blade.stub');
267
            $homeBlade = file_get_contents(__DIR__ . '/../Views/home.blade.stub');
268
            $loginBlade = file_get_contents(__DIR__ . '/../Views/auth/login.blade.stub');
269
            $registerBlade = file_get_contents(__DIR__ . '/../Views/auth/register.blade.stub');
270
            $resetBlade = file_get_contents(__DIR__ . '/../Views/auth/passwords/reset.blade.stub');
271
            $emailBlade = file_get_contents(__DIR__ . '/../Views/auth/passwords/email.blade.stub');
272
            $update_infoBlade = file_get_contents(__DIR__
273
                . '/../Views/auth/account/update_info.blade.stub');
274
            $change_passwordBlade = file_get_contents(__DIR__
275
                . '/../Views/auth/account/change_password.blade.stub');
276
277
        }
278
279
280
        $createFolder = $this->getViewsFolderPath().DIRECTORY_SEPARATOR."$nameSmall";
281
        if (!file_exists($createFolder)) {
282
            mkdir($createFolder);
283
        }
284
285
        $createFolderLayouts = $this->getViewsFolderPath().DIRECTORY_SEPARATOR
286
            ."$nameSmall"
287
            .DIRECTORY_SEPARATOR."layouts";
288
        if (!file_exists($createFolderLayouts)) {
289
            mkdir($createFolderLayouts);
290
        }
291
292
        $createFolderInc = $this->getViewsFolderPath().DIRECTORY_SEPARATOR
293
            ."$nameSmall"
294
            .DIRECTORY_SEPARATOR."inc";
295
        if (!file_exists($createFolderInc)) {
296
            mkdir($createFolderInc);
297
        }
298
299
        $createFolderAuth = $this->getViewsFolderPath().DIRECTORY_SEPARATOR."$nameSmall"
300
            .DIRECTORY_SEPARATOR."auth";
301
        if (!file_exists($createFolderAuth)) {
302
            mkdir($createFolderAuth);
303
        }
304
305
        $createFolderAuthPasswords = $this->getViewsFolderPath().DIRECTORY_SEPARATOR.
306
            "$nameSmall".DIRECTORY_SEPARATOR
307
            ."auth".DIRECTORY_SEPARATOR."passwords";
308
        if (!file_exists($createFolderAuthPasswords)) {
309
            mkdir($createFolderAuthPasswords);
310
        }
311
312
        $createFolderAuthAccount = $this->getViewsFolderPath().DIRECTORY_SEPARATOR.
313
            "$nameSmall".DIRECTORY_SEPARATOR
314
            ."auth".DIRECTORY_SEPARATOR."account";
315
        if (!file_exists($createFolderAuthAccount)) {
316
            mkdir($createFolderAuthAccount);
317
        }
318
319
        $appBladeNew = str_replace([
320
            '{{$nameSmall}}',
321
        ], [
322
            $nameSmall
323
        ], $appBlade);
324
325
        $welcomeBladeNew = str_replace([
326
            '{{$nameSmall}}',
327
        ], [
328
            $nameSmall
329
        ], $welcomeBlade);
0 ignored issues
show
Bug introduced by
The variable $welcomeBlade does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
330
331
        $homeBladeNew = str_replace([
332
            '{{$nameSmall}}',
333
            '{{$name}}',
334
335
        ], [
336
            $nameSmall
337
        ], $homeBlade);
338
339
        $loginBladeNew = str_replace([
340
            '{{$nameSmall}}',
341
        ], [
342
            $nameSmall
343
        ], $loginBlade);
344
345
        $registerBladeNew = str_replace([
346
            '{{$nameSmall}}',
347
        ], [
348
            $nameSmall
349
        ], $registerBlade);
350
351
        $emailBladeNew = str_replace([
352
            '{{$nameSmall}}',
353
        ], [
354
            $nameSmall
355
        ], $emailBlade);
356
357
        $resetBladeNew = str_replace([
358
            '{{$nameSmall}}',
359
        ], [
360
            $nameSmall
361
        ], $resetBlade);
362
363
        $update_infoBladeNew = str_replace([
364
            '{{$nameSmall}}',
365
            '{{$name}}',
366
        ], [
367
            $nameSmall,
368
            $name
369
        ], $update_infoBlade);
370
371
        $change_passwordBladeNew = str_replace([
372
            '{{$nameSmall}}',
373
        ], [
374
            $nameSmall,
375
            $name
376
        ], $change_passwordBlade);
377
378
379
        if ($is_backpack_enabled == true) {
380
            $sidemenuBladeNew = str_replace([
0 ignored issues
show
Unused Code introduced by
$sidemenuBladeNew is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
381
                '{{$nameSmall}}',
382
            ], [
383
                $nameSmall
384
            ], $sidemenuBlade);
0 ignored issues
show
Bug introduced by
The variable $sidemenuBlade does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
385
386
            $main_headerBladeNew = str_replace([
387
                '{{$nameSmall}}',
388
            ], [
389
                $nameSmall
390
            ], $main_headerBlade);
0 ignored issues
show
Bug introduced by
The variable $main_headerBlade does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
391
392
            $menuBladeNew = str_replace([
393
                '{{$nameSmall}}',
394
            ], [
395
                $nameSmall
396
            ], $menuBlade);
0 ignored issues
show
Bug introduced by
The variable $menuBlade does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
397
398
            $sidebarBladeNew = str_replace([
399
                '{{$nameSmall}}',
400
            ], [
401
                $nameSmall
402
            ], $sidebarBlade);
0 ignored issues
show
Bug introduced by
The variable $sidebarBlade does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
403
404
            $sidebar_user_panelBladeNew = str_replace([
405
                '{{$nameSmall}}',
406
            ], [
407
                $nameSmall
408
            ], $sidebar_user_panelBlade);
0 ignored issues
show
Bug introduced by
The variable $sidebar_user_panelBlade does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
409
410
411
            file_put_contents($createFolderLayouts.'/layout.blade.php', $appBladeNew);
412
413
            file_put_contents($createFolderAuthAccount.'/sidemenu.blade.php', $main_headerBladeNew);
414
            file_put_contents($createFolderInc.'/main_header.blade.php', $main_headerBladeNew);
415
            file_put_contents($createFolderInc.'/menu.blade.php', $menuBladeNew);
416
            file_put_contents($createFolderInc.'/sidebar.blade.php', $sidebarBladeNew);
417
            file_put_contents($createFolderInc.'/sidebar_user_panel.blade.php', $sidebar_user_panelBladeNew);
418
419
        } else {
420
            file_put_contents($createFolderLayouts.'/app.blade.php', $appBladeNew);
421
            file_put_contents($createFolder.'/welcome.blade.php', $welcomeBladeNew);
422
        }
423
        file_put_contents($createFolder.'/home.blade.php', $homeBladeNew);
424
        file_put_contents($createFolderAuth.'/login.blade.php', $loginBladeNew);
425
        file_put_contents($createFolderAuth.'/register.blade.php', $registerBladeNew);
426
        file_put_contents($createFolderAuthPasswords.'/email.blade.php', $emailBladeNew);
427
        file_put_contents($createFolderAuthPasswords.'/reset.blade.php', $resetBladeNew);
428
429
        file_put_contents($createFolderAuthAccount.'/update_info.blade.php', $update_infoBladeNew);
430
        file_put_contents($createFolderAuthAccount.'/change_password.blade.php', $change_passwordBladeNew);
431
432
        return true;
433
434
    }
435
436
    /**
437
     * Install RouteMaps.
438
     *
439
     * @return boolean
440
     */
441
442
    public function installRouteMaps($is_backpack_enabled)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $is_backpack_enabled is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
443
    {
444
        $nameSmall = snake_case($this->getParsedNameInput());
445
        $name = ucfirst($this->getParsedNameInput());
446
        $mapCallFunction = file_get_contents(__DIR__ . '/../Route/mapRoute.stub');
447
        $mapCallFunctionNew = str_replace('{{$name}}', "$name", $mapCallFunction);
448
        $this->insert($this->getRouteServicesPath(), '$this->mapWebRoutes();', $mapCallFunctionNew, true);
449
        $mapFunction = file_get_contents(__DIR__ . '/../Route/mapRouteFunction.stub');
450
        $mapFunctionNew = str_replace([
451
            '{{$name}}',
452
            '{{$nameSmall}}'
453
        ], [
454
            "$name",
455
            "$nameSmall"
456
        ], $mapFunction);
457
        $this->insert($this->getRouteServicesPath(), '        //
458
    }', $mapFunctionNew, true);
459
        return true;
460
    }
461
462
    /**
463
     * Install RouteFile.
464
     *
465
     * @return boolean
466
     */
467
468
    public function installRouteFiles($is_backpack_enabled)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $is_backpack_enabled is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
469
    {
470
        $nameSmall = snake_case($this->getParsedNameInput());
471
        $name = ucfirst($this->getParsedNameInput());
472
        $createFolder = $this->getRoutesFolderPath().DIRECTORY_SEPARATOR.$nameSmall;
473
        if (!file_exists($createFolder)) {
474
            mkdir($createFolder);
475
        }
476
        $routeFileContent = file_get_contents(__DIR__ . '/../Route/routeFile.stub');
477
        $routeFileContentNew = str_replace([
478
            '{{$name}}',
479
            '{{$nameSmall}}'
480
        ], [
481
            "$name",
482
            "$nameSmall"
483
        ], $routeFileContent);
484
        $routeFile = $createFolder.DIRECTORY_SEPARATOR.$nameSmall.".php";
485
        file_put_contents($routeFile, $routeFileContentNew);
486
        return true;
487
    }
488
489
    /**
490
     * Install Requests.
491
     *
492
     * @return boolean
493
     */
494
495
    public function installRequests($is_backpack_enabled)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $is_backpack_enabled is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
496
    {
497
        $nameSmall = snake_case($this->getParsedNameInput());
498
        $name = ucfirst($this->getParsedNameInput());
499
500
        $nameFolder = $this->getControllersPath().DIRECTORY_SEPARATOR.$name;
501
        if (!file_exists($nameFolder)) {
502
            mkdir($nameFolder);
503
        }
504
505
        $requestsFolder = $nameFolder.DIRECTORY_SEPARATOR."Requests";
506
        if (!file_exists($requestsFolder)) {
507
            mkdir($requestsFolder);
508
        }
509
        $accountInfoContent = file_get_contents(__DIR__ . '/../Request/AccountInfoRequest.stub');
510
        $changePasswordContent = file_get_contents(__DIR__ . '/../Request/ChangePasswordRequest.stub');
511
512
        $accountInfoContentNew = str_replace([
513
            '{{$name}}',
514
            '{{$nameSmall}}'
515
        ], [
516
            "$name",
517
            "$nameSmall"
518
        ], $accountInfoContent);
519
520
        $changePasswordContentNew = str_replace([
521
            '{{$name}}',
522
            '{{$nameSmall}}'
523
        ], [
524
            "$name",
525
            "$nameSmall"
526
        ], $changePasswordContent);
527
528
        $accountInfoFile = $requestsFolder.DIRECTORY_SEPARATOR."{$name}AccountInfoRequest.php";
529
        $changePasswordFile = $requestsFolder.DIRECTORY_SEPARATOR."{$name}ChangePasswordRequest.php";
530
531
        file_put_contents($accountInfoFile, $accountInfoContentNew);
532
        file_put_contents($changePasswordFile, $changePasswordContentNew);
533
534
        return true;
535
536
    }
537
    /**
538
     * Install Controller.
539
     *
540
     * @return boolean
541
     */
542
543
    public function installControllers($is_backpack_enabled)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $is_backpack_enabled is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
544
    {
545
        $nameSmall = snake_case($this->getParsedNameInput());
546
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
547
        $name = ucfirst($this->getParsedNameInput());
548
549
        $nameFolder = $this->getControllersPath().DIRECTORY_SEPARATOR.$name;
550
        if (!file_exists($nameFolder)) {
551
            mkdir($nameFolder);
552
        }
553
554
        $authFolder = $nameFolder.DIRECTORY_SEPARATOR."Auth";
555
        if (!file_exists($authFolder)) {
556
            mkdir($authFolder);
557
        }
558
559
        $controllerContent = file_get_contents(__DIR__ . '/../Controllers/Controller.stub');
560
        $homeControllerContent = file_get_contents(__DIR__ . '/../Controllers/HomeController.stub');
561
        $loginControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/LoginController.stub');
562
        $forgotControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/ForgotPasswordController.stub');
563
        $registerControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/RegisterController.stub');
564
        $resetControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/ResetPasswordController.stub');
565
        $myAccountControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/MyAccountController.stub');
566
567
        $controllerFileContentNew = str_replace('{{$name}}', "$name", $controllerContent);
568
569
        $homeFileContentNew = str_replace([
570
            '{{$name}}',
571
            '{{$nameSmall}}'
572
        ], [
573
            "$name",
574
            "$nameSmall"
575
        ], $homeControllerContent);
576
577
        $loginFileContentNew = str_replace([
578
            '{{$name}}',
579
            '{{$nameSmall}}'
580
        ], [
581
            "$name",
582
            "$nameSmall"
583
        ], $loginControllerContent);
584
585
        $forgotFileContentNew = str_replace([
586
            '{{$name}}',
587
            '{{$nameSmall}}',
588
            '{{$nameSmallPlural}}'
589
        ], [
590
            "$name",
591
            "$nameSmall",
592
            "$nameSmallPlural"
593
        ], $forgotControllerContent);
594
595
        $registerFileContentNew = str_replace([
596
            '{{$name}}',
597
            '{{$nameSmall}}',
598
            '{{$nameSmallPlural}}'
599
        ], [
600
            "$name",
601
            "$nameSmall",
602
            "$nameSmallPlural"
603
        ], $registerControllerContent);
604
605
        $resetFileContentNew = str_replace([
606
            '{{$name}}',
607
            '{{$nameSmall}}',
608
            '{{$nameSmallPlural}}'
609
        ], [
610
            "$name",
611
            "$nameSmall",
612
            "$nameSmallPlural"
613
        ], $resetControllerContent);
614
615
        $myAccountFileContentNew = str_replace([
616
            '{{$name}}',
617
            '{{$nameSmall}}'
618
        ], [
619
            "$name",
620
            "$nameSmall"
621
        ], $myAccountControllerContent);
622
623
        $controllerFile = $nameFolder.DIRECTORY_SEPARATOR."Controller.php";
624
        $homeFile = $nameFolder.DIRECTORY_SEPARATOR."HomeController.php";
625
        $loginFile = $authFolder.DIRECTORY_SEPARATOR."LoginController.php";
626
        $forgotFile = $authFolder.DIRECTORY_SEPARATOR."ForgotPasswordController.php";
627
        $registerFile = $authFolder.DIRECTORY_SEPARATOR."RegisterController.php";
628
        $resetFile = $authFolder.DIRECTORY_SEPARATOR."ResetPasswordController.php";
629
630
        $myAccountFile = $authFolder.DIRECTORY_SEPARATOR."{$name}AccountController.php";
631
632
633
        file_put_contents($controllerFile, $controllerFileContentNew);
634
        file_put_contents($homeFile, $homeFileContentNew);
635
        file_put_contents($loginFile, $loginFileContentNew);
636
        file_put_contents($forgotFile, $forgotFileContentNew);
637
        file_put_contents($registerFile, $registerFileContentNew);
638
        file_put_contents($resetFile, $resetFileContentNew);
639
        file_put_contents($myAccountFile, $myAccountFileContentNew);
640
641
        return true;
642
643
    }
644
645
    /**
646
     * Install Configs.
647
     *
648
     * @return boolean
649
     */
650
651
    public function installConfigs($is_backpack_enabled)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $is_backpack_enabled is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
652
    {
653
        $nameSmall = snake_case($this->getParsedNameInput());
654
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
655
        $name = ucfirst($this->getParsedNameInput());
656
657
        $authConfigFile = $this->getConfigsFolderPath().DIRECTORY_SEPARATOR."auth.php";
658
659
        $guardContent = file_get_contents(__DIR__ . '/../Config/guard.stub');
660
        $passwordContent = file_get_contents(__DIR__ . '/../Config/password.stub');
661
        $providerContent = file_get_contents(__DIR__ . '/../Config/provider.stub');
662
663
        $guardFileContentNew = str_replace([
664
            '{{$nameSmall}}',
665
            '{{$nameSmallPlural}}'
666
        ], [
667
            "$nameSmall",
668
            "$nameSmallPlural"
669
        ], $guardContent);
670
671
        $passwordFileContentNew = str_replace('{{$nameSmallPlural}}', "$nameSmallPlural", $passwordContent);
672
673
        $providerFileContentNew = str_replace([
674
            '{{$name}}',
675
            '{{$nameSmallPlural}}'
676
        ], [
677
            "$name",
678
            "$nameSmallPlural"
679
        ], $providerContent);
680
681
        $this->insert($authConfigFile, '    \'guards\' => [', $guardFileContentNew, true);
682
683
        $this->insert($authConfigFile, '    \'passwords\' => [', $passwordFileContentNew, true);
684
685
        $this->insert($authConfigFile, '    \'providers\' => [', $providerFileContentNew, true);
686
687
        return true;
688
689
    }
690
691
    /**
692
     * Install Unauthenticated Handler.
693
     *
694
     * @return boolean
695
     */
696
    public function installUnauthenticated($is_backpack_enabled)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $is_backpack_enabled is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
697
    {
698
        $nameSmall = snake_case($this->getParsedNameInput());
699
        $exceptionHandlerFile = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Exceptions".DIRECTORY_SEPARATOR
700
            ."Handler.php";
701
        $exceptionHandlerFileContent = file_get_contents($exceptionHandlerFile);
702
        $exceptionHandlerFileContentNew = file_get_contents(__DIR__ . '/../Exceptions/handlerUnauthorized.stub');
703
704
705
        if (!str_contains($exceptionHandlerFileContent, 'MultiAuthUnAuthenticated')) {
706
            // replace old file
707
            $deleted = unlink($exceptionHandlerFile);
708
            if ($deleted) {
709
                file_put_contents($exceptionHandlerFile, $exceptionHandlerFileContentNew);
710
            }
711
        }
712
713
        $exceptionHandlerGuardContentNew = file_get_contents(__DIR__ . '/../Exceptions/handlerGuard.stub');
714
        $exceptionHandlerGuardContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
715
            $exceptionHandlerGuardContentNew);
716
717
        $this->insert($exceptionHandlerFile, '        switch(array_get($exception->guards(), 0)) {',
718
            $exceptionHandlerGuardContentNew2, true);
719
720
        return true;
721
722
    }
723
724
    /**
725
     * Install Middleware.
726
     *
727
     * @return boolean
728
     */
729
730
    public function installMiddleware($is_backpack_enabled)
0 ignored issues
show
Coding Style Naming introduced by
The parameter $is_backpack_enabled is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
731
    {
732
        $nameSmall = snake_case($this->getParsedNameInput());
733
734
        $redirectIfMiddlewareFile = $this->getMiddlewarePath().DIRECTORY_SEPARATOR."RedirectIfAuthenticated.php";
735
        $middlewareKernelFile = $this->getHttpPath().DIRECTORY_SEPARATOR."Kernel.php";
736
737
        $redirectIfMiddlewareFileContent = file_get_contents($redirectIfMiddlewareFile);
738
739
        $redirectIfMiddlewareContentNew = file_get_contents(__DIR__ . '/../Middleware/redirectIf.stub');
740
741
        if (!str_contains($redirectIfMiddlewareFileContent, 'MultiAuthGuards')) {
742
            // replace old file
743
            $deleted = unlink($redirectIfMiddlewareFile);
744
            if ($deleted) {
745
                file_put_contents($redirectIfMiddlewareFile, $redirectIfMiddlewareContentNew);
746
            }
747
        }
748
749
        $redirectIfMiddlewareGroupContentNew = file_get_contents(__DIR__ . '/../Middleware/redirectMiddleware.stub');
750
        $redirectIfMiddlewareGuardContentNew = file_get_contents(__DIR__ . '/../Middleware/redirectMiddlewareGuard.stub');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
751
752
        $redirectIfMiddlewareGroupContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
753
            $redirectIfMiddlewareGroupContentNew);
754
        $redirectIfMiddlewareGuardContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
755
            $redirectIfMiddlewareGuardContentNew);
756
757
        $this->insert($middlewareKernelFile, '    protected $middlewareGroups = [',
758
            $redirectIfMiddlewareGroupContentNew2, true);
759
760
        $this->insert($redirectIfMiddlewareFile, '        switch ($guard) {',
761
            $redirectIfMiddlewareGuardContentNew2, true);
762
763
        return true;
764
765
    }
766
767
    /**
768
     * Run a SSH command.
769
     *
770
     * @param string $command      The SSH command that needs to be run
771
     * @param bool   $beforeNotice Information for the user before the command is run
772
     * @param bool   $afterNotice  Information for the user after the command is run
773
     *
774
     * @return mixed Command-line output
775
     */
776
    public function executeProcess($command, $beforeNotice = false, $afterNotice = false)
777
    {
778
        if ($beforeNotice) {
779
            $this->info('### '.$beforeNotice);
780
        } else {
781
            $this->info('### Running: '.$command);
782
        }
783
        $process = new Process($command);
784
        $process->run(function ($type, $buffer) {
785
            if (Process::ERR === $type) {
786
                echo '... > '.$buffer;
787
            } else {
788
                echo 'OUT > '.$buffer;
789
            }
790
        });
791
        // executes after the command finishes
792
        if (!$process->isSuccessful()) {
793
            throw new ProcessFailedException($process);
794
        }
795
        if ($afterNotice) {
796
            $this->info('### '.$afterNotice);
797
        }
798
    }
799
800
    /**
801
     * Get the desired class name from the input.
802
     *
803
     * @return string
804
     */
805
    protected function getParsedNameInput()
806
    {
807
        return mb_strtolower(str_singular($this->getNameInput()));
808
    }
809
    /**
810
     * Get the desired class name from the input.
811
     *
812
     * @return string
813
     */
814
    protected function getNameInput()
815
    {
816
        return trim($this->argument('name'));
817
    }
818
819
    /**
820
     * Write the migration file to disk.
821
     *
822
     * @param  string  $name
823
     * @param  string  $table
824
     * @param  bool    $create
825
     * @return mixed
826
     */
827
    protected function writeMigration($name, $table, $create)
828
    {
829
        $file = pathinfo($this->creator->create(
830
            $name, $this->getMigrationPath(), $table, $create
831
        ), PATHINFO_FILENAME);
832
        $this->line("<info>Created Migration:</info> {$file}");
833
    }
834
835
    /**
836
     * Get migration path.
837
     *
838
     * @return string
839
     */
840
    protected function getMigrationPath()
841
    {
842
        return parent::getMigrationPath();
843
    }
844
845
    /**
846
     * Get Routes Provider Path.
847
     *
848
     * @return string
849
     */
850
    protected function getRouteServicesPath()
851
    {
852
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Providers'.DIRECTORY_SEPARATOR.'RouteServiceProvider.php';
853
    }
854
855
    /**
856
     * Get Routes Folder Path.
857
     *
858
     * @return string
859
     */
860
    protected function getAppFolderPath()
861
    {
862
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'app';
863
    }
864
865
    /**
866
     * Get Routes Folder Path.
867
     *
868
     * @return string
869
     */
870
    protected function getRoutesFolderPath()
871
    {
872
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'routes';
873
    }
874
875
    /**
876
     * Get Config Folder Path.
877
     *
878
     * @return string
879
     */
880
    protected function getConfigsFolderPath()
881
    {
882
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'config';
883
    }
884
885
    /**
886
     * Get Config Folder Path.
887
     *
888
     * @return string
889
     */
890
    protected function getViewsFolderPath()
891
    {
892
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'views';
893
    }
894
895
    /**
896
     * Get Controllers Path.
897
     *
898
     * @return string
899
     */
900
    protected function getControllersPath()
901
    {
902
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Controllers';
903
    }
904
905
    /**
906
     * Get Http Path.
907
     *
908
     * @return string
909
     */
910
    protected function getHttpPath()
911
    {
912
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http';
913
    }
914
915
    /**
916
     * Get Middleware Path.
917
     *
918
     * @return string
919
     */
920
    protected function getMiddlewarePath()
921
    {
922
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Middleware';
923
    }
924
925
    /**
926
     * insert text into file
927
     *
928
     * @param string $filePath
929
     * @param string $insertMarker
930
     * @param string $text
931
     * @param boolean $after
932
     *
933
     * @return integer
934
     */
935
    public function insertIntoFile($filePath, $insertMarker, $text, $after = true) {
936
        $contents = file_get_contents($filePath);
937
        $new_contents = preg_replace($insertMarker,($after) ? '$0' . $text : $text . '$0', $contents);
938
        return file_put_contents($filePath, $new_contents);
939
    }
940
941
    /**
942
     * insert text into file
943
     *
944
     * @param string $filePath
945
     * @param string $keyword
946
     * @param string $body
947
     * @param boolean $after
948
     *
949
     * @return integer
950
     */
951
    public function insert($filePath, $keyword, $body, $after = true) {
952
953
        $contents = file_get_contents($filePath);
954
        $new_contents = substr_replace($contents, PHP_EOL . $body,
955
            ($after) ? strpos($contents, $keyword) + strlen($keyword) : strpos($contents, $keyword)
956
            , 0);
957
        return file_put_contents($filePath, $new_contents);
958
    }
959
}
960