Completed
Push — master ( 897d6c...e0547f )
by Mokhlas
02:30
created

MultiAuthPrepare::installRouteMaps()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
namespace iMokhles\MultiAuthCommand\Command;
4
5
use Illuminate\Database\Console\Migrations\BaseCommand;
6
use Illuminate\Database\Migrations\MigrationCreator;
7
use Illuminate\Support\Composer;
8
use Symfony\Component\Process\Exception\ProcessFailedException;
9
use Symfony\Component\Process\Process;
10
11
class MultiAuthPrepare extends BaseCommand
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'make:multi-auth {name}';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Create MultiAuth for your project';
26
27
    /**
28
     * The migration creator instance.
29
     *
30
     * @var \Illuminate\Database\Migrations\MigrationCreator
31
     */
32
    protected $creator;
33
34
    /**
35
     * The Composer instance.
36
     *
37
     * @var \Illuminate\Support\Composer
38
     */
39
    protected $composer;
40
41
    /**
42
     * Create a new migration install command instance.
43
     *
44
     * @param  \Illuminate\Database\Migrations\MigrationCreator  $creator
45
     * @param  \Illuminate\Support\Composer  $composer
46
     */
47
    public function __construct(MigrationCreator $creator, Composer $composer)
48
    {
49
        parent::__construct();
50
        $this->creator = $creator;
51
        $this->composer = $composer;
52
    }
53
54
    /**
55
     * Execute the console command.
56
     *
57
     * @return boolean
58
     */
59
    public function handle()
60
    {
61
62
        $this->info("### Preparing For MultiAuth. Please wait...");
63
64
        $this->installMigration();
65
        $this->installModel();
66
        $this->installRouteMaps();
67
        $this->installRouteFiles();
68
        $this->installControllers();
69
        $this->installConfigs();
70
        $this->installMiddleware();
71
        $this->installView();
72
        $this->installPrologueAlert();
73
74
75
        $this->composer->dumpAutoloads();
76
77
        $this->info("### Finished MultiAuth.");
78
79
        return true;
80
    }
81
82
    /**
83
     * Publish Prologue Alert
84
     *
85
     * @return boolean
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
86
     */
87
    public function installPrologueAlert() {
88
        $alertsConfigFile = $this->getConfigsFolderPath().DIRECTORY_SEPARATOR."prologue/alerts.php";
89
        if (!file_exists($alertsConfigFile)) {
90
            $this->executeProcess('php artisan vendor:publish --provider="Prologue\Alerts\AlertsServiceProvider"', 'publishing config for notifications - prologue/alerts');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 172 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...
91
        }
92
    }
93
94
    /**
95
     * Install Migration.
96
     *
97
     * @return boolean
98
     */
99
    public function installMigration()
100
    {
101
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
102
        $name = ucfirst($this->getParsedNameInput());
103
        $namePlural = str_plural($name);
104
105
106
107
        $modelTableContent = file_get_contents(__DIR__ . '/../Migration/modelTable.stub');
108
        $modelTableContentNew = str_replace([
109
            '{{$namePlural}}',
110
            '{{$nameSmallPlural}}',
111
        ], [
112
            $namePlural,
113
            $nameSmallPlural
114
        ], $modelTableContent);
115
116
117
        $modelResetPasswordTableContent = file_get_contents(__DIR__ . '/../Migration/passwordResetsTable.stub');
118
        $modelResetPasswordTableContentNew = str_replace([
119
            '{{$namePlural}}',
120
            '{{$nameSmallPlural}}',
121
        ], [
122
            $namePlural,
123
            $nameSmallPlural
124
        ], $modelResetPasswordTableContent);
125
126
127
        $migrationName = date('Y_m_d_His') . '_'.'create_' . str_plural(snake_case($name)) .'_table.php';
128
        $migrationModelPath = $this->getMigrationPath().DIRECTORY_SEPARATOR.$migrationName;
129
        file_put_contents($migrationModelPath, $modelTableContentNew);
130
131
        $migrationResetName = date('Y_m_d_His') . '_'
132
            .'create_' . str_plural(snake_case($name))
133
            .'_password_resets_table.php';
134
        $migrationResetModelPath = $this->getMigrationPath().DIRECTORY_SEPARATOR.$migrationResetName;
135
        file_put_contents($migrationResetModelPath, $modelResetPasswordTableContentNew);
136
137
        return true;
138
139
    }
