/**
* Parse a controller class.
*
* @param string $class
* @return array
*/
public function parseController($class)
{
$reflectionClass = new ReflectionClass($class);
$classAnnotations = $this->reader->getClassAnnotations($reflectionClass);
$controllerMetadata = [];
$middleware = [];
// find entity parameters and plugins
foreach ($classAnnotations as $annotation) {
// controller attributes
if ($annotation instanceof \ProAI\Annotations\Annotations\Controller) {
$prefix = $annotation->prefix;
$middleware = $this->addMiddleware($middleware, $annotation->middleware);
}
if ($annotation instanceof \ProAI\Annotations\Annotations\Middleware) {
$middleware = $this->addMiddleware($middleware, $annotation->value);
}
// resource controller
if ($annotation instanceof \ProAI\Annotations\Annotations\Resource) {
$resourceMethods = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy'];
if (!empty($annotation->only)) {
$resourceMethods = array_intersect($resourceMethods, $annotation->only);
} elseif (!empty($annotation->except)) {
$resourceMethods = array_diff($resourceMethods, $annotation->except);
}
$resource = ['name' => $annotation->value, 'methods' => $resourceMethods];
}
}
// find routes
foreach ($reflectionClass->getMethods() as $reflectionMethod) {
$name = $reflectionMethod->getName();
$methodAnnotations = $this->reader->getMethodAnnotations($reflectionMethod);
$routeMetadata = [];
// controller method is resource route
if (!empty($resource) && in_array($name, $resource['methods'])) {
$routeMetadata = ['uri' => $resource['name'] . $this->getResourcePath($name), 'controller' => $class, 'controllerMethod' => $name, 'httpMethod' => $this->getResourceHttpMethod($name), 'as' => $resource['name'] . '.' . $name, 'middleware' => ''];
}
// controller method is route
if ($route = $this->hasHttpMethodAnnotation($name, $methodAnnotations)) {
$routeMetadata = ['uri' => $route['uri'], 'controller' => $class, 'controllerMethod' => $name, 'httpMethod' => $route['httpMethod'], 'as' => $route['as'], 'middleware' => $route['middleware']];
}
// add more route options to route metadata
if (!empty($routeMetadata)) {
if (!empty($middleware)) {
$routeMetadata['middleware'] = $middleware;
}
// add other method annotations
foreach ($methodAnnotations as $annotation) {
if ($annotation instanceof \ProAI\Annotations\Annotations\Middleware) {
$middleware = $this->addMiddleware($middleware, $routeMetadata['middleware']);
}
}
// add global prefix and middleware
if (!empty($prefix)) {
$routeMetadata['uri'] = $prefix . '/' . $routeMetadata['uri'];
}
$controllerMetadata[$name] = $routeMetadata;
}
}
return $controllerMetadata;
}