private function processAction(CompiledActionDefinitionInterface $action) { $href = $action->getEndpointUrl(); $routeName = $action->getRouteName(); $controller = sprintf('%s::%s', $action->getResourceDefinition()->getControllerClass(), 'handle'); $defaults = ['_controller' => $controller, '_format' => 'json', '_rested' => ['action' => $action->getType(), 'controller' => $action->getControllerName(), 'route_name' => $routeName]]; // add constraints and validators to the cache $requirements = []; foreach ($action->getTokens() as $token) { if ($token->acceptAnyValue() === false) { $requirements[$token->getName()] = Parameter::getValidatorPattern($token->getDataType()); } } $route = new Route($href, $defaults, $requirements, [], '', [], $action->getHttpMethod()); $this->routes->add($routeName, $route); }
protected function addAction(CompiledResourceDefinitionInterface $resourceDefinition, CompiledActionDefinitionInterface $action, $instance = null) { $fields = []; $links = []; $operation = $action->getHttpMethod() === Request::METHOD_GET ? GetterField::OPERATION : SetterField::OPERATION; $transform = $action->getTransform(); $transformMapping = $action->getTransformMapping(); $url = $this->urlGenerator->route($action->getRouteName()); // FIXME: refactor so that this runs when the action is compiled $modelFields = $transformMapping->executeFieldFilterCallback($operation, $instance); foreach ($modelFields as $field) { $fields[] = $this->processField($field); $links = array_merge($links, $transformMapping->getLinks()); } if ($action->getType() !== ActionDefinition::TYPE_CREATE) { $instanceAction = $resourceDefinition->findFirstAction(ActionDefinition::TYPE_INSTANCE); $url = $this->urlGenerator->route($action->getRouteName(), ['id' => $transform->retrieveIdFromInstance($instanceAction->getTransformMapping(), $instance)]); } $this->data['_actions'][] = ['fields' => $fields, 'href' => $url, 'id' => $action->getId(), 'method' => $action->getHttpMethod(), 'type' => $action->getAcceptedContentType()]; return $links; }