Completed
Push — master ( 84f4b4...a644c8 )
by Mokhlas
01:09
created

MultiAuthPrepare::installView()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 86
Code Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 86
rs 8.3234
c 0
b 0
f 0
cc 5
eloc 57
nc 16
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        $mapCallFunction = file_get_contents(__DIR__ . '/Route/mapRoute.stub');
264
        $mapCallFunctionNew = str_replace('{{$name}}', "$name", $mapCallFunction);
265
        $this->insert($this->getRouteServicesPath(), '$this->mapWebRoutes();', $mapCallFunctionNew, true);
266
        $mapFunction = file_get_contents(__DIR__ . '/Route/mapRouteFunction.stub');
267
        $mapFunctionNew = str_replace([
268
            '{{$name}}',
269
            '{{$nameSmall}}'
270
        ], [
271
            "$name",
272
            "$nameSmall"
273
        ], $mapFunction);
274
        $this->insert($this->getRouteServicesPath(), '        //
275
    }', $mapFunctionNew, true);
276
        return true;
277
    }
278
279
    /**
280
     * Install RouteFile.
281
     *
282
     * @return boolean
283
     */
284
285
    public function installRouteFiles()
286
    {
287
        $nameSmall = snake_case($this->getParsedNameInput());
288
        $name = ucfirst($this->getParsedNameInput());
289
        $createFolder = $this->getRoutesFolderPath().DIRECTORY_SEPARATOR.$nameSmall;
290
        if (!file_exists($createFolder)) {
291
            mkdir($createFolder);
292
        }
293
        $routeFileContent = file_get_contents(__DIR__ . '/Route/routeFile.stub');
294
        $routeFileContentNew = str_replace([
295
            '{{$name}}',
296
            '{{$nameSmall}}'
297
        ], [
298
            "$name",
299
            "$nameSmall"
300
        ], $routeFileContent);
301
        $routeFile = $createFolder.DIRECTORY_SEPARATOR.$nameSmall.".php";
302
        file_put_contents($routeFile, $routeFileContentNew);
303
        return true;
304
    }
305
306
    /**
307
     * Install Controller.
308
     *
309
     * @return boolean
310
     */
311
312
    public function installControllers()
313
    {
314
        $nameSmall = snake_case($this->getParsedNameInput());
315
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
316
        $name = ucfirst($this->getParsedNameInput());
317
318
        $nameFolder = $this->getControllersPath().DIRECTORY_SEPARATOR.$name;
319
        if (!file_exists($nameFolder)) {
320
            mkdir($nameFolder);
321
        }
322
323
        $authFolder = $nameFolder.DIRECTORY_SEPARATOR."Auth";
324
        if (!file_exists($authFolder)) {
325
            mkdir($authFolder);
326
        }
327
328
        $controllerContent = file_get_contents(__DIR__ . '/Controllers/Controller.stub');
329
        $homeControllerContent = file_get_contents(__DIR__ . '/Controllers/HomeController.stub');
330
        $loginControllerContent = file_get_contents(__DIR__ . '/Controllers/Auth/LoginController.stub');
331
        $forgotControllerContent = file_get_contents(__DIR__ . '/Controllers/Auth/ForgotPasswordController.stub');
332
        $registerControllerContent = file_get_contents(__DIR__ . '/Controllers/Auth/RegisterController.stub');
333
        $resetControllerContent = file_get_contents(__DIR__ . '/Controllers/Auth/ResetPasswordController.stub');
334
335
        $controllerFileContentNew = str_replace('{{$name}}', "$name", $controllerContent);
336
337
        $homeFileContentNew = str_replace([
338
            '{{$name}}',
339
            '{{$nameSmall}}'
340
        ], [
341
            "$name",
342
            "$nameSmall"
343
        ], $homeControllerContent);
344
345
        $loginFileContentNew = str_replace([
346
            '{{$name}}',
347
            '{{$nameSmall}}'
348
        ], [
349
            "$name",
350
            "$nameSmall"
351
        ], $loginControllerContent);
352
353
        $forgotFileContentNew = str_replace([
354
            '{{$name}}',
355
            '{{$nameSmall}}',
356
            '{{$nameSmallPlural}}'
357
        ], [
358
            "$name",
359
            "$nameSmall",
360
            "$nameSmallPlural"
361
        ], $forgotControllerContent);
362
363
        $registerFileContentNew = str_replace([
364
            '{{$name}}',
365
            '{{$nameSmall}}',
366
            '{{$nameSmallPlural}}'
367
        ], [
368
            "$name",
369
            "$nameSmall",
370
            "$nameSmallPlural"
371
        ], $registerControllerContent);
372
373
        $resetFileContentNew = str_replace([
374
            '{{$name}}',
375
            '{{$nameSmall}}',
376
            '{{$nameSmallPlural}}'
377
        ], [
378
            "$name",
379
            "$nameSmall",
380
            "$nameSmallPlural"
381
        ], $resetControllerContent);
382
383
        $controllerFile = $nameFolder.DIRECTORY_SEPARATOR."Controller.php";
384
        $homeFile = $nameFolder.DIRECTORY_SEPARATOR."HomeController.php";
385
        $loginFile = $authFolder.DIRECTORY_SEPARATOR."LoginController.php";
386
        $forgotFile = $authFolder.DIRECTORY_SEPARATOR."ForgotPasswordController.php";
387
        $registerFile = $authFolder.DIRECTORY_SEPARATOR."RegisterController.php";
388
        $resetFile = $authFolder.DIRECTORY_SEPARATOR."ResetPasswordController.php";
389
390
        file_put_contents($controllerFile, $controllerFileContentNew);
391
        file_put_contents($homeFile, $homeFileContentNew);
392
        file_put_contents($loginFile, $loginFileContentNew);
393
        file_put_contents($forgotFile, $forgotFileContentNew);
394
        file_put_contents($registerFile, $registerFileContentNew);
395
        file_put_contents($resetFile, $resetFileContentNew);
396
397
        return true;
398
399
    }
