mirror of
https://github.com/ZorgCC/lumen-generators.git
synced 2025-02-10 13:48:52 +03:00
wn:model command done
This commit is contained in:
parent
4a60029604
commit
96b1866e35
9
formats/rules.json
Normal file
9
formats/rules.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"type": "array",
|
||||||
|
"separator": " ",
|
||||||
|
"fields": {
|
||||||
|
"type": "object",
|
||||||
|
"separator": "=",
|
||||||
|
"fields": ["name","rule"]
|
||||||
|
}
|
||||||
|
}
|
@ -1,21 +0,0 @@
|
|||||||
<?php namespace App;
|
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
|
||||||
|
|
||||||
class Test extends Model {
|
|
||||||
|
|
||||||
protected $fillable = [];
|
|
||||||
|
|
||||||
protected $dates = [];
|
|
||||||
|
|
||||||
public function users()
|
|
||||||
{
|
|
||||||
return $this->hasMany('App\User');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function number()
|
|
||||||
{
|
|
||||||
return $this->hasMany('Phone');
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
8
lumen-test/composer.lock
generated
8
lumen-test/composer.lock
generated
@ -3699,12 +3699,12 @@
|
|||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/webNeat/lumen-generators.git",
|
"url": "https://github.com/webNeat/lumen-generators.git",
|
||||||
"reference": "04ce3077cd2723b38424e1ea94844582b67c6b79"
|
"reference": "4a60029604f1b12d81e135761dc255fe2f500b04"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/webNeat/lumen-generators/zipball/04ce3077cd2723b38424e1ea94844582b67c6b79",
|
"url": "https://api.github.com/repos/webNeat/lumen-generators/zipball/4a60029604f1b12d81e135761dc255fe2f500b04",
|
||||||
"reference": "04ce3077cd2723b38424e1ea94844582b67c6b79",
|
"reference": "4a60029604f1b12d81e135761dc255fe2f500b04",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
@ -3736,7 +3736,7 @@
|
|||||||
"lumen",
|
"lumen",
|
||||||
"rest"
|
"rest"
|
||||||
],
|
],
|
||||||
"time": "2015-09-23 03:58:39"
|
"time": "2015-09-23 04:38:56"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"aliases": [],
|
"aliases": [],
|
||||||
|
@ -6,6 +6,7 @@ $I->runShellCommand('php artisan wn:model TestingModel --path=tests/tmp');
|
|||||||
$I->seeInShellOutput('Model TestingModel Generated');
|
$I->seeInShellOutput('Model TestingModel Generated');
|
||||||
$I->seeFileFound('./tests/tmp/TestingModel.php');
|
$I->seeFileFound('./tests/tmp/TestingModel.php');
|
||||||
$I->openFile('./tests/tmp/TestingModel.php');
|
$I->openFile('./tests/tmp/TestingModel.php');
|
||||||
|
|
||||||
$I->seeFileContentsEqual('<?php namespace Tests\Tmp;
|
$I->seeFileContentsEqual('<?php namespace Tests\Tmp;
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
@ -16,6 +17,12 @@ class TestingModel extends Model {
|
|||||||
|
|
||||||
protected $dates = [];
|
protected $dates = [];
|
||||||
|
|
||||||
|
public $rules = [
|
||||||
|
// Validation rules
|
||||||
|
];
|
||||||
|
|
||||||
|
// Relationships
|
||||||
|
|
||||||
}
|
}
|
||||||
');
|
');
|
||||||
|
|
||||||
@ -24,63 +31,48 @@ $I->runShellCommand('php artisan wn:model TestingModel --fillable=name,title --p
|
|||||||
$I->seeInShellOutput('Model TestingModel Generated');
|
$I->seeInShellOutput('Model TestingModel Generated');
|
||||||
$I->seeFileFound('./tests/tmp/TestingModel.php');
|
$I->seeFileFound('./tests/tmp/TestingModel.php');
|
||||||
$I->openFile('./tests/tmp/TestingModel.php');
|
$I->openFile('./tests/tmp/TestingModel.php');
|
||||||
$I->seeFileContentsEqual('<?php namespace Tests\Tmp;
|
$I->seeInThisFile('protected $fillable = ["name", "title"];');
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
|
||||||
|
|
||||||
class TestingModel extends Model {
|
|
||||||
|
|
||||||
protected $fillable = ["name", "title"];
|
|
||||||
|
|
||||||
protected $dates = [];
|
|
||||||
|
|
||||||
}
|
|
||||||
');
|
|
||||||
|
|
||||||
$I->wantTo('generate a model with dates fields');
|
$I->wantTo('generate a model with dates fields');
|
||||||
$I->runShellCommand('php artisan wn:model TestingModel --dates=started_at --path=tests/tmp');
|
$I->runShellCommand('php artisan wn:model TestingModel --dates=started_at --path=tests/tmp');
|
||||||
$I->seeInShellOutput('Model TestingModel Generated');
|
$I->seeInShellOutput('Model TestingModel Generated');
|
||||||
$I->seeFileFound('./tests/tmp/TestingModel.php');
|
$I->seeFileFound('./tests/tmp/TestingModel.php');
|
||||||
$I->openFile('./tests/tmp/TestingModel.php');
|
$I->openFile('./tests/tmp/TestingModel.php');
|
||||||
$I->seeFileContentsEqual('<?php namespace Tests\Tmp;
|
$I->seeInThisFile('protected $dates = ["started_at"];');
|
||||||
|
|
||||||
use Illuminate\Database\Eloquent\Model;
|
|
||||||
|
|
||||||
class TestingModel extends Model {
|
|
||||||
|
|
||||||
protected $fillable = [];
|
|
||||||
|
|
||||||
protected $dates = ["started_at"];
|
|
||||||
|
|
||||||
}
|
|
||||||
');
|
|
||||||
|
|
||||||
$I->wantTo('generate a model with relations');
|
$I->wantTo('generate a model with relations');
|
||||||
$I->runShellCommand('php artisan wn:model TestingModel --has-many=accounts,friends:App\User,numbers:Phone --path=tests/tmp');
|
$I->runShellCommand('php artisan wn:model TestingModel --has-many=accounts --belongs-to="owner:App\User" --has-one=number:Phone --path=tests/tmp');
|
||||||
$I->seeInShellOutput('Model TestingModel Generated');
|
$I->seeInShellOutput('Model TestingModel Generated');
|
||||||
$I->seeFileFound('./tests/tmp/TestingModel.php');
|
$I->seeFileFound('./tests/tmp/TestingModel.php');
|
||||||
$I->openFile('./tests/tmp/TestingModel.php');
|
$I->openFile('./tests/tmp/TestingModel.php');
|
||||||
$I->seeFileContentsEqual('<?php namespace Tests\Tmp;
|
$I->seeInThisFile('
|
||||||
|
public function accounts()
|
||||||
use Illuminate\Database\Eloquent\Model;
|
{
|
||||||
|
|
||||||
class TestingModel extends Model {
|
|
||||||
|
|
||||||
protected $fillable = [];
|
|
||||||
|
|
||||||
protected $dates = [];
|
|
||||||
|
|
||||||
public function accounts(){
|
|
||||||
return $this->hasMany("Tests\\Tmp\\Account");
|
return $this->hasMany("Tests\\Tmp\\Account");
|
||||||
}
|
}
|
||||||
|
');
|
||||||
public function friends(){
|
$I->seeInThisFile('
|
||||||
return $this->hasMany("App\\User");
|
public function owner()
|
||||||
}
|
{
|
||||||
|
return $this->belongsTo("App\\User");
|
||||||
public function numbers(){
|
}
|
||||||
return $this->hasMany("Tests\\Tmp\\Phone");
|
');
|
||||||
}
|
$I->seeInThisFile('
|
||||||
|
public function number()
|
||||||
}
|
{
|
||||||
|
return $this->hasOne("Tests\\Tmp\\Phone");
|
||||||
|
}
|
||||||
|
');
|
||||||
|
|
||||||
|
$I->wantTo('generate a model with validation rules');
|
||||||
|
$I->runShellCommand('php artisan wn:model TestingModel --rules="name=required age=integer|min:13 email=email|unique:users,email_address" --path=tests/tmp');
|
||||||
|
$I->seeInShellOutput('Model TestingModel Generated');
|
||||||
|
$I->seeFileFound('./tests/tmp/TestingModel.php');
|
||||||
|
$I->openFile('./tests/tmp/TestingModel.php');
|
||||||
|
$I->seeInThisFile('
|
||||||
|
public $rules = [
|
||||||
|
"name" => "required",
|
||||||
|
"age" => "integer|min:13",
|
||||||
|
"email" => "email|unique:users,email_address",
|
||||||
|
];
|
||||||
');
|
');
|
@ -8,6 +8,12 @@ class TestingModel extends Model {
|
|||||||
|
|
||||||
protected $dates = [];
|
protected $dates = [];
|
||||||
|
|
||||||
|
public $rules = [
|
||||||
|
"name" => "required",
|
||||||
|
"age" => "integer|min:13",
|
||||||
|
"email" => "email|unique:users,email_address",
|
||||||
|
];
|
||||||
|
|
||||||
|
// Relationships
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,11 +4,14 @@
|
|||||||
class ModelCommand extends BaseCommand {
|
class ModelCommand extends BaseCommand {
|
||||||
|
|
||||||
protected $signature = 'wn:model
|
protected $signature = 'wn:model
|
||||||
{name : Name of the model}
|
{name : Name of the model.}
|
||||||
{--fillable= : the fillable fields of the model}
|
{--fillable= : the fillable fields.}
|
||||||
{--dates= : date fields of the model}
|
{--dates= : date fields.}
|
||||||
{--has-many= : on-to-many relationships of the model}
|
{--has-many= : hasMany relationships.}
|
||||||
{--path=app : where to store the model php file}';
|
{--has-one= : hasOne relationships.}
|
||||||
|
{--belongs-to= : belongsTo relationships.}
|
||||||
|
{--rules= : fields validation rules.}
|
||||||
|
{--path=app : where to store the model php file.}';
|
||||||
|
|
||||||
protected $description = 'Generates a model class for a RESTfull resource';
|
protected $description = 'Generates a model class for a RESTfull resource';
|
||||||
|
|
||||||
@ -25,7 +28,8 @@ class ModelCommand extends BaseCommand {
|
|||||||
'namespace' => $this->getNamespace(),
|
'namespace' => $this->getNamespace(),
|
||||||
'fillable' => $this->getAsArrayFields('fillable'),
|
'fillable' => $this->getAsArrayFields('fillable'),
|
||||||
'dates' => $this->getAsArrayFields('dates'),
|
'dates' => $this->getAsArrayFields('dates'),
|
||||||
'relations' => $this->getRelations()
|
'relations' => $this->getRelations(),
|
||||||
|
'rules' => $this->getRules()
|
||||||
])
|
])
|
||||||
->get();
|
->get();
|
||||||
|
|
||||||
@ -55,10 +59,16 @@ class ModelCommand extends BaseCommand {
|
|||||||
protected function getRelations()
|
protected function getRelations()
|
||||||
{
|
{
|
||||||
$relations = array_merge([],
|
$relations = array_merge([],
|
||||||
$this->getRelationsByType('hasMany', 'has-many')
|
$this->getRelationsByType('hasOne', 'has-one'),
|
||||||
|
$this->getRelationsByType('hasMany', 'has-many'),
|
||||||
|
$this->getRelationsByType('belongsTo', 'belongs-to')
|
||||||
);
|
);
|
||||||
|
|
||||||
return implode("\n\n", $relations);
|
if(empty($relations)){
|
||||||
|
return "\t// Relationships";
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode(PHP_EOL, $relations);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getRelationsByType($type, $option)
|
protected function getRelationsByType($type, $option)
|
||||||
@ -81,4 +91,22 @@ class ModelCommand extends BaseCommand {
|
|||||||
}
|
}
|
||||||
return $relations;
|
return $relations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getRules()
|
||||||
|
{
|
||||||
|
$rules = $this->option('rules');
|
||||||
|
if(! $rules){
|
||||||
|
return "\t\t// Validation rules";
|
||||||
|
}
|
||||||
|
$parser = $this->getArgumentParser('rules');
|
||||||
|
$template = $this->getTemplate('model/rule');
|
||||||
|
$items = $parser->parse($rules);
|
||||||
|
$rules = [];
|
||||||
|
foreach ($items as $item) {
|
||||||
|
$rules[] = $template->with($item)->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode(PHP_EOL, $rules);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -8,6 +8,10 @@ class {{name}} extends Model {
|
|||||||
|
|
||||||
protected $dates = [{{dates}}];
|
protected $dates = [{{dates}}];
|
||||||
|
|
||||||
|
public $rules = [
|
||||||
|
{{rules}}
|
||||||
|
];
|
||||||
|
|
||||||
{{relations}}
|
{{relations}}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
public function {{name}}()
|
public function {{name}}()
|
||||||
{
|
{
|
||||||
return $this->{{type}}('{{model}}');
|
return $this->{{type}}("{{model}}");
|
||||||
}
|
}
|
1
templates/model/rule.wnt
Normal file
1
templates/model/rule.wnt
Normal file
@ -0,0 +1 @@
|
|||||||
|
"{{name}}" => "{{rule}}",
|
Loading…
x
Reference in New Issue
Block a user