[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Dispatcher/ -> ComponentDispatcher.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\Dispatcher;
  11  
  12  use Joomla\CMS\Access\Exception\NotAllowed;
  13  use Joomla\CMS\Application\CMSApplicationInterface;
  14  use Joomla\CMS\Component\ComponentHelper;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\MVC\Controller\BaseController;
  17  use Joomla\CMS\MVC\Factory\MVCFactoryInterface;
  18  use Joomla\Input\Input;
  19  
  20  // phpcs:disable PSR1.Files.SideEffects
  21  \defined('_JEXEC') or die;
  22  // phpcs:enable PSR1.Files.SideEffects
  23  
  24  /**
  25   * Base class for a Joomla Component Dispatcher
  26   *
  27   * Dispatchers are responsible for checking ACL of a component if appropriate and
  28   * choosing an appropriate controller (and if necessary, a task) and executing it.
  29   *
  30   * @since  4.0.0
  31   */
  32  class ComponentDispatcher extends Dispatcher
  33  {
  34      /**
  35       * The URL option for the component.
  36       *
  37       * @var    string
  38       * @since  4.0.0
  39       */
  40      protected $option;
  41  
  42      /**
  43       * The MVC factory
  44       *
  45       * @var  MVCFactoryInterface
  46       *
  47       * @since   4.0.0
  48       */
  49      protected $mvcFactory;
  50  
  51      /**
  52       * Constructor for ComponentDispatcher
  53       *
  54       * @param   CMSApplicationInterface  $app         The application instance
  55       * @param   Input                    $input       The input instance
  56       * @param   MVCFactoryInterface      $mvcFactory  The MVC factory instance
  57       *
  58       * @since   4.0.0
  59       */
  60      public function __construct(CMSApplicationInterface $app, Input $input, MVCFactoryInterface $mvcFactory)
  61      {
  62          parent::__construct($app, $input);
  63  
  64          $this->mvcFactory = $mvcFactory;
  65  
  66          // If option is not provided, detect it from dispatcher class name, ie ContentDispatcher
  67          if (empty($this->option)) {
  68              $this->option = ComponentHelper::getComponentName(
  69                  $this,
  70                  str_replace('com_', '', $input->get('option'))
  71              );
  72          }
  73  
  74          $this->loadLanguage();
  75      }
  76  
  77      /**
  78       * Load the language
  79       *
  80       * @return  void
  81       *
  82       * @since   4.0.0
  83       */
  84      protected function loadLanguage()
  85      {
  86          // Load common and local language files.
  87          $this->app->getLanguage()->load($this->option, JPATH_BASE) ||
  88          $this->app->getLanguage()->load($this->option, JPATH_COMPONENT);
  89      }
  90  
  91      /**
  92       * Method to check component access permission
  93       *
  94       * @return  void
  95       *
  96       * @since   4.0.0
  97       */
  98      protected function checkAccess()
  99      {
 100          // Check the user has permission to access this component if in the backend
 101          if ($this->app->isClient('administrator') && !$this->app->getIdentity()->authorise('core.manage', $this->option)) {
 102              throw new NotAllowed($this->app->getLanguage()->_('JERROR_ALERTNOAUTHOR'), 403);
 103          }
 104      }
 105  
 106      /**
 107       * Dispatch a controller task. Redirecting the user if appropriate.
 108       *
 109       * @return  void
 110       *
 111       * @since   4.0.0
 112       */
 113      public function dispatch()
 114      {
 115          // Check component access permission
 116          $this->checkAccess();
 117  
 118          $command = $this->input->getCmd('task', 'display');
 119  
 120          // Check for a controller.task command.
 121          if (strpos($command, '.') !== false) {
 122              // Explode the controller.task command.
 123              list ($controller, $task) = explode('.', $command);
 124  
 125              $this->input->set('controller', $controller);
 126              $this->input->set('task', $task);
 127          } else {
 128              // Do we have a controller?
 129              $controller = $this->input->get('controller', 'display');
 130              $task       = $command;
 131          }
 132  
 133          // Build controller config data
 134          $config['option'] = $this->option;
 135  
 136          // Set name of controller if it is passed in the request
 137          if ($this->input->exists('controller')) {
 138              $config['name'] = strtolower($this->input->get('controller'));
 139          }
 140  
 141          // Execute the task for this component
 142          $controller = $this->getController($controller, ucfirst($this->app->getName()), $config);
 143          $controller->execute($task);
 144          $controller->redirect();
 145      }
 146  
 147      /**
 148       * Get a controller from the component
 149       *
 150       * @param   string  $name    Controller name
 151       * @param   string  $client  Optional client (like Administrator, Site etc.)
 152       * @param   array   $config  Optional controller config
 153       *
 154       * @return  BaseController
 155       *
 156       * @since   4.0.0
 157       */
 158      public function getController(string $name, string $client = '', array $config = array()): BaseController
 159      {
 160          // Set up the client
 161          $client = $client ?: ucfirst($this->app->getName());
 162  
 163          // Get the controller instance
 164          $controller = $this->mvcFactory->createController(
 165              $name,
 166              $client,
 167              $config,
 168              $this->app,
 169              $this->input
 170          );
 171  
 172          // Check if the controller could be created
 173          if (!$controller) {
 174              throw new \InvalidArgumentException(Text::sprintf('JLIB_APPLICATION_ERROR_INVALID_CONTROLLER_CLASS', $name));
 175          }
 176  
 177          return $controller;
 178      }
 179  }


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