[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/console/src/Event/ -> CommandErrorEvent.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Console Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Console\Event;
  10  
  11  use Joomla\Console\Application;
  12  use Joomla\Console\Command\AbstractCommand;
  13  use Joomla\Console\ConsoleEvents;
  14  
  15  /**
  16   * Event triggered when an uncaught Throwable is received by the application from a command.
  17   *
  18   * @since  2.0.0
  19   */
  20  class CommandErrorEvent extends ConsoleEvent
  21  {
  22      /**
  23       * The Throwable object with the error data.
  24       *
  25       * @var    \Throwable
  26       * @since  2.0.0
  27       */
  28      private $error;
  29  
  30      /**
  31       * The exit code to use for the application.
  32       *
  33       * @var    integer|null
  34       * @since  2.0.0
  35       */
  36      private $exitCode;
  37  
  38      /**
  39       * Event constructor.
  40       *
  41       * @param   \Throwable            $error        The Throwable object with the error data.
  42       * @param   Application           $application  The active application.
  43       * @param   AbstractCommand|null  $command      The command being executed.
  44       *
  45       * @since   2.0.0
  46       */
  47  	public function __construct(\Throwable $error, Application $application, ?AbstractCommand $command = null)
  48      {
  49          parent::__construct(ConsoleEvents::COMMAND_ERROR, $application, $command);
  50  
  51          $this->error = $error;
  52      }
  53  
  54      /**
  55       * Get the error object.
  56       *
  57       * @return  \Throwable
  58       *
  59       * @since   2.0.0
  60       */
  61  	public function getError(): \Throwable
  62      {
  63          return $this->error;
  64      }
  65  
  66      /**
  67       * Gets the exit code.
  68       *
  69       * @return  integer
  70       *
  71       * @since   2.0.0
  72       */
  73  	public function getExitCode(): int
  74      {
  75          if ($this->exitCode !== null)
  76          {
  77              return $this->exitCode;
  78          }
  79  
  80          return \is_int($this->error->getCode()) && $this->error->getCode() !== 0 ? $this->error->getCode() : 1;
  81      }
  82  
  83      /**
  84       * Set the error object.
  85       *
  86       * @param   \Throwable  $error  The error object to set to the event.
  87       *
  88       * @return  void
  89       *
  90       * @since   2.0.0
  91       */
  92  	public function setError(\Throwable $error): void
  93      {
  94          $this->error = $error;
  95      }
  96  
  97      /**
  98       * Sets the exit code.
  99       *
 100       * @param   integer  $exitCode  The command exit code.
 101       *
 102       * @return  void
 103       *
 104       * @since   2.0.0
 105       */
 106  	public function setExitCode(int $exitCode): void
 107      {
 108          $this->exitCode = $exitCode;
 109  
 110          $r = new \ReflectionProperty($this->error, 'code');
 111          $r->setAccessible(true);
 112          $r->setValue($this->error, $this->exitCode);
 113      }
 114  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer