PageRenderTime 27ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/cms/component/router/view.php

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