Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Generate an error message if an invalid parameter is passed to a task…

…. Refs #3989
  • Loading branch information...
commit 94ac7f28d338877fc2fc10e1672b44d9e6f54607 1 parent 724ae17
Jeremy Bush zombor authored
27 classes/kohana/minion/task.php
View
@@ -45,7 +45,7 @@ public static function factory($options)
}
$class = new $class;
- $class->options($options);
+ $class->set_config_options($options);
// Show the help page for this task if requested
if (array_key_exists('help', $options))
@@ -90,9 +90,9 @@ public function __toString()
*
* @return this
*/
- public function options(array $options)
+ public function set_config_options(array $options = NULL)
{
- $this->_options = $options;
+ $this->_options = arr::merge($options, $this->_defaults);
return $this;
}
@@ -104,7 +104,17 @@ public function options(array $options)
*/
public function get_config_options()
{
- return (array) $this->_config;
+ return (array) $this->_options;
+ }
+
+ /**
+ * Get a set of config options that this task can accept
+ *
+ * @return array
+ */
+ public function get_config_defaults()
+ {
+ return (array) $this->_defaults;
}
/**
@@ -122,6 +132,12 @@ public function get_config_options()
*/
public function build_validation(Validation $validation)
{
+ // Add a rule to each key making sure it's in the task
+ foreach ($validation->as_array() as $key => $value)
+ {
+ $validation->rule($key, array('Minion_Valid', 'option'), array(':validation', ':field', $this));
+ }
+
return $validation;
}
@@ -147,8 +163,7 @@ public function execute()
if ( ! empty($defaults))
{
$options = array_keys($defaults);
- $options = call_user_func_array(array('CLI', 'options'), $options);
- $config = Arr::merge($defaults, $options);
+ $config = Arr::merge($defaults, $this->_options);
}
else
{
25 classes/kohana/minion/valid.php
View
@@ -0,0 +1,25 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+/**
+ * Minion valid class
+ *
+ * @package Kohana
+ * @category Minion
+ * @author Kohana Team
+ * @copyright (c) 2009-2011 Kohana Team
+ * @license http://kohanaframework.org/license
+ */
+class Kohana_Minion_Valid
+{
+ /**
+ * Validates that an option is part of a task
+ *
+ * @return null
+ */
+ public static function option(Validation $v, $key, Minion_Task $task)
+ {
+ if ( ! array_key_exists($key, $task->get_config_defaults()))
+ {
+ $v->error($key, 'minion_option');
+ }
+ }
+}
4 classes/minion/task/demo.php
View
@@ -14,7 +14,7 @@
*/
class Minion_Task_Demo extends Minion_Task
{
- protected $_config = array(
+ protected $_defaults = array(
'foo' => 'bar',
'bar' => NULL,
);
@@ -26,7 +26,7 @@ class Minion_Task_Demo extends Minion_Task
*/
protected function _execute(array $params)
{
- var_dump(valid::email('mcnama1_patr@bentley.edu'));
+ // var_dump(valid::email('mcnama1_patr@bentley.edu'));
var_dump($params);
echo 'foobar';
}
40 classes/minion/task/demo/foo.php
View
@@ -0,0 +1,40 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+/**
+ * This is a demo task.....
+ *
+ * It can accept the following options:
+ * - foo: this parameter does something. It is required.
+ * - bar: this parameter does something else. It should be numeric.
+ *
+ * @package Kohana
+ * @category Helpers
+ * @author Kohana Team
+ * @copyright (c) 2009-2011 Kohana Team
+ * @license http://kohanaframework.org/license
+ */
+class Minion_Task_Demo_Foo extends Minion_Task
+{
+ protected $_config = array(
+ 'foo',
+ 'bar',
+ );
+
+ /**
+ *
+ *
+ * @return
+ */
+ protected function _execute(array $params)
+ {
+ var_dump(valid::email('mcnama1_patr@bentley.edu'));
+ var_dump($params);
+ echo 'foobar';
+ }
+
+ public function build_validation(Validation $validation)
+ {
+ return parent::build_validation($validation)
+ ->rule('foo', 'not_empty') // Require this param
+ ->rule('bar', 'numeric');
+ }
+}
9 classes/minion/valid.php
View
@@ -0,0 +1,9 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+
+/**
+ * Utility class for Minion
+ */
+class Minion_Valid extends Kohana_Minion_Valid {
+
+}
14 guide/minion/tasks.md
View
@@ -6,9 +6,9 @@ Writing a task in minion is very easy. Simply create a new class called `Minion_
class Minion_Task_Demo extends Minion_Task
{
- protected $_config = array(
- 'foo',
- 'bar',
+ protected $_defaults = array(
+ 'foo' = 'bar',
+ 'bar' => NULL,
);
/**
@@ -27,8 +27,12 @@ You'll notice a few things here:
- You need a main `_execute()` method. It should take one array parameter.
- This parameter contains any command line options passed to the task.
- - For example, if you call a task with `./minion --task=demo --foo=foobar` then `$params` will contain: `array('foo' => 'bar')`
- - It can optionally have a `protected $_config` array. This is a list of parameters you want to accept for this task. More on this below.
+ - For example, if you call the task above with `./minion --task=demo --foo=foobar` then `$params` will contain: `array('foo' => 'foobar', 'bar' => NULL)`
+ - It needs to have a `protected $_defaults` array. This is a list of parameters you want to accept for this task. Any parameters passed to the task not in this list will be rejected.
+
+## Namespacing Tasks
+
+You can "namespace" tasks by placing them all in a subdirectory: `classes/minion/task/database/generate.php`. This task will be named `database:generate` and can be called with this task name.
# Parameter Validations
5 messages/validation.php
View
@@ -0,0 +1,5 @@
+<?php
+
+return array(
+ 'minion_option' => ':field is not a valid option for this task!',
+);
Please sign in to comment.
Something went wrong with that request. Please try again.