Modules

abstract Minion_Task
extends Kohana_Minion_Task

Interface that all minion tasks must implement

Class declared in MODPATH/minion/classes/Minion/Task.php on line 6.

Properties

public static string $task_separator

The separator used to separate different levels of tasks

string(1) ":"

protected array $_accepted_options

Populated with the accepted options for this task. This array is automatically populated based on $_options.

Default value:
array(0) 

protected string|null $_errors_file

The file that get's passes to Validation::errors() when validation fails

Default value:
string(10) "validation"

protected $_method

Default value:
string(8) "_execute"

protected array $_options

The list of options this task accepts and their default values.

protected $_options = [
    'limit' => 4,
    'table' => null,
];
Default value:
array(0) 

Methods

public __toString( ) (defined in Kohana_Minion_Task)

Gets the task name for the task

Return Values

  • string

Source Code

public function __toString()
{
    static $task_name = null;

    if ($task_name === null) {
        $task_name = Minion_Task::convert_class_to_task($this);
    }

    return $task_name;
}

public build_validation( Validation $validation ) (defined in Kohana_Minion_Task)

Adds any validation rules/labels for validating _options

public function build_validation(Validation $validation)
{
    return parent::build_validation($validation)
        ->rule('paramname', 'not_empty'); // Require this param
}

Parameters

  • Validation $validation required - The validation object to add rules to

Return Values

  • Validation

Source Code

public function build_validation(Validation $validation)
{
    // Add a rule to each key making sure it's in the task
    foreach ($validation->data() as $key => $value) {
        $validation->rule($key, [$this, 'valid_option'], [':validation', ':field']);
    }

    return $validation;
}

public static convert_class_to_task( string|Minion_Task $class ) (defined in Kohana_Minion_Task)

Gets the task name of a task class / task object

Parameters

  • string|Minion_Task $class required - The task class / object

Return Values

  • string - The task name

Source Code

public static function convert_class_to_task($class)
{
    if (is_object($class)) {
        $class = get_class($class);
    }

    return strtolower(str_replace('_', Minion_Task::$task_separator, substr($class, 5)));
}

public static convert_task_to_class_name( string $task ) (defined in Kohana_Minion_Task)

Converts a task (e.g. db:migrate to a class name)

Parameters

  • string $task required - Task name

Return Values

  • string - Class name

Source Code

public static function convert_task_to_class_name($task)
{
    $task = trim($task);

    if (empty($task))
        return '';

    return 'Task_' . implode('_', array_map('ucfirst', explode(Minion_Task::$task_separator, $task)));
}

public execute( ) (defined in Kohana_Minion_Task)

Execute the task with the specified set of options

Return Values

  • null

Source Code

public function execute()
{
    $options = $this->get_options();

    // Validate $options
    $validation = Validation::factory($options);
    $validation = $this->build_validation($validation);

    if ($this->_method != '_help' AND ! $validation->check()) {
        echo View::factory('minion/error/validation')
            ->set('task', Minion_Task::convert_class_to_task($this))
            ->set('errors', $validation->errors($this->get_errors_file()));
    } else {
        // Finally, run the task
        $method = $this->_method;
        echo $this->{$method}($options);
    }
}

public static factory( array $options ) (defined in Kohana_Minion_Task)

Factory for loading minion tasks

Parameters

  • array $options required - An array of command line options. It should contain the 'task' key

Tags

Return Values

  • Minion_Task - The Minion task

Source Code

public static function factory($options)
{
    if (($task = Arr::get($options, 'task')) !== null) {
        unset($options['task']);
    } else if (($task = Arr::get($options, 0)) !== null) {
        // The first positional argument (aka 0) may be the task name
        unset($options[0]);
    } else {
        // If we didn't get a valid task, generate the help
        $task = 'help';
    }

    $class = Minion_Task::convert_task_to_class_name($task);

    if (!class_exists($class)) {
        throw new Minion_Exception_InvalidTask("Task ':task' is not a valid minion task", [':task' => $class]);
    }

    $class = new $class;

    if (!$class instanceof Minion_Task) {
        throw new Minion_Exception_InvalidTask("Task ':task' is not a valid minion task", [':task' => $class]);
    }

    $class->set_options($options);

    // Show the help page for this task if requested
    if (array_key_exists('help', $options)) {
        $class->_method = '_help';
    }

    return $class;
}

