[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Document/ -> Factory.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2017 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\Document;
  11  
  12  use Joomla\CMS\Cache\CacheControllerFactoryAwareInterface;
  13  use Joomla\CMS\Cache\CacheControllerFactoryAwareTrait;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('_JEXEC') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * Default factory for creating Document objects
  21   *
  22   * @since  4.0.0
  23   */
  24  class Factory implements FactoryInterface
  25  {
  26      use CacheControllerFactoryAwareTrait;
  27  
  28      /**
  29       * Creates a new Document object for the requested format.
  30       *
  31       * @param   string  $type        The document type to instantiate
  32       * @param   array   $attributes  Array of attributes
  33       *
  34       * @return  Document
  35       *
  36       * @since   4.0.0
  37       */
  38      public function createDocument(string $type = 'html', array $attributes = []): Document
  39      {
  40          $type  = preg_replace('/[^A-Z0-9_\.-]/i', '', $type);
  41          $ntype = null;
  42  
  43          $class = __NAMESPACE__ . '\\' . ucfirst($type) . 'Document';
  44  
  45          if (!class_exists($class)) {
  46              $class = 'JDocument' . ucfirst($type);
  47          }
  48  
  49          if (!class_exists($class)) {
  50              $ntype = $type;
  51              $class = RawDocument::class;
  52          }
  53  
  54          // Inject this factory into the document unless one was provided
  55          if (!isset($attributes['factory'])) {
  56              $attributes['factory'] = $this;
  57          }
  58  
  59          /** @var Document $instance */
  60          $instance = new $class($attributes);
  61  
  62          if (!\is_null($ntype)) {
  63              // Set the type to the Document type originally requested
  64              $instance->setType($ntype);
  65          }
  66  
  67          if ($instance instanceof CacheControllerFactoryAwareInterface) {
  68              $instance->setCacheControllerFactory($this->getCacheControllerFactory());
  69          }
  70  
  71          return $instance;
  72      }
  73  
  74      /**
  75       * Creates a new renderer object.
  76       *
  77       * @param   Document  $document  The Document instance to attach to the renderer
  78       * @param   string    $type      The renderer type to instantiate
  79       * @param   string    $docType   The document type the renderer is part of
  80       *
  81       * @return  RendererInterface
  82       *
  83       * @since   4.0.0
  84       */
  85      public function createRenderer(Document $document, string $type, string $docType = ''): RendererInterface
  86      {
  87          $docType = $docType ? ucfirst($docType) : ucfirst($document->getType());
  88  
  89          // Determine the path and class
  90          $class = __NAMESPACE__ . '\\Renderer\\' . $docType . '\\' . ucfirst($type) . 'Renderer';
  91  
  92          if (!class_exists($class)) {
  93              $class = 'JDocumentRenderer' . $docType . ucfirst($type);
  94          }
  95  
  96          if (!class_exists($class)) {
  97              // "Legacy" class name structure
  98              $class = '\\JDocumentRenderer' . $type;
  99  
 100              if (!class_exists($class)) {
 101                  throw new \RuntimeException(sprintf('Unable to load renderer class %s', $type), 500);
 102              }
 103          }
 104  
 105          $instance = new $class($document);
 106  
 107          if ($instance instanceof CacheControllerFactoryAwareInterface) {
 108              $instance->setCacheControllerFactory($this->getCacheControllerFactory());
 109          }
 110  
 111          return $instance;
 112      }
 113  }


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