diff --git a/.gitignore b/.gitignore index 3ce5adb..6858055 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea vendor +.phpintel \ No newline at end of file diff --git a/README.md b/README.md index c472e2e..fb0fbaf 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ Options: - --fillable="" Rules for $fillable array columns (default: "") - --guarded="" Rules for $guarded array columns (default: "ends:_id|ids,equals:id") - --timestamps="" Rules for $timestamps columns (default: "ends:_at") + - --ignore=""|-i="" A table names to ignore + - --ignoresystem|-s List of system tables (auth, migrations, entrust package) # Running the generator ```php artisan make:models``` diff --git a/src/Commands/MakeModelsCommand.php b/src/Commands/MakeModelsCommand.php index 490bab3..1923033 100644 --- a/src/Commands/MakeModelsCommand.php +++ b/src/Commands/MakeModelsCommand.php @@ -1,6 +1,7 @@ ['_at']]; + /** + * Contains the template stub for set function + * @var string + */ + protected $setFunctionStub; + /** + * Contains the template stub for get function + * @var string + */ + protected $getFunctionStub; + /** * Execute the console command. * @@ -70,6 +82,16 @@ class MakeModelsCommand extends GeneratorCommand */ public function fire() { + if ($this->option("getset")) { + // load the get/set function stubs + $folder = __DIR__ . '/../stubs/'; + + $this->setFunctionStub = $this->files->get($folder."setFunction.stub"); + $this->getFunctionStub = $this->files->get($folder."getFunction.stub"); + } + + // create rule processor + $this->ruleProcessor = new RuleProcessor(); $tables = $this->getSchemaTables(); @@ -101,6 +123,24 @@ protected function generateTable($table) //prefix is the sub-directory within app $prefix = $this->option('dir'); + $ignoreTable = $this->option("ignore"); + + if ($this->option("ignoresystem")) { + $ignoreSystem = "users,permissions,permission_role,roles,role_user,users,migrations,password_resets"; + + if (is_string($ignoreTable)) { + $ignoreTable.=",".$ignoreSystem; + } else { + $ignoreTable = $ignoreSystem; + } + } + + // if we have ignore tables, we need to find all the posibilites + if (is_string($ignoreTable) && preg_match("/^".$table."|^".$table.",|,".$table.",|,".$table."$/", $ignoreTable)) { + $this->info($table." is ignored"); + return; + } + $class = VariableConversion::convertTableNameToClassName($table); $name = rtrim($this->parseName($prefix . $class), 's'); @@ -135,9 +175,41 @@ protected function replaceTokens($name, $table) $class = str_replace('{{guarded}}', 'protected $guarded = ' . VariableConversion::convertArrayToString($properties['guarded']) . ';', $class); $class = str_replace('{{timestamps}}', 'public $timestamps = ' . VariableConversion::convertBooleanToString($properties['timestamps']) . ';', $class); + if ($this->option("getset")) { + $class = $this->replaceTokensWithSetGetFunctions($properties, $class); + } + return $class; } + /** + * Replaces setters and getters from the stub. The functions are created + * from provider properties. + * + * @param array $properties + * @param string $class + * @return string + */ + protected function replaceTokensWithSetGetFunctions($properties, $class) { + $getters = ""; + $setters = ""; + + $fillableGetSet = new SetGetGenerator($properties['fillable'], $this->getFunctionStub, $this->setFunctionStub); + $getters .= $fillableGetSet->generateGetFunctions(); + $setters .= $fillableGetSet->generateSetFunctions(); + + $guardedGetSet = new SetGetGenerator($properties['guarded'], $this->getFunctionStub, $this->setFunctionStub); + $getters .= $guardedGetSet->generateGetFunctions(); + + return str_replace([ + '{{setters}}', + '{{getters}}' + ], [ + $setters, + $getters + ], $class); + } + /** * Fill up $fillable/$guarded/$timestamps properties based on table columns. * @@ -220,6 +292,10 @@ protected function getOptions() ['fillable', null, InputOption::VALUE_OPTIONAL, 'Rules for $fillable array columns', $this->fillableRules], ['guarded', null, InputOption::VALUE_OPTIONAL, 'Rules for $guarded array columns', $this->guardedRules], ['timestamps', null, InputOption::VALUE_OPTIONAL, 'Rules for $timestamps columns', $this->timestampRules], + ['ignore', "i", InputOption::VALUE_OPTIONAL, 'Ignores the tables you define, separated with ,', null], + ['ignoresystem', "s", InputOption::VALUE_NONE, 'If you want to ignore system tables. + Just type --ignoresystem or -s'], + ['getset', 'm', InputOption::VALUE_NONE, 'Defines if you want to generate set and get methods'] ]; } } diff --git a/src/Utilities/SetGetGenerator.php b/src/Utilities/SetGetGenerator.php new file mode 100644 index 0000000..3e01017 --- /dev/null +++ b/src/Utilities/SetGetGenerator.php @@ -0,0 +1,142 @@ +attributes = $attributes; + $this->getStub = $getStub; + $this->setStub = $setStub; + + } + + /** + * Returns the get functions in string + * @return string + */ + public function generateGetFunctions() { + return $this->generateWithFunction("createGetFunctionFromAttributeName"); + } + + /** + * Returns the set functions in string + * @return string + */ + public function generateSetFunctions() { + return $this->generateWithFunction("createSetFunctionFromAttributeName"); + } + + /** + * Loops the attributes and build the string with given function name + * @param string $function + * @return string + */ + protected function generateWithFunction($function) { + $string = ""; + + foreach ($this->attributes as $attributeName) { + $string .= $this->$function($attributeName); + } + + return $string; + } + + /** + * Bulds the get function for the attribute name + * @param string $attributeName + * @return string + */ + public function createGetFunctionFromAttributeName($attributeName) { + return $this->createFunctionFromAttributeName("get", $attributeName, $this->getStub); + } + + /** + * Bulds the set function for the attribute name + * @param string $attributeName + * @return string + */ + public function createSetFunctionFromAttributeName($attributeName) { + return $this->createFunctionFromAttributeName("set", $attributeName, $this->setStub); + } + + /** + * Builds the funciton and creates the function from the stub template + * @param string $prefixFunction + * @param string $attributeName + * @param string $stubTemplate + * @return string + */ + protected function createFunctionFromAttributeName($prefixFunction, $attributeName, $stubTemplate) { + $function = $this->attributeNameToFunction($prefixFunction, $attributeName); + + // change to stub? + + return $this->createAttributeFunction($stubTemplate, $function, $attributeName); + } + + /** + * Replaces the stub template with the data + * @param string $stubTemplate + * @param string $function + * @param string $attributeName + * @return string + */ + protected function createAttributeFunction($stubTemplate, $function, $attributeName) { + return str_replace([ + "{{ attribute }}", + "{{ function }}" + ], [ + $attributeName, + $function + ], $stubTemplate); + } + + /** + * Converts the given string to function. Support database names (underscores) + * @param string $prefixFunction desired function prefix (get/set) + * @param string $str attribute name + * @param array $noStrip + * @return string + */ + public function attributeNameToFunction($prefixFunction, $str, array $noStrip = array()) + { + // non-alpha and non-numeric characters become spaces + $str = preg_replace('/[^a-z0-9' . implode("", $noStrip) . ']+/i', ' ', $str); + $str = trim($str); + // uppercase the first character of each word + $str = ucwords($str); + $str = str_replace(" ", "", $str); + $str = ucfirst($str); + + return $prefixFunction.$str; + } +} \ No newline at end of file diff --git a/src/stubs/getFunction.stub b/src/stubs/getFunction.stub new file mode 100644 index 0000000..9f03fb9 --- /dev/null +++ b/src/stubs/getFunction.stub @@ -0,0 +1,7 @@ + + /** + * @return mixed + */ + public function {{ function }}() { + return $this->{{ attribute }}; + } diff --git a/src/stubs/model.stub b/src/stubs/model.stub index 3a5a705..6cfb19f 100644 --- a/src/stubs/model.stub +++ b/src/stubs/model.stub @@ -1,12 +1,16 @@ -{{ attribute }} = $value; + return $this; + } diff --git a/tests/SetGetGeneratorTest.php b/tests/SetGetGeneratorTest.php new file mode 100644 index 0000000..ec2b9e5 --- /dev/null +++ b/tests/SetGetGeneratorTest.php @@ -0,0 +1,66 @@ +getFunctionStub = file_get_contents("src/stubs/getFunction.stub"); + $this->setFunctionStub = file_get_contents("src/stubs/setFunction.stub"); + } + + public function setUp() { + $this->generator = new Iber\Generator\Utilities\SetGetGenerator([ + "attribute_name", + "test" + ], $this->getFunctionStub, $this->setFunctionStub); + } + + + public function testCamelCaseFunction() + { + $this->assertEquals("getTest",$this->generator->attributeNameToFunction("get", "test")); + + $this->assertEquals("getTestName",$this->generator->attributeNameToFunction("get", "test_name")); + + $this->assertEquals("getTestname",$this->generator->attributeNameToFunction("get", "testname")); + + $this->assertEquals("getTestName",$this->generator->attributeNameToFunction("get", "test name")); + } + + + public function testCreateGetFunctionFromAttributeName() { + $function = $this->generator->createGetFunctionFromAttributeName("test"); + $this->assertContains("getTest", $function); + $this->assertContains('$this->test', $function); + } + + public function testCreateSetFunctionFromAttributeName() { + $function = $this->generator->createSetFunctionFromAttributeName("test"); + $this->assertContains("setTest", $function); + $this->assertContains('$this->attributes', $function); + $this->assertContains('"test"', $function); + } + + public function testGenerateGet() { + $text = $this->generator->generateGetFunctions(); + $this->assertNotEquals("", $text); + $this->assertNotContains("setTest", $text); + $this->assertContains("getTest", $text); + $this->assertContains("getAttributeName", $text); + } + + public function testGenerateSet() { + $text = $this->generator->generateSetFunctions(); + $this->assertNotEquals("", $text); + $this->assertNotContains("getTest", $text); + $this->assertContains("setTest", $text); + $this->assertContains("setAttributeName", $text); + } +}