Skip to content

Commit c355345

Browse files
authored
Merge pull request #68 from Laravel-Backpack/4.1
Support for Backpack 4.1
2 parents 7011e79 + 7a57f7c commit c355345

8 files changed

+218
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
}
2020
],
2121
"require": {
22-
"backpack/crud": "4.0.*"
22+
"backpack/crud": "4.1.*"
2323
},
2424
"require-dev": {
2525
"phpunit/phpunit" : "^9.0||^7.0",
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Backpack\Generators\Console\Commands;
4+
5+
use Artisan;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Support\Str;
8+
9+
class ChartBackpackCommand extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'backpack:chart {name}';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Create a ChartController and route';
24+
25+
/**
26+
* Execute the console command.
27+
*
28+
* @return mixed
29+
*/
30+
public function handle()
31+
{
32+
$studlyName = Str::studly($this->argument('name')); // aka Pascal Case
33+
$kebabName = Str::kebab($this->argument('name'));
34+
35+
// Create the ChartController and show output
36+
Artisan::call('backpack:chart-controller', ['name' => $studlyName]);
37+
echo Artisan::output();
38+
39+
// Create the chart route
40+
Artisan::call('backpack:add-custom-route', [
41+
'code' => "Route::get('charts/".$kebabName."', 'Charts\\".$studlyName."ChartController@response')->name('charts.'.$kebabName.'.index');",
42+
]);
43+
echo Artisan::output();
44+
}
45+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
namespace Backpack\Generators\Console\Commands;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Support\Str;
7+
8+
class ChartControllerBackpackCommand extends GeneratorCommand
9+
{
10+
/**
11+
* The console command name.
12+
*
13+
* @var string
14+
*/
15+
protected $name = 'backpack:chart-controller';
16+
17+
/**
18+
* The name and signature of the console command.
19+
*
20+
* @var string
21+
*/
22+
protected $signature = 'backpack:chart-controller {name}';
23+
24+
/**
25+
* The console command description.
26+
*
27+
* @var string
28+
*/
29+
protected $description = 'Generate a Backpack ChartController';
30+
31+
/**
32+
* The type of class being generated.
33+
*
34+
* @var string
35+
*/
36+
protected $type = 'Controller';
37+
38+
/**
39+
* Get the destination class path.
40+
*
41+
* @param string $name
42+
*
43+
* @return string
44+
*/
45+
protected function getPath($name)
46+
{
47+
$name = str_replace($this->laravel->getNamespace(), '', $name);
48+
49+
return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'ChartController.php';
50+
}
51+
52+
/**
53+
* Get the stub file for the generator.
54+
*
55+
* @return string
56+
*/
57+
protected function getStub()
58+
{
59+
return __DIR__.'/../stubs/chart-controller.stub';
60+
}
61+
62+
/**
63+
* Get the default namespace for the class.
64+
*
65+
* @param string $rootNamespace
66+
*
67+
* @return string
68+
*/
69+
protected function getDefaultNamespace($rootNamespace)
70+
{
71+
return $rootNamespace.'\Http\Controllers\Admin\Charts';
72+
}
73+
74+
/**
75+
* Replace the table name for the given stub.
76+
*
77+
* @param string $stub
78+
* @param string $name
79+
*
80+
* @return string
81+
*/
82+
protected function replaceRouteStrings(&$stub)
83+
{
84+
$stub = str_replace('dummy-class', Str::kebab($this->argument('name')), $stub);
85+
86+
return $this;
87+
}
88+
89+
/**
90+
* Build the class with the given name.
91+
*
92+
* @param string $name
93+
*
94+
* @return string
95+
*/
96+
protected function buildClass($name)
97+
{
98+
$stub = $this->files->get($this->getStub());
99+
100+
return $this->replaceNamespace($stub, $name)
101+
->replaceRouteStrings($stub)
102+
->replaceClass($stub, $name);
103+
}
104+
105+
/**
106+
* Get the console command options.
107+
*
108+
* @return array
109+
*/
110+
protected function getOptions()
111+
{
112+
return [
113+
114+
];
115+
}
116+
}

src/Console/Commands/CrudBackpackCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function handle()
5252

5353
// Create the sidebar item
5454
Artisan::call('backpack:add-sidebar-content', [
55-
'code' => "<li class='nav-item'><a class='nav-link' href='{{ backpack_url('".$lowerName."') }}'><i class='nav-icon fa fa-question'></i> ".Str::plural($name).'</a></li>',
55+
'code' => "<li class='nav-item'><a class='nav-link' href='{{ backpack_url('".$lowerName."') }}'><i class='nav-icon la la-question'></i> ".Str::plural($name).'</a></li>',
5656
]);
5757
echo Artisan::output();
5858
}

