Working on module CRUD

This commit is contained in:
Hillel Coren 2016-12-08 20:00:13 +02:00
parent bb55beaa64
commit 8399f8b95f
10 changed files with 72 additions and 109 deletions

View File

@ -11,7 +11,7 @@ use Nwidart\Modules\Commands\GeneratorCommand;
use Nwidart\Modules\Support\Stub; use Nwidart\Modules\Support\Stub;
use Nwidart\Modules\Traits\ModuleCommandTrait; use Nwidart\Modules\Traits\ModuleCommandTrait;
class MakeRepository extends GeneratorCommand class MakeClass extends GeneratorCommand
{ {
use ModuleCommandTrait; use ModuleCommandTrait;
@ -22,20 +22,21 @@ class MakeRepository extends GeneratorCommand
* *
* @var string * @var string
*/ */
protected $name = 'ninja:make-repository'; protected $name = 'ninja:make-class';
/** /**
* The console command description. * The console command description.
* *
* @var string * @var string
*/ */
protected $description = 'Create repository stub'; protected $description = 'Create class stub';
protected function getArguments() protected function getArguments()
{ {
return [ return [
['name', InputArgument::REQUIRED, 'The name of the datatable.'], ['name', InputArgument::REQUIRED, 'The name of the datatable.'],
['module', InputArgument::OPTIONAL, 'The name of module will be used.'], ['module', InputArgument::REQUIRED, 'The name of module will be used.'],
['class', InputArgument::REQUIRED, 'The name of the class.'],
]; ];
} }
@ -43,8 +44,8 @@ class MakeRepository extends GeneratorCommand
{ {
$module = $this->laravel['modules']->findOrFail($this->getModuleName()); $module = $this->laravel['modules']->findOrFail($this->getModuleName());
return (new Stub('/repository.stub', [ return (new Stub('/' . $this->argument('class') . '.stub', [
'NAMESPACE' => $this->getClassNamespace($module) . "\\" . config('modules.paths.generator.repository'), 'NAMESPACE' => $this->getClassNamespace($module) . "\\" . config('modules.paths.generator.' . $this->argument('class')),
'LOWER_NAME' => $module->getLowerName(), 'LOWER_NAME' => $module->getLowerName(),
'CLASS' => $this->getClass(), 'CLASS' => $this->getClass(),
'STUDLY_NAME' => Str::studly($module->getLowerName()), 'STUDLY_NAME' => Str::studly($module->getLowerName()),
@ -54,7 +55,7 @@ class MakeRepository extends GeneratorCommand
public function getDestinationFilePath() public function getDestinationFilePath()
{ {
$path = $this->laravel['modules']->getModulePath($this->getModuleName()); $path = $this->laravel['modules']->getModulePath($this->getModuleName());
$seederPath = $this->laravel['modules']->config('paths.generator.repository'); $seederPath = $this->laravel['modules']->config('paths.generator.' . $this->argument('class'));
return $path . $seederPath . '/' . $this->getFileName() . '.php'; return $path . $seederPath . '/' . $this->getFileName() . '.php';
} }
@ -64,7 +65,7 @@ class MakeRepository extends GeneratorCommand
*/ */
protected function getFileName() protected function getFileName()
{ {
return studly_case($this->argument('name')) . 'Repository'; return studly_case($this->argument('name')) . Str::studly($this->argument('class'));
} }
} }

View File

@ -1,68 +0,0 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Nwidart\Modules\Commands\GeneratorCommand;
use Nwidart\Modules\Support\Stub;
use Nwidart\Modules\Traits\ModuleCommandTrait;
class MakeDatatable extends GeneratorCommand
{
use ModuleCommandTrait;
protected $argumentName = 'name';
/**
* The name and signature of the console command.
*
* @var string
*/
protected $name = 'ninja:make-datatable';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create datatable stub';
protected function getArguments()
{
return [
['name', InputArgument::REQUIRED, 'The name of the datatable.'],
['module', InputArgument::OPTIONAL, 'The name of module will be used.'],
];
}
public function getTemplateContents()
{
$module = $this->laravel['modules']->findOrFail($this->getModuleName());
return (new Stub('/datatable.stub', [
'NAMESPACE' => $this->getClassNamespace($module) . "\\" . config('modules.paths.generator.datatables'),
'LOWER_NAME' => $module->getLowerName(),
'CLASS' => $this->getClass(),
]))->render();
}
public function getDestinationFilePath()
{
$path = $this->laravel['modules']->getModulePath($this->getModuleName());
$seederPath = $this->laravel['modules']->config('paths.generator.datatables');
return $path . $seederPath . '/' . $this->getFileName() . '.php';
}
/**
* @return string
*/
protected function getFileName()
{
return studly_case($this->argument('name')) . 'Datatable';
}
}

View File

@ -40,16 +40,22 @@ class MakeModule extends Command
{ {
$name = $this->argument('name'); $name = $this->argument('name');
$fields = $this->argument('fields'); $fields = $this->argument('fields');
$lower = strtolower($name);
$this->info("Creating module: {$name}"); $this->info("Creating module: {$name}");
$this->info("Fields: {$fields}"); //$this->info("Fields: {$fields}");
Artisan::call('module:make', ['name' => [$name]]); Artisan::call('module:make', ['name' => [$name]]);
Artisan::call('module:make-migration', ['name' => "create_{$name}_table", '--fields' => $fields, 'module' => $name]); Artisan::call('module:make-migration', ['name' => "create_{$lower}_table", '--fields' => $fields, 'module' => $name]);
Artisan::call('module:migrate', ['module' => $name]);
Artisan::call('module:make-model', ['model' => $name, 'module' => $name]); Artisan::call('module:make-model', ['model' => $name, 'module' => $name]);
Artisan::call('ninja:make-datatable', ['name' => $name, 'module' => $name]); Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'datatable']);
Artisan::call('ninja:make-repository', ['name' => $name, 'module' => $name]); Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'repository']);
Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'policy']);
Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'auth-provider']);
Artisan::call('module:dump');
} }
protected function getArguments() protected function getArguments()

