Skip to content

Commit

Permalink
add publish command
Browse files Browse the repository at this point in the history
  • Loading branch information
komeylkalbali committed Mar 13, 2021
1 parent 5ed8aa2 commit 189af89
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 67 deletions.
63 changes: 4 additions & 59 deletions src/Classes/ControllerGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\PhpNamespace;
use Shetabit\ModuleGenerator\Helpers\Helper;
Expand All @@ -27,10 +28,7 @@ class ControllerGenerator
*/
protected $baseRelationName;
protected $attributes;
<<<<<<< HEAD
=======
protected $config;
>>>>>>> 73a07f9... first commit

public function __construct($module, $models)
{
Expand Down Expand Up @@ -80,18 +78,6 @@ public function generateControllerTemplates($option): PhpNamespace

public function setMethodToController($class, $option, $namespace)
{
<<<<<<< HEAD
if (strpos($option , 'R') == true) {
$this->indexAndShowMethodGenerator($class);
}
if (strpos($option, 'C') == true) {
$this->createAndStoreMethodGenerator($class);
}
if (strpos($option, 'U') == true) {
$this->editAndUpdateMethodGenerator($class, $namespace);
}
if (strpos($option, 'D') == true) {
=======
if (str_contains($option , 'R')) {
$this->indexAndShowMethodGenerator($class);
}
Expand All @@ -102,7 +88,6 @@ public function setMethodToController($class, $option, $namespace)
$this->editAndUpdateMethodGenerator($class , $namespace);
}
if (str_contains($option, 'D')) {
>>>>>>> 73a07f9... first commit
$this->destroyMethodGenerator($class);
}
}
Expand All @@ -112,16 +97,6 @@ public function indexAndShowMethodGenerator(classType $class)
$method = $class->addMethod('index');
if (key_exists('Relations', $this->attributes)) {
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::withCommonRelations()->get();' . PHP_EOL)
<<<<<<< HEAD
->addBody('return response()->json($' . strtolower($this->modelName) . 's);');
} else {
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::query()->get();' . PHP_EOL)
->addBody('return response()->json($' . strtolower($this->modelName) . 's);');
}
$class->addMethod('show')
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
->addBody('return response()->json($' . strtolower($this->modelName) . ');')
=======
->addBody($this->config['return']);
} else {
$method->addBody('$' . strtolower($this->modelName) . 's = ' . ucfirst($this->modelName) . '::query()->get();' . PHP_EOL)
Expand All @@ -130,7 +105,6 @@ public function indexAndShowMethodGenerator(classType $class)
$class->addMethod('show')
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
->addBody($this->config['return'])
>>>>>>> 73a07f9... first commit
->addParameter('id')->setType('Int');
}

Expand Down Expand Up @@ -159,12 +133,9 @@ public function associateInStore($method)
{
if (key_exists('Relations', $this->attributes)) {
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
<<<<<<< HEAD
=======
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
return '';
}
>>>>>>> 73a07f9... first commit
foreach ($relations as $value) {
$this->baseRelationName = explode('::', $value)[1];
$this->relationName = Helper::configurationRelationsName($this->baseRelationName, $typeRelation);
Expand All @@ -174,36 +145,23 @@ public function associateInStore($method)
}
}

<<<<<<< HEAD
public function editAndUpdateMethodGenerator(ClassType $class, $namespace)
=======

public function editAndUpdateMethodGenerator(ClassType $class , $namespace)
>>>>>>> 73a07f9... first commit
{
$method = $class->addMethod('edit');
if (key_exists('Relations', $this->attributes)) {
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::withCommonRelations()->findOrFail($id);' . PHP_EOL)
<<<<<<< HEAD
->addBody('return response()->json($' . strtolower($this->modelName) . ');');
} else {
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
->addBody('return response()->json($' . strtolower($this->modelName) . ');');
=======
->addBody($this->config['return']);
} else {
$method->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);' . PHP_EOL)
->addBody($this->config['return']);
>>>>>>> 73a07f9... first commit
};
$method->addParameter('id')->setType('Int');

$method = $class->addMethod('update')
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::query()->findOrFail($id);');
<<<<<<< HEAD
$this->UpdateMethodFindIntoRelation($method, $namespace);
=======

$this->UpdateMethodFindIntoRelation($method , $namespace);
>>>>>>> 73a07f9... first commit
$this->associateInUpdate($method);
$method->addBody('$' . strtolower($this->modelName) . '->fill($request->all());')
->addBody('$' . strtolower($this->modelName) . '->save();'.PHP_EOL)
Expand All @@ -215,20 +173,14 @@ public function editAndUpdateMethodGenerator(ClassType $class , $namespace)
$method->addParameter('id')->setType('Int');
}