src/Console/Commands/CrudControllerBackpackCommand.php

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

55
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Support\Str;
67

78
class CrudControllerBackpackCommand extends GeneratorCommand
89
{
@@ -80,7 +81,7 @@ protected function getDefaultNamespace($rootNamespace)
8081
*/
8182
protected function replaceNameStrings(&$stub, $name)
8283
{
83-
$table = str_plural(ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_'));
84+
$table = Str::plural(ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_'));
8485

8586
$stub = str_replace('DummyTable', $table, $stub);
8687
$stub = str_replace('dummy_class', strtolower(str_replace($this->getNamespace($name).'\\', '', $name)), $stub);

src/Console/Commands/CrudOperationBackpackCommand.php

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

55
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Support\Str;
67

78
class CrudOperationBackpackCommand extends GeneratorCommand
89
{
@@ -80,7 +81,7 @@ protected function getDefaultNamespace($rootNamespace)
8081
*/
8182
protected function replaceNameStrings(&$stub, $name)
8283
{
83-
$table = str_plural(ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_'));
84+
$table = Str::plural(ltrim(strtolower(preg_replace('/[A-Z]/', '_$0', str_replace($this->getNamespace($name).'\\', '', $name))), '_'));
8485

8586
$stub = str_replace('DummyTable', $table, $stub);
8687
$stub = str_replace('dummy_class', strtolower(str_replace($this->getNamespace($name).'\\', '', $name)), $stub);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use Backpack\CRUD\app\Http\Controllers\ChartController;
6+
use ConsoleTVs\Charts\Classes\Chartjs\Chart;
7+
8+
/**
9+
* Class DummyClassChartController
10+
* @package App\Http\Controllers\Admin\Charts
11+
* @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
12+
*/
13+
class DummyClassChartController extends ChartController
14+
{
15+
public function setup()
16+
{
17+
$this->chart = new Chart();
18+
19+
// MANDATORY. Set the labels for the dataset points
20+
$this->chart->labels([
21+
'Today',
22+
]);
23+
24+
// RECOMMENDED. Set URL that the ChartJS library should call, to get its data using AJAX.
25+
$this->chart->load(backpack_url('charts/dummy-class'));
26+
27+
// OPTIONAL
28+
// $this->chart->minimalist(false);
29+
// $this->chart->displayLegend(true);
30+
}
31+
32+
/**
33+
* Respond to AJAX calls with all the chart data points.
34+
*
35+
* @return json
36+
*/
37+
// public function data()
38+
// {
39+
// $users_created_today = \App\User::whereDate('created_at', today())->count();
40+
41+
// $this->chart->dataset('Users Created', 'bar', [
42+
// $users_created_today,
43+
// ])
44+
// ->color('rgba(205, 32, 31, 1)')
45+
// ->backgroundColor('rgba(205, 32, 31, 0.4)');
46+
// }
47+
}

src/GeneratorsServiceProvider.php

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

33
namespace Backpack\Generators;
44

5+
use Backpack\Generators\Console\Commands\ChartBackpackCommand;
6+
use Backpack\Generators\Console\Commands\ChartControllerBackpackCommand;
57
use Backpack\Generators\Console\Commands\ConfigBackpackCommand;
68
use Backpack\Generators\Console\Commands\CrudBackpackCommand;
79
use Backpack\Generators\Console\Commands\CrudControllerBackpackCommand;
@@ -19,9 +21,11 @@ class GeneratorsServiceProvider extends ServiceProvider
1921
ConfigBackpackCommand::class,
2022
CrudModelBackpackCommand::class,
2123
CrudControllerBackpackCommand::class,
24+
ChartControllerBackpackCommand::class,
2225
CrudOperationBackpackCommand::class,
2326
CrudRequestBackpackCommand::class,
2427
CrudBackpackCommand::class,
28+
ChartBackpackCommand::class,
2529
ModelBackpackCommand::class,
2630
RequestBackpackCommand::class,
2731
ViewBackpackCommand::class,

0 commit comments

Comments
 (0)