Skip to content

Commit 081be99

Browse files
committed
Merge pull request #3 from pionl/master
Fixed 5.1 and added new generators (get/set) and option
2 parents 12bb173 + be4a1f5 commit 081be99

File tree

8 files changed

+309
-2
lines changed

8 files changed

+309
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
22
vendor
3+
.phpintel

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Options:
1717
- --fillable="" Rules for $fillable array columns (default: "")
1818
- --guarded="" Rules for $guarded array columns (default: "ends:_id|ids,equals:id")
1919
- --timestamps="" Rules for $timestamps columns (default: "ends:_at")
20+
- --ignore=""|-i="" A table names to ignore
21+
- --ignoresystem|-s List of system tables (auth, migrations, entrust package)
2022

2123
# Running the generator
2224
```php artisan make:models```

src/Commands/MakeModelsCommand.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace Iber\Generator\Commands;
22

33
use Iber\Generator\Utilities\RuleProcessor;
4+
use Iber\Generator\Utilities\SetGetGenerator;
45
use Iber\Generator\Utilities\VariableConversion;
56
use Symfony\Component\Console\Input\InputOption;
67
use Illuminate\Console\GeneratorCommand;
@@ -63,13 +64,34 @@ class MakeModelsCommand extends GeneratorCommand
6364
*/
6465
protected $timestampRules = 'ends:_at'; //['ends' => ['_at']];
6566

67+
/**
68+
* Contains the template stub for set function
69+
* @var string
70+
*/
71+
protected $setFunctionStub;
72+
/**
73+
* Contains the template stub for get function
74+
* @var string
75+
*/
76+
protected $getFunctionStub;
77+
6678
/**
6779
* Execute the console command.
6880
*
6981
* @return mixed
7082
*/
7183
public function fire()
7284
{
85+
if ($this->option("getset")) {
86+
// load the get/set function stubs
87+
$folder = __DIR__ . '/../stubs/';
88+
89+
$this->setFunctionStub = $this->files->get($folder."setFunction.stub");
90+
$this->getFunctionStub = $this->files->get($folder."getFunction.stub");
91+
}
92+
93+
// create rule processor
94+
7395
$this->ruleProcessor = new RuleProcessor();
7496

7597
$tables = $this->getSchemaTables();
@@ -101,6 +123,24 @@ protected function generateTable($table)
101123
//prefix is the sub-directory within app
102124
$prefix = $this->option('dir');
103125

126+
$ignoreTable = $this->option("ignore");
127+
128+
if ($this->option("ignoresystem")) {
129+
$ignoreSystem = "users,permissions,permission_role,roles,role_user,users,migrations,password_resets";
130+
131+
if (is_string($ignoreTable)) {
132+
$ignoreTable.=",".$ignoreSystem;
133+
} else {
134+
$ignoreTable = $ignoreSystem;
135+
}
136+
}
137+
138+
// if we have ignore tables, we need to find all the posibilites
139+
if (is_string($ignoreTable) && preg_match("/^".$table."|^".$table.",|,".$table.",|,".$table."$/", $ignoreTable)) {
140+
$this->info($table." is ignored");
141+
return;
142+
}
143+
104144
$class = VariableConversion::convertTableNameToClassName($table);
105145

106146
$name = rtrim($this->parseName($prefix . $class), 's');
@@ -135,9 +175,41 @@ protected function replaceTokens($name, $table)
135175
$class = str_replace('{{guarded}}', 'protected $guarded = ' . VariableConversion::convertArrayToString($properties['guarded']) . ';', $class);
136176
$class = str_replace('{{timestamps}}', 'public $timestamps = ' . VariableConversion::convertBooleanToString($properties['timestamps']) . ';', $class);
137177

178+
if ($this->option("getset")) {
179+
$class = $this->replaceTokensWithSetGetFunctions($properties, $class);
180+
}
181+
138182
return $class;
139183
}
140184

