command.php 2.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
<?php namespace Laravel\CLI;

use Laravel\IoC;
use Laravel\Str;
use Laravel\Bundle;

class Command {

	/**
	 * Run a CLI task with the given arguments.
	 *
	 * @param  array  $arguments
	 * @return void
	 */
	public static function run($arguments = array())
	{
		if ( ! isset($arguments[0]))
		{
			throw new \Exception("Whoops! You forgot to provide the task name.");
		}

		list($bundle, $task, $method) = static::parse($arguments[0]);

		// If the task exists within a bundle, we will start the bundle so that
		// any dependencies can be registered in the application IoC container.
		// If the task is registered in the container, it will be resolved
		// via the container instead of by this class.
		if (Bundle::exists($bundle)) Bundle::start($bundle);

Taylor Otwell committed
30 31 32
		// Once the bundle has been started, we will attempt to resolve the
		// task instance. Tasks may be resolved through the file system or
		// through the application IoC container.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
		if (is_null($task = static::resolve($bundle, $task)))
		{
			throw new \Exception("Sorry, I can't find that task.");
		}

		$task->$method(array_slice($arguments, 1));
	}

	/**
	 * Parse the task name to extract the bundle, task, and method.
	 *
	 * @param  string  $task
	 * @return array
	 */
	protected static function parse($task)
	{
		list($bundle, $task) = Bundle::parse($task);

		// Extract the task method from the task string. Methods are called
		// on tasks by separating the task and method with a single colon.
Taylor Otwell committed
53
		// If no task is specified, "run" is used as the default.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
		if (str_contains($task, ':'))
		{
			list($task, $method) = explode(':', $task);
		}
		else
		{
			$method = 'run';
		}

		return array($bundle, $task, $method);
	}

	/**
	 * Resolve an instance of the given task name.
	 *
	 * @param  string  $bundle
	 * @param  string  $task
	 * @return object
	 */
	public static function resolve($bundle, $task)
	{
		$identifier = Bundle::identifier($bundle, $task);

77 78 79
		// First we'll check to see if the task has been registered in the
		// application IoC container. This allows all dependencies to be
		// injected into tasks for more testability.
80 81 82 83 84
		if (IoC::registered("task: {$identifier}"))
		{
			return IoC::resolve("task: {$identifier}");
		}

85 86 87
		// If the task file exists, we'll format the bundle and task name
		// into a task class name and resolve an instance of the so that
		// the requested method may be executed.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
		if (file_exists($path = Bundle::path($bundle).'tasks/'.$task.EXT))
		{
			require $path;

			$task = static::format($bundle, $task);

			return new $task;
		}
	}

	/**
	 * Format a bundle and task into a task class name.
	 *
	 * @param  string  $bundle
	 * @param  string  $task
	 * @return string
	 */
	protected static function format($bundle, $task)
	{
		$prefix = Bundle::class_prefix($bundle);

Kelly Banman committed
109
		return '\\'.$prefix.Str::classify($task).'_Task';
110 111 112
	}

}