140
141
142
    /**
143
     * Install Model.
144
     *
145
     * @return boolean
146
     */
147
    public function installModel()
148
    {
149
        $nameSmall = snake_case($this->getParsedNameInput());
150
        $name = ucfirst($this->getParsedNameInput());
151
152
153
        $modelContent = file_get_contents(__DIR__ . '/../Model/model.stub');
154
        $modelContentNew = str_replace([
155
            '{{$name}}',
156
        ], [
157
            $name,
158
        ], $modelContent);
159
        $modelPath = $this->getAppFolderPath().DIRECTORY_SEPARATOR.$name.".php";
160
        file_put_contents($modelPath, $modelContentNew);
161
162
163
        $resetNotificationContent = file_get_contents(__DIR__ . '/../Notification/resetPasswordNotification.stub');
164
        $resetNotificationContentNew = str_replace([
165
            '{{$name}}',
166
            '{{$nameSmall}}',
167
        ], [
168
            $name,
169
            $nameSmall
170
        ], $resetNotificationContent);
171
172
        $createFolder = $this->getAppFolderPath().DIRECTORY_SEPARATOR."Notifications";
173
        if (!file_exists($createFolder)) {
174
            mkdir($createFolder);
175
        }
176
177
        $resetNotificationPath = $createFolder.DIRECTORY_SEPARATOR.$name."ResetPasswordNotification.php";
178
        file_put_contents($resetNotificationPath, $resetNotificationContentNew);
179
180
        return true;
181
182
    }
183
184
    /**
185
     * Install View.
186
     *
187
     * @return boolean
188
     */
189
    public function installView()
190
    {
191
        $nameSmall = snake_case($this->getParsedNameInput());
192
193
        $appBlade = file_get_contents(__DIR__ . '/../Views/layouts/app.blade.stub');
194
        $welcomeBlade = file_get_contents(__DIR__ . '/../Views/welcome.blade.stub');
195
        $homeBlade = file_get_contents(__DIR__ . '/../Views/home.blade.stub');
196
        $loginBlade = file_get_contents(__DIR__ . '/../Views/auth/login.blade.stub');
197
        $registerBlade = file_get_contents(__DIR__ . '/../Views/auth/register.blade.stub');
198
        $resetBlade = file_get_contents(__DIR__ . '/../Views/auth/passwords/reset.blade.stub');
199
        $emailBlade = file_get_contents(__DIR__ . '/../Views/auth/passwords/email.blade.stub');
200
201
        $createFolder = $this->getViewsFolderPath().DIRECTORY_SEPARATOR."$nameSmall";
202
        if (!file_exists($createFolder)) {
203
            mkdir($createFolder);
204
        }
205
206
        $createFolderLayouts = $this->getViewsFolderPath().DIRECTORY_SEPARATOR
207
            ."$nameSmall"
208
            .DIRECTORY_SEPARATOR."layouts";
209
        if (!file_exists($createFolderLayouts)) {
210
            mkdir($createFolderLayouts);
211
        }
212
213
        $createFolderAuth = $this->getViewsFolderPath().DIRECTORY_SEPARATOR."$nameSmall"
214
            .DIRECTORY_SEPARATOR."auth";
215
        if (!file_exists($createFolderAuth)) {
216
            mkdir($createFolderAuth);
217
        }
218
219
        $createFolderAuthPasswords = $this->getViewsFolderPath().DIRECTORY_SEPARATOR.
220
            "$nameSmall".DIRECTORY_SEPARATOR
221
            ."auth".DIRECTORY_SEPARATOR."passwords";
222
        if (!file_exists($createFolderAuthPasswords)) {
223
            mkdir($createFolderAuthPasswords);
224
        }
225
226
        $appBladeNew = str_replace([
227
            '{{$nameSmall}}',
228
        ], [
229
            $nameSmall
230
        ], $appBlade);
231
232
        $welcomeBladeNew = str_replace([
233
            '{{$nameSmall}}',
234
        ], [
235
            $nameSmall
236
        ], $welcomeBlade);
237
238
        $homeBladeNew = str_replace([
239
            '{{$nameSmall}}',
240
        ], [
241
            $nameSmall
242
        ], $homeBlade);
243
244
        $loginBladeNew = str_replace([
245
            '{{$nameSmall}}',
246
        ], [
247
            $nameSmall
248
        ], $loginBlade);
249
250
        $registerBladeNew = str_replace([
251
            '{{$nameSmall}}',
252
        ], [
253
            $nameSmall
254
        ], $registerBlade);
255
256
        $emailBladeNew = str_replace([
257
            '{{$nameSmall}}',
258
        ], [
259
            $nameSmall
260
        ], $emailBlade);
261
262
        $resetBladeNew = str_replace([
263
            '{{$nameSmall}}',
264
        ], [
265
            $nameSmall
266
        ], $resetBlade);
267
268
        file_put_contents($createFolderLayouts.'/app.blade.php', $appBladeNew);
269
        file_put_contents($createFolder.'/welcome.blade.php', $welcomeBladeNew);
270
        file_put_contents($createFolder.'/home.blade.php', $homeBladeNew);
271
        file_put_contents($createFolderAuth.'/login.blade.php', $loginBladeNew);
272
        file_put_contents($createFolderAuth.'/register.blade.php', $registerBladeNew);
273
        file_put_contents($createFolderAuthPasswords.'/email.blade.php', $emailBladeNew);
274
        file_put_contents($createFolderAuthPasswords.'/reset.blade.php', $resetBladeNew);
275
276
        return true;
277
278
    }
279
280
    /**
281
     * Install RouteMaps.
282
     *
283
     * @return boolean
284
     */
285
286
    public function installRouteMaps()
287
    {
288
        $nameSmall = snake_case($this->getParsedNameInput());
289
        $name = ucfirst($this->getParsedNameInput());
290
        $mapCallFunction = file_get_contents(__DIR__ . '/../Route/mapRoute.stub');
291
        $mapCallFunctionNew = str_replace('{{$name}}', "$name", $mapCallFunction);
292
        $this->insert($this->getRouteServicesPath(), '$this->mapWebRoutes();', $mapCallFunctionNew, true);
293
        $mapFunction = file_get_contents(__DIR__ . '/../Route/mapRouteFunction.stub');
294
        $mapFunctionNew = str_replace([
295
            '{{$name}}',
296
            '{{$nameSmall}}'
297
        ], [
298
            "$name",
299
            "$nameSmall"
300
        ], $mapFunction);
301
        $this->insert($this->getRouteServicesPath(), '        //
302
    }', $mapFunctionNew, true);
303
        return true;
304
    }
305
306
    /**
307
     * Install RouteFile.
308
     *
309
     * @return boolean
310
     */
311
312
    public function installRouteFiles()
313
    {
314
        $nameSmall = snake_case($this->getParsedNameInput());
315
        $name = ucfirst($this->getParsedNameInput());
316
        $createFolder = $this->getRoutesFolderPath().DIRECTORY_SEPARATOR.$nameSmall;
317
        if (!file_exists($createFolder)) {
318
            mkdir($createFolder);
319
        }
320
        $routeFileContent = file_get_contents(__DIR__ . '/../Route/routeFile.stub');
321
        $routeFileContentNew = str_replace([
322
            '{{$name}}',
323
            '{{$nameSmall}}'
324
        ], [
325
            "$name",
326
            "$nameSmall"
327
        ], $routeFileContent);
328
        $routeFile = $createFolder.DIRECTORY_SEPARATOR.$nameSmall.".php";
329
        file_put_contents($routeFile, $routeFileContentNew);
330
        return true;
331
    }
332
333
    /**
334
     * Install Controller.
335
     *
336
     * @return boolean
337
     */
338
339
    public function installControllers()
340
    {
341
        $nameSmall = snake_case($this->getParsedNameInput());
342
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
343
        $name = ucfirst($this->getParsedNameInput());
344
345
        $nameFolder = $this->getControllersPath().DIRECTORY_SEPARATOR.$name;
346
        if (!file_exists($nameFolder)) {
347
            mkdir($nameFolder);
348
        }
349
350
        $authFolder = $nameFolder.DIRECTORY_SEPARATOR."Auth";
351
        if (!file_exists($authFolder)) {
352
            mkdir($authFolder);
353
        }
354
355
        $controllerContent = file_get_contents(__DIR__ . '/../Controllers/Controller.stub');
356
        $homeControllerContent = file_get_contents(__DIR__ . '/../Controllers/HomeController.stub');
357
        $loginControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/LoginController.stub');
358
        $forgotControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/ForgotPasswordController.stub');
359
        $registerControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/RegisterController.stub');
360
        $resetControllerContent = file_get_contents(__DIR__ . '/../Controllers/Auth/ResetPasswordController.stub');
361
362
        $controllerFileContentNew = str_replace('{{$name}}', "$name", $controllerContent);
363
364
        $homeFileContentNew = str_replace([
365
            '{{$name}}',
366
            '{{$nameSmall}}'
367
        ], [
368
            "$name",
369
            "$nameSmall"
370
        ], $homeControllerContent);
371
372
        $loginFileContentNew = str_replace([
373
            '{{$name}}',
374
            '{{$nameSmall}}'
375
        ], [
376
            "$name",
377
            "$nameSmall"
378
        ], $loginControllerContent);
379
380
        $forgotFileContentNew = str_replace([
381
            '{{$name}}',
382
            '{{$nameSmall}}',
383
            '{{$nameSmallPlural}}'
384
        ], [
385
            "$name",
386
            "$nameSmall",
387
            "$nameSmallPlural"
388
        ], $forgotControllerContent);
389
390
        $registerFileContentNew = str_replace([
391
            '{{$name}}',
392
            '{{$nameSmall}}',
393
            '{{$nameSmallPlural}}'
394
        ], [
395
            "$name",
396
            "$nameSmall",
397
            "$nameSmallPlural"
398
        ], $registerControllerContent);
399
400
        $resetFileContentNew = str_replace([
401
            '{{$name}}',
402
            '{{$nameSmall}}',
403
            '{{$nameSmallPlural}}'
404
        ], [
405
            "$name",
406
            "$nameSmall",
407
            "$nameSmallPlural"
408
        ], $resetControllerContent);
409
410
        $controllerFile = $nameFolder.DIRECTORY_SEPARATOR."Controller.php";
411
        $homeFile = $nameFolder.DIRECTORY_SEPARATOR."HomeController.php";
412
        $loginFile = $authFolder.DIRECTORY_SEPARATOR."LoginController.php";
413
        $forgotFile = $authFolder.DIRECTORY_SEPARATOR."ForgotPasswordController.php";
414
        $registerFile = $authFolder.DIRECTORY_SEPARATOR."RegisterController.php";
415
        $resetFile = $authFolder.DIRECTORY_SEPARATOR."ResetPasswordController.php";
416
417
        file_put_contents($controllerFile, $controllerFileContentNew);
418
        file_put_contents($homeFile, $homeFileContentNew);
419
        file_put_contents($loginFile, $loginFileContentNew);
420
        file_put_contents($forgotFile, $forgotFileContentNew);
421
        file_put_contents($registerFile, $registerFileContentNew);
422
        file_put_contents($resetFile, $resetFileContentNew);
423
424
        return true;
425
426
    }
427
428
    /**
429
     * Install Configs.
430
     *
431
     * @return boolean
432
     */
433
434
    public function installConfigs()
435
    {
436
        $nameSmall = snake_case($this->getParsedNameInput());
437
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
438
        $name = ucfirst($this->getParsedNameInput());
439
440
        $authConfigFile = $this->getConfigsFolderPath().DIRECTORY_SEPARATOR."auth.php";
441
442
        $guardContent = file_get_contents(__DIR__ . '/../Config/guard.stub');
443
        $passwordContent = file_get_contents(__DIR__ . '/../Config/password.stub');
444
        $providerContent = file_get_contents(__DIR__ . '/../Config/provider.stub');
445
446
        $guardFileContentNew = str_replace([
447
            '{{$nameSmall}}',
448
            '{{$nameSmallPlural}}'
449
        ], [
450
            "$nameSmall",
451
            "$nameSmallPlural"
452
        ], $guardContent);
453
454
        $passwordFileContentNew = str_replace('{{$nameSmallPlural}}', "$nameSmallPlural", $passwordContent);
455
456
        $providerFileContentNew = str_replace([
457
            '{{$name}}',
458
            '{{$nameSmallPlural}}'
459
        ], [
460
            "$name",
461
            "$nameSmallPlural"
462
        ], $providerContent);
463
464
        $this->insert($authConfigFile, '    \'guards\' => [', $guardFileContentNew, true);
465
466
        $this->insert($authConfigFile, '    \'passwords\' => [', $passwordFileContentNew, true);
467
468
        $this->insert($authConfigFile, '    \'providers\' => [', $providerFileContentNew, true);
469
470
        return true;
471
472
    }
473
474
    /**
475
     * Install Middleware.
476
     *
477
     * @return boolean
478
     */
479
480
    public function installMiddleware()
481
    {
482
        $nameSmall = snake_case($this->getParsedNameInput());
483
484
        $redirectIfMiddlewareFile = $this->getMiddlewarePath().DIRECTORY_SEPARATOR."RedirectIfAuthenticated.php";
485
        $middlewareKernelFile = $this->getHttpPath().DIRECTORY_SEPARATOR."Kernel.php";
486
487
        $redirectIfMiddlewareFileContent = file_get_contents($redirectIfMiddlewareFile);
488
489
        $redirectIfMiddlewareContentNew = file_get_contents(__DIR__ . '/../Middleware/redirectIf.stub');
490
491
        if (!str_contains($redirectIfMiddlewareFileContent, 'MultiAuthGuards')) {
492
            // replace old file
493
            $deleted = unlink($redirectIfMiddlewareFile);
494
            if ($deleted) {
495
                file_put_contents($redirectIfMiddlewareFile, $redirectIfMiddlewareContentNew);
496
            }
497
        }
498
499
        $redirectIfMiddlewareGroupContentNew = file_get_contents(__DIR__ . '/../Middleware/redirectMiddleware.stub');
500
        $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...
501
502
        $redirectIfMiddlewareGroupContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
503
            $redirectIfMiddlewareGroupContentNew);
504
        $redirectIfMiddlewareGuardContentNew2 = str_replace('{{$nameSmall}}', "$nameSmall",
505
            $redirectIfMiddlewareGuardContentNew);
506
507
        $this->insert($middlewareKernelFile, '    protected $middlewareGroups = [',
508
            $redirectIfMiddlewareGroupContentNew2, true);
509
510
        $this->insert($redirectIfMiddlewareFile, '        switch ($guard) {',
511
            $redirectIfMiddlewareGuardContentNew2, true);
512
513
        return true;
514
515
    }
516
517
    /**
518
     * Run a SSH command.
519
     *
520
     * @param string $command      The SSH command that needs to be run
521
     * @param bool   $beforeNotice Information for the user before the command is run
522
     * @param bool   $afterNotice  Information for the user after the command is run
523
     *
524
     * @return mixed Command-line output
525
     */
526
    public function executeProcess($command, $beforeNotice = false, $afterNotice = false)
527
    {
528
        if ($beforeNotice) {
529
            $this->info('### '.$beforeNotice);
530
        } else {
531
            $this->info('### Running: '.$command);
532
        }
533
        $process = new Process($command);
534
        $process->run(function ($type, $buffer) {
535
            if (Process::ERR === $type) {
536
                echo '... > '.$buffer;
537
            } else {
538
                echo 'OUT > '.$buffer;
539
            }
540
        });
541
        // executes after the command finishes
542
        if (!$process->isSuccessful()) {
543
            throw new ProcessFailedException($process);
544
        }
545
        if ($afterNotice) {
546
            $this->info('### '.$afterNotice);
547
        }
548
    }