185+
/**
186+
* Replaces setters and getters from the stub. The functions are created
187+
* from provider properties.
188+
*
189+
* @param array $properties
190+
* @param string $class
191+
* @return string
192+
*/
193+
protected function replaceTokensWithSetGetFunctions($properties, $class) {
194+
$getters = "";
195+
$setters = "";
196+
197+
$fillableGetSet = new SetGetGenerator($properties['fillable'], $this->getFunctionStub, $this->setFunctionStub);
198+
$getters .= $fillableGetSet->generateGetFunctions();
199+
$setters .= $fillableGetSet->generateSetFunctions();
200+
201+
$guardedGetSet = new SetGetGenerator($properties['guarded'], $this->getFunctionStub, $this->setFunctionStub);
202+
$getters .= $guardedGetSet->generateGetFunctions();
203+
204+
return str_replace([
205+
'{{setters}}',
206+
'{{getters}}'
207+
], [
208+
$setters,
209+
$getters
210+
], $class);
211+
}
212+
141213
/**
142214
* Fill up $fillable/$guarded/$timestamps properties based on table columns.
143215
*
@@ -220,6 +292,10 @@ protected function getOptions()
220292
['fillable', null, InputOption::VALUE_OPTIONAL, 'Rules for $fillable array columns', $this->fillableRules],
221293
['guarded', null, InputOption::VALUE_OPTIONAL, 'Rules for $guarded array columns', $this->guardedRules],
222294
['timestamps', null, InputOption::VALUE_OPTIONAL, 'Rules for $timestamps columns', $this->timestampRules],
295+
['ignore', "i", InputOption::VALUE_OPTIONAL, 'Ignores the tables you define, separated with ,', null],
296+
['ignoresystem', "s", InputOption::VALUE_NONE, 'If you want to ignore system tables.
297+
Just type --ignoresystem or -s'],
298+
['getset', 'm', InputOption::VALUE_NONE, 'Defines if you want to generate set and get methods']
223299
];
224300
}
225301
}

src/Utilities/SetGetGenerator.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
namespace Iber\Generator\Utilities;
3+
4+
use \Illuminate\Filesystem\Filesystem;
5+
6+
class SetGetGenerator {
7+
8+
/**
9+
* Lists of attributes to convert
10+
* @var array
11+
*/
12+
protected $attributes = array();
13+
14+
/**
15+
* Returns a template stub for set function
16+
* @var string
17+
*/
18+
protected $setStub = "";
19+
20+
/**
21+
* Returns a template stub for get function
22+
* @var string
23+
*/
24+
protected $getStub = "";
25+
26+
/**
27+
* [__construct description]
28+
* @param array $attributes with attributes names
29+
* @param string $getStub set stub template
30+
* @param string $setStub get stub template
31+
*/
32+
public function __construct(array $attributes, $getStub, $setStub)
33+
{
34+
$folder = __DIR__ . '/../stubs/';
35+
36+
$this->attributes = $attributes;
37+
$this->getStub = $getStub;
38+
$this->setStub = $setStub;
39+
40+
}
41+
42+
/**
43+
* Returns the get functions in string
44+
* @return string
45+
*/
46+
public function generateGetFunctions() {
47+
return $this->generateWithFunction("createGetFunctionFromAttributeName");
48+
}
49+
50+
/**
51+
* Returns the set functions in string
52+
* @return string
53+
*/
54+
public function generateSetFunctions() {
55+
return $this->generateWithFunction("createSetFunctionFromAttributeName");
56+
}
57+
58+
/**
59+
* Loops the attributes and build the string with given function name
60+
* @param string $function
61+
* @return string
62+
*/
63+
protected function generateWithFunction($function) {
64+
$string = "";
65+
66+
foreach ($this->attributes as $attributeName) {
67+
$string .= $this->$function($attributeName);
68+
}
69+
70+
return $string;
71+
}
72+
73+
/**
74+
* Bulds the get function for the attribute name
75+
* @param string $attributeName
76+
* @return string
77+
*/
78+
public function createGetFunctionFromAttributeName($attributeName) {
79+
return $this->createFunctionFromAttributeName("get", $attributeName, $this->getStub);
80+
}
81+
82+
/**
83+
* Bulds the set function for the attribute name
84+
* @param string $attributeName
85+
* @return string
86+
*/
87+
public function createSetFunctionFromAttributeName($attributeName) {
88+
return $this->createFunctionFromAttributeName("set", $attributeName, $this->setStub);
89+
}
90+
91+
/**
92+
* Builds the funciton and creates the function from the stub template
93+
* @param string $prefixFunction
94+
* @param string $attributeName
95+
* @param string $stubTemplate
96+
* @return string
97+
*/
98+
protected function createFunctionFromAttributeName($prefixFunction, $attributeName, $stubTemplate) {
99+
$function = $this->attributeNameToFunction($prefixFunction, $attributeName);
100+
101+
// change to stub?
102+
103+
return $this->createAttributeFunction($stubTemplate, $function, $attributeName);
104+
}
105+
106+
/**
107+
* Replaces the stub template with the data
108+
* @param string $stubTemplate
109+
* @param string $function
110+
* @param string $attributeName
111+
* @return string
112+
*/
113+
protected function createAttributeFunction($stubTemplate, $function, $attributeName) {
114+
return str_replace([
115+
"{{ attribute }}",
116+
"{{ function }}"
117+
], [
118+
$attributeName,
119+
$function
120+
], $stubTemplate);
121+
}
122+
123+
/**
124+
* Converts the given string to function. Support database names (underscores)
125+
* @param string $prefixFunction desired function prefix (get/set)
126+
* @param string $str attribute name
127+
* @param array $noStrip
128+
* @return string
129+
*/
130+
public function attributeNameToFunction($prefixFunction, $str, array $noStrip = array())
131+
{
132+
// non-alpha and non-numeric characters become spaces
133+
$str = preg_replace('/[^a-z0-9' . implode("", $noStrip) . ']+/i', ' ', $str);
134+
$str = trim($str);
135+
// uppercase the first character of each word
136+
$str = ucwords($str);
137+
$str = str_replace(" ", "", $str);
138+
$str = ucfirst($str);
139+
140+
return $prefixFunction.$str;
141+
}
142+
}

