Skip to content

Commit

Permalink
Merge pull request #189 from Laravel-Backpack/v6
Browse files Browse the repository at this point in the history
v6
  • Loading branch information
tabacitu committed Jul 1, 2023
2 parents 679a8bc + 91c76e0 commit 6f1c0d7
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 28 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Quickly generate Backpack templated Models, Requests, Views and Config files for
Via Composer

``` bash
composer require --dev backpack/generators
composer require --dev backpack/generators
```

## Usage
Expand Down Expand Up @@ -74,7 +74,7 @@ php artisan backpack:request {Entity_name}

``` bash
php artisan backpack:view {Entity_name}
```
```

- Generate a config file

Expand Down Expand Up @@ -124,6 +124,18 @@ php artisan backpack:widget {widget_name}
php artisan backpack:widget {widget_name} --from={original_widget_name}
```

- Generate a custom operation

``` bash
php artisan backpack:crud-operation {OperationName}
```

- Generate a custom form operation

``` bash
php artisan backpack:crud-form-operation {OperationName}
```

## Change log

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
}
],
"require": {
"php": "^7.4|^8.0",
"backpack/crud": "^5.3.11"
"backpack/crud": "^6.0"
},
"require-dev": {
"phpunit/phpunit" : "^9.0||^7.0",
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Commands/CrudBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public function handle()
'code' => "Route::crud('$nameKebab', '{$this->convertSlashesForNamespace($nameTitle)}CrudController');",
]);

// Create the sidebar item
$this->call('backpack:add-sidebar-content', [
'code' => "<li class=\"nav-item\"><a class=\"nav-link\" href=\"{{ backpack_url('$nameKebab') }}\"><i class=\"nav-icon la la-question\"></i> $fullNameWithSpaces</a></li>",
// Create the menu item
$this->call('backpack:add-menu-content', [
'code' => '<x-backpack::menu-item title="'.$fullNameWithSpaces."\" icon=\"la la-question\" :link=\"backpack_url('".$nameKebab."')\" />",
]);

// if the application uses cached routes, we should rebuild the cache so the previous added route will
Expand Down
119 changes: 119 additions & 0 deletions src/Console/Commands/CrudFormOperationBackpackCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php

namespace Backpack\Generators\Console\Commands;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;

class CrudFormOperationBackpackCommand extends GeneratorCommand
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'backpack:crud-form-operation';

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'backpack:crud-form-operation {name}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate an operation trait with a Backpack form';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Trait';

/**
* Get the destination class path.
*
* @param string $name
* @return string
*/
protected function getPath($name)
{
$name = str_replace($this->laravel->getNamespace(), '', $name);

return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'Operation.php';
}

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__.'/../stubs/crud-form-operation.stub';
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\Http\Controllers\Admin\Operations';
}

/**
* Replace the table name for the given stub.
*
* @param string $stub
* @param string $name
* @return string
*/
protected function replaceNameStrings(&$stub, $name)
{
$name = Str::of($name)->afterLast('\\');

$stub = str_replace('DummyClass', $name->studly(), $stub);
$stub = str_replace('dummyClass', $name->lcfirst(), $stub);
$stub = str_replace('Dummy Class', $name->snake()->replace('_', ' ')->title(), $stub);
$stub = str_replace('dummy-class', $name->snake('-'), $stub);
$stub = str_replace('dummy_class', $name->snake(), $stub);

return $this;
}

/**
* Build the class with the given name.
*
* @param string $name
* @return string
*/
protected function buildClass($name)
{
$stub = $this->files->get($this->getStub());

return $this
->replaceNamespace($stub, $name)
->replaceNameStrings($stub, $name)
->replaceClass($stub, $name);
}

/**
* Get the desired class name from the input.
*
* @return string
*/
protected function getNameInput()
{
return Str::of($this->argument('name'))
->trim()
->studly();
}
}
1 change: 0 additions & 1 deletion src/Console/Commands/CrudModelBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Backpack\Generators\Console\Commands;

use Backpack\Generators\Services\BackpackCommand;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

class CrudModelBackpackCommand extends BackpackCommand
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Commands/PageBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ public function handle()
'code' => "Route::get('{$route}', '{$nameTitle->studly()}Controller@index')->name('page.{$nameSnake}.index');",
]);

