Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #10 from baselbj/develop
Browse files Browse the repository at this point in the history
Add namespace support and add generate skeleton migration file
  • Loading branch information
lonnieezell committed Jul 12, 2018
2 parents ef7a814 + 0f1b324 commit d766b2d
Show file tree
Hide file tree
Showing 9 changed files with 2,813 additions and 313 deletions.
33 changes: 30 additions & 3 deletions Commands/MakeCommand.php
Expand Up @@ -22,13 +22,40 @@ class MakeCommand extends BaseCommand
* @var string
*/
protected $name = 'make:command';


/**
* the Command's short description
*
* @var string
*/
protected $description = 'Creates a skeleton command file.';
protected $description = 'Creates a skeleton command file.';

/**
* the Command's usage
*
* @var string
*/
protected $usage = 'make:command [command_name] [Options]';

/**
* the Command's Arguments
*
* @var array
*/
protected $arguments = [
'command_name' => 'The command file name'
];

/**
* the Command's Options
*
* @var array
*/
protected $options = [
'-n' => 'Set command namespace',
'-f' => 'overwrite files'
];

/**
* Creates a skeleton command file.
Expand All @@ -52,12 +79,12 @@ public function run(array $params=[])
$view = 'Command/Command';

$data = [
'namespace' => 'Codeigniter\Commands',
'namespace' => 'namespace' => CLI::getOption('n') ?? 'App',
'name' => $name,
'today' => date('Y-m-d H:i:a')
];

$destination = $this->determineOutputPath('Commands').$name.'.php';
$destination = $this->determineOutputPath('Commands',$data['namespace']).$name.'.php';

$overwrite = (bool)CLI::getOption('f');

Expand Down
32 changes: 29 additions & 3 deletions Commands/MakeController.php
Expand Up @@ -34,6 +34,32 @@ class MakeController extends BaseCommand
*
* @param array $params
*/

/**
* the Command's usage
*
* @var string
*/
protected $usage = 'make:controller [controller_name] [Options]';

/**
* the Command's Arguments
*
* @var array
*/
protected $arguments = [
'controller_name' => 'The controller file name'
];