src/stubs/getFunction.stub

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
/**
3+
* @return mixed
4+
*/
5+
public function {{ function }}() {
6+
return $this->{{ attribute }};
7+
}

src/stubs/model.stub

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
<?php namespace {{namespace}};
1+
<?php namespace DummyNamespace;
22

33
use Illuminate\Database\Eloquent\Model;
44

5-
class {{class}} extends {{extends}} {
5+
class DummyClass extends {{extends}} {
66

77
{{timestamps}}
88

99
{{fillable}}
1010

1111
{{guarded}}
12+
13+
{{getters}}
14+
15+
{{setters}}
1216
}

src/stubs/setFunction.stub

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
/**
3+
* @param $value
4+
* @return $this
5+
*/
6+
public function {{ function }}($value) {
7+
$this->{{ attribute }} = $value;
8+
return $this;
9+
}

tests/SetGetGeneratorTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
use \Illuminate\Filesystem\Filesystem;
4+
5+
class SetGetGeneratorTest extends PHPUnit_Framework_TestCase
6+
{
7+
protected $generator;
8+
9+
protected $setFunctionStub;
10+
protected $getFunctionStub;
11+
12+
public function __construct() {
13+
// load only once
14+
$this->getFunctionStub = file_get_contents("src/stubs/getFunction.stub");
15+
$this->setFunctionStub = file_get_contents("src/stubs/setFunction.stub");
16+
}
17+
18+
public function setUp() {
19+
$this->generator = new Iber\Generator\Utilities\SetGetGenerator([
20+
"attribute_name",
21+
"test"
22+
], $this->getFunctionStub, $this->setFunctionStub);
23+
}
24+
25+
26+
public function testCamelCaseFunction()
27+
{
28+
$this->assertEquals("getTest",$this->generator->attributeNameToFunction("get", "test"));
29+
30+
$this->assertEquals("getTestName",$this->generator->attributeNameToFunction("get", "test_name"));
31+
32+
$this->assertEquals("getTestname",$this->generator->attributeNameToFunction("get", "testname"));
33+
34+
$this->assertEquals("getTestName",$this->generator->attributeNameToFunction("get", "test name"));
35+
}
36+
37+
38+
public function testCreateGetFunctionFromAttributeName() {
39+
$function = $this->generator->createGetFunctionFromAttributeName("test");
40+
$this->assertContains("getTest", $function);
41+
$this->assertContains('$this->test', $function);
42+
}
43+
44+
public function testCreateSetFunctionFromAttributeName() {
45+
$function = $this->generator->createSetFunctionFromAttributeName("test");
46+
$this->assertContains("setTest", $function);
47+
$this->assertContains('$this->attributes', $function);
48+
$this->assertContains('"test"', $function);
49+
}
50+
51+
public function testGenerateGet() {
52+
$text = $this->generator->generateGetFunctions();
53+
$this->assertNotEquals("", $text);
54+
$this->assertNotContains("setTest", $text);
55+
$this->assertContains("getTest", $text);
56+
$this->assertContains("getAttributeName", $text);
57+
}
58+
59+
public function testGenerateSet() {
60+
$text = $this->generator->generateSetFunctions();
61+
$this->assertNotEquals("", $text);
62+
$this->assertNotContains("getTest", $text);
63+
$this->assertContains("setTest", $text);
64+
$this->assertContains("setAttributeName", $text);
65+
}
66+
}

0 commit comments

Comments
 (0)