Skip to content

Commit 4ae93dc

Browse files
committed
use namespace to insert crud trait
1 parent c5543d2 commit 4ae93dc

File tree

1 file changed

+24
-31
lines changed

1 file changed

+24
-31
lines changed

src/Console/Commands/CrudModelBackpackCommand.php

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Backpack\Generators\Console\Commands;
44

55
use Backpack\Generators\Services\BackpackCommand;
6+
use Illuminate\Support\Facades\File;
67
use Illuminate\Support\Str;
78

89
class CrudModelBackpackCommand extends BackpackCommand
@@ -101,45 +102,37 @@ public function handle()
101102
if (! $this->hasOption('force') || ! $this->option('force')) {
102103
$this->progressBlock('Adding CrudTrait to the Model');
103104

104-
$file = $this->files->get($path);
105-
$lines = preg_split('/(\r\n)|\r|\n/', $file);
105+
$content = Str::of($this->files->get($path));
106106

107107
// check if it already uses CrudTrait
108108
// if it does, do nothing
109-
if (Str::contains($file, $this->crudTrait)) {
109+
if ($content->contains($this->crudTrait)) {
110110
$this->closeProgressBlock('Already existed', 'yellow');
111111

112112
return false;
113-
}
114-
115-
// if it does not have CrudTrait, add the trait on the Model
116-
foreach ($lines as $key => $line) {
117-
if (Str::contains($line, "class {$name} extends")) {
118-
if (Str::endsWith($line, '{')) {
119-
// add the trait on the next
120-
$position = $key + 1;
121-
} elseif ($lines[$key + 1] == '{') {
122-
// add the trait on the next next line
123-
$position = $key + 2;
124-
}
125-
126-
// keep in mind that the line number shown in IDEs is not
127-
// the same as the array index - arrays start counting from 0,
128-
// IDEs start counting from 1
129-
130-
// add CrudTrait
131-
array_splice($lines, $position, 0, " use \\{$this->crudTrait};");
132-
133-
// save the file
134-
$this->files->put($path, implode(PHP_EOL, $lines));
135-
136-
// let the user know what we've done
137-
$this->closeProgressBlock();
138-
139-
return false;
113+
} else {
114+
$modifiedContent = Str::of($content->before('namespace'))
115+
->append('namespace'.$content->after('namespace')->before(';'))
116+
->append(';'.PHP_EOL.PHP_EOL.'use Backpack\CRUD\app\Models\Traits\CrudTrait;');
117+
118+
$content = $content->after('namespace')->after(';');
119+
120+
while(str_starts_with($content, PHP_EOL) || str_starts_with($content, "\n")) {
121+
$content = substr($content, 1);
140122
}
123+
124+
$modifiedContent = $modifiedContent->append(PHP_EOL.$content);
125+
126+
// use the CrudTrait on the class
127+
$modifiedContent = $modifiedContent->replaceFirst('{', '{'.PHP_EOL.' use CrudTrait;');
128+
129+
// save the file
130+
$this->files->put($path, $modifiedContent);
131+
// let the user know what we've done
132+
$this->closeProgressBlock();
133+
134+
return true;
141135
}
142-
143136
// In case we couldn't add the CrudTrait
144137
$this->errorProgressBlock();
145138
$this->note("Model already existed on '$name' and we couldn't add CrudTrait. Please add it manually.", 'red');

0 commit comments

Comments
 (0)