-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "create:repository" command (#21)
- Loading branch information
Showing
11 changed files
with
425 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,5 @@ | |
[ ] --force | ||
[x] custom fields | ||
[ ] multi relations | ||
[ ] multi relations | ||
[ ] create:repository |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Rougin\Combustor\Commands; | ||
|
||
use Rougin\Combustor\Command; | ||
use Rougin\Combustor\Inflector; | ||
|
||
/** | ||
* @package Combustor | ||
* | ||
* @author Rougin Gutib <[email protected]> | ||
*/ | ||
class CreateRepo extends Command | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $name = 'create:repository'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $description = 'Create a new entity repository'; | ||
|
||
/** | ||
* Executes the command. | ||
* | ||
* @return integer | ||
*/ | ||
public function run() | ||
{ | ||
/** @var string */ | ||
$table = $this->getArgument('table'); | ||
|
||
// Create the repository file ------------------- | ||
$name = Inflector::singular($table); | ||
|
||
$name = ucfirst(Inflector::singular($table)); | ||
|
||
$path = $this->path . '/repositories/'; | ||
|
||
if (! is_dir($path)) | ||
{ | ||
mkdir($path); | ||
} | ||
|
||
$file = $path . $name . '_repository.php'; | ||
|
||
$plate = $this->getTemplate(self::TYPE_DOCTRINE); | ||
|
||
$plate = $this->maker->make($plate); | ||
|
||
file_put_contents($file, $plate); | ||
// ---------------------------------------------- | ||
|
||
$this->showPass('Repository successfully created!'); | ||
|
||
return self::RETURN_SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
<?php | ||
|
||
namespace Rougin\Combustor\Template; | ||
|
||
use Rougin\Classidy\Classidy; | ||
use Rougin\Classidy\Method; | ||
use Rougin\Combustor\Inflector; | ||
|
||
/** | ||
* @package Combustor | ||
* | ||
* @author Rougin Gutib <[email protected]> | ||
*/ | ||
class Repository extends Classidy | ||
{ | ||
/** | ||
* @var \Rougin\Describe\Column[] | ||
*/ | ||
protected $cols; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
protected $excluded = array(); | ||
|
||
/** | ||
* @param string $table | ||
* @param \Rougin\Describe\Column[] $cols | ||
* @param string[] $excluded | ||
*/ | ||
public function __construct($table, $cols, $excluded = array()) | ||
{ | ||
$this->cols = $cols; | ||
|
||
$this->excluded = $excluded; | ||
|
||
$this->init($table); | ||
} | ||
|
||
/** | ||
* @param string $table | ||
* | ||
* @return void | ||
*/ | ||
public function init($table) | ||
{ | ||
$model = ucfirst(Inflector::singular($table)); | ||
|
||
$this->setName($model . '_repository'); | ||
$this->extendsTo('Rougin\Credo\Repository'); | ||
|
||
$comment = '@extends \Rougin\Credo\Repository<\\' . $model . '>'; | ||
$this->setComment($comment); | ||
|
||
$this->addClassProperty('db', 'CI_DB_query_builder')->asTag(); | ||
|
||
$method = new Method('create'); | ||
$method->addArrayArgument('data', 'array<string, mixed>'); | ||
$method->addClassArgument('entity', '\\' . $model); | ||
$this->addMethod($method->asTag()); | ||
|
||
$method = new Method('delete'); | ||
$method->addClassArgument('entity', '\\' . $model); | ||
$this->addMethod($method->asTag()); | ||
|
||
$method = new Method('find'); | ||
$method->setReturn('\\' . $model . '|null'); | ||
$method->addIntegerArgument('id'); | ||
$this->addMethod($method->asTag()); | ||
|
||
$method = new Method('get'); | ||
$method->setReturn('\\' . $model . '[]'); | ||
$method->addIntegerArgument('limit', true); | ||
$method->addIntegerArgument('offset', true); | ||
$this->addMethod($method->asTag()); | ||
|
||
$method = new Method('set'); | ||
$method->setReturn('\\' . $model); | ||
$method->addArrayArgument('data', 'array<string, mixed>'); | ||
$method->addClassArgument('entity', '\\' . $model); | ||
$method->addIntegerArgument('id', true); | ||
$this->addMethod($method->asTag()); | ||
|
||
$method = new Method('update'); | ||
$method->addClassArgument('entity', '\\' . $model); | ||
$method->addArrayArgument('data', 'array<string, mixed>'); | ||
$this->addMethod($method->asTag()); | ||
|
||
$this->setExistsMethod(); | ||
|
||
$this->setSetMethod($table); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function setExistsMethod() | ||
{ | ||
$method = new Method('exists'); | ||
|
||
$method->setReturn('boolean'); | ||
|
||
$method->addArrayArgument('data', 'array<string, mixed>'); | ||
|
||
$method->addIntegerArgument('id', true); | ||
|
||
$method->setCodeLine(function ($lines) | ||
{ | ||
$lines[] = '// Specify logic here if applicable ---'; | ||
$lines[] = '// ------------------------------------'; | ||
$lines[] = ''; | ||
$lines[] = 'return false;'; | ||
|
||
return $lines; | ||
}); | ||
|
||
$this->addMethod($method); | ||
} | ||
|
||
/** | ||
* @param string $table | ||
* | ||
* @return void | ||
*/ | ||
protected function setSetMethod($table) | ||
{ | ||
$model = ucfirst(Inflector::singular($table)); | ||
|
||
$method = new Method('set'); | ||
|
||
$method->setReturn($model); | ||
$method->addArrayArgument('data', 'array<string, mixed>'); | ||
$method->addClassArgument('entity', $model); | ||
$method->addIntegerArgument('id', true); | ||
|
||
$cols = $this->cols; | ||
|
||
$method->setCodeLine(function ($lines) use ($cols) | ||
{ | ||
$lines[] = '// List editable fields from table ---'; | ||
|
||
foreach ($cols as $index => $col) | ||
{ | ||
$name = $col->getField(); | ||
|
||
if ($col->isPrimaryKey() || in_array($name, $this->excluded)) | ||
{ | ||
continue; | ||
} | ||
|
||
$type = $col->getDataType(); | ||
|
||
if ($col->isNull()) | ||
{ | ||
$type = $type . '|null'; | ||
} | ||
|
||
$lines[] = '/** @var ' . $type . ' */'; | ||
$lines[] = '$' . $name . ' = $data[\'' . $name . '\'];'; | ||
|
||
if ($col->isNull()) | ||
{ | ||
$lines[] = 'if ($' . $name . ')'; | ||
$lines[] = '{'; | ||
$lines[] = ' $entity->set_' . $name . '($' . $name . ');'; | ||
$lines[] = '}'; | ||
} | ||
else | ||
{ | ||
$lines[] = '$entity->set_' . $name . '($' . $name . ');'; | ||
} | ||
|
||
if (array_key_exists($index + 1, $cols)) | ||
{ | ||
$next = $cols[$index + 1]; | ||
|
||
if (! in_array($next->getField(), $this->excluded)) | ||
{ | ||
$lines[] = ''; | ||
} | ||
} | ||
} | ||
|
||
$lines[] = '// -----------------------------------'; | ||
$lines[] = ''; | ||
$lines[] = 'return $entity;'; | ||
|
||
return $lines; | ||
}); | ||
|
||
$this->addMethod($method); | ||
} | ||
} |
Oops, something went wrong.