/**
* the Command's Options
*
* @var array
*/
protected $options = [
'-n' => 'Set Controller namespace',
'-f' => 'overwrite files'
];
public function run(array $params=[])
{
/*
Expand All @@ -58,7 +84,7 @@ public function run(array $params=[])
: 'Controller/SimpleController';

$data = [
'namespace' => 'App\Controllers',
'namespace' => 'namespace' => CLI::getOption('n') ?? 'App',
'name' => $name,
'today' => date('Y-m-d H:i:a')
];
Expand All @@ -69,7 +95,7 @@ public function run(array $params=[])
$data = array_merge($data, $this->getCRUDOptions());
}

$destination = $this->determineOutputPath('Controllers').$name.'.php';
$destination = $this->determineOutputPath('Controllers',$data['namespace']).$name.'.php';

$overwrite = (bool)CLI::getOption('f');

Expand Down Expand Up @@ -115,4 +141,4 @@ public function getCRUDOptions()
];
}

}
}
96 changes: 96 additions & 0 deletions Commands/MakeMigration.php
@@ -0,0 +1,96 @@
<?php
namespace Vulcan\Commands;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use Vulcan\Libraries\GeneratorTrait;

/**
* Creates a skeleton Migration
*
* @package Vulcan\Commands
*/
class MakeMigration extends BaseCommand
{
use GeneratorTrait;

protected $group = 'Vulcan';

/**
* The Migration's name
*
* @var string
*/
protected $name = 'make:migration';

/**
* the Migration's short description
*
* @var string
*/
protected $description = 'Creates a skeleton migration file.';

/**
* the Command's usage
*
* @var string
*/
protected $usage = 'make:migration [migration_name] [Options]';

/**
* the Command's Arguments
*
* @var array
*/
protected $arguments = [
'migration_name' => 'The migration file name'
];

/**
* the Command's Options
*
* @var array
*/
protected $options = [
'-n' => 'Set migration namespace',
'-f' => 'overwrite files'
];

/**
* Creates a skeleton Migration file.
*/
public function run(array $params=[])
{
/*
* Name
*/
$name = array_shift($params);

if (empty($name))
{
$name = CLI::prompt('Migration name');
}

// Format to CI standards
$name = ucfirst($name);
$view = 'Migration/Migration';

$data = [
'namespace' => 'namespace' => CLI::getOption('n') ?? 'App',
'name' => $name,
'today' => date('Y-m-d H:i:a')
];

$destination = $this->determineOutputPath('Database\Migrations',$data['namespace']).$name.'.php';

$overwrite = (bool)CLI::getOption('f');

try {
$this->copyTemplate($view, $destination, $data, $overwrite);
}
catch (\Exception $e)
{
$this->showError($e);
}
}
}
90 changes: 59 additions & 31 deletions Commands/MakeModel.php
Expand Up @@ -21,7 +21,33 @@ class MakeModel extends BaseCommand

protected $description = 'Creates a skeleton Model file, optionally from a database.';

protected $options = [
/**
* the Command's usage
*
* @var string
*/
protected $usage = 'make:model [model_name] [Options]';

/**
* the Command's Arguments
*
* @var array
*/
protected $arguments = [
'model_name' => 'The model file name'
];

/**
* the Command's Options
*
* @var array
*/
protected $options = array(
'-n' => 'Set model namespace',
'-f' => 'overwrite files'
);

protected $optionsList = [
'name' => '',
'table' => '',
'primaryKey' => '',
Expand Down Expand Up @@ -50,15 +76,15 @@ public function run(array $params = [])
$name = CLI::prompt('Model name');
}

$this->options['name'] = ucfirst($name);
$this->optionsList['name'] = ucfirst($name);

$this->collectOptions($this->options['name'], CLI::getOptions());
$this->collectOptions($this->optionsList['name'], CLI::getOptions());

$data = $this->prepareData();

$overwrite = (bool)CLI::getOption('f');

$destination = $this->determineOutputPath('Models').$name.'.php';
$destination = $this->determineOutputPath('Models', $this->optionsList['namespace']).$name.'.php';

try
{
Expand All @@ -76,23 +102,25 @@ protected function collectOptions(string $name, array $options = [])
helper('inflector');

// Table name
if (empty($this->options['table']))
if (empty($this->optionsList['table']))
{
$this->options['table'] = empty($options['table'])
$this->optionsList['table'] = empty($options['table'])
? CLI::prompt('Table name', plural(strtolower(str_replace('Model', '', $name))))
: $options['table'];
: $optionsList['table'];
}

// Primary Key
if (empty($this->options['primaryKey']))
if (empty($this->optionsList['primaryKey']))
{
$this->options['primaryKey'] = empty($options['primaryKey'])
$this->optionsList['primaryKey'] = empty($options['primaryKey'])
? CLI::prompt('Primary key', 'id')
: $options['primaryKey'];
: $optionsList['primaryKey'];
}

$this->optionsList['namespace'] = 'namespace' => CLI::getOption('n') ?? 'App',

// Collect the fields from the table itself, if we have one
$this->options['allowedFields'] = $this->tableInfo($this->options['table'], $options);
$this->optionsList['allowedFields'] = $this->tableInfo($this->optionsList['table'], $options);
}

/**
Expand Down Expand Up @@ -182,7 +210,7 @@ protected function tableInfo(string $table, array $options = [])
return false;
}

if (! $db->tableExists($this->options['table']))
if (! $db->tableExists($this->optionsList['table']))
{
if (empty($options['fields']))
{
Expand All @@ -201,8 +229,8 @@ protected function tableInfo(string $table, array $options = [])
return false;
}

$this->options['useTimestamps'] = false;
$this->options['useSoftDeletes'] = false;
$this->optionsList['useTimestamps'] = false;
$this->optionsList['useSoftDeletes'] = false;

// Still here? Try to determine correct values from the database
// for things like primary key, etc.
Expand All @@ -211,20 +239,20 @@ protected function tableInfo(string $table, array $options = [])
// Primary key?
if (! empty($field->primary_key) && $field->primary_key == 1)
{
$this->options['primaryKey'] = $field->name;
$this->optionsList['primaryKey'] = $field->name;
} // Timestamps
elseif ($field->name == $this->options['createdField'])
elseif ($field->name == $this->optionsList['createdField'])
{
$this->options['useTimestamps'] = true;
$this->optionsList['useTimestamps'] = true;
} // Soft Deletes
elseif ($field->name == 'deleted')
{
$this->options['useSoftDeletes'] = true;
$this->optionsList['useSoftDeletes'] = true;
}
}

// Set our validation rules based on these fields.
$this->options['validationRules'] = $this->buildValidationRules($fields);
$this->optionsList['validationRules'] = $this->buildValidationRules($fields);

return $fields;
}
Expand Down Expand Up @@ -313,24 +341,24 @@ protected function buildValidationRules(array $fields)
public function prepareData()
{
$data = [
'name' => $this->options['name'],
'table' => $this->options['table'],
'primaryKey' => $this->options['primaryKey'],
'useSoftDeletes' => $this->options['useSoftDeletes'] === true ? 'true' : 'false',
'useTimestamps' => $this->options['useTimestamps'] === true ? 'true' : 'false',
'createdField' => $this->options['createdField'],
'updatedField' => $this->options['updatedField'],
'returnType' => $this->options['returnType'],
'validationRules' => $this->options['validationRules'],
'dateFormat' => $this->options['dateFormat'],
'name' => $this->optionsList['name'],
'table' => $this->optionsList['table'],
'primaryKey' => $this->optionsList['primaryKey'],
'useSoftDeletes' => $this->optionsList['useSoftDeletes'] === true ? 'true' : 'false',
'useTimestamps' => $this->optionsList['useTimestamps'] === true ? 'true' : 'false',
'createdField' => $this->optionsList['createdField'],
'updatedField' => $this->optionsList['updatedField'],
'returnType' => $this->optionsList['returnType'],
'validationRules' => $this->optionsList['validationRules'],
'dateFormat' => $this->optionsList['dateFormat'],
'today' => date('Y-m-d H:ia'),
];

if (is_array($this->options['allowedFields']))
if (is_array($this->optionsList['allowedFields']))
{
$fields = [];

foreach ($this->options['allowedFields'] as $field)
foreach ($this->optionsList['allowedFields'] as $field)
{
if ($field->name == $data['primaryKey']
|| $field->name == $data['createdField']
Expand Down

0 comments on commit d766b2d

Please sign in to comment.