Permalink
Browse files

Remove short-arrays option from model generator

  • Loading branch information...
andrzejo committed Mar 29, 2017
1 parent efa679a commit 8e503d2b98ad9b62b208c5dcf7f59e25834a5f06
@@ -37,8 +37,7 @@ public function configure()
->addOption('file', 'f', InputOption::VALUE_REQUIRED, 'Class file path. If not specified namespace and class name is used.') ->addOption('file', 'f', InputOption::VALUE_REQUIRED, 'Class file path. If not specified namespace and class name is used.')
->addOption('namespace', 's', InputOption::VALUE_REQUIRED, 'Class namespace (e.g \'Model\MyModel\'). Hint: Remember to escape backslash (\\\\)!', $defaultNamespace) ->addOption('namespace', 's', InputOption::VALUE_REQUIRED, 'Class namespace (e.g \'Model\MyModel\'). Hint: Remember to escape backslash (\\\\)!', $defaultNamespace)
->addOption('remove-prefix', 'p', InputOption::VALUE_REQUIRED, 'Remove prefix from table name when generating class name.', 't') ->addOption('remove-prefix', 'p', InputOption::VALUE_REQUIRED, 'Remove prefix from table name when generating class name.', 't')
->addOption('output-only', 'o', InputOption::VALUE_NONE, 'Only displaying generated model class.') ->addOption('output-only', 'o', InputOption::VALUE_NONE, 'Only displaying generated model class.');
->addOption('short-arrays', 'a', InputOption::VALUE_NONE, 'Generate model class with short arrays. (PHP 5.4).');
} }
public function execute(InputInterface $input, OutputInterface $output) public function execute(InputInterface $input, OutputInterface $output)
@@ -60,12 +59,11 @@ private function generateModel()
$fileName = $this->input->getOption('file'); $fileName = $this->input->getOption('file');
$nameSpace = $this->input->getOption('namespace'); $nameSpace = $this->input->getOption('namespace');
$tablePrefixToRemove = $this->input->getOption('remove-prefix') ?: 't'; $tablePrefixToRemove = $this->input->getOption('remove-prefix') ?: 't';
$shortArrays = $this->input->getOption('short-arrays');
if (empty($tableName)) { if (empty($tableName)) {
$this->fail("Specify table name e.g. users"); $this->fail("Specify table name e.g. users");
} }
try { try {
$modelGenerator = new Generator($tableName, $className, $nameSpace, $tablePrefixToRemove, $shortArrays); $modelGenerator = new Generator($tableName, $className, $nameSpace, $tablePrefixToRemove);
$this->output->writeln('---------------------------------'); $this->output->writeln('---------------------------------');
$this->writeInfo('Database name: <info>%s</info>', Config::getValue('db', 'dbname')); $this->writeInfo('Database name: <info>%s</info>', Config::getValue('db', 'dbname'));
$this->writeInfo('Class name: <info>%s</info>', $modelGenerator->getTemplateClassName()); $this->writeInfo('Class name: <info>%s</info>', $modelGenerator->getTemplateClassName());
@@ -16,23 +16,16 @@ class ClassStub
private $_stubContent; private $_stubContent;
private $_attributes = []; private $_attributes = [];
private $_placeholderWithReplacements = []; private $_placeholderWithReplacements = [];
private $shortArrays;
public function __construct($shortArrays = false) public function __construct()
{ {
$this->shortArrays = $shortArrays;
$stubFilePath = $this->_getStubFilePath(); $stubFilePath = $this->_getStubFilePath();
$this->_stubContent = file_get_contents($stubFilePath); $this->_stubContent = file_get_contents($stubFilePath);
} }
private function _getStubFilePath() private function _getStubFilePath()
{ {
if ($this->shortArrays) { return Path::join(__DIR__, 'stubs', 'class.stub');
$stubFileName = 'class_short_arrays.stub';
} else {
$stubFileName = 'class.stub';
}
return Path::join(__DIR__, 'stubs', $stubFileName);
} }
public function addColumn(DatabaseColumn $databaseColumn) public function addColumn(DatabaseColumn $databaseColumn)
@@ -18,12 +18,12 @@ class ClassStubPlaceholderReplacer
private $classStub; private $classStub;
private $classNamespace; private $classNamespace;
public function __construct($className, $tableInfo, $classNamespace = '', $shortArrays = false) public function __construct($className, $tableInfo, $classNamespace = '')
{ {
$this->className = $className; $this->className = $className;
$this->tableInfo = $tableInfo; $this->tableInfo = $tableInfo;
$this->classNamespace = $classNamespace; $this->classNamespace = $classNamespace;
$this->classStub = new ClassStub($shortArrays); $this->classStub = new ClassStub();
} }
private function _setupTablePlaceholderReplacements() private function _setupTablePlaceholderReplacements()
@@ -22,17 +22,15 @@ class Generator
private $dialectShortName; private $dialectShortName;
private $tablePrefix; private $tablePrefix;
private $namespace; private $namespace;
private $shortArrays;
public function __construct($tableName, $className = null, $nameSpace = '', $tablePrefix = 't', $shortArrays = false) public function __construct($tableName, $className = null, $nameSpace = '', $tablePrefix = 't')
{ {
$this->tableName = $tableName; $this->tableName = $tableName;
$this->className = $className; $this->className = $className;
$this->namespace = $this->normalizeNameSpace($nameSpace); $this->namespace = $this->normalizeNameSpace($nameSpace);
$this->tablePrefix = $tablePrefix; $this->tablePrefix = $tablePrefix;
$this->adapter = $this->dialectAdapter(); $this->adapter = $this->dialectAdapter();
$this->dialectShortName = $this->getDialectShortName($this->adapter); $this->dialectShortName = $this->getDialectShortName($this->adapter);
$this->shortArrays = $shortArrays;
} }
private function normalizeNameSpace($nameSpace) private function normalizeNameSpace($nameSpace)
@@ -89,7 +87,7 @@ public function getTemplateClassName()
public function templateContents() public function templateContents()
{ {
$tableInfo = new TableInfo($this->adapter); $tableInfo = new TableInfo($this->adapter);
$stubReplacer = new ClassStubPlaceholderReplacer($this->getTemplateClassName(), $tableInfo, $this->getClassNamespace(), $this->shortArrays); $stubReplacer = new ClassStubPlaceholderReplacer($this->getTemplateClassName(), $tableInfo, $this->getClassNamespace());
return $stubReplacer->contents(); return $stubReplacer->contents();
} }
@@ -8,14 +8,14 @@ use Ouzo\Model;
*/ */
class {class} extends Model class {class} extends Model
{ {
public function __construct($attributes = array()) public function __construct($attributes = [])
{ {
parent::__construct(array( parent::__construct([
{table_table} {table_table}
{table_primaryKey} {table_primaryKey}
{table_sequence} {table_sequence}
'attributes' => $attributes, 'attributes' => $attributes,
'fields' => array({fields}) 'fields' => [{fields}]
)); ]);
} }
} }

This file was deleted.

Oops, something went wrong.

0 comments on commit 8e503d2

Please sign in to comment.