[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Component/Router/ -> RouterView.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2015 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\Component\Router;
  11  
  12  use Joomla\CMS\Component\ComponentHelper;
  13  use Joomla\CMS\Component\Router\Rules\RulesInterface;
  14  
  15  // phpcs:disable PSR1.Files.SideEffects
  16  \defined('JPATH_PLATFORM') or die;
  17  // phpcs:enable PSR1.Files.SideEffects
  18  
  19  /**
  20   * View-based component routing class
  21   *
  22   * @since  3.5
  23   */
  24  abstract class RouterView extends RouterBase
  25  {
  26      /**
  27       * Name of the router of the component
  28       *
  29       * @var    string
  30       * @since  3.5
  31       */
  32      protected $name;
  33  
  34      /**
  35       * Array of rules
  36       *
  37       * @var    RulesInterface[]
  38       * @since  3.5
  39       */
  40      protected $rules = array();
  41  
  42      /**
  43       * Views of the component
  44       *
  45       * @var    RouterViewConfiguration[]
  46       * @since  3.5
  47       */
  48      protected $views = array();
  49  
  50      /**
  51       * Register the views of a component
  52       *
  53       * @param   RouterViewConfiguration  $view  View configuration object
  54       *
  55       * @return  void
  56       *
  57       * @since   3.5
  58       */
  59      public function registerView(RouterViewConfiguration $view)
  60      {
  61          $this->views[$view->name] = $view;
  62      }
  63  
  64      /**
  65       * Return an array of registered view objects
  66       *
  67       * @return  RouterViewConfiguration[] Array of registered view objects
  68       *
  69       * @since   3.5
  70       */
  71      public function getViews()
  72      {
  73          return $this->views;
  74      }
  75  
  76      /**
  77       * Get the path of views from target view to root view
  78       * including content items of a nestable view
  79       *
  80       * @param   array  $query  Array of query elements
  81       *
  82       * @return  array List of views including IDs of content items
  83       *
  84       * @since   3.5
  85       */
  86      public function getPath($query)
  87      {
  88          $views  = $this->getViews();
  89          $result = array();
  90  
  91          // Get the right view object
  92          if (isset($query['view']) && isset($views[$query['view']])) {
  93              $viewobj = $views[$query['view']];
  94          }
  95  
  96          // Get the path from the current item to the root view with all IDs
  97          if (isset($viewobj)) {
  98              $path     = array_reverse($viewobj->path);
  99              $start    = true;
 100              $childkey = false;
 101  
 102              foreach ($path as $element) {
 103                  $view = $views[$element];
 104  
 105                  if ($start) {
 106                      $key   = $view->key;
 107                      $start = false;
 108                  } else {
 109                      $key = $childkey;
 110                  }
 111  
 112                  $childkey = $view->parent_key;
 113  
 114                  if (($key || $view->key) && \is_callable(array($this, 'get' . ucfirst($view->name) . 'Segment'))) {
 115                      if (isset($query[$key])) {
 116                          $result[$view->name] = \call_user_func_array(array($this, 'get' . ucfirst($view->name) . 'Segment'), array($query[$key], $query));
 117                      } elseif (isset($query[$view->key])) {
 118                          $result[$view->name] = \call_user_func_array(array($this, 'get' . ucfirst($view->name) . 'Segment'), array($query[$view->key], $query));
 119                      } else {
 120                          $result[$view->name] = array();
 121                      }
 122                  } else {
 123                      $result[$view->name] = true;
 124                  }
 125              }
 126          }
 127  
 128          return $result;
 129      }
 130  
 131      /**
 132       * Get all currently attached rules
 133       *
 134       * @return  RulesInterface[]  All currently attached rules in an array
 135       *
 136       * @since   3.5
 137       */
 138      public function getRules()
 139      {
 140          return $this->rules;
 141      }
 142  
 143      /**
 144       * Add a number of router rules to the object
 145       *
 146       * @param   RulesInterface[]  $rules  Array of JComponentRouterRulesInterface objects
 147       *
 148       * @return  void
 149       *
 150       * @since   3.5
 151       */
 152      public function attachRules($rules)
 153      {
 154          foreach ($rules as $rule) {
 155              $this->attachRule($rule);
 156          }
 157      }
 158  
 159      /**
 160       * Attach a build rule
 161       *
 162       * @param   RulesInterface  $rule  The function to be called.
 163       *
 164       * @return  void
 165       *
 166       * @since   3.5
 167       */
 168      public function attachRule(RulesInterface $rule)
 169      {
 170          $this->rules[] = $rule;
 171      }
 172  
 173      /**
 174       * Remove a build rule
 175       *
 176       * @param   RulesInterface  $rule  The rule to be removed.
 177       *
 178       * @return   boolean  Was a rule removed?
 179       *
 180       * @since   3.5
 181       */
 182      public function detachRule(RulesInterface $rule)
 183      {
 184          foreach ($this->rules as $id => $r) {
 185              if ($r == $rule) {
 186                  unset($this->rules[$id]);
 187  
 188                  return true;
 189              }
 190          }
 191  
 192          return false;
 193      }
 194  
 195      /**
 196       * Generic method to preprocess a URL
 197       *
 198       * @param   array  $query  An associative array of URL arguments
 199       *
 200       * @return  array  The URL arguments to use to assemble the subsequent URL.
 201       *
 202       * @since   3.5
 203       */
 204      public function preprocess($query)
 205      {
 206          // Process the parsed variables based on custom defined rules
 207          foreach ($this->rules as $rule) {
 208              $rule->preprocess($query);
 209          }
 210  
 211          return $query;
 212      }
 213  
 214      /**
 215       * Build method for URLs
 216       *
 217       * @param   array  &$query  Array of query elements
 218       *
 219       * @return  array  Array of URL segments
 220       *
 221       * @since   3.5
 222       */
 223      public function build(&$query)
 224      {
 225          $segments = array();
 226  
 227          // Process the parsed variables based on custom defined rules
 228          foreach ($this->rules as $rule) {
 229              $rule->build($query, $segments);
 230          }
 231  
 232          return $segments;
 233      }
 234  
 235      /**
 236       * Parse method for URLs
 237       *
 238       * @param   array  &$segments  Array of URL string-segments
 239       *
 240       * @return  array  Associative array of query values
 241       *
 242       * @since   3.5
 243       */
 244      public function parse(&$segments)
 245      {
 246          $vars = array();
 247  
 248          // Process the parsed variables based on custom defined rules
 249          foreach ($this->rules as $rule) {
 250              $rule->parse($segments, $vars);
 251          }
 252  
 253          return $vars;
 254      }
 255  
 256      /**
 257       * Method to return the name of the router
 258       *
 259       * @return  string  Name of the router
 260       *
 261       * @since   3.5
 262       */
 263      public function getName()
 264      {
 265          if (empty($this->name)) {
 266              $r = null;
 267  
 268              if (!preg_match('/(.*)Router/i', \get_class($this), $r)) {
 269                  throw new \Exception('JLIB_APPLICATION_ERROR_ROUTER_GET_NAME', 500);
 270              }
 271  
 272              $this->name = str_replace('com_', '', ComponentHelper::getComponentName($this, strtolower($r[1])));
 273          }
 274  
 275          return $this->name;
 276      }
 277  }


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