View File

@ -12,14 +12,15 @@ class $CLASS$ extends Migration
*/ */
public function up() public function up()
{ {
Schema::create('$TABLE$', function (Blueprint $table) { Schema::create(strtolower('$TABLE$'), function (Blueprint $table) {
$table->increments('id'); $table->increments('id');
$table->unsignedInteger('user_id')->index(); $table->unsignedInteger('user_id')->index();
$table->unsignedInteger('account_id')->index(); $table->unsignedInteger('account_id')->index();
$table->unsignedInteger('client_id')->index()->nullable(); $table->unsignedInteger('client_id')->index()->nullable();
$FIELDS$ $FIELDS$
$table->timestamps(); $table->timestamps();
$table->softDeletes();
$table->boolean('is_deleted')->default(false); $table->boolean('is_deleted')->default(false);
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade'); $table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
@ -38,6 +39,6 @@ $FIELDS$
*/ */
public function down() public function down()
{ {
Schema::dropIfExists('$TABLE$'); Schema::dropIfExists(strtolower('$TABLE$'));
} }
} }

View File

@ -0,0 +1,8 @@
<?php
namespace $NAMESPACE$;
class $STUDLY_NAME$Policy extends EntityPolicy
{
}

View File

@ -1,9 +1,10 @@
<?php <?php
namespace App\Ninja\Repositories; namespace $NAMESPACE$;
use DB; use DB;
use App\Models\$STUDLY_NAME$; use App\Models\$STUDLY_NAME$;
use App\Ninja\Repositories\BaseRepository;
//use App\Events\$STUDLY_NAME$WasCreated; //use App\Events\$STUDLY_NAME$WasCreated;
//use App\Events\$STUDLY_NAME$WasUpdated; //use App\Events\$STUDLY_NAME$WasUpdated;
@ -41,7 +42,7 @@ class $STUDLY_NAME$Repository extends BaseRepository
$query->where('clients.user_id', '=', $userId); $query->where('clients.user_id', '=', $userId);
} }
*/ */
return $query; return $query;
} }

View File