public get_accepted_options( ) (defined in Kohana_Minion_Task)

Get a set of options that this task can accept

Return Values

  • array

Source Code

public function get_accepted_options()
{
    return (array) $this->_accepted_options;
}

public get_errors_file( ) (defined in Kohana_Minion_Task)

Returns $_errors_file

Return Values

  • string

Source Code

public function get_errors_file()
{
    return $this->_errors_file;
}

public get_options( ) (defined in Kohana_Minion_Task)

Get the options that were passed into this task with their defaults

Return Values

  • array

Source Code

public function get_options()
{
    return (array) $this->_options;
}

public set_options( ) (defined in Kohana_Minion_Task)

Sets options for this task

$param array the array of options to set

Return Values

  • this

Source Code

public function set_options(array $options)
{
    foreach ($options as $key => $value) {
        $this->_options[$key] = $value;
    }

    return $this;
}

public valid_option( ) (defined in Kohana_Minion_Task)

Source Code

public function valid_option(Validation $validation, $option)
{
    if (!in_array($option, $this->_accepted_options)) {
        $validation->error($option, 'minion_option');
    }
}

protected __construct( ) (defined in Kohana_Minion_Task)

Source Code

protected function __construct()
{
    // Populate $_accepted_options based on keys from $_options
    $this->_accepted_options = array_keys($this->_options);
}

protected _compile_task_list( array $files [, string $prefix = string(0) "" ] ) (defined in Kohana_Minion_Task)

Compiles a list of available tasks from a directory structure

Parameters

  • array $files required - Directory structure of tasks
  • string $prefix = string(0) "" - Prefix

Return Values

  • array - Compiled tasks

Source Code

protected function _compile_task_list(array $files, $prefix = '')
{
    $output = [];

    foreach ($files as $file => $path) {
        $file = substr($file, strrpos($file, DIRECTORY_SEPARATOR) + 1);

        if (is_array($path) AND count($path)) {
            $task = $this->_compile_task_list($path, $prefix . $file . Minion_Task::$task_separator);

            if ($task) {
                $output = array_merge($output, $task);
            }
        } else {
            $output[] = strtolower($prefix . substr($file, 0, -strlen(EXT)));
        }
    }

    return $output;
}

abstract protected _execute( ) (defined in Kohana_Minion_Task)

Source Code

abstract protected function _execute(array $params);

protected _help( ) (defined in Kohana_Minion_Task)

Outputs help for this task

Return Values

  • null

Source Code

protected function _help(array $params)
{
    $tasks = $this->_compile_task_list(Kohana::list_files('classes/task'));

    $inspector = new ReflectionClass($this);

    list($description, $tags) = $this->_parse_doccomment($inspector->getDocComment());

    $view = View::factory('minion/help/task')
        ->set('description', $description)
        ->set('tags', (array) $tags)
        ->set('task', Minion_Task::convert_class_to_task($this));

    echo $view;
}

protected _parse_doccomment( string $comment ) (defined in Kohana_Minion_Task)

Parses a doccomment, extracting both the comment and any tags associated

Based on the code in Kodoc::parse()

Parameters

  • string $comment required - The comment to parse

Return Values

  • array - First element is the comment, second is an array of tags

Source Code

protected function _parse_doccomment($comment)
{
    // Normalize all new lines to \n
    $comment = str_replace(["\r\n", "\n"], "\n", $comment);

    // Remove the phpdoc open/close tags and split
    $comment = array_slice(explode("\n", $comment), 1, -1);

    // Tag content
    $tags = [];

    foreach ($comment as $i => $line) {
        // Remove all leading whitespace
        $line = preg_replace('/^\s*\* ?/m', '', $line);

        // Search this line for a tag
        if (preg_match('/^@(\S+)(?:\s*(.+))?$/', $line, $matches)) {
            // This is a tag line
            unset($comment[$i]);

            $name = $matches[1];
            $text = isset($matches[2]) ? $matches[2] : '';

            $tags[$name] = $text;
        } else {
            $comment[$i] = (string) $line;
        }
    }

    $comment = trim(implode("\n", $comment));

    return [$comment, $tags];
}