mirror of
https://github.com/ZorgCC/lumen-generators.git
synced 2024-12-27 06:25:28 +03:00
added --has-many option to model command
This commit is contained in:
parent
558ebd5f0a
commit
04ce3077cd
14
formats/relations.json
Normal file
14
formats/relations.json
Normal 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
0
lumen-test/artisan
Normal file → Executable file
8
lumen-test/composer.lock
generated
8
lumen-test/composer.lock
generated
@ -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": [],
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -7,6 +7,8 @@
|
||||
class_name: AcceptanceTester
|
||||
modules:
|
||||
enabled:
|
||||
- PhpBrowser:
|
||||
url: http://localhost/myapp
|
||||
- \Helper\Acceptance
|
||||
# - PhpBrowser:
|
||||
# url: http://localhost/myapp
|
||||
- \Helper\Acceptance
|
||||
- Cli
|
||||
- Filesystem
|
86
lumen-test/tests/acceptance/ModelCommandCept.php
Normal file
86
lumen-test/tests/acceptance/ModelCommandCept.php
Normal 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");
|
||||
}
|
||||
|
||||
}
|
||||
');
|
11
lumen-test/tests/tmp/TestingModel.php
Normal file
11
lumen-test/tests/tmp/TestingModel.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php namespace Tests\Tmp;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class TestingModel extends Model {
|
||||
|
||||
protected $fillable = [];
|
||||
|
||||
protected $dates = ["started_at"];
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -7,5 +7,7 @@ class {{name}} extends Model {
|
||||
protected $fillable = [{{fillable}}];
|
||||
|
||||
protected $dates = [{{dates}}];
|
||||
|
||||
{{relations}}
|
||||
|
||||
}
|
||||
|
4
templates/model/relation.wnt
Normal file
4
templates/model/relation.wnt
Normal file
@ -0,0 +1,4 @@
|
||||
public function {{name}}()
|
||||
{
|
||||
return $this->{{type}}('{{model}}');
|
||||
}
|
Loading…
Reference in New Issue
Block a user