|
| 1 | +<?php namespace Illuminate\Routing\Controllers; |
| 2 | + |
| 3 | +use ReflectionClass; |
| 4 | +use ReflectionMethod; |
| 5 | + |
| 6 | +class Inspector { |
| 7 | + |
| 8 | + /** |
| 9 | + * An array of HTTP verbs. |
| 10 | + * |
| 11 | + * @var array |
| 12 | + */ |
| 13 | + protected $verbs = array( |
| 14 | + 'any', 'get', 'post', 'put', |
| 15 | + 'delete', 'head', 'options' |
| 16 | + ); |
| 17 | + |
| 18 | + /** |
| 19 | + * Get the routable methods for a controller. |
| 20 | + * |
| 21 | + * @param string $controller |
| 22 | + * @param string $prefix |
| 23 | + * @return array |
| 24 | + */ |
| 25 | + public function getRoutable($controller, $prefix) |
| 26 | + { |
| 27 | + $routable = array(); |
| 28 | + |
| 29 | + $reflection = new ReflectionClass($controller); |
| 30 | + |
| 31 | + // To get the routable methods, we will simply spin through all methods on the |
| 32 | + // controller instance checking to see if it belongs to the given class and |
| 33 | + // is a publicly routable method. If so, we will add it to this listings. |
| 34 | + foreach ($reflection->getMethods() as $method) |
| 35 | + { |
| 36 | + if ($this->isRoutable($method, $reflection->name)) |
| 37 | + { |
| 38 | + $data = $this->getMethodData($method, $prefix); |
| 39 | + |
| 40 | + // If the routable method is an index method, we will create a special index |
| 41 | + // route which is simply the prefix and the verb and does not contain any |
| 42 | + // the wildcard place-holders that each "typical" routes would contain. |
| 43 | + if ($data['plain'] == $prefix.'/index') |
| 44 | + { |
| 45 | + $routable[$method->name][] = $data; |
| 46 | + |
| 47 | + $routable[$method->name][] = $this->getIndexData($data, $prefix); |
| 48 | + } |
| 49 | + |
| 50 | + // If the routable method is not a special index method, we will just add in |
| 51 | + // the data to the returned results straight away. We do not need to make |
| 52 | + // any special routes for this scenario but only just add these routes. |
| 53 | + else |
| 54 | + { |
| 55 | + $routable[$method->name][] = $data; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + return $routable; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Determine if the given controller method is routable. |
| 65 | + * |
| 66 | + * @param ReflectionMethod $method |
| 67 | + * @param string $controller |
| 68 | + * @return bool |
| 69 | + */ |
| 70 | + public function isRoutable(ReflectionMethod $method, $controller) |
| 71 | + { |
| 72 | + if ($method->class == 'Illuminate\Routing\Controllers\Controller') return false; |
| 73 | + |
| 74 | + return $method->isPublic() and starts_with($method->name, $this->verbs); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get the method data for a given method. |
| 79 | + * |
| 80 | + * @param ReflectionMethod $method |
| 81 | + * @return array |
| 82 | + */ |
| 83 | + public function getMethodData(ReflectionMethod $method, $prefix) |
| 84 | + { |
| 85 | + $verb = $this->getVerb($name = $method->name); |
| 86 | + |
| 87 | + $uri = $this->addUriWildcards($plain = $this->getPlainUri($name, $prefix)); |
| 88 | + |
| 89 | + return compact('verb', 'plain', 'uri'); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Get the routable data for an index method. |
| 94 | + * |
| 95 | + * @param array $data |
| 96 | + * @param string $prefix |
| 97 | + * @return array |
| 98 | + */ |
| 99 | + protected function getIndexData($data, $prefix) |
| 100 | + { |
| 101 | + return array('verb' => $data['verb'], 'plain' => $prefix, 'uri' => $prefix); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Extract the verb from a controller action. |
| 106 | + * |
| 107 | + * @param string $name |
| 108 | + * @return string |
| 109 | + */ |
| 110 | + public function getVerb($name) |
| 111 | + { |
| 112 | + return head(explode('_', snake_case($name))); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Determine the URI from the given method name. |
| 117 | + * |
| 118 | + * @param string $name |
| 119 | + * @param string $prefix |
| 120 | + * @return string |
| 121 | + */ |
| 122 | + public function getPlainUri($name, $prefix) |
| 123 | + { |
| 124 | + return $prefix.'/'.implode('-', array_slice(explode('_', snake_case($name)), 1)); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Add wildcards to the given URI. |
| 129 | + * |
| 130 | + * @param string $uri |
| 131 | + * @return string |
| 132 | + */ |
| 133 | + public function addUriWildcards($uri) |
| 134 | + { |
| 135 | + return $uri.'/{v1?}/{v2?}/{v3?}/{v4?}/{v5?}'; |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments