Completed
Branch master (84f4b4)
by Mokhlas
01:16
created

MultiAuthPrepare::installRouteMaps()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8571
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
9
class MultiAuthPrepare extends BaseCommand
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'make:multi-auth {name}';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Create MultiAuth for your project';
24
25
    /**
26
     * The migration creator instance.
27
     *
28
     * @var \Illuminate\Database\Migrations\MigrationCreator
29
     */
30
    protected $creator;
31
32
    /**
33
     * The Composer instance.
34
     *
35
     * @var \Illuminate\Support\Composer
36
     */
37
    protected $composer;
38
39
    /**
40
     * Create a new migration install command instance.
41
     *
42
     * @param  \Illuminate\Database\Migrations\MigrationCreator  $creator
43
     * @param  \Illuminate\Support\Composer  $composer
44
     * @return mixed
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

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

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
630
     */
631
    public function insertIntoFile($filePath, $insertMarker, $text, $after = true) {
632
        $contents = file_get_contents($filePath);
633
        $new_contents = preg_replace($insertMarker,($after) ? '$0' . $text : $text . '$0', $contents);
634
        return file_put_contents($filePath, $new_contents);
635
    }
636
637
    /**
638
     * insert text into file
639
     *
640
     * @param string $filePath
641
     * @param string $keyword
642
     * @param string $body
643
     * @param boolean $after
644
     *
645
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use integer.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
646
     */
647
    public function insert($filePath, $keyword, $body, $after = true) {
648
649
        $contents = file_get_contents($filePath);
650
        $new_contents = substr_replace($contents, PHP_EOL . $body, ($after) ? strpos($contents, $keyword) + strlen($keyword) : strpos($contents, $keyword), 0);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 159 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...
651
        return file_put_contents($filePath, $new_contents);
652
    }
653
}
654