/vendor/laravel/framework/src/Illuminate/Console/Application.php

https://gitlab.com/Pasantias/pasantiasASLG · PHP · 172 lines · 74 code · 27 blank · 71 comment · 1 complexity · 5e3dd369ffd170ce9ab9a5d3d4a688f9 MD5 · raw file

  1. <?php
  2. namespace Illuminate\Console;
  3. use Illuminate\Contracts\Events\Dispatcher;
  4. use Illuminate\Contracts\Container\Container;
  5. use Symfony\Component\Console\Input\ArrayInput;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Output\BufferedOutput;
  8. use Symfony\Component\Console\Application as SymfonyApplication;
  9. use Symfony\Component\Console\Command\Command as SymfonyCommand;
  10. use Illuminate\Contracts\Console\Application as ApplicationContract;
  11. class Application extends SymfonyApplication implements ApplicationContract
  12. {
  13. /**
  14. * The Laravel application instance.
  15. *
  16. * @var \Illuminate\Contracts\Container\Container
  17. */
  18. protected $laravel;
  19. /**
  20. * The output from the previous command.
  21. *
  22. * @var \Symfony\Component\Console\Output\BufferedOutput
  23. */
  24. protected $lastOutput;
  25. /**
  26. * Create a new Artisan console application.
  27. *
  28. * @param \Illuminate\Contracts\Container\Container $laravel
  29. * @param \Illuminate\Contracts\Events\Dispatcher $events
  30. * @param string $version
  31. * @return void
  32. */
  33. public function __construct(Container $laravel, Dispatcher $events, $version)
  34. {
  35. parent::__construct('Laravel Framework', $version);
  36. $this->laravel = $laravel;
  37. $this->setAutoExit(false);
  38. $this->setCatchExceptions(false);
  39. $events->fire('artisan.start', [$this]);
  40. }
  41. /**
  42. * Run an Artisan console command by name.
  43. *
  44. * @param string $command
  45. * @param array $parameters
  46. * @return int
  47. */
  48. public function call($command, array $parameters = [])
  49. {
  50. $parameters = collect($parameters)->prepend($command);
  51. $this->lastOutput = new BufferedOutput;
  52. $this->setCatchExceptions(false);
  53. $result = $this->run(new ArrayInput($parameters->toArray()), $this->lastOutput);
  54. $this->setCatchExceptions(true);
  55. return $result;
  56. }
  57. /**
  58. * Get the output for the last run command.
  59. *
  60. * @return string
  61. */
  62. public function output()
  63. {
  64. return $this->lastOutput ? $this->lastOutput->fetch() : '';
  65. }
  66. /**
  67. * Add a command to the console.
  68. *
  69. * @param \Symfony\Component\Console\Command\Command $command
  70. * @return \Symfony\Component\Console\Command\Command
  71. */
  72. public function add(SymfonyCommand $command)
  73. {
  74. if ($command instanceof Command) {
  75. $command->setLaravel($this->laravel);
  76. }
  77. return $this->addToParent($command);
  78. }
  79. /**
  80. * Add the command to the parent instance.
  81. *
  82. * @param \Symfony\Component\Console\Command\Command $command
  83. * @return \Symfony\Component\Console\Command\Command
  84. */
  85. protected function addToParent(SymfonyCommand $command)
  86. {
  87. return parent::add($command);
  88. }
  89. /**
  90. * Add a command, resolving through the application.
  91. *
  92. * @param string $command
  93. * @return \Symfony\Component\Console\Command\Command
  94. */
  95. public function resolve($command)
  96. {
  97. return $this->add($this->laravel->make($command));
  98. }
  99. /**
  100. * Resolve an array of commands through the application.
  101. *
  102. * @param array|mixed $commands
  103. * @return $this
  104. */
  105. public function resolveCommands($commands)
  106. {
  107. $commands = is_array($commands) ? $commands : func_get_args();
  108. foreach ($commands as $command) {
  109. $this->resolve($command);
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Get the default input definitions for the applications.
  115. *
  116. * This is used to add the --env option to every available command.
  117. *
  118. * @return \Symfony\Component\Console\Input\InputDefinition
  119. */
  120. protected function getDefaultInputDefinition()
  121. {
  122. $definition = parent::getDefaultInputDefinition();
  123. $definition->addOption($this->getEnvironmentOption());
  124. return $definition;
  125. }
  126. /**
  127. * Get the global environment option for the definition.
  128. *
  129. * @return \Symfony\Component\Console\Input\InputOption
  130. */
  131. protected function getEnvironmentOption()
  132. {
  133. $message = 'The environment the command should run under.';
  134. return new InputOption('--env', null, InputOption::VALUE_OPTIONAL, $message);
  135. }
  136. /**
  137. * Get the Laravel application instance.
  138. *
  139. * @return \Illuminate\Contracts\Foundation\Application
  140. */
  141. public function getLaravel()
  142. {
  143. return $this->laravel;
  144. }
  145. }