[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Error/ -> AbstractRenderer.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2005 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  namespace Joomla\CMS\Error;
  11  
  12  use Joomla\CMS\Document\Document;
  13  use Joomla\CMS\Document\FactoryInterface;
  14  use Joomla\CMS\Factory;
  15  
  16  // phpcs:disable PSR1.Files.SideEffects
  17  \defined('JPATH_PLATFORM') or die;
  18  // phpcs:enable PSR1.Files.SideEffects
  19  
  20  /**
  21   * Base class for error page renderers
  22   *
  23   * @since  4.0.0
  24   */
  25  abstract class AbstractRenderer implements RendererInterface
  26  {
  27      /**
  28       * The Document instance
  29       *
  30       * @var    Document
  31       * @since  4.0.0
  32       */
  33      protected $document;
  34  
  35      /**
  36       * The format (type) of the error page
  37       *
  38       * @var    string
  39       * @since  4.0.0
  40       */
  41      protected $type;
  42  
  43      /**
  44       * Retrieve the Document instance attached to this renderer
  45       *
  46       * @return  Document
  47       *
  48       * @since   4.0.0
  49       */
  50      public function getDocument(): Document
  51      {
  52          // Load the document if not already
  53          if (!$this->document) {
  54              $this->document = $this->loadDocument();
  55          }
  56  
  57          return $this->document;
  58      }
  59  
  60      /**
  61       * Get a renderer instance for the given type
  62       *
  63       * @param   string  $type  The type of renderer to fetch
  64       *
  65       * @return  static
  66       *
  67       * @since   4.0.0
  68       * @throws  \InvalidArgumentException
  69       */
  70      public static function getRenderer(string $type)
  71      {
  72          // Build the class name
  73          $class = __NAMESPACE__ . '\\Renderer\\' . ucfirst(strtolower($type)) . 'Renderer';
  74  
  75          // First check if an object may exist in the container and prefer that over everything else
  76          if (Factory::getContainer()->has($class)) {
  77              return Factory::getContainer()->get($class);
  78          }
  79  
  80          // Next check if a local class exists and use that
  81          if (class_exists($class)) {
  82              return new $class();
  83          }
  84  
  85          // 404 Resource Not Found
  86          throw new \InvalidArgumentException(sprintf('There is not an error renderer for the "%s" format.', $type));
  87      }
  88  
  89      /**
  90       * Create the Document object for this renderer
  91       *
  92       * @return  Document
  93       *
  94       * @since   4.0.0
  95       */
  96      protected function loadDocument(): Document
  97      {
  98          $attributes = [
  99              'charset'   => 'utf-8',
 100              'lineend'   => 'unix',
 101              'tab'       => "\t",
 102              'language'  => 'en-GB',
 103              'direction' => 'ltr',
 104          ];
 105  
 106          // If there is a Language instance in Factory then let's pull the language and direction from its metadata
 107          if (Factory::$language) {
 108              $attributes['language']  = Factory::getLanguage()->getTag();
 109              $attributes['direction'] = Factory::getLanguage()->isRtl() ? 'rtl' : 'ltr';
 110          }
 111  
 112          return Factory::getContainer()->get(FactoryInterface::class)->createDocument($this->type, $attributes);
 113      }
 114  }


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