/** * Set the default exposure fields using the configured exposure depth * @param EntityManager $em * @param integer $exposureDepth * @param integer $exposureRelationsFetchType * @return ExposeFields $this object instance */ public function configureExposeDepth(EntityManager $em, $exposureDepth = 0, $exposureRelationsFetchType = null) { if (!empty($this->route_expose)) { $this->fields = $this->route_expose; } else { $this->processExposeDepth($this->fields, $this->route->getClassMetaData()->getClassName(), $em, $exposureDepth, $exposureRelationsFetchType); } return $this; }
public function testConfigureExposurePullRequestWithExplicitExpose() { $routeMetaData = new RouteMetaData(); $explicit_expose = array('username', 'email'); $routeMetaData->setExpose($explicit_expose); $expose = ExposeFields::create($routeMetaData); $request = new Request(); $request->setPost('expose', 'username|address'); $expose->configurePullRequest(array(Configuration::EXPOSE_REQUEST_PARAM_POST => 'expose'), $request); $this->assertEquals($explicit_expose, $expose->toArray()); }
/** * Ensure our method is in out list of allowed verbs * @param $httpMethod * @return bool */ protected function methodIsInOurListOfAllowedVerbs($httpMethod) { if (!in_array($httpMethod, $this->routeMetaData->getVerbs())) { return false; } return true; }
/** * Process all routes defined * @param array $routes * @param ClassMetaData $metadata * @throws DrestException */ protected function processRoutes(array $routes, ClassMetaData $metadata) { $originFound = false; foreach ($routes as $route) { $routeMetaData = new RouteMetaData(); // Set name $route['name'] = preg_replace("/[^a-zA-Z0-9_\\s]/", "", $route['name']); if ($route['name'] == '') { throw DrestException::routeNameIsEmpty(); } if ($metadata->getRouteMetaData($route['name']) !== false) { throw DrestException::routeAlreadyDefinedWithName($metadata->getClassName(), $route['name']); } $routeMetaData->setName($route['name']); // Set verbs (will throw if invalid) if (isset($route['verbs'])) { $routeMetaData->setVerbs($route['verbs']); } if (isset($route['collection'])) { $routeMetaData->setCollection($route['collection']); } // Add the route pattern $routeMetaData->setRoutePattern($route['routePattern']); if (isset($route['routeConditions']) && is_array($route['routeConditions'])) { $routeMetaData->setRouteConditions($route['routeConditions']); } // Set the exposure array if (isset($route['expose']) && is_array($route['expose'])) { $routeMetaData->setExpose($route['expose']); } // Set disable expose lookup if (isset($route['disableExpose'])) { $routeMetaData->setDisableExpose((bool) $route['disableExpose']); } // Set the allow options value if (isset($route['allowOptions'])) { $routeMetaData->setAllowedOptionRequest($route['allowOptions']); } // If the origin flag is set, set the name on the class meta data if (isset($route['origin']) && !is_null($route['origin'])) { if ($originFound) { throw DrestException::resourceCanOnlyHaveOneRouteSetAsOrigin(); } $metadata->originRouteName = $route['name']; $originFound = true; } $metadata->addRouteMetaData($routeMetaData); } }
/** * Gets an instance of the "default" action based of request information * @throws DrestException * @return AbstractAction $action */ protected function getDefaultAction() { $httpMethod = $this->dm->calledWithANamedRoute() ? array_slice($this->matched_route->getVerbs(), 0, 1)[0] : $this->getRequest()->getHttpMethod(); $className = '\\Drest\\Service\\Action\\' . ucfirst(strtolower($httpMethod)); switch ($httpMethod) { case Request::METHOD_GET: case Request::METHOD_DELETE: $className .= $this->matched_route->isCollection() ? 'Collection' : 'Element'; break; default: $className .= 'Element'; break; } if (!class_exists($className)) { throw DrestException::unknownActionClass($className); } return new $className($this); }
/** * Add a route metadata object * @param RouteMetaData $route */ public function addRouteMetaData(RouteMetaData $route) { $route->setClassMetaData($this); $this->routes[$route->getName()] = $route; }
/** * Register a route definition (pulled from annotations) into the router stack * @param RouteMetaData $route */ public function registerRoute(RouteMetaData $route) { $this->routes[$route->getName()] = $route; }
/** * Does this registry have a service action for this route * @param \Drest\Mapping\RouteMetaData $routeMetaData * @return bool */ public function hasServiceAction(\Drest\Mapping\RouteMetaData $routeMetaData) { return array_key_exists($routeMetaData->getNamedRoute(), $this->routes); }
/** * Handle a pull requests' exposure configuration (GET) * @param RouteMetaData $route (referenced object) */ protected function handlePullExposureConfiguration(RouteMetaData &$route) { $route->setExpose(Query\ExposeFields::create($route)->configureExposeDepth($this->em, $this->config->getExposureDepth(), $this->config->getExposureRelationsFetchType())->configurePullRequest($this->config->getExposeRequestOptions(), $this->request)->toArray()); }
public static function handleAlreadyDefinedForRoute(Mapping\RouteMetaData $route) { return new self('There is a handle already defined for the route ' . $route->getName() . ' on class ' . $route->getClassMetaData()->getClassName()); }
/** * Load metadata for a class name * @param object|string $class - Pass in either the class name, or an instance of that class * @return Mapping\ClassMetaData $metaData - return null if metadata couldn't be populated from annotations * @throws DrestException */ public function loadMetadataForClass($class) { $resourceFound = false; if (is_string($class)) { $class = new \ReflectionClass($class); } $metadata = new Mapping\ClassMetaData($class); foreach ($this->reader->getClassAnnotations($class) as $annotatedObject) { if ($annotatedObject instanceof Annotation\Resource) { $resourceFound = true; $originFound = false; if ($annotatedObject->routes === null) { throw DrestException::annotatedResourceRequiresAtLeastOneServiceDefinition($class->name); } $metadata->addRepresentations($annotatedObject->representations); foreach ($annotatedObject->routes as $route) { $routeMetaData = new Mapping\RouteMetaData(); // Set name $route->name = preg_replace("/[^a-zA-Z0-9_\\s]/", "", $route->name); if ($route->name == '') { throw DrestException::routeNameIsEmpty(); } if ($metadata->getRoutesMetaData($route->name) !== false) { throw DrestException::routeAlreadyDefinedWithName($class->name, $route->name); } $routeMetaData->setName($route->name); // Set verbs (will throw if invalid) if (isset($route->verbs)) { $routeMetaData->setVerbs($route->verbs); } if (isset($route->collection)) { $routeMetaData->setCollection($route->collection); } // Add the route pattern $routeMetaData->setRoutePattern($route->routePattern); if (is_array($route->routeConditions)) { $routeMetaData->setRouteConditions($route->routeConditions); } // Set the exposure array if (is_array($route->expose)) { $routeMetaData->setExpose($route->expose); } // Set the allow options value if (isset($route->allowOptions)) { $routeMetaData->setAllowedOptionRequest($route->allowOptions); } // Add action class if (isset($route->action)) { $routeMetaData->setActionClass($route->action); } // If the origin flag is set, set the name on the class meta data if (!is_null($route->origin)) { if ($originFound) { throw DrestException::resourceCanOnlyHaveOneRouteSetAsOrigin(); } $metadata->originRouteName = $route->name; $originFound = true; } $metadata->addRouteMetaData($routeMetaData); } // Set the handle calls foreach ($class->getMethods() as $method) { /* @var \ReflectionMethod $method */ if ($method->isPublic()) { foreach ($this->reader->getMethodAnnotations($method) as $methodAnnotation) { if ($methodAnnotation instanceof Annotation\Handle) { // Make sure the for is not empty if (empty($methodAnnotation->for) || !is_string($methodAnnotation->for)) { throw DrestException::handleForCannotBeEmpty(); } if (($routeMetaData = $metadata->getRoutesMetaData($methodAnnotation->for)) === false) { throw DrestException::handleAnnotationDoesntMatchRouteName($methodAnnotation->for); } if ($routeMetaData->hasHandleCall()) { // There is already a handle set for this route throw DrestException::alreadyHandleDefinedForRoute($routeMetaData); } $routeMetaData->setHandleCall($method->getName()); $routeMetaData->setInjectRequestIntoHandle($methodAnnotation->injectRequest); } } } } // Error for any push metadata routes that don't have a handle foreach ($metadata->getRoutesMetaData() as $routeMetaData) { /* @var RouteMetaData $routeMetaData */ if ($routeMetaData->needsHandleCall() && !$routeMetaData->hasHandleCall()) { throw DrestException::routeRequiresHandle($routeMetaData->getName()); } } } } return $resourceFound ? $metadata : null; }