400
401
    /**
402
     * Install Configs.
403
     *
404
     * @return boolean
405
     */
406
407
    public function installConfigs()
408
    {
409
        $nameSmall = snake_case($this->getParsedNameInput());
410
        $nameSmallPlural = str_plural(snake_case($this->getParsedNameInput()));
411
        $name = ucfirst($this->getParsedNameInput());
412
413
        $authConfigFile = $this->getConfigsFolderPath().DIRECTORY_SEPARATOR."auth.php";
414
415
        $guardContent = file_get_contents(__DIR__ . '/Config/guard.stub');
416
        $passwordContent = file_get_contents(__DIR__ . '/Config/password.stub');
417
        $providerContent = file_get_contents(__DIR__ . '/Config/provider.stub');
418
419
        $guardFileContentNew = str_replace([
420
            '{{$nameSmall}}',
421
            '{{$nameSmallPlural}}'
422
        ], [
423
            "$nameSmall",
424
            "$nameSmallPlural"
425
        ], $guardContent);
426
427
        $passwordFileContentNew = str_replace('{{$nameSmallPlural}}', "$nameSmallPlural", $passwordContent);
428
429
        $providerFileContentNew = str_replace([
430
            '{{$name}}',
431
            '{{$nameSmallPlural}}'
432
        ], [
433
            "$name",
434
            "$nameSmallPlural"
435
        ], $providerContent);
436
437
        $this->insert($authConfigFile, '    \'guards\' => [', $guardFileContentNew, true);
438
439
        $this->insert($authConfigFile, '    \'passwords\' => [', $passwordFileContentNew, true);
440
441
        $this->insert($authConfigFile, '    \'providers\' => [', $providerFileContentNew, true);
442
443
        return true;
444
445
    }
446
447
    /**
448
     * Install Middleware.
449
     *
450
     * @return boolean
451
     */
452
453
    public function installMiddleware()
454
    {
455
        $nameSmall = snake_case($this->getParsedNameInput());
456
457
        $redirectIfMiddlewareFile = $this->getMiddlewarePath().DIRECTORY_SEPARATOR."RedirectIfAuthenticated.php";
458
        $middlewareKernelFile = $this->getHttpPath().DIRECTORY_SEPARATOR."Kernel.php";
459
460
        $redirectIfMiddlewareFileContent = file_get_contents($redirectIfMiddlewareFile);
461
462
        $redirectIfMiddlewareContentNew = file_get_contents(__DIR__ . '/Middleware/redirectIf.stub');
463
464
        if (!str_contains($redirectIfMiddlewareFileContent, 'MultiAuthGuards')) {
465
            // replace old file
466
            $deleted = unlink($redirectIfMiddlewareFile);
467
            if ($deleted) {
468
                file_put_contents($redirectIfMiddlewareFile, $redirectIfMiddlewareContentNew);
469
            }
470
        }
471
472
        $redirectIfMiddlewareGroupContentNew = file_get_contents(__DIR__ . '/Middleware/redirectMiddleware.stub');
473
        $redirectIfMiddlewareGuardContentNew = file_get_contents(__DIR__ . '/Middleware/redirectMiddlewareGuard.stub');
474
475
        $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...
476
        $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...
477
478
        $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...
479
480
        $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...
481
482
        return true;
483
484
    }
