Skip to content

Fixed 5.1 and added new generators (get/set) and option #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
vendor
.phpintel
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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```
Expand Down
76 changes: 76 additions & 0 deletions src/Commands/MakeModelsCommand.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php namespace Iber\Generator\Commands;

use Iber\Generator\Utilities\RuleProcessor;
use Iber\Generator\Utilities\SetGetGenerator;
use Iber\Generator\Utilities\VariableConversion;
use Symfony\Component\Console\Input\InputOption;
use Illuminate\Console\GeneratorCommand;
Expand Down Expand Up @@ -63,13 +64,34 @@ class MakeModelsCommand extends GeneratorCommand
*/
protected $timestampRules = 'ends:_at'; //['ends' => ['_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.
*
* @return mixed
*/
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();
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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']
];
}
}
142 changes: 142 additions & 0 deletions src/Utilities/SetGetGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php
namespace Iber\Generator\Utilities;

use \Illuminate\Filesystem\Filesystem;

class SetGetGenerator {

/**
* Lists of attributes to convert
* @var array
*/
protected $attributes = array();

/**
* Returns a template stub for set function
* @var string
*/
protected $setStub = "";

/**
* Returns a template stub for get function
* @var string
*/
protected $getStub = "";

/**
* [__construct description]
* @param array $attributes with attributes names
* @param string $getStub set stub template
* @param string $setStub get stub template
*/
public function __construct(array $attributes, $getStub, $setStub)
{
$folder = __DIR__ . '/../stubs/';

$this->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;
}
}
7 changes: 7 additions & 0 deletions src/stubs/getFunction.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

/**
* @return mixed
*/
public function {{ function }}() {
return $this->{{ attribute }};
}
8 changes: 6 additions & 2 deletions src/stubs/model.stub
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<?php namespace {{namespace}};
<?php namespace DummyNamespace;

use Illuminate\Database\Eloquent\Model;

class {{class}} extends {{extends}} {
class DummyClass extends {{extends}} {

{{timestamps}}

{{fillable}}

{{guarded}}

{{getters}}

{{setters}}
}
9 changes: 9 additions & 0 deletions src/stubs/setFunction.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

/**
* @param $value
* @return $this
*/
public function {{ function }}($value) {
$this->{{ attribute }} = $value;
return $this;
}
66 changes: 66 additions & 0 deletions tests/SetGetGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

use \Illuminate\Filesystem\Filesystem;

class SetGetGeneratorTest extends PHPUnit_Framework_TestCase
{
protected $generator;

protected $setFunctionStub;
protected $getFunctionStub;

public function __construct() {
// load only once
$this->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);
}
}