[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Toolbar/ -> ContainerAwareToolbarFactory.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\Toolbar;
  11  
  12  use Joomla\CMS\Filesystem\Path;
  13  use Joomla\CMS\Filter\InputFilter;
  14  use Joomla\CMS\Language\Text;
  15  use Joomla\CMS\Log\Log;
  16  use Joomla\DI\ContainerAwareInterface;
  17  use Joomla\DI\ContainerAwareTrait;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('_JEXEC') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * Default factory for creating toolbar objects
  25   *
  26   * @since  4.0.0
  27   */
  28  class ContainerAwareToolbarFactory implements ToolbarFactoryInterface, ContainerAwareInterface
  29  {
  30      use ContainerAwareTrait;
  31  
  32      /**
  33       * Creates a new toolbar button.
  34       *
  35       * @param   Toolbar  $toolbar  The Toolbar instance to attach to the button
  36       * @param   string   $type     Button Type
  37       *
  38       * @return  ToolbarButton
  39       *
  40       * @since   3.8.0
  41       * @throws  \InvalidArgumentException
  42       */
  43      public function createButton(Toolbar $toolbar, string $type): ToolbarButton
  44      {
  45          $normalisedType = ucfirst($type);
  46          $buttonClass    = $this->loadButtonClass($normalisedType);
  47  
  48          if (!$buttonClass) {
  49              $dirs = $toolbar->getButtonPath();
  50  
  51              $file = InputFilter::getInstance()->clean(str_replace('_', DIRECTORY_SEPARATOR, strtolower($type)) . '.php', 'path');
  52  
  53              if ($buttonFile = Path::find($dirs, $file)) {
  54                  include_once $buttonFile;
  55              } else {
  56                  Log::add(Text::sprintf('JLIB_HTML_BUTTON_NO_LOAD', $buttonClass, $buttonFile), Log::WARNING, 'jerror');
  57  
  58                  throw new \InvalidArgumentException(Text::sprintf('JLIB_HTML_BUTTON_NO_LOAD', $buttonClass, $buttonFile));
  59              }
  60          }
  61  
  62          if (!class_exists($buttonClass)) {
  63              throw new \InvalidArgumentException(sprintf('Class `%1$s` does not exist, could not create a toolbar button.', $buttonClass));
  64          }
  65  
  66          // Check for a possible service from the container otherwise manually instantiate the class
  67          if ($this->getContainer()->has($buttonClass)) {
  68              return $this->getContainer()->get($buttonClass);
  69          }
  70  
  71          /** @var ToolbarButton $button */
  72          $button = new $buttonClass($normalisedType);
  73  
  74          return $button->setParent($toolbar);
  75      }
  76  
  77      /**
  78       * Creates a new Toolbar object.
  79       *
  80       * @param   string  $name  The toolbar name.
  81       *
  82       * @return  Toolbar
  83       *
  84       * @since   4.0.0
  85       */
  86      public function createToolbar(string $name = 'toolbar'): Toolbar
  87      {
  88          return new Toolbar($name, $this);
  89      }
  90  
  91      /**
  92       * Load the button class including the deprecated ones.
  93       *
  94       * @param   string  $type  Button Type (normalized)
  95       *
  96       * @return  string|null
  97       *
  98       * @since   4.0.0
  99       */
 100      private function loadButtonClass(string $type)
 101      {
 102          $buttonClasses = [
 103              'Joomla\\CMS\\Toolbar\\Button\\' . $type . 'Button',
 104              // @deprecated 5.0
 105              'JToolbarButton' . $type,
 106          ];
 107  
 108          foreach ($buttonClasses as $buttonClass) {
 109              if (!class_exists($buttonClass)) {
 110                  continue;
 111              }
 112  
 113              return $buttonClass;
 114          }
 115  
 116          return null;
 117      }
 118  }


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