PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/Streaming-Safe-for-Kids/vendor/laravel/framework/src/Illuminate/Routing/ResourceRegistrar.php

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