PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/application/vendor/laravel/framework/src/Illuminate/Routing/ResourceRegistrar.php

https://gitlab.com/MineYourMind/BoNeMEAL
PHP | 392 lines | 144 code | 59 blank | 189 comment | 6 complexity | accb08ef00f96d37f04b71d1d3777875 MD5 | raw file
  1. <?php namespace Illuminate\Routing;
  2. class ResourceRegistrar {
  3. /**
  4. * Create a new resource registrar.
  5. *
  6. * @param \Illuminate\Routing\Router
  7. */
  8. protected $router;
  9. /**
  10. * The default actions for a resourceful controller.
  11. *
  12. * @var array
  13. */
  14. protected $resourceDefaults = array('index', 'create', 'store', 'show', 'edit', 'update', 'destroy');
  15. /**
  16. * Create a new resource registrar instance.
  17. *
  18. * @param \Illuminate\Routing\Router $router
  19. * @return void
  20. */
  21. public function __construct(Router $router)
  22. {
  23. $this->router = $router;
  24. }
  25. /**
  26. * Route a resource to a controller.
  27. *
  28. * @param string $name
  29. * @param string $controller
  30. * @param array $options
  31. * @return void
  32. */
  33. public function register($name, $controller, array $options = array())
  34. {
  35. // If the resource name contains a slash, we will assume the developer wishes to
  36. // register these resource routes with a prefix so we will set that up out of
  37. // the box so they don't have to mess with it. Otherwise, we will continue.
  38. if (str_contains($name, '/'))
  39. {
  40. $this->prefixedResource($name, $controller, $options);
  41. return;
  42. }
  43. // We need to extract the base resource from the resource name. Nested resources
  44. // are supported in the framework, but we need to know what name to use for a
  45. // place-holder on the route wildcards, which should be the base resources.
  46. $base = $this->getResourceWildcard(last(explode('.', $name)));
  47. $defaults = $this->resourceDefaults;
  48. foreach ($this->getResourceMethods($defaults, $options) as $m)
  49. {
  50. $this->{'addResource'.ucfirst($m)}($name, $base, $controller, $options);
  51. }
  52. }
  53. /**
  54. * Build a set of prefixed resource routes.
  55. *
  56. * @param string $name
  57. * @param string $controller
  58. * @param array $options
  59. * @return void
  60. */
  61. protected function prefixedResource($name, $controller, array $options)
  62. {
  63. list($name, $prefix) = $this->getResourcePrefix($name);
  64. // We need to extract the base resource from the resource name. Nested resources
  65. // are supported in the framework, but we need to know what name to use for a
  66. // place-holder on the route wildcards, which should be the base resources.
  67. $callback = function($me) use ($name, $controller, $options)
  68. {
  69. $me->resource($name, $controller, $options);
  70. };
  71. return $this->router->group(compact('prefix'), $callback);
  72. }
  73. /**
  74. * Extract the resource and prefix from a resource name.
  75. *
  76. * @param string $name
  77. * @return array
  78. */
  79. protected function getResourcePrefix($name)
  80. {
  81. $segments = explode('/', $name);
  82. // To get the prefix, we will take all of the name segments and implode them on
  83. // a slash. This will generate a proper URI prefix for us. Then we take this
  84. // last segment, which will be considered the final resources name we use.
  85. $prefix = implode('/', array_slice($segments, 0, -1));
  86. return array(end($segments), $prefix);
  87. }
  88. /**
  89. * Get the applicable resource methods.
  90. *
  91. * @param array $defaults
  92. * @param array $options
  93. * @return array
  94. */
  95. protected function getResourceMethods($defaults, $options)
  96. {
  97. if (isset($options['only']))
  98. {
  99. return array_intersect($defaults, (array) $options['only']);
  100. }
  101. elseif (isset($options['except']))
  102. {
  103. return array_diff($defaults, (array) $options['except']);
  104. }
  105. return $defaults;
  106. }
  107. /**
  108. * Get the base resource URI for a given resource.
  109. *
  110. * @param string $resource
  111. * @return string
  112. */
  113. public function getResourceUri($resource)
  114. {
  115. if ( ! str_contains($resource, '.')) return $resource;
  116. // Once we have built the base URI, we'll remove the wildcard holder for this
  117. // base resource name so that the individual route adders can suffix these
  118. // paths however they need to, as some do not have any wildcards at all.
  119. $segments = explode('.', $resource);
  120. $uri = $this->getNestedResourceUri($segments);
  121. return str_replace('/{'.$this->getResourceWildcard(last($segments)).'}', '', $uri);
  122. }
  123. /**
  124. * Get the URI for a nested resource segment array.
  125. *
  126. * @param array $segments
  127. * @return string
  128. */
  129. protected function getNestedResourceUri(array $segments)
  130. {
  131. // We will spin through the segments and create a place-holder for each of the
  132. // resource segments, as well as the resource itself. Then we should get an
  133. // entire string for the resource URI that contains all nested resources.
  134. return implode('/', array_map(function($s)
  135. {
  136. return $s.'/{'.$this->getResourceWildcard($s).'}';
  137. }, $segments));
  138. }
  139. /**
  140. * Get the action array for a resource route.
  141. *
  142. * @param string $resource
  143. * @param string $controller
  144. * @param string $method
  145. * @param array $options
  146. * @return array
  147. */
  148. protected function getResourceAction($resource, $controller, $method, $options)
  149. {
  150. $name = $this->getResourceName($resource, $method, $options);
  151. return array('as' => $name, 'uses' => $controller.'@'.$method);
  152. }
  153. /**
  154. * Get the name for a given resource.
  155. *
  156. * @param string $resource
  157. * @param string $method
  158. * @param array $options
  159. * @return string
  160. */
  161. protected function getResourceName($resource, $method, $options)
  162. {
  163. if (isset($options['names'][$method])) return $options['names'][$method];
  164. // If a global prefix has been assigned to all names for this resource, we will
  165. // grab that so we can prepend it onto the name when we create this name for
  166. // the resource action. Otherwise we'll just use an empty string for here.
  167. $prefix = isset($options['as']) ? $options['as'].'.' : '';
  168. if ( ! $this->router->hasGroupStack())
  169. {
  170. return $prefix.$resource.'.'.$method;
  171. }
  172. return $this->getGroupResourceName($prefix, $resource, $method);
  173. }
  174. /**
  175. * Get the resource name for a grouped resource.
  176. *
  177. * @param string $prefix
  178. * @param string $resource
  179. * @param string $method
  180. * @return string
  181. */
  182. protected function getGroupResourceName($prefix, $resource, $method)
  183. {
  184. $group = trim(str_replace('/', '.', $this->router->getLastGroupPrefix()), '.');
  185. if (empty($group))
  186. {
  187. return trim("{$prefix}{$resource}.{$method}", '.');
  188. }
  189. return trim("{$prefix}{$group}.{$resource}.{$method}", '.');
  190. }
  191. /**
  192. * Format a resource wildcard for usage.
  193. *
  194. * @param string $value
  195. * @return string
  196. */
  197. public function getResourceWildcard($value)
  198. {
  199. return str_replace('-', '_', $value);
  200. }
  201. /**
  202. * Add the index method for a resourceful route.
  203. *
  204. * @param string $name
  205. * @param string $base
  206. * @param string $controller
  207. * @param array $options
  208. * @return \Illuminate\Routing\Route
  209. */
  210. protected function addResourceIndex($name, $base, $controller, $options)
  211. {
  212. $uri = $this->getResourceUri($name);
  213. $action = $this->getResourceAction($name, $controller, 'index', $options);
  214. return $this->router->get($uri, $action);
  215. }
  216. /**
  217. * Add the create method for a resourceful route.
  218. *
  219. * @param string $name
  220. * @param string $base
  221. * @param string $controller
  222. * @param array $options
  223. * @return \Illuminate\Routing\Route
  224. */
  225. protected function addResourceCreate($name, $base, $controller, $options)
  226. {
  227. $uri = $this->getResourceUri($name).'/create';
  228. $action = $this->getResourceAction($name, $controller, 'create', $options);
  229. return $this->router->get($uri, $action);
  230. }
  231. /**
  232. * Add the store method for a resourceful route.
  233. *
  234. * @param string $name
  235. * @param string $base
  236. * @param string $controller
  237. * @param array $options
  238. * @return \Illuminate\Routing\Route
  239. */
  240. protected function addResourceStore($name, $base, $controller, $options)
  241. {
  242. $uri = $this->getResourceUri($name);
  243. $action = $this->getResourceAction($name, $controller, 'store', $options);
  244. return $this->router->post($uri, $action);
  245. }
  246. /**
  247. * Add the show method for a resourceful route.
  248. *
  249. * @param string $name
  250. * @param string $base
  251. * @param string $controller
  252. * @param array $options
  253. * @return \Illuminate\Routing\Route
  254. */
  255. protected function addResourceShow($name, $base, $controller, $options)
  256. {
  257. $uri = $this->getResourceUri($name).'/{'.$base.'}';
  258. $action = $this->getResourceAction($name, $controller, 'show', $options);
  259. return $this->router->get($uri, $action);
  260. }
  261. /**
  262. * Add the edit method for a resourceful route.
  263. *
  264. * @param string $name
  265. * @param string $base
  266. * @param string $controller
  267. * @param array $options
  268. * @return \Illuminate\Routing\Route
  269. */
  270. protected function addResourceEdit($name, $base, $controller, $options)
  271. {
  272. $uri = $this->getResourceUri($name).'/{'.$base.'}/edit';
  273. $action = $this->getResourceAction($name, $controller, 'edit', $options);
  274. return $this->router->get($uri, $action);
  275. }
  276. /**
  277. * Add the update method for a resourceful route.
  278. *
  279. * @param string $name
  280. * @param string $base
  281. * @param string $controller
  282. * @param array $options
  283. * @return void
  284. */
  285. protected function addResourceUpdate($name, $base, $controller, $options)
  286. {
  287. $this->addPutResourceUpdate($name, $base, $controller, $options);
  288. return $this->addPatchResourceUpdate($name, $base, $controller);
  289. }
  290. /**
  291. * Add the update method for a resourceful route.
  292. *
  293. * @param string $name
  294. * @param string $base
  295. * @param string $controller
  296. * @param array $options
  297. * @return \Illuminate\Routing\Route
  298. */
  299. protected function addPutResourceUpdate($name, $base, $controller, $options)
  300. {
  301. $uri = $this->getResourceUri($name).'/{'.$base.'}';
  302. $action = $this->getResourceAction($name, $controller, 'update', $options);
  303. return $this->router->put($uri, $action);
  304. }
  305. /**
  306. * Add the update method for a resourceful route.
  307. *
  308. * @param string $name
  309. * @param string $base
  310. * @param string $controller
  311. * @return void
  312. */
  313. protected function addPatchResourceUpdate($name, $base, $controller)
  314. {
  315. $uri = $this->getResourceUri($name).'/{'.$base.'}';
  316. $this->router->patch($uri, $controller.'@update');
  317. }
  318. /**
  319. * Add the destroy method for a resourceful route.
  320. *
  321. * @param string $name
  322. * @param string $base
  323. * @param string $controller
  324. * @param array $options
  325. * @return \Illuminate\Routing\Route
  326. */
  327. protected function addResourceDestroy($name, $base, $controller, $options)
  328. {
  329. $uri = $this->getResourceUri($name).'/{'.$base.'}';
  330. $action = $this->getResourceAction($name, $controller, 'destroy', $options);
  331. return $this->router->delete($uri, $action);
  332. }
  333. }