mirror of
https://github.com/Oreolek/kohana-migrations.git
synced 2024-06-16 23:11:06 +03:00
Limiting command line to executing a single task.
This reduces both the complexity for the end user (no pesky task namespace on config options) and also removes a good few LOC Minion_Master has been removed in favour of Controller_Minion + Minion_Test::factory()
This commit is contained in:
parent
e64ce18a71
commit
c026cbb415
|
@ -69,46 +69,27 @@ class Controller_Minion extends Controller
|
|||
*/
|
||||
public function action_execute()
|
||||
{
|
||||
$tasks = trim($this->request->param('task'));
|
||||
$task = trim($this->request->param('task'));
|
||||
|
||||
if(empty($tasks))
|
||||
if(empty($task))
|
||||
return $this->action_help();
|
||||
|
||||
$tasks = explode(',', $tasks);
|
||||
|
||||
$master = new Minion_Master;
|
||||
|
||||
$options = $master->load($tasks)->get_config_options();
|
||||
|
||||
$config = array();
|
||||
|
||||
// Allow the user to specify config for each task, namespacing each
|
||||
// config option with the name of the task that "owns" it
|
||||
foreach($options as $task_name => $task_options)
|
||||
try
|
||||
{
|
||||
$namespace = $task_name.Minion_Util::$task_separator;
|
||||
$task = Minion_Task::factory($task);
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
echo View::factory('minion/help/error')
|
||||
->set('error', 'Task "'.$task.'" does not exist');
|
||||
|
||||
// Namespace each config option
|
||||
foreach($task_options as $i => $task_option)
|
||||
{
|
||||
$task_options[$i] = $namespace.$task_option;
|
||||
}
|
||||
|
||||
// Get any config options the user's passed
|
||||
$task_config = call_user_func_array(array('CLI', 'options'), $task_options);
|
||||
|
||||
if( ! empty($task_config))
|
||||
{
|
||||
$namespace_length = strlen($namespace);
|
||||
|
||||
// Strip the namespace off all the config options
|
||||
foreach($task_config as $key => $value)
|
||||
{
|
||||
$config[$task_name][substr($key, $namespace_length)] = $value;
|
||||
}
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$master->execute($config);
|
||||
$options = $task->get_config_options();
|
||||
|
||||
$config = call_user_func_array(array('CLI', 'options'), $options);
|
||||
|
||||
$task->execute($config);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,93 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* The Minion Master is responsible for loading and executing the various minion
|
||||
* tasks requested by the user
|
||||
*
|
||||
* @author Matt Button <matthew@sigswitch.com>
|
||||
*/
|
||||
class Minion_Master {
|
||||
|
||||
/**
|
||||
* Tasks the master will execute
|
||||
* @var array
|
||||
*/
|
||||
protected $_tasks = array();
|
||||
|
||||
/**
|
||||
* Get a list of config options that the loaded tasks accept at execution
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_config_options()
|
||||
{
|
||||
$config = array();
|
||||
|
||||
foreach($this->_tasks as $task)
|
||||
{
|
||||
$config[(string) $task] = (array) $task->get_config_options();
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads a number of tasks into the task master
|
||||
*
|
||||
* Passed task can either be an instance of Minion_Task, a task name (e.g.
|
||||
* db:migrate) or an array of the above
|
||||
*
|
||||
* If an invalid task is passed then a Kohana_Exception will be thrown
|
||||
*
|
||||
* @chainable
|
||||
* @throws Kohana_Exception
|
||||
* @param array|string|Minion_Task The task(s) to load
|
||||
* @returns Minion_Master Chainable instance
|
||||
*/
|
||||
public function load($task)
|
||||
{
|
||||
if(is_array($task))
|
||||
{
|
||||
array_map(array($this, 'load'), $task);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
if(is_string($task))
|
||||
{
|
||||
$class = Minion_Util::convert_task_to_class_name($task);
|
||||
|
||||
$task = new $class;
|
||||
}
|
||||
|
||||
if( ! $task instanceof Minion_Task)
|
||||
{
|
||||
throw new Kohana_Exception(
|
||||
"Task ':task' is not a valid minion task",
|
||||
array(':task' => get_class($task))
|
||||
);
|
||||
}
|
||||
|
||||
$this->_tasks[(string) $task] = $task;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the loaded tasks one at a time
|
||||
*
|
||||
* @return Minion_Master Chainable instance
|
||||
*/
|
||||
public function execute(array $config = array())
|
||||
{
|
||||
if(empty($this->_tasks))
|
||||
return $this;
|
||||
|
||||
foreach($this->_tasks as $task)
|
||||
{
|
||||
$task->execute(Arr::get($config, (string) $task, array()));
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
|
@ -6,6 +6,32 @@
|
|||
*/
|
||||
abstract class Minion_Task {
|
||||
|
||||
/**
|
||||
* Factory for loading minion tasks
|
||||
*
|
||||
* @throws Kohana_Exception
|
||||
* @param string The task to load
|
||||
* @return Minion_Task The Minion task
|
||||
*/
|
||||
public static function factory($task)
|
||||
{
|
||||
if(is_string($task))
|
||||
{
|
||||
$class = Minion_Util::convert_task_to_class_name($task);
|
||||
|
||||
$task = new $class;
|
||||
}
|
||||
|
||||
if( ! $task instanceof Minion_Task)
|
||||
{
|
||||
throw new Kohana_Exception(
|
||||
"Task ':task' is not a valid minion task",
|
||||
array(':task' => get_class($task))
|
||||
);
|
||||
}
|
||||
|
||||
return $task;
|
||||
}
|
||||
/**
|
||||
* A set of config options that the task accepts on the command line
|
||||
* @var array
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
*
|
||||
* Available config options are:
|
||||
*
|
||||
* db:migration:versions=[location:]version
|
||||
* --versions=[location:]version
|
||||
*
|
||||
* Used to specify the version to migrate the database to. The location prefix
|
||||
* is used to specify the target version of an individual location. Version
|
||||
|
@ -22,13 +22,13 @@
|
|||
* If you specify TRUE / FALSE without a location then the default migration
|
||||
* direction for locations without a specified version will be up / down respectively
|
||||
*
|
||||
* db:migrate:locations=location[,location2[,location3...]]
|
||||
* --locations=location[,location2[,location3...]]
|
||||
*
|
||||
* A list of locations (under the migrations folder in the cascading
|
||||
* filesystem) that will be used to source migration files. By default
|
||||
* migrations will be loaded from all available locations
|
||||
*
|
||||
* db:migrate:dry-run
|
||||
* --dry-run
|
||||
*
|
||||
* No value taken, if this is specified then instead of executing the SQL it
|
||||
* will be printed to the console
|
||||
|
|
|
@ -1,26 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Test case for the Minion Master
|
||||
*
|
||||
* @group minion
|
||||
**/
|
||||
class Minion_MasterTest extends Kohana_Unittest_TestCase
|
||||
{
|
||||
/**
|
||||
* Tests that Minion_Master::load() will accept an instance of Minion_Task
|
||||
* as a task
|
||||
*
|
||||
* @test
|
||||
* @covers Minion_Master::load
|
||||
*/
|
||||
public function test_load_accepts_objects_as_valid_tasks()
|
||||
{
|
||||
$master = new Minion_Master;
|
||||
$task = $this->getMockForAbstractClass('Minion_Task');
|
||||
|
||||
$this->assertSame($master, $master->load($task));
|
||||
|
||||
$this->assertAttributeContains($task, '_tasks', $master);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue