/** * Generates a Url for an action. * * @param string $path The path for the resource the action belongs to. * @param \Rested\Definition\ActionDefinitionInterface $action Action to generate a Url for. * * @return string */ protected function generateUrlForAction(ActionDefinitionInterface $action, $path) { $tokens = $action->getTokens(); $components = array_map(function (Parameter $value) { return sprintf('{%s}', $value->getName()); }, $tokens); if ($action->shouldAppendId() === true) { $components[] = $action->getId(); } array_unshift($components, $path); array_unshift($components, $this->urlGenerator->getMountPath()); $u = join('/', $components); $u = preg_replace('/\\/{2,}/', '/', $u); return $u; }
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; }
protected function makeUrlForInstance(CompiledResourceDefinitionInterface $resourceDefinition, $instance) { $action = $resourceDefinition->findFirstAction(ActionDefinition::TYPE_INSTANCE); return $this->urlGenerator->route($action->getRouteName(), ['id' => $this->retrieveIdFromInstance($action->getTransformMapping(), $instance)]); }