command.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. // Once the bundle has been started, we will attempt to resolve the
  25. // task instance. Tasks may be resolved through the file system or
  26. // through the application IoC container.
  27. if (is_null($task = static::resolve($bundle, $task)))
  28. {
  29. throw new \Exception("Sorry, I can't find that task.");
  30. }
  31. $task->$method(array_slice($arguments, 1));
  32. }
  33. /**
  34. * Parse the task name to extract the bundle, task, and method.
  35. *
  36. * @param string $task
  37. * @return array
  38. */
  39. protected static function parse($task)
  40. {
  41. list($bundle, $task) = Bundle::parse($task);
  42. // Extract the task method from the task string. Methods are called
  43. // on tasks by separating the task and method with a single colon.
  44. // If no task is specified, "run" is used as the default.
  45. if (str_contains($task, ':'))
  46. {
  47. list($task, $method) = explode(':', $task);
  48. }
  49. else
  50. {
  51. $method = 'run';
  52. }
  53. return array($bundle, $task, $method);
  54. }
  55. /**
  56. * Resolve an instance of the given task name.
  57. *
  58. * @param string $bundle
  59. * @param string $task
  60. * @return object
  61. */
  62. public static function resolve($bundle, $task)
  63. {
  64. $identifier = Bundle::identifier($bundle, $task);
  65. // First we'll check to see if the task has been registered in the
  66. // application IoC container. This allows all dependencies to be
  67. // injected into tasks for more testability.
  68. if (IoC::registered("task: {$identifier}"))
  69. {
  70. return IoC::resolve("task: {$identifier}");
  71. }
  72. // If the task file exists, we'll format the bundle and task name
  73. // into a task class name and resolve an instance of the so that
  74. // the requested method may be executed.
  75. if (file_exists($path = Bundle::path($bundle).'tasks/'.$task.EXT))
  76. {
  77. require $path;
  78. $task = static::format($bundle, $task);
  79. return new $task;
  80. }
  81. }
  82. /**
  83. * Format a bundle and task into a task class name.
  84. *
  85. * @param string $bundle
  86. * @param string $task
  87. * @return string
  88. */
  89. protected static function format($bundle, $task)
  90. {
  91. $prefix = Bundle::class_prefix($bundle);
  92. return '\\'.$prefix.Str::classify($task).'_Task';
  93. }
  94. }