From 4ae93dc9a63a232a78df8fd050c8524c1df92187 Mon Sep 17 00:00:00 2001 From: Pedro X Date: Wed, 1 Mar 2023 23:42:21 +0000 Subject: [PATCH 1/2] use namespace to insert crud trait --- .../Commands/CrudModelBackpackCommand.php | 55 ++++++++----------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/src/Console/Commands/CrudModelBackpackCommand.php b/src/Console/Commands/CrudModelBackpackCommand.php index 2e1459f..d42aac6 100644 --- a/src/Console/Commands/CrudModelBackpackCommand.php +++ b/src/Console/Commands/CrudModelBackpackCommand.php @@ -3,6 +3,7 @@ namespace Backpack\Generators\Console\Commands; use Backpack\Generators\Services\BackpackCommand; +use Illuminate\Support\Facades\File; use Illuminate\Support\Str; class CrudModelBackpackCommand extends BackpackCommand @@ -101,45 +102,37 @@ public function handle() if (! $this->hasOption('force') || ! $this->option('force')) { $this->progressBlock('Adding CrudTrait to the Model'); - $file = $this->files->get($path); - $lines = preg_split('/(\r\n)|\r|\n/', $file); + $content = Str::of($this->files->get($path)); // check if it already uses CrudTrait // if it does, do nothing - if (Str::contains($file, $this->crudTrait)) { + if ($content->contains($this->crudTrait)) { $this->closeProgressBlock('Already existed', 'yellow'); return false; - } - - // if it does not have CrudTrait, add the trait on the Model - foreach ($lines as $key => $line) { - if (Str::contains($line, "class {$name} extends")) { - if (Str::endsWith($line, '{')) { - // add the trait on the next - $position = $key + 1; - } elseif ($lines[$key + 1] == '{') { - // add the trait on the next next line - $position = $key + 2; - } - - // keep in mind that the line number shown in IDEs is not - // the same as the array index - arrays start counting from 0, - // IDEs start counting from 1 - - // add CrudTrait - array_splice($lines, $position, 0, " use \\{$this->crudTrait};"); - - // save the file - $this->files->put($path, implode(PHP_EOL, $lines)); - - // let the user know what we've done - $this->closeProgressBlock(); - - return false; + } else { + $modifiedContent = Str::of($content->before('namespace')) + ->append('namespace'.$content->after('namespace')->before(';')) + ->append(';'.PHP_EOL.PHP_EOL.'use Backpack\CRUD\app\Models\Traits\CrudTrait;'); + + $content = $content->after('namespace')->after(';'); + + while(str_starts_with($content, PHP_EOL) || str_starts_with($content, "\n")) { + $content = substr($content, 1); } + + $modifiedContent = $modifiedContent->append(PHP_EOL.$content); + + // use the CrudTrait on the class + $modifiedContent = $modifiedContent->replaceFirst('{', '{'.PHP_EOL.' use CrudTrait;'); + + // save the file + $this->files->put($path, $modifiedContent); + // let the user know what we've done + $this->closeProgressBlock(); + + return true; } - // In case we couldn't add the CrudTrait $this->errorProgressBlock(); $this->note("Model already existed on '$name' and we couldn't add CrudTrait. Please add it manually.", 'red'); From 04cf6499094faeb9be52903a90934c48b33f96b7 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 1 Mar 2023 23:43:36 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI --- src/Console/Commands/CrudModelBackpackCommand.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Console/Commands/CrudModelBackpackCommand.php b/src/Console/Commands/CrudModelBackpackCommand.php index d42aac6..cb000bb 100644 --- a/src/Console/Commands/CrudModelBackpackCommand.php +++ b/src/Console/Commands/CrudModelBackpackCommand.php @@ -116,13 +116,13 @@ public function handle() ->append(';'.PHP_EOL.PHP_EOL.'use Backpack\CRUD\app\Models\Traits\CrudTrait;'); $content = $content->after('namespace')->after(';'); - - while(str_starts_with($content, PHP_EOL) || str_starts_with($content, "\n")) { + + while (str_starts_with($content, PHP_EOL) || str_starts_with($content, "\n")) { $content = substr($content, 1); } - + $modifiedContent = $modifiedContent->append(PHP_EOL.$content); - + // use the CrudTrait on the class $modifiedContent = $modifiedContent->replaceFirst('{', '{'.PHP_EOL.' use CrudTrait;');