|
3 | 3 | namespace Backpack\Generators\Console\Commands; |
4 | 4 |
|
5 | 5 | use Backpack\Generators\Services\BackpackCommand; |
| 6 | +use Illuminate\Support\Facades\File; |
6 | 7 | use Illuminate\Support\Str; |
7 | 8 |
|
8 | 9 | class CrudModelBackpackCommand extends BackpackCommand |
@@ -101,45 +102,37 @@ public function handle() |
101 | 102 | if (! $this->hasOption('force') || ! $this->option('force')) { |
102 | 103 | $this->progressBlock('Adding CrudTrait to the Model'); |
103 | 104 |
|
104 | | - $file = $this->files->get($path); |
105 | | - $lines = preg_split('/(\r\n)|\r|\n/', $file); |
| 105 | + $content = Str::of($this->files->get($path)); |
106 | 106 |
|
107 | 107 | // check if it already uses CrudTrait |
108 | 108 | // if it does, do nothing |
109 | | - if (Str::contains($file, $this->crudTrait)) { |
| 109 | + if ($content->contains($this->crudTrait)) { |
110 | 110 | $this->closeProgressBlock('Already existed', 'yellow'); |
111 | 111 |
|
112 | 112 | 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); |
140 | 122 | } |
| 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; |
141 | 135 | } |
142 | | - |
143 | 136 | // In case we couldn't add the CrudTrait |
144 | 137 | $this->errorProgressBlock(); |
145 | 138 | $this->note("Model already existed on '$name' and we couldn't add CrudTrait. Please add it manually.", 'red'); |
|
0 commit comments