command.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php namespace Laravel\CLI;
  2. use Laravel\IoC;
  3. use Laravel\Str;
  4. use Laravel\Bundle;
  5. class Command {
  6. /**
  7. * Run a CLI task with the given arguments.
  8. *
  9. * @param array $arguments
  10. * @return void
  11. */
  12. public static function run($arguments = array())
  13. {
  14. if ( ! isset($arguments[0]))
  15. {
  16. throw new \Exception("Whoops! You forgot to provide the task name.");
  17. }
  18. list($bundle, $task, $method) = static::parse($arguments[0]);
  19. // If the task exists within a bundle, we will start the bundle so that
  20. // any dependencies can be registered in the application IoC container.
  21. // If the task is registered in the container, it will be resolved
  22. // via the container instead of by this class.
  23. if (Bundle::exists($bundle)) Bundle::start($bundle);
  24. if (is_null($task = static::resolve($bundle, $task)))
  25. {
  26. throw new \Exception("Sorry, I can't find that task.");
  27. }
  28. $task->$method(array_slice($arguments, 1));
  29. }
  30. /**
  31. * Parse the task name to extract the bundle, task, and method.
  32. *
  33. * @param string $task
  34. * @return array
  35. */
  36. protected static function parse($task)
  37. {
  38. list($bundle, $task) = Bundle::parse($task);
  39. // Extract the task method from the task string. Methods are called
  40. // on tasks by separating the task and method with a single colon.
  41. // If no task is specified, "run" is used as the default method.
  42. if (str_contains($task, ':'))
  43. {
  44. list($task, $method) = explode(':', $task);
  45. }
  46. else
  47. {
  48. $method = 'run';
  49. }
  50. return array($bundle, $task, $method);
  51. }
  52. /**
  53. * Resolve an instance of the given task name.
  54. *
  55. * @param string $bundle
  56. * @param string $task
  57. * @return object
  58. */
  59. public static function resolve($bundle, $task)
  60. {
  61. $identifier = Bundle::identifier($bundle, $task);
  62. // First we'll check to see if the task has been registered in the
  63. // application IoC container. This allows all dependencies to be
  64. // injected into tasks for more testability.
  65. if (IoC::registered("task: {$identifier}"))
  66. {
  67. return IoC::resolve("task: {$identifier}");
  68. }
  69. // If the task file exists, we'll format the bundle and task name
  70. // into a task class name and resolve an instance of the so that
  71. // the requested method may be executed.
  72. if (file_exists($path = Bundle::path($bundle).'tasks/'.$task.EXT))
  73. {
  74. require $path;
  75. $task = static::format($bundle, $task);
  76. return new $task;
  77. }
  78. }
  79. /**
  80. * Format a bundle and task into a task class name.
  81. *
  82. * @param string $bundle
  83. * @param string $task
  84. * @return string
  85. */
  86. protected static function format($bundle, $task)
  87. {
  88. $prefix = Bundle::class_prefix($bundle);
  89. return '\\'.$prefix.Str::clasify($task).'_Task';
  90. }
  91. }