485
486
    /**
487
     * Get the desired class name from the input.
488
     *
489
     * @return string
490
     */
491
    protected function getParsedNameInput()
492
    {
493
        return mb_strtolower(str_singular($this->getNameInput()));
494
    }
495
    /**
496
     * Get the desired class name from the input.
497
     *
498
     * @return string
499
     */
500
    protected function getNameInput()
501
    {
502
        return trim($this->argument('name'));
503
    }
504
505
    /**
506
     * Write the migration file to disk.
507
     *
508
     * @param  string  $name
509
     * @param  string  $table
510
     * @param  bool    $create
511
     * @return mixed
512
     */
513
    protected function writeMigration($name, $table, $create)
514
    {
515
        $file = pathinfo($this->creator->create(
516
            $name, $this->getMigrationPath(), $table, $create
517
        ), PATHINFO_FILENAME);
518
        $this->line("<info>Created Migration:</info> {$file}");
519
    }
520
521
    /**
522
     * Get migration path (either specified by '--path' option or default location).
523
     *
524
     * @return string
525
     */
526
    protected function getMigrationPath()
527
    {
528
        return parent::getMigrationPath();
529
    }
530
531
    /**
532
     * Get Routes Provider Path.
533
     *
534
     * @return string
535
     */
536
    protected function getRouteServicesPath()
537
    {
538
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Providers'.DIRECTORY_SEPARATOR.'RouteServiceProvider.php';
539
    }
540
541
    /**
542
     * Get Routes Folder Path.
543
     *
544
     * @return string
545
     */
546
    protected function getAppFolderPath()
547
    {
548
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'app';
549
    }
550
551
    /**
552
     * Get Routes Folder Path.
553
     *
554
     * @return string
555
     */
556
    protected function getRoutesFolderPath()
557
    {
558
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'routes';
559
    }
560
561
    /**
562
     * Get Config Folder Path.
563
     *
564
     * @return string
565
     */
566
    protected function getConfigsFolderPath()
567
    {
568
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'config';
569
    }
570
571
    /**
572
     * Get Config Folder Path.
573
     *
574
     * @return string
575
     */
576
    protected function getViewsFolderPath()
577
    {
578
        return $this->laravel->basePath().DIRECTORY_SEPARATOR.'resources'.DIRECTORY_SEPARATOR.'views';
579
    }
580
581
    /**
582
     * Get Controllers Path.
583
     *
584
     * @return string
585
     */
586
    protected function getControllersPath()
587
    {
588
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Controllers';
589
    }
590
591
    /**
592
     * Get Http Path.
593
     *
594
     * @return string
595
     */
596
    protected function getHttpPath()
597
    {
598
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http';
599
    }
600
601
    /**
602
     * Get Middleware Path.
603
     *
604
     * @return string
605
     */
606
    protected function getMiddlewarePath()
607
    {
608
        return $this->getAppFolderPath().DIRECTORY_SEPARATOR.'Http'.DIRECTORY_SEPARATOR.'Middleware';
609
    }
610
611
    /**
612
     * insert text into file
613
     *
614
     * @param string $filePath
615
     * @param string $insertMarker
616
     * @param string $text
617
     * @param boolean $after
618
     *
619
     * @return integer
620
     */
621
    public function insertIntoFile($filePath, $insertMarker, $text, $after = true) {
622
        $contents = file_get_contents($filePath);
623
        $new_contents = preg_replace($insertMarker,($after) ? '$0' . $text : $text . '$0', $contents);
624
        return file_put_contents($filePath, $new_contents);
625
    }
626
627
    /**
628
     * insert text into file
629
     *
630
     * @param string $filePath
631
     * @param string $keyword
632
     * @param string $body
633
     * @param boolean $after
634
     *
635
     * @return integer
636
     */
637
    public function insert($filePath, $keyword, $body, $after = true) {
638
639
        $contents = file_get_contents($filePath);
640
        $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...
641
        return file_put_contents($filePath, $new_contents);
642
    }
643
}
644