// create the sidebar item
$this->call('backpack:add-sidebar-content', [
'code' => "<li class=\"nav-item\"><a class=\"nav-link\" href=\"{{ backpack_url('{$route}') }}\"><i class=\"nav-icon la la-question\"></i> {$nameTitle}</a></li>",
// create the menu item
$this->call('backpack:add-menu-content', [
'code' => '<x-backpack::menu-item title="'.$nameTitle."\" icon=\"la la-question\" :link=\"backpack_url('".$route."')\" />",
]);

$url = backpack_url($route);
Expand Down
66 changes: 50 additions & 16 deletions src/Console/Commands/Views/PublishOrCreateViewBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@ abstract class PublishOrCreateViewBackpackCommand extends GeneratorCommand
{
use \Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;

/**
* The source file to copy from.
*/
public ?string $sourceFile = null;

/**
* The source file view namespace.
*/
public ?string $sourceViewNamespace = null;

/**
* Stub file name.
*
* @var string
*/
protected $stub = '';

/**
* View Namespace.
*
* @var string
*/
protected $viewNamespace = '';

/**
* Get the stub file for the generator.
*
Expand All @@ -27,6 +51,12 @@ protected function getStub()
*/
public function handle()
{
$this->setupSourceFile();

if ($this->sourceFile === false) {
return false;
}

$name = Str::of($this->getNameInput());
$path = Str::of($this->getPath($name));
$pathRelative = $path->after(base_path())->replace('\\', '/')->trim('/');
Expand All @@ -40,45 +70,49 @@ public function handle()
return false;
}

$source = null;
$this->makeDirectory($path);

if ($this->sourceFile) {
$this->files->copy($this->sourceFile, $path);
} else {
$this->files->put($path, $this->buildClass($name));
}

$this->closeProgressBlock();
}

private function setupSourceFile()
{
if ($this->option('from')) {
$from = $this->option('from');
$namespaces = ViewNamespaces::getFor($this->viewNamespace);
foreach ($namespaces as $namespace) {
$viewPath = "$namespace.$from";

if (view()->exists($viewPath)) {
$source = view($viewPath)->getPath();
$this->sourceFile = view($viewPath)->getPath();
$this->sourceViewNamespace = $viewPath;
break;
}
}

// full or relative file path may be provided
if (file_exists($from)) {
$source = realpath($from);
$this->sourceFile = realpath($from);
}
// remove the first slash to make absolute paths relative in unix systems
elseif (file_exists(substr($from, 1))) {
$source = realpath(substr($from, 1));
$this->sourceFile = realpath(substr($from, 1));
}

if (! $source) {
if (! $this->sourceFile) {
$this->errorProgressBlock();
$this->note("$this->type '$from' does not exist!", 'red');
$this->newLine();

return false;
$this->sourceFile = false;
}
}

$this->makeDirectory($path);

if ($source) {
$this->files->copy($source, $path);
} else {
$this->files->put($path, $this->buildClass($name));
}

$this->closeProgressBlock();
}

/**
Expand Down
18 changes: 17 additions & 1 deletion src/Console/Commands/Views/WidgetBackpackCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Backpack\Generators\Console\Commands\Views;

use Illuminate\Support\Str;

class WidgetBackpackCommand extends PublishOrCreateViewBackpackCommand
{
/**
Expand Down Expand Up @@ -54,6 +56,20 @@ class WidgetBackpackCommand extends PublishOrCreateViewBackpackCommand
*/
protected function getPath($name)
{
return resource_path("views/vendor/backpack/base/{$this->viewNamespace}/$name.blade.php");
if ($this->sourceViewNamespace) {
$themePath = Str::contains($this->sourceViewNamespace, '::') ?
Str::before($this->sourceViewNamespace, '::') :
Str::beforeLast($this->sourceViewNamespace, '.');

$themePath = Str::replace('.', '/', $themePath);

$path = 'views/vendor/'.$themePath.'/'.$this->viewNamespace.'/'.$name.'.blade.php';

return resource_path($path);
}

$path = 'views/vendor/backpack/ui/'.$this->viewNamespace.'/'.$name.'.blade.php';

return resource_path($path);
}
}
Loading

0 comments on commit 6f1c0d7

Please sign in to comment.