[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Application/ -> CliApplication.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2011 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Application;
  11  
  12  use Joomla\Application\AbstractApplication;
  13  use Joomla\CMS\Application\CLI\CliInput;
  14  use Joomla\CMS\Application\CLI\CliOutput;
  15  use Joomla\CMS\Application\CLI\Output\Stdout;
  16  use Joomla\CMS\Extension\ExtensionManagerTrait;
  17  use Joomla\CMS\Factory;
  18  use Joomla\CMS\Language\Language;
  19  use Joomla\DI\Container;
  20  use Joomla\DI\ContainerAwareTrait;
  21  use Joomla\Event\DispatcherAwareInterface;
  22  use Joomla\Event\DispatcherAwareTrait;
  23  use Joomla\Event\DispatcherInterface;
  24  use Joomla\Input\Input;
  25  use Joomla\Registry\Registry;
  26  use Joomla\Session\SessionInterface;
  27  
  28  // phpcs:disable PSR1.Files.SideEffects
  29  \defined('JPATH_PLATFORM') or die;
  30  // phpcs:enable PSR1.Files.SideEffects
  31  
  32  /**
  33   * Base class for a Joomla! command line application.
  34   *
  35   * @since       2.5.0
  36   * @deprecated  5.0  Use the ConsoleApplication instead
  37   */
  38  abstract class CliApplication extends AbstractApplication implements DispatcherAwareInterface, CMSApplicationInterface
  39  {
  40      use DispatcherAwareTrait;
  41      use EventAware;
  42      use IdentityAware;
  43      use ContainerAwareTrait;
  44      use ExtensionManagerTrait;
  45      use ExtensionNamespaceMapper;
  46  
  47      /**
  48       * Output object
  49       *
  50       * @var    CliOutput
  51       * @since  4.0.0
  52       */
  53      protected $output;
  54  
  55      /**
  56       * The input.
  57       *
  58       * @var    \Joomla\Input\Input
  59       * @since  4.0.0
  60       */
  61      protected $input = null;
  62  
  63      /**
  64       * CLI Input object
  65       *
  66       * @var    CliInput
  67       * @since  4.0.0
  68       */
  69      protected $cliInput;
  70  
  71      /**
  72       * The application language object.
  73       *
  74       * @var    Language
  75       * @since  4.0.0
  76       */
  77      protected $language;
  78  
  79      /**
  80       * The application message queue.
  81       *
  82       * @var    array
  83       * @since  4.0.0
  84       */
  85      protected $messages = [];
  86  
  87      /**
  88       * The application instance.
  89       *
  90       * @var    CliApplication
  91       * @since  1.7.0
  92       */
  93      protected static $instance;
  94  
  95      /**
  96       * Class constructor.
  97       *
  98       * @param   Input                $input       An optional argument to provide dependency injection for the application's
  99       *                                            input object.  If the argument is a JInputCli object that object will become
 100       *                                            the application's input object, otherwise a default input object is created.
 101       * @param   Registry             $config      An optional argument to provide dependency injection for the application's
 102       *                                            config object.  If the argument is a Registry object that object will become
 103       *                                            the application's config object, otherwise a default config object is created.
 104       * @param   CliOutput            $output      The output handler.
 105       * @param   CliInput             $cliInput    The CLI input handler.
 106       * @param   DispatcherInterface  $dispatcher  An optional argument to provide dependency injection for the application's
 107       *                                            event dispatcher.  If the argument is a DispatcherInterface object that object will become
 108       *                                            the application's event dispatcher, if it is null then the default event dispatcher
 109       *                                            will be created based on the application's loadDispatcher() method.
 110       * @param   Container            $container   Dependency injection container.
 111       *
 112       * @since   1.7.0
 113       */
 114      public function __construct(
 115          Input $input = null,
 116          Registry $config = null,
 117          CliOutput $output = null,
 118          CliInput $cliInput = null,
 119          DispatcherInterface $dispatcher = null,
 120          Container $container = null
 121      ) {
 122          // Close the application if we are not executed from the command line.
 123          if (!\defined('STDOUT') || !\defined('STDIN') || !isset($_SERVER['argv'])) {
 124              $this->close();
 125          }
 126  
 127          $container = $container ?: Factory::getContainer();
 128          $this->setContainer($container);
 129          $this->setDispatcher($dispatcher ?: $container->get(\Joomla\Event\DispatcherInterface::class));
 130  
 131          if (!$container->has('session')) {
 132              $container->alias('session', 'session.cli')
 133                  ->alias('JSession', 'session.cli')
 134                  ->alias(\Joomla\CMS\Session\Session::class, 'session.cli')
 135                  ->alias(\Joomla\Session\Session::class, 'session.cli')
 136                  ->alias(\Joomla\Session\SessionInterface::class, 'session.cli');
 137          }
 138  
 139          $this->input    = new \Joomla\CMS\Input\Cli();
 140          $this->language = Factory::getLanguage();
 141          $this->output   = $output ?: new Stdout();
 142          $this->cliInput = $cliInput ?: new CliInput();
 143  
 144          parent::__construct($config);
 145  
 146          // Set the current directory.
 147          $this->set('cwd', getcwd());
 148  
 149          // Set up the environment
 150          $this->input->set('format', 'cli');
 151      }
 152  
 153      /**
 154       * Magic method to access properties of the application.
 155       *
 156       * @param   string  $name  The name of the property.
 157       *
 158       * @return  mixed   A value if the property name is valid, null otherwise.
 159       *
 160       * @since       4.0.0
 161       * @deprecated  5.0  This is a B/C proxy for deprecated read accesses
 162       */
 163      public function __get($name)
 164      {
 165          switch ($name) {
 166              case 'input':
 167                  @trigger_error(
 168                      'Accessing the input property of the application is deprecated, use the getInput() method instead.',
 169                      E_USER_DEPRECATED
 170                  );
 171  
 172                  return $this->getInput();
 173  
 174              default:
 175                  $trace = debug_backtrace();
 176                  trigger_error(
 177                      sprintf(
 178                          'Undefined property via __get(): %1$s in %2$s on line %3$s',
 179                          $name,
 180                          $trace[0]['file'],
 181                          $trace[0]['line']
 182                      ),
 183                      E_USER_NOTICE
 184                  );
 185          }
 186      }
 187  
 188      /**
 189       * Method to get the application input object.
 190       *
 191       * @return  Input
 192       *
 193       * @since   4.0.0
 194       */
 195      public function getInput(): Input
 196      {
 197          return $this->input;
 198      }
 199  
 200      /**
 201       * Method to get the application language object.
 202       *
 203       * @return  Language  The language object
 204       *
 205       * @since   4.0.0
 206       */
 207      public function getLanguage()
 208      {
 209          return $this->language;
 210      }
 211  
 212      /**
 213       * Returns a reference to the global CliApplication object, only creating it if it doesn't already exist.
 214       *
 215       * This method must be invoked as: $cli = CliApplication::getInstance();
 216       *
 217       * @param   string  $name  The name (optional) of the Application Cli class to instantiate.
 218       *
 219       * @return  CliApplication
 220       *
 221       * @since       1.7.0
 222       * @deprecated  5.0 Load the app through the container
 223       * @throws  \RuntimeException
 224       */
 225      public static function getInstance($name = null)
 226      {
 227          // Only create the object if it doesn't exist.
 228          if (empty(static::$instance)) {
 229              if (!class_exists($name)) {
 230                  throw new \RuntimeException(sprintf('Unable to load application: %s', $name), 500);
 231              }
 232  
 233              static::$instance = new $name();
 234          }
 235  
 236          return static::$instance;
 237      }
 238  
 239      /**
 240       * Execute the application.
 241       *
 242       * @return  void
 243       *
 244       * @since   1.7.0
 245       */
 246      public function execute()
 247      {
 248          $this->createExtensionNamespaceMap();
 249  
 250          // Trigger the onBeforeExecute event
 251          $this->triggerEvent('onBeforeExecute');
 252  
 253          // Perform application routines.
 254          $this->doExecute();
 255  
 256          // Trigger the onAfterExecute event.
 257          $this->triggerEvent('onAfterExecute');
 258      }
 259  
 260      /**
 261       * Get an output object.
 262       *
 263       * @return  CliOutput
 264       *
 265       * @since   4.0.0
 266       */
 267      public function getOutput()
 268      {
 269          return $this->output;
 270      }
 271  
 272      /**
 273       * Get a CLI input object.
 274       *
 275       * @return  CliInput
 276       *
 277       * @since   4.0.0
 278       */
 279      public function getCliInput()
 280      {
 281          return $this->cliInput;
 282      }
 283  
 284      /**
 285       * Write a string to standard output.
 286       *
 287       * @param   string   $text  The text to display.
 288       * @param   boolean  $nl    True (default) to append a new line at the end of the output string.
 289       *
 290       * @return  $this
 291       *
 292       * @since   4.0.0
 293       */
 294      public function out($text = '', $nl = true)
 295      {
 296          $this->getOutput()->out($text, $nl);
 297  
 298          return $this;
 299      }
 300  
 301      /**
 302       * Get a value from standard input.
 303       *
 304       * @return  string  The input string from standard input.
 305       *
 306       * @codeCoverageIgnore
 307       * @since   4.0.0
 308       */
 309      public function in()
 310      {
 311          return $this->getCliInput()->in();
 312      }
 313  
 314      /**
 315       * Set an output object.
 316       *
 317       * @param   CliOutput  $output  CliOutput object
 318       *
 319       * @return  $this
 320       *
 321       * @since   3.3
 322       */
 323      public function setOutput(CliOutput $output)
 324      {
 325          $this->output = $output;
 326  
 327          return $this;
 328      }
 329  
 330      /**
 331       * Enqueue a system message.
 332       *
 333       * @param   string  $msg   The message to enqueue.
 334       * @param   string  $type  The message type.
 335       *
 336       * @return  void
 337       *
 338       * @since   4.0.0
 339       */
 340      public function enqueueMessage($msg, $type = self::MSG_INFO)
 341      {
 342          if (!\array_key_exists($type, $this->messages)) {
 343              $this->messages[$type] = [];
 344          }
 345  
 346          $this->messages[$type][] = $msg;
 347      }
 348  
 349      /**
 350       * Get the system message queue.
 351       *
 352       * @return  array  The system message queue.
 353       *
 354       * @since   4.0.0
 355       */
 356      public function getMessageQueue()
 357      {
 358          return $this->messages;
 359      }
 360  
 361      /**
 362       * Check the client interface by name.
 363       *
 364       * @param   string  $identifier  String identifier for the application interface
 365       *
 366       * @return  boolean  True if this application is of the given type client interface.
 367       *
 368       * @since   4.0.0
 369       */
 370      public function isClient($identifier)
 371      {
 372          return $identifier === 'cli';
 373      }
 374  
 375      /**
 376       * Method to get the application session object.
 377       *
 378       * @return  SessionInterface  The session object
 379       *
 380       * @since   4.0.0
 381       */
 382      public function getSession()
 383      {
 384          return $this->container->get(SessionInterface::class);
 385      }
 386  
 387      /**
 388       * Retrieve the application configuration object.
 389       *
 390       * @return  Registry
 391       *
 392       * @since   4.0.0
 393       */
 394      public function getConfig()
 395      {
 396          return $this->config;
 397      }
 398  
 399      /**
 400       * Flag if the application instance is a CLI or web based application.
 401       *
 402       * Helper function, you should use the native PHP functions to detect if it is a CLI application.
 403       *
 404       * @return  boolean
 405       *
 406       * @since       4.0.0
 407       * @deprecated  5.0  Will be removed without replacements
 408       */
 409      public function isCli()
 410      {
 411          return true;
 412      }
 413  }


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