[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/application/src/Controller/ -> ControllerResolver.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Application Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Application\Controller;
  10  
  11  use Joomla\Controller\ControllerInterface;
  12  use Joomla\Router\ResolvedRoute;
  13  
  14  /**
  15   * Resolves a controller for the given route.
  16   *
  17   * @since  2.0.0
  18   */
  19  class ControllerResolver implements ControllerResolverInterface
  20  {
  21      /**
  22       * Resolve the controller for a route
  23       *
  24       * @param   ResolvedRoute  $route  The route to resolve the controller for
  25       *
  26       * @return  callable
  27       *
  28       * @since   2.0.0
  29       * @throws  \InvalidArgumentException
  30       */
  31  	public function resolve(ResolvedRoute $route): callable
  32      {
  33          $controller = $route->getController();
  34  
  35          // Try to resolve a callable defined as an array
  36          if (\is_array($controller))
  37          {
  38              if (isset($controller[0]) && \is_string($controller[0]) && isset($controller[1]))
  39              {
  40                  if (!class_exists($controller[0]))
  41                  {
  42                      throw new \InvalidArgumentException(sprintf('Cannot resolve controller for URI `%s`', $route->getUri()));
  43                  }
  44  
  45                  try
  46                  {
  47                      $controller[0] = $this->instantiateController($controller[0]);
  48                  }
  49                  catch (\ArgumentCountError $error)
  50                  {
  51                      throw new \InvalidArgumentException(
  52                          sprintf(
  53                              'Controller `%s` has required constructor arguments, cannot instantiate the class', $controller[0]
  54                          ),
  55                          0,
  56                          $error
  57                      );
  58                  }
  59              }
  60  
  61              if (!\is_callable($controller))
  62              {
  63                  throw new \InvalidArgumentException(sprintf('Cannot resolve controller for URI `%s`', $route->getUri()));
  64              }
  65  
  66              return $controller;
  67          }
  68  
  69          // Try to resolve an invokable object
  70          if (\is_object($controller))
  71          {
  72              if (!\is_callable($controller))
  73              {
  74                  throw new \InvalidArgumentException(sprintf('Cannot resolve controller for URI `%s`', $route->getUri()));
  75              }
  76  
  77              return $controller;
  78          }
  79  
  80          // Try to resolve a known function
  81          if (\function_exists($controller))
  82          {
  83              return $controller;
  84          }
  85  
  86          // Try to resolve a class name if it implements our ControllerInterface
  87          if (\is_string($controller) && interface_exists(ControllerInterface::class))
  88          {
  89              if (!class_exists($controller))
  90              {
  91                  throw new \InvalidArgumentException(sprintf('Cannot resolve controller for URI `%s`', $route->getUri()));
  92              }
  93  
  94              try
  95              {
  96                  return [$this->instantiateController($controller), 'execute'];
  97              }
  98              catch (\ArgumentCountError $error)
  99              {
 100                  throw new \InvalidArgumentException(
 101                      sprintf(
 102                          'Controller `%s` has required constructor arguments, cannot instantiate the class', $controller
 103                      ),
 104                      0,
 105                      $error
 106                  );
 107              }
 108          }
 109  
 110          // Unsupported resolution
 111          throw new \InvalidArgumentException(sprintf('Cannot resolve controller for URI `%s`', $route->getUri()));
 112      }
 113  
 114      /**
 115       * Instantiate a controller class
 116       *
 117       * @param   string  $class  The class to instantiate
 118       *
 119       * @return  object  Controller class instance
 120       *
 121       * @since   2.0.0
 122       */
 123  	protected function instantiateController(string $class): object
 124      {
 125          return new $class;
 126      }
 127  }


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