1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-19 16:01:34 +02:00

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\Traits\ModuleCommandTrait;
class MakeRepository extends GeneratorCommand
class MakeClass extends GeneratorCommand
{
use ModuleCommandTrait;
@ -22,20 +22,21 @@ class MakeRepository extends GeneratorCommand
*
* @var string
*/
protected $name = 'ninja:make-repository';
protected $name = 'ninja:make-class';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create repository stub';
protected $description = 'Create class stub';
protected function getArguments()
{
return [
['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());
return (new Stub('/repository.stub', [
'NAMESPACE' => $this->getClassNamespace($module) . "\\" . config('modules.paths.generator.repository'),
return (new Stub('/' . $this->argument('class') . '.stub', [
'NAMESPACE' => $this->getClassNamespace($module) . "\\" . config('modules.paths.generator.' . $this->argument('class')),
'LOWER_NAME' => $module->getLowerName(),
'CLASS' => $this->getClass(),
'STUDLY_NAME' => Str::studly($module->getLowerName()),
@ -54,7 +55,7 @@ class MakeRepository extends GeneratorCommand
public function getDestinationFilePath()
{
$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';
}
@ -64,7 +65,7 @@ class MakeRepository extends GeneratorCommand
*/
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');
$fields = $this->argument('fields');
$lower = strtolower($name);
$this->info("Creating module: {$name}");
$this->info("Fields: {$fields}");
//$this->info("Fields: {$fields}");
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('ninja:make-datatable', ['name' => $name, 'module' => $name]);
Artisan::call('ninja:make-repository', ['name' => $name, 'module' => $name]);
Artisan::call('ninja:make-class', ['name' => $name, 'module' => $name, 'class' => 'datatable']);
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()

View File

@ -12,14 +12,15 @@ class $CLASS$ extends Migration
*/
public function up()
{
Schema::create('$TABLE$', function (Blueprint $table) {
Schema::create(strtolower('$TABLE$'), function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('user_id')->index();
$table->unsignedInteger('account_id')->index();
$table->unsignedInteger('client_id')->index()->nullable();
$FIELDS$
$table->timestamps();
$table->softDeletes();
$table->boolean('is_deleted')->default(false);
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade');
@ -38,6 +39,6 @@ $FIELDS$
*/
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
namespace App\Ninja\Repositories;
namespace $NAMESPACE$;
use DB;
use App\Models\$STUDLY_NAME$;
use App\Ninja\Repositories\BaseRepository;
//use App\Events\$STUDLY_NAME$WasCreated;
//use App\Events\$STUDLY_NAME$WasUpdated;
@ -41,7 +42,7 @@ class $STUDLY_NAME$Repository extends BaseRepository
$query->where('clients.user_id', '=', $userId);
}
*/
return $query;
}

View File

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

View File

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

View File

@ -3,8 +3,9 @@
namespace App\Policies;
use App\Models\User;
use Utils;
use App\Models\User;
use Illuminate\Support\Str;
use Illuminate\Auth\Access\HandlesAuthorization;
/**
@ -16,14 +17,14 @@ class GenericEntityPolicy
/**
* @param User $user
* @param $itemType
* @param $entityType
* @param $ownerUserId
* @return bool|mixed
*/
public static function editByOwner(User $user, $itemType, $ownerUserId) {
$itemType = Utils::getEntityName($itemType);
if (method_exists("App\\Policies\\{$itemType}Policy", 'editByOwner')) {
return call_user_func(["App\\Policies\\{$itemType}Policy", 'editByOwner'], $user, $ownerUserId);
public static function editByOwner(User $user, $entityType, $ownerUserId) {
$className = static::className($entityType);
if (method_exists($className, 'editByOwner')) {
return call_user_func([$className, 'editByOwner'], $user, $ownerUserId);
}
return false;
@ -31,14 +32,14 @@ class GenericEntityPolicy
/**
* @param User $user
* @param $itemType
* @param $entityTypee
* @param $ownerUserId
* @return bool|mixed
*/
public static function viewByOwner(User $user, $itemType, $ownerUserId) {
$itemType = Utils::getEntityName($itemType);
if (method_exists("App\\Policies\\{$itemType}Policy", 'viewByOwner')) {
return call_user_func(["App\\Policies\\{$itemType}Policy", 'viewByOwner'], $user, $ownerUserId);
public static function viewByOwner(User $user, $entityType, $ownerUserId) {
$className = static::className($entityType);
if (method_exists($className, 'viewByOwner')) {
return call_user_func([$className, 'viewByOwner'], $user, $ownerUserId);
}
return false;
@ -46,13 +47,13 @@ class GenericEntityPolicy
/**
* @param User $user
* @param $itemType
* @param $entityType
* @return bool|mixed
*/
public static function create(User $user, $itemType) {
$entityName = Utils::getEntityName($itemType);
if (method_exists("App\\Policies\\{$entityName}Policy", 'create')) {
return call_user_func(["App\\Policies\\{$entityName}Policy", 'create'], $user, $itemType);
public static function create(User $user, $entityType) {
$className = static::className($entityType);
if (method_exists($className, 'create')) {
return call_user_func([$className, 'create'], $user, $entityType);
}
return false;
@ -60,16 +61,28 @@ class GenericEntityPolicy
/**
* @param User $user
* @param $itemType
* @param $entityType
* @return bool|mixed
*/
public static function view(User $user, $itemType) {
$entityName = Utils::getEntityName($itemType);
if (method_exists("App\\Policies\\{$entityName}Policy", 'view')) {
return call_user_func(["App\\Policies\\{$entityName}Policy", 'view'], $user, $itemType);
public static function view(User $user, $entityType) {
$className = static::className($entityType);
if (method_exists($className, 'view')) {
return call_user_func([$className, 'view'], $user, $entityType);
}
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',
'emails' => 'Emails',
'notifications' => 'Notifications',
'datatables' => 'Datatables',
'datatable' => 'Datatables',
'policy' => 'Policies',
],
],
/*