549
550
    /**
551
     * Get the desired class name from the input.
552
     *
553
     * @return string
554
     */
555
    protected function getParsedNameInput()
556
    {
557
        return mb_strtolower(str_singular($this->getNameInput()));
558
    }
559
    /**
560
     * Get the desired class name from the input.
561
     *
562
     * @return string
563
     */
564
    protected function getNameInput()
565
    {
566
        return trim($this->argument('name'));
567
    }
568
569
    /**
570
     * Write the migration file to disk.
571
     *
572
     * @param  string  $name
573
     * @param  string  $table
574
     * @param  bool    $create
575
     * @return mixed
576
     */
577
    protected function writeMigration($name, $table, $create)
578
    {
579
        $file = pathinfo($this->creator->create(
580
            $name, $this->getMigrationPath(), $table, $create
581
        ), PATHINFO_FILENAME);
582
        $this->line("<info>Created Migration:</info> {$file}");
583
    }
584
585
    /**
586
     * Get migration path (either specified by '--path' option or default location).
587
     *
588
     * @return string
589
     */
590
    protected function getMigrationPath()
591
    {
592
        return parent::getMigrationPath();
593
    }
594
595
    /**
596
     * Get Routes Provider Path.
597
     *
598
     * @return string
599
     */
600
    protected function getRouteServicesPath()
601
    {
602
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Providers'.DIRECTORY_SEPARATOR.'RouteServiceProvider.php';
603
    }
604
605
    /**
606
     * Get Routes Folder Path.
607
     *
608
     * @return string
609
     */
610
    protected function getAppFolderPath()
611
    {
612
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'app';
613
    }
614
615
    /**
616
     * Get Routes Folder Path.
617
     *
618
     * @return string
619
     */
620
    protected function getRoutesFolderPath()
621
    {
622
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'routes';
623
    }
624
625
    /**
626
     * Get Config Folder Path.
627
     *
628
     * @return string
629
     */
630
    protected function getConfigsFolderPath()
631
    {
632
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'config';
633
    }
634
635
    /**
636
     * Get Config Folder Path.
637
     *
638
     * @return string
639
     */
640
    protected function getViewsFolderPath()
641
    {
642
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'views';
643
    }
644
645
    /**
646
     * Get Controllers Path.
647
     *
648
     * @return string
649
     */
650
    protected function getControllersPath()
651
    {
652
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Controllers';
653
    }
654
655
    /**
656
     * Get Http Path.
657
     *
658
     * @return string
659
     */
660
    protected function getHttpPath()
661
    {
662
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http';
663
    }
664
665
    /**
666
     * Get Middleware Path.
667
     *
668
     * @return string
669
     */
670
    protected function getMiddlewarePath()
671
    {
672
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Middleware';
673
    }
674
675
    /**
676
     * insert text into file
677
     *
678
     * @param string $filePath
679
     * @param string $insertMarker
680
     * @param string $text
681
     * @param boolean $after
682
     *
683
     * @return integer
684
     */
685
    public function insertIntoFile($filePath, $insertMarker, $text, $after = true) {
686
        $contents = file_get_contents($filePath);
687
        $new_contents = preg_replace($insertMarker,($after) ? '$0' . $text : $text . '$0', $contents);
688
        return file_put_contents($filePath, $new_contents);
689
    }
690
691
    /**
692
     * insert text into file
693
     *
694
     * @param string $filePath
695
     * @param string $keyword
696
     * @param string $body
697
     * @param boolean $after
698
     *
699
     * @return integer
700
     */
701
    public function insert($filePath, $keyword, $body, $after = true) {
702
703
        $contents = file_get_contents($filePath);
704
        $new_contents = substr_replace($contents, PHP_EOL . $body,
705
            ($after) ? strpos($contents, $keyword) + strlen($keyword) : strpos($contents, $keyword)
706
            , 0);
707
        return file_put_contents($filePath, $new_contents);
708
    }
709
}
710