[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/HTML/ -> Registry.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\HTML;
  11  
  12  // phpcs:disable PSR1.Files.SideEffects
  13  \defined('JPATH_PLATFORM') or die;
  14  // phpcs:enable PSR1.Files.SideEffects
  15  
  16  /**
  17   * Service registry for JHtml services
  18   *
  19   * @since  4.0.0
  20   */
  21  final class Registry
  22  {
  23      /**
  24       * Mapping array of the core CMS JHtml helpers
  25       *
  26       * @var    array
  27       * @since  4.0.0
  28       */
  29      private $serviceMap = [
  30          'access'          => Helpers\Access::class,
  31          'actionsdropdown' => Helpers\ActionsDropdown::class,
  32          'adminlanguage'   => Helpers\AdminLanguage::class,
  33          'behavior'        => Helpers\Behavior::class,
  34          'bootstrap'       => Helpers\Bootstrap::class,
  35          'category'        => Helpers\Category::class,
  36          'content'         => Helpers\Content::class,
  37          'contentlanguage' => Helpers\ContentLanguage::class,
  38          'date'            => Helpers\Date::class,
  39          'debug'           => Helpers\Debug::class,
  40          'draggablelist'   => Helpers\DraggableList::class,
  41          'dropdown'        => Helpers\Dropdown::class,
  42          'email'           => Helpers\Email::class,
  43          'form'            => Helpers\Form::class,
  44          'formbehavior'    => Helpers\FormBehavior::class,
  45          'grid'            => Helpers\Grid::class,
  46          'icons'           => Helpers\Icons::class,
  47          'jgrid'           => Helpers\JGrid::class,
  48          'jquery'          => Helpers\Jquery::class,
  49          'links'           => Helpers\Links::class,
  50          'list'            => Helpers\ListHelper::class,
  51          'menu'            => Helpers\Menu::class,
  52          'number'          => Helpers\Number::class,
  53          'searchtools'     => Helpers\SearchTools::class,
  54          'select'          => Helpers\Select::class,
  55          'sidebar'         => Helpers\Sidebar::class,
  56          'sortablelist'    => Helpers\SortableList::class,
  57          'string'          => Helpers\StringHelper::class,
  58          'tag'             => Helpers\Tag::class,
  59          'tel'             => Helpers\Telephone::class,
  60          'uitab'           => Helpers\UiTab::class,
  61          'user'            => Helpers\User::class,
  62          'workflowstage'   => Helpers\WorkflowStage::class,
  63      ];
  64  
  65      /**
  66       * Get the service for a given key
  67       *
  68       * @param   string  $key  The service key to look up
  69       *
  70       * @return  string|object
  71       *
  72       * @since   4.0.0
  73       */
  74      public function getService(string $key)
  75      {
  76          if (!$this->hasService($key)) {
  77              throw new \InvalidArgumentException("The '$key' service key is not registered.");
  78          }
  79  
  80          return $this->serviceMap[$key];
  81      }
  82  
  83      /**
  84       * Check if the registry has a service for the given key
  85       *
  86       * @param   string  $key  The service key to look up
  87       *
  88       * @return  boolean
  89       *
  90       * @since   4.0.0
  91       */
  92      public function hasService(string $key): bool
  93      {
  94          return isset($this->serviceMap[$key]);
  95      }
  96  
  97      /**
  98       * Register a service
  99       *
 100       * @param   string         $key      The service key to be registered
 101       * @param   string|object  $handler  The handler for the service as either a PHP class name or class object
 102       * @param   boolean        $replace  Flag indicating the service key may replace an existing definition
 103       *
 104       * @return  void
 105       *
 106       * @since   4.0.0
 107       */
 108      public function register(string $key, $handler, bool $replace = false)
 109      {
 110          // If the key exists already and we aren't instructed to replace existing services, bail early
 111          if (isset($this->serviceMap[$key]) && !$replace) {
 112              throw new \RuntimeException("The '$key' service key is already registered.");
 113          }
 114  
 115          // If the handler is a string, it must be a class that exists
 116          if (\is_string($handler) && !class_exists($handler)) {
 117              throw new \RuntimeException("The '$handler' class for service key '$key' does not exist.");
 118          }
 119  
 120          // Otherwise the handler must be a class object
 121          if (!\is_string($handler) && !\is_object($handler)) {
 122              throw new \RuntimeException(
 123                  sprintf(
 124                      'The handler for service key %1$s must be a PHP class name or class object, a %2$s was given.',
 125                      $key,
 126                      \gettype($handler)
 127                  )
 128              );
 129          }
 130  
 131          $this->serviceMap[$key] = $handler;
 132      }
 133  }


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