[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/application/src/ -> AbstractApplication.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Application 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\Application;
  10  
  11  use Joomla\Event\DispatcherAwareInterface;
  12  use Joomla\Event\DispatcherAwareTrait;
  13  use Joomla\Event\EventInterface;
  14  use Joomla\Registry\Registry;
  15  use Psr\Log\LoggerAwareInterface;
  16  use Psr\Log\LoggerAwareTrait;
  17  use Psr\Log\LoggerInterface;
  18  use Psr\Log\NullLogger;
  19  
  20  /**
  21   * Joomla Framework Base Application Class
  22   *
  23   * @since  1.0
  24   */
  25  abstract class AbstractApplication implements ConfigurationAwareApplicationInterface, LoggerAwareInterface, DispatcherAwareInterface
  26  {
  27      use LoggerAwareTrait, DispatcherAwareTrait;
  28  
  29      /**
  30       * The application configuration object.
  31       *
  32       * @var    Registry
  33       * @since  1.0
  34       */
  35      protected $config;
  36  
  37      /**
  38       * Class constructor.
  39       *
  40       * @param   Registry  $config  An optional argument to provide dependency injection for the application's config object.  If the argument
  41       *                             is a Registry object that object will become the application's config object, otherwise a default config
  42       *                             object is created.
  43       *
  44       * @since   1.0
  45       */
  46  	public function __construct(Registry $config = null)
  47      {
  48          $this->config = $config ?: new Registry;
  49  
  50          // Set the execution datetime and timestamp;
  51          $this->set('execution.datetime', gmdate('Y-m-d H:i:s'));
  52          $this->set('execution.timestamp', time());
  53          $this->set('execution.microtimestamp', microtime(true));
  54  
  55          $this->initialise();
  56      }
  57  
  58      /**
  59       * Method to close the application.
  60       *
  61       * @param   integer  $code  The exit code (optional; default is 0).
  62       *
  63       * @return  void
  64       *
  65       * @codeCoverageIgnore
  66       * @since   1.0
  67       */
  68  	public function close($code = 0)
  69      {
  70          exit($code);
  71      }
  72  
  73      /**
  74       * Dispatches an application event if the dispatcher has been set.
  75       *
  76       * @param   string               $eventName  The event to dispatch.
  77       * @param   EventInterface|null  $event      The event object.
  78       *
  79       * @return  EventInterface|null  The dispatched event or null if no dispatcher is set
  80       *
  81       * @since   2.0.0
  82       */
  83  	protected function dispatchEvent(string $eventName, ?EventInterface $event = null): ?EventInterface
  84      {
  85          try
  86          {
  87              $dispatcher = $this->getDispatcher();
  88          }
  89          catch (\UnexpectedValueException $exception)
  90          {
  91              return null;
  92          }
  93  
  94          return $dispatcher->dispatch($eventName, $event ?: new Event\ApplicationEvent($eventName, $this));
  95      }
  96  
  97      /**
  98       * Method to run the application routines.
  99       *
 100       * Most likely you will want to instantiate a controller and execute it, or perform some sort of task directly.
 101       *
 102       * @return  mixed
 103       *
 104       * @since   1.0
 105       */
 106      abstract protected function doExecute();
 107  
 108      /**
 109       * Execute the application.
 110       *
 111       * @return  void
 112       *
 113       * @since   1.0
 114       */
 115  	public function execute()
 116      {
 117          try
 118          {
 119              $this->dispatchEvent(ApplicationEvents::BEFORE_EXECUTE);
 120  
 121              // Perform application routines.
 122              $this->doExecute();
 123  
 124              $this->dispatchEvent(ApplicationEvents::AFTER_EXECUTE);
 125          }
 126          catch (\Throwable $throwable)
 127          {
 128              $this->dispatchEvent(ApplicationEvents::ERROR, new Event\ApplicationErrorEvent($throwable, $this));
 129          }
 130      }
 131  
 132      /**
 133       * Returns a property of the object or the default value if the property is not set.
 134       *
 135       * @param   string  $key      The name of the property.
 136       * @param   mixed   $default  The default value (optional) if none is set.
 137       *
 138       * @return  mixed   The value of the configuration.
 139       *
 140       * @since   1.0
 141       */
 142  	public function get($key, $default = null)
 143      {
 144          return $this->config->get($key, $default);
 145      }
 146  
 147      /**
 148       * Get the logger.
 149       *
 150       * @return  LoggerInterface
 151       *
 152       * @since   1.0
 153       */
 154  	public function getLogger()
 155      {
 156          // If a logger hasn't been set, use NullLogger
 157          if (!($this->logger instanceof LoggerInterface))
 158          {
 159              $this->setLogger(new NullLogger);
 160          }
 161  
 162          return $this->logger;
 163      }
 164  
 165      /**
 166       * Custom initialisation method.
 167       *
 168       * Called at the end of the AbstractApplication::__construct method.
 169       * This is for developers to inject initialisation code for their application classes.
 170       *
 171       * @return  void
 172       *
 173       * @codeCoverageIgnore
 174       * @since   1.0
 175       */
 176  	protected function initialise()
 177      {
 178      }
 179  
 180      /**
 181       * Modifies a property of the object, creating it if it does not already exist.
 182       *
 183       * @param   string  $key    The name of the property.
 184       * @param   mixed   $value  The value of the property to set (optional).
 185       *
 186       * @return  mixed   Previous value of the property
 187       *
 188       * @since   1.0
 189       */
 190  	public function set($key, $value = null)
 191      {
 192          $previous = $this->config->get($key);
 193          $this->config->set($key, $value);
 194  
 195          return $previous;
 196      }
 197  
 198      /**
 199       * Sets the configuration for the application.
 200       *
 201       * @param   Registry  $config  A registry object holding the configuration.
 202       *
 203       * @return  $this
 204       *
 205       * @since   1.0
 206       */
 207  	public function setConfiguration(Registry $config)
 208      {
 209          $this->config = $config;
 210  
 211          return $this;
 212      }
 213  }


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