@ -24,8 +24,7 @@ class Kernel extends ConsoleKernel
'App\Console\Commands\GenerateResources', 'App\Console\Commands\GenerateResources',
'App\Console\Commands\TestOFX', 'App\Console\Commands\TestOFX',
'App\Console\Commands\MakeModule', 'App\Console\Commands\MakeModule',
'App\Console\Commands\MakeDatatable', 'App\Console\Commands\MakeClass',
'App\Console\Commands\MakeRepository',
]; ];
/** /**

View File

@ -73,6 +73,7 @@ class EntityPolicy
private static function checkModuleEnabled(User $user, $item) private static function checkModuleEnabled(User $user, $item)
{ {
$entityType = is_string($item) ? $item : $item->getEntityType(); $entityType = is_string($item) ? $item : $item->getEntityType();
return $user->account->isModuleEnabled($entityType); return $user->account->isModuleEnabled($entityType);
} }
} }

View File

@ -3,8 +3,9 @@
namespace App\Policies; namespace App\Policies;
use App\Models\User;
use Utils; use Utils;
use App\Models\User;
use Illuminate\Support\Str;
use Illuminate\Auth\Access\HandlesAuthorization; use Illuminate\Auth\Access\HandlesAuthorization;
/** /**
@ -16,14 +17,14 @@ class GenericEntityPolicy
/** /**
* @param User $user * @param User $user
* @param $itemType * @param $entityType
* @param $ownerUserId * @param $ownerUserId
* @return bool|mixed * @return bool|mixed
*/ */
public static function editByOwner(User $user, $itemType, $ownerUserId) { public static function editByOwner(User $user, $entityType, $ownerUserId) {
$itemType = Utils::getEntityName($itemType); $className = static::className($entityType);
if (method_exists("App\\Policies\\{$itemType}Policy", 'editByOwner')) { if (method_exists($className, 'editByOwner')) {
return call_user_func(["App\\Policies\\{$itemType}Policy", 'editByOwner'], $user, $ownerUserId); return call_user_func([$className, 'editByOwner'], $user, $ownerUserId);
} }
return false; return false;
@ -31,14 +32,14 @@ class GenericEntityPolicy
/** /**
* @param User $user * @param User $user
* @param $itemType * @param $entityTypee
* @param $ownerUserId * @param $ownerUserId
* @return bool|mixed * @return bool|mixed
*/ */
public static function viewByOwner(User $user, $itemType, $ownerUserId) { public static function viewByOwner(User $user, $entityType, $ownerUserId) {
$itemType = Utils::getEntityName($itemType); $className = static::className($entityType);
if (method_exists("App\\Policies\\{$itemType}Policy", 'viewByOwner')) { if (method_exists($className, 'viewByOwner')) {
return call_user_func(["App\\Policies\\{$itemType}Policy", 'viewByOwner'], $user, $ownerUserId); return call_user_func([$className, 'viewByOwner'], $user, $ownerUserId);
} }
return false; return false;
@ -46,13 +47,13 @@ class GenericEntityPolicy
/** /**
* @param User $user * @param User $user
* @param $itemType * @param $entityType
* @return bool|mixed * @return bool|mixed
*/ */
public static function create(User $user, $itemType) { public static function create(User $user, $entityType) {
$entityName = Utils::getEntityName($itemType); $className = static::className($entityType);
if (method_exists("App\\Policies\\{$entityName}Policy", 'create')) { if (method_exists($className, 'create')) {
return call_user_func(["App\\Policies\\{$entityName}Policy", 'create'], $user, $itemType); return call_user_func([$className, 'create'], $user, $entityType);
} }
return false; return false;
@ -60,16 +61,28 @@ class GenericEntityPolicy
/** /**
* @param User $user * @param User $user
* @param $itemType * @param $entityType
* @return bool|mixed * @return bool|mixed
*/ */
public static function view(User $user, $itemType) { public static function view(User $user, $entityType) {
$entityName = Utils::getEntityName($itemType); $className = static::className($entityType);
if (method_exists("App\\Policies\\{$entityName}Policy", 'view')) { if (method_exists($className, 'view')) {
return call_user_func(["App\\Policies\\{$entityName}Policy", 'view'], $user, $itemType); return call_user_func([$className, 'view'], $user, $entityType);
} }
return false; return false;
} }
private static function className($entityType)
{
if ( ! Utils::isNinjaProd()) {
if ($module = \Module::find($entityType)) {
return "Modules\\{$module->getName()}\\Policies\\{$module->getName()}Policy";
}
}
$studly = Str::studly($entityType);
return "App\\Policies\\{$studly}Policy";
}
} }

View File

@ -113,7 +113,8 @@ return [
'jobs' => 'Jobs', 'jobs' => 'Jobs',
'emails' => 'Emails', 'emails' => 'Emails',
'notifications' => 'Notifications', 'notifications' => 'Notifications',
'datatables' => 'Datatables', 'datatable' => 'Datatables',
'policy' => 'Policies',
], ],
], ],
/* /*