<<<<<<< HEAD
public function UpdateMethodFindIntoRelation($method, $namespace)
{
if (key_exists('Relations', $this->attributes)) {
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
=======

public function UpdateMethodFindIntoRelation($method ,$namespace)
{
if (key_exists('Relations', $this->attributes)) {
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
return '';
}
>>>>>>> 73a07f9... first commit
foreach ($relations as $value) {
$this->baseRelationName = explode('::', $value)[1];
$method->addBody('$' . strtolower($this->baseRelationName) . ' = ' . ucfirst($this->baseRelationName) . '::query()->findOrFail($request->' . strtolower($this->baseRelationName) . '_id);');
Expand All @@ -242,12 +194,9 @@ public function associateInUpdate($method)
{
if (key_exists('Relations', $this->attributes)) {
foreach ($this->attributes['Relations'] as $typeRelation => $relations) {
<<<<<<< HEAD
=======
if (!is_array($relations) && Str::camel($relations) == 'morphTo'){
return '';
}
>>>>>>> 73a07f9... first commit
foreach ($relations as $value) {
$this->baseRelationName = explode('::', $value)[1];
$this->relationName = Helper::configurationRelationsName($this->baseRelationName, $typeRelation);
Expand All @@ -261,11 +210,7 @@ public function destroyMethodGenerator(ClassType $class)
{
$class->addMethod('destroy')
->addBody('$' . strtolower($this->modelName) . ' = ' . ucfirst($this->modelName) . '::destroy($id);' . PHP_EOL)
<<<<<<< HEAD
->addBody('return response()->json($' . strtolower($this->modelName) . ');')
=======
->addBody($this->config['return'])
>>>>>>> 73a07f9... first commit
->addParameter('id')->setType('Int');
}

Expand Down
7 changes: 1 addition & 6 deletions src/Classes/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ class ModelGenerator
protected $module;
protected $pathOfModel;
protected array $withRelation = [];
<<<<<<< HEAD
=======
protected $modelName;
>>>>>>> 73a07f9... first commit


public function __construct($module , $models)
{
Expand Down Expand Up @@ -131,8 +129,6 @@ public function addWithCommonRelations(ClassType $class)
->addBody('}')
->addParameter('query');
}
<<<<<<< HEAD
=======

public function morphRelation($class)
{
Expand All @@ -142,5 +138,4 @@ public function morphRelation($class)
->addBody('return $this->morphTo();')
->setReturnType('Illuminate\Database\Eloquent\Relations\morphTo');
}
>>>>>>> 73a07f9... first commit
}
2 changes: 0 additions & 2 deletions src/Commands/GenerateModuleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ public function handle()
die();
}

$this->callSilent('vendor:publish', ['--tag' => 'config' ,'--force' => true]);

$processes = count(\config()->get('modulegenerator') , COUNT_RECURSIVE);
$Progress = $this->getOutput()->createProgressBar($processes);
$Progress->setBarCharacter("/");
Expand Down
49 changes: 49 additions & 0 deletions src/Commands/PublishModuleGeneratorCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Shetabit\ModuleGenerator\Commands;


use Illuminate\Console\Command;
use Shetabit\ModuleGenerator\Contracts\ModuleGenerator;
use Symfony\Component\Console\Input\InputArgument;

class PublishModuleGeneratorCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'generator:publish';

/**
* The console command description.
*
* @var string
*/
protected $description = 'create your module';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();

}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->info("publishing ...");
$this->callSilent('vendor:publish', ['--tag' => 'config' ,'--force' => true]);
$this->info("publish successfully");

}
}
2 changes: 2 additions & 0 deletions src/Provider/GeneratorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\ServiceProvider;
use Shetabit\ModuleGenerator\Commands\GenerateModuleCommand;
use Shetabit\ModuleGenerator\Commands\PublishModuleGeneratorCommand;
use Shetabit\ModuleGenerator\Contracts\ControllerGenerator;
use Shetabit\ModuleGenerator\Contracts\ForeignKeyGenerator;
use Shetabit\ModuleGenerator\Contracts\MigrationGenerator;
Expand Down Expand Up @@ -44,6 +45,7 @@ public function boot()
if ($this->app->runningInConsole()) {
$this->commands([
GenerateModuleCommand::class,
PublishModuleGeneratorCommand::class
]);
}

Expand Down

0 comments on commit 189af89

Please sign in to comment.