Skip to content

Commit 71f20ad

Browse files
committed
Merge branch 'generating-crud-controllers-with-explicit-fields-and-columns' of https://github.com/Laravel-Backpack/Generators into generating-crud-controllers-with-explicit-fields-and-columns
2 parents ac48480 + 881b624 commit 71f20ad

File tree

4 files changed

+163
-1
lines changed

4 files changed

+163
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Via Composer
2121
composer require backpack/generators --dev
2222
```
2323

24-
For Laravel 5.5 - you're done.
24+
For Laravel 7, 6 or 5.5+ - you're done.
2525

2626
For Laravel 5.4 or 5.3 you'll only want to use these generators for ```local``` development, so you don't want to update the ```production``` providers array in ```config/app.php```. Instead, add the provider in ```app/Providers/AppServiceProvider.php```, like so:
2727

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Backpack\Generators\Console\Commands;
4+
5+
use Artisan;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Str;
9+
10+
class BuildBackpackCommand extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'backpack:build';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Create CRUDs for all Models that don\'t already have one.';
25+
26+
/**
27+
* Execute the console command.
28+
*
29+
* @return mixed
30+
*/
31+
public function handle()
32+
{
33+
// make a list of all models
34+
$models = $this->getModels(base_path().'/app');
35+
36+
if (! count($models)) {
37+
$this->info('No models found.');
38+
39+
return false;
40+
}
41+
42+
foreach ($models as $key => $model) {
43+
$this->info('--- '.$model.' ---');
44+
// Create the CrudController & Request
45+
// Attach CrudTrait to Model
46+
// Add sidebar item
47+
// Add routes
48+
Artisan::call('backpack:crud', ['name' => $model]);
49+
echo Artisan::output();
50+
}
51+
}
52+
53+
private function getModels($path)
54+
{
55+
$out = [];
56+
$results = scandir($path);
57+
58+
foreach ($results as $result) {
59+
if ($result === '.' or $result === '..') {
60+
continue;
61+
}
62+
$filename = $path.'/'.$result;
63+
64+
if (is_dir($filename)) {
65+
$out = array_merge($out, $this->getModels($filename));
66+
} else {
67+
$file_content = file_get_contents($filename);
68+
if (Str::contains($file_content, 'Illuminate\Database\Eloquent\Model') &&
69+
Str::contains($file_content, 'extends Model')) {
70+
$out[] = Arr::last(explode('/', substr($filename, 0, -4)));
71+
}
72+
}
73+
}
74+
75+
return $out;
76+
}
77+
}

src/Console/Commands/CrudModelBackpackCommand.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,89 @@ class CrudModelBackpackCommand extends GeneratorCommand
3535
*/
3636
protected $type = 'Model';
3737

38+
/**
39+
* The trait that allows a model to have an admin panel.
40+
*
41+
* @var string
42+
*/
43+
protected $crudTrait = 'Backpack\CRUD\app\Models\Traits\CrudTrait';
44+
45+
/**
46+
* Execute the console command.
47+
*
48+
* @return bool|null
49+
*
50+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
51+
*/
52+
public function handle()
53+
{
54+
$name = $this->qualifyClass($this->getNameInput());
55+
56+
$path = $this->getPath($name);
57+
58+
// First we will check to see if the class already exists. If it does, we don't want
59+
// to create the class and overwrite the user's code. We just make sure it uses CrudTrait
60+
// We add that one line. Otherwise, we will continue generating this class' files.
61+
if ((! $this->hasOption('force') ||
62+
! $this->option('force')) &&
63+
$this->alreadyExists($this->getNameInput())) {
64+
$file = $this->files->get($path);
65+
$file_array = explode(PHP_EOL, $file);
66+
67+
// check if it already uses CrudTrait
68+
// if it does, do nothing
69+
if (Str::contains($file, [$this->crudTrait])) {
70+
$this->info('Model already exists and uses CrudTrait.');
71+
72+
return false;
73+
}
74+
75+
// if it does not have CrudTrait, add the trait on the Model
76+
77+
$classDefinition = 'class '.$this->getNameInput().' extends';
78+
79+
foreach ($file_array as $key => $line) {
80+
if (Str::contains($line, $classDefinition)) {
81+
if (Str::endsWith($line, '{')) {
82+
// add the trait on the next
83+
$position = $key + 1;
84+
} elseif ($file_array[$key + 1] == '{') {
85+
// add the trait on the next next line
86+
$position = $key + 2;
87+
}
88+
89+
// keep in mind that the line number shown in IDEs is not
90+
// the same as the array index - arrays start counting from 0,
91+
// IDEs start counting from 1
92+
93+
// add CrudTrait
94+
array_splice($file_array, $position, 0, ' use \\'.$this->crudTrait.';');
95+
96+
// save the file
97+
$this->files->put($path, implode(PHP_EOL, $file_array));
98+
99+
// let the user know what we've done
100+
$this->info('Model already exists! We just added CrudTrait on it.');
101+
102+
return false;
103+
}
104+
}
105+
106+
$this->error('Model already exists! Could not add CrudTrait - please add manually.');
107+
108+
return false;
109+
}
110+
111+
// Next, we will generate the path to the location where this class' file should get
112+
// written. Then, we will build the class and make the proper replacements on the
113+
// stub files so that it gets the correctly formatted namespace and class name.
114+
$this->makeDirectory($path);
115+
116+
$this->files->put($path, $this->sortImports($this->buildClass($name)));
117+
118+
$this->info($this->type.' created successfully.');
119+
}
120+
38121
/**
39122
* Get the stub file for the generator.
40123
*

src/GeneratorsServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Backpack\Generators;
44

5+
use Backpack\Generators\Console\Commands\BuildBackpackCommand;
56
use Backpack\Generators\Console\Commands\ChartBackpackCommand;
67
use Backpack\Generators\Console\Commands\ChartControllerBackpackCommand;
78
use Backpack\Generators\Console\Commands\ConfigBackpackCommand;
@@ -18,6 +19,7 @@
1819
class GeneratorsServiceProvider extends ServiceProvider
1920
{
2021
protected $commands = [
22+
BuildBackpackCommand::class,
2123
ConfigBackpackCommand::class,
2224
CrudModelBackpackCommand::class,
2325
CrudControllerBackpackCommand::class,

0 commit comments

Comments
 (0)