added --has-many option to model command

This commit is contained in:
Amine Ben hammou 2015-09-23 04:58:39 +01:00
parent 558ebd5f0a
commit 04ce3077cd
10 changed files with 342 additions and 1719 deletions

14
formats/relations.json Normal file
View File

@ -0,0 +1,14 @@
{
"type": "array",
"format": {
"type": "object",
"fields": [
"name",
{
"name": "model",
"type": "string",
"default": false
}
]
}
}

0
lumen-test/artisan Normal file → Executable file
View File

View File

@ -3699,12 +3699,12 @@
"source": {
"type": "git",
"url": "https://github.com/webNeat/lumen-generators.git",
"reference": "bde59982cd78e12fdbb0d0187aa8b8ad1e12e478"
"reference": "558ebd5f0a9e2c7e9043d9bff5073873057c5df9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/webNeat/lumen-generators/zipball/bde59982cd78e12fdbb0d0187aa8b8ad1e12e478",
"reference": "bde59982cd78e12fdbb0d0187aa8b8ad1e12e478",
"url": "https://api.github.com/repos/webNeat/lumen-generators/zipball/558ebd5f0a9e2c7e9043d9bff5073873057c5df9",
"reference": "558ebd5f0a9e2c7e9043d9bff5073873057c5df9",
"shasum": ""
},
"require": {
@ -3736,7 +3736,7 @@
"lumen",
"rest"
],
"time": "2015-09-23 01:39:13"
"time": "2015-09-23 02:22:02"
}
],
"aliases": [],

View File

@ -7,6 +7,8 @@
class_name: AcceptanceTester
modules:
enabled:
- PhpBrowser:
url: http://localhost/myapp
# - PhpBrowser:
# url: http://localhost/myapp
- \Helper\Acceptance
- Cli
- Filesystem

View File

@ -0,0 +1,86 @@
<?php
$I = new AcceptanceTester($scenario);
$I->wantTo('generate a model without fillable fields or dates');
$I->runShellCommand('php artisan wn:model TestingModel --path=tests/tmp');
$I->seeInShellOutput('Model TestingModel Generated');
$I->seeFileFound('./tests/tmp/TestingModel.php');
$I->openFile('./tests/tmp/TestingModel.php');
$I->seeFileContentsEqual('<?php namespace Tests\Tmp;
use Illuminate\Database\Eloquent\Model;
class TestingModel extends Model {
protected $fillable = [];
protected $dates = [];
}
');
$I->wantTo('generate a model with fillable fields');
$I->runShellCommand('php artisan wn:model TestingModel --fillable=name,title --path=tests/tmp');
$I->seeInShellOutput('Model TestingModel Generated');
$I->seeFileFound('./tests/tmp/TestingModel.php');
$I->openFile('./tests/tmp/TestingModel.php');
$I->seeFileContentsEqual('<?php namespace Tests\Tmp;
use Illuminate\Database\Eloquent\Model;
class TestingModel extends Model {
protected $fillable = ["name", "title"];
protected $dates = [];
}
');
$I->wantTo('generate a model with dates fields');
$I->runShellCommand('php artisan wn:model TestingModel --dates=started_at --path=tests/tmp');
$I->seeInShellOutput('Model TestingModel Generated');
$I->seeFileFound('./tests/tmp/TestingModel.php');
$I->openFile('./tests/tmp/TestingModel.php');
$I->seeFileContentsEqual('<?php namespace Tests\Tmp;
use Illuminate\Database\Eloquent\Model;
class TestingModel extends Model {
protected $fillable = [];
protected $dates = ["started_at"];
}
');
$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->seeInShellOutput('Model TestingModel Generated');
$I->seeFileFound('./tests/tmp/TestingModel.php');
$I->openFile('./tests/tmp/TestingModel.php');
$I->seeFileContentsEqual('<?php namespace Tests\Tmp;
use Illuminate\Database\Eloquent\Model;
class TestingModel extends Model {
protected $fillable = [];
protected $dates = [];
public function accounts(){
return $this->hasMany("Tests\\Tmp\\Account");
}
public function friends(){
return $this->hasMany("App\\User");
}
public function numbers(){
return $this->hasMany("Tests\\Tmp\\Phone");
}
}
');

View File

@ -0,0 +1,11 @@
<?php namespace Tests\Tmp;
use Illuminate\Database\Eloquent\Model;
class TestingModel extends Model {
protected $fillable = [];
protected $dates = ["started_at"];
}

View File

@ -7,6 +7,7 @@ class ModelCommand extends BaseCommand {
{name : Name of the model}
{--fillable= : the fillable fields of the model}
{--dates= : date fields of the model}
{--has-many= : on-to-many relationships of the model}
{--path=app : where to store the model php file}';
protected $description = 'Generates a model class for a RESTfull resource';
@ -23,7 +24,8 @@ class ModelCommand extends BaseCommand {
'name' => $name,
'namespace' => $this->getNamespace(),
'fillable' => $this->getAsArrayFields('fillable'),
'dates' => $this->getAsArrayFields('dates')
'dates' => $this->getAsArrayFields('dates'),
'relations' => $this->getRelations()
])
->get();
@ -50,4 +52,31 @@ class ModelCommand extends BaseCommand {
return str_replace(' ', '\\', ucwords(str_replace('/', ' ', $this->option('path'))));
}
protected function getRelations()
{
$relations = array_merge([],
$this->getRelationsByType('hasMany', 'has-many')
);
return implode("\n\n", $relations);
}
protected function getRelationsByType($type, $option)
{
$relations = [];
$option = $this->option($option);
if($option){
$parser = $this->getArgumentParser('relations');
$template = $this->getTemplate('model/relation');
$items = $parser->parse($option);
foreach ($items as $item) {
$item['type'] = $type;
if(! $item['model']){
$item['model'] = $this->getNamespace() . '\\' . ucwords(str_singular($item['name']));
}
$relations[] = $template->with($item)->get();
}
}
return $relations;
}
}

View File

@ -8,4 +8,6 @@ class {{name}} extends Model {
protected $dates = [{{dates}}];
{{relations}}
}

View File

@ -0,0 +1,4 @@
public function {{name}}()
{
return $this->{{type}}('{{model}}');
}