PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/concrete/src/Routing/Router.php

https://gitlab.com/koodersmiikka/operaatio-terveys
PHP | 214 lines | 139 code | 26 blank | 49 comment | 18 complexity | f3bdbb7d97c3b29133db4c081310230d MD5 | raw file
  1. <?php
  2. namespace Concrete\Core\Routing;
  3. use Symfony\Component\Routing\Generator\UrlGenerator;
  4. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  5. use Symfony\Component\Routing\RequestContext;
  6. use \Symfony\Component\Routing\RouteCollection as SymfonyRouteCollection;
  7. use Request;
  8. use Loader;
  9. class Router implements RouterInterface
  10. {
  11. /**
  12. * @var UrlGeneratorInterface|null
  13. */
  14. protected $generator;
  15. /**
  16. * @var RequestContext|null
  17. */
  18. protected $context;
  19. /**
  20. * @var SymfonyRouteCollection
  21. */
  22. protected $collection;
  23. protected $request;
  24. protected $themePaths = array();
  25. public $routes = array();
  26. public function __construct() {
  27. $this->collection = new SymfonyRouteCollection();
  28. }
  29. /**
  30. * @return RequestContext
  31. */
  32. public function getContext()
  33. {
  34. if (!$this->context) {
  35. $this->context = new RequestContext;
  36. $this->context->fromRequest(\Request::getInstance());
  37. }
  38. return $this->context;
  39. }
  40. /**
  41. * @param RequestContext $context
  42. */
  43. public function setContext(RequestContext $context)
  44. {
  45. $this->context = $context;
  46. }
  47. /**
  48. * @return UrlGeneratorInterface
  49. */
  50. public function getGenerator()
  51. {
  52. if (!$this->generator) {
  53. $this->generator = new UrlGenerator($this->getList(), $this->getContext());
  54. }
  55. return $this->generator;
  56. }
  57. /**
  58. * @param $generator
  59. */
  60. public function setGenerator(UrlGeneratorInterface $generator)
  61. {
  62. $this->generator = $generator;
  63. }
  64. public function getList() {
  65. return $this->collection;
  66. }
  67. public function setRequest(Request $req) {
  68. $this->request = $req;
  69. }
  70. /**
  71. * Register a symfony route with as little as a path and a callback.
  72. *
  73. * @param string $path The full path for the route
  74. * @param \Closure|string $callback `\Closure` or "dispatcher" or "\Namespace\Controller::action_method"
  75. * @param string|null $handle The route handle, if one is not provided the handle is generated from the path "/" => "_"
  76. * @param array $requirements The Parameter requirements, see Symfony Route constructor
  77. * @param array $options The route options, see Symfony Route constructor
  78. * @param string $host The host pattern this route requires, see Symfony Route constructor
  79. * @param array|string $schemes The schemes or scheme this route requires, see Symfony Route constructor
  80. * @param array|string $methods The HTTP methods this route requires, see see Symfony Route constructor
  81. * @param string $condition see Symfony Route constructor
  82. * @return \Symfony\Component\Routing\Route
  83. */
  84. public function register(
  85. $path,
  86. $callback,
  87. $handle = null,
  88. array $requirements = array(),
  89. array $options = array(),
  90. $host = '',
  91. $schemes = array(),
  92. $methods = array(),
  93. $condition = null)
  94. {
  95. // setup up standard concrete5 routing.
  96. $trimmed_path = trim($path, '/');
  97. if (!$handle) {
  98. $handle = preg_replace('/[^A-Za-z0-9\_]/', '_', $trimmed_path);
  99. $handle = preg_replace('/\_+/', '_', $handle);
  100. $handle = trim($handle, '_');
  101. }
  102. $path = '/' . $trimmed_path . '/';
  103. if ($callback instanceof \Closure) {
  104. $attributes = ClosureRouteCallback::getRouteAttributes($callback);
  105. } else if ($callback == 'dispatcher') {
  106. $attributes = DispatcherRouteCallback::getRouteAttributes($callback);
  107. } else {
  108. $attributes = ControllerRouteCallback::getRouteAttributes($callback);
  109. }
  110. $attributes['path'] = $path;
  111. $route = new Route($path, $attributes, $requirements, $options, $host, $schemes, $methods, $condition);
  112. $this->collection->add($handle, $route);
  113. return $route;
  114. }
  115. public function registerMultiple(array $routes)
  116. {
  117. foreach ($routes as $route => $route_settings) {
  118. array_unshift($route_settings, $route);
  119. call_user_func_array(array($this, 'register'), $route_settings);
  120. }
  121. }
  122. public function execute(Route $route, $parameters) {
  123. $callback = $route->getCallback();
  124. $response = $callback->execute($this->request, $route, $parameters);
  125. return $response;
  126. }
  127. /**
  128. * Used by the theme_paths and site_theme_paths files in config/ to hard coded certain paths to various themes
  129. * @access public
  130. * @param $path string
  131. * @param $theme object, if null site theme is default
  132. * @return void
  133. */
  134. public function setThemeByRoute($path, $theme = NULL, $wrapper = FILENAME_THEMES_VIEW) {
  135. $this->themePaths[$path] = array($theme, $wrapper);
  136. }
  137. public function setThemesbyRoutes(array $routes)
  138. {
  139. foreach($routes as $route => $theme) {
  140. if (is_array($theme)) {
  141. $this->setThemeByRoute($route, $theme[0], $theme[1]);
  142. } else {
  143. $this->setThemeByRoute($route, $theme);
  144. }
  145. }
  146. }
  147. /**
  148. * This grabs the theme for a particular path, if one exists in the themePaths array
  149. * @param string $path
  150. * @return string|boolean
  151. */
  152. public function getThemeByRoute($path) {
  153. // there's probably a more efficient way to do this
  154. $txt = Loader::helper('text');
  155. foreach ($this->themePaths as $lp => $layout) {
  156. if ($txt->fnmatch($lp, $path)) {
  157. return $layout;
  158. }
  159. }
  160. return false;
  161. }
  162. public function route($data)
  163. {
  164. if (is_array($data)) {
  165. $path = $data[0];
  166. $pkg = $data[1];
  167. } else {
  168. $path = $data;
  169. }
  170. $path = trim($path, '/');
  171. $pkgHandle = null;
  172. if ($pkg) {
  173. if (is_object($pkg)) {
  174. $pkgHandle = $pkg->getPackageHandle();
  175. } else {
  176. $pkgHandle = $pkg;
  177. }
  178. }
  179. $route = '/ccm';
  180. if ($pkgHandle) {
  181. $route .= "/{$pkgHandle}";
  182. } else {
  183. $route .= '/system';
  184. }
  185. $route .= "/{$path}";
  186. return $route;
  187. }
  188. }