/** * 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); } }
/** * Process the method * @param $methods * @param Mapping\ClassMetaData $metadata * @throws DrestException */ protected function processMethods($methods, Mapping\ClassMetaData $metadata) { // Set the handle calls foreach ($methods 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->getRouteMetaData($methodAnnotation->for)) === false) { throw DrestException::handleAnnotationDoesntMatchRouteName($methodAnnotation->for); } if ($routeMetaData->hasHandleCall()) { // There is already a handle set for this route throw DrestException::handleAlreadyDefinedForRoute($routeMetaData); } $routeMetaData->setHandleCall($method->getName()); } } } } }
/** * Generate the location string from the provided object * @param object $object * @param string $url - the Url to be prepended to the location * @param EntityManager $em - Optionally pass the entity manager to assist in determining a GET origin location * @return string|false */ public function getOriginLocation($object, $url, EntityManager $em = null) { $exposedObjectArray = ObjectToArray::execute($object); if (($route = $this->class_metadata->getOriginRoute($em)) !== null) { if (!is_null($em)) { $pattern = $route->getRoutePattern(); $ormClassMetadata = $em->getClassMetadata($this->getClassMetaData()->getClassName()); foreach ($ormClassMetadata->getIdentifierFieldNames() as $identifier) { if (isset($exposedObjectArray[$identifier])) { $pattern = str_replace(':' . $identifier, $exposedObjectArray[$identifier], $pattern); } } return $url . '/' . ltrim($pattern, '/'); } } return false; }
/** * Get the origin route (it could be this instance) * @param EntityManager $em - Optionally pass the entity manager to assist in determining a GET origin location * @return null|RouteMetaData $route */ public function getOriginRoute(EntityManager $em = null) { return $this->class_metadata->getOriginRoute($em); }
public function testClassMetadataNotExpired() { $className = 'DrestTests\\Entities\\Typical\\User'; $cmd = new ClassMetaData(new \ReflectionClass($className)); $this->assertFalse($cmd->expired()); }
/** * Process the method * @param $resource * @param Mapping\ClassMetaData $metadata * @throws DrestException */ protected function processMethods($resource, Mapping\ClassMetaData $metadata) { /* @var \ReflectionMethod $method */ foreach ($resource['routes'] as $route) { // Make sure the for is not empty if (!isset($route['name']) || !is_string($route['name'])) { throw DrestException::handleForCannotBeEmpty(); } if (($routeMetaData = $metadata->getRouteMetaData($route['name'])) === false) { throw DrestException::handleAnnotationDoesntMatchRouteName($route['name']); } if ($routeMetaData->hasHandleCall()) { // There is already a handle set for this route throw DrestException::handleAlreadyDefinedForRoute($routeMetaData); } // Set the handle if (isset($route['handle_call'])) { $routeMetaData->setHandleCall($route['handle_call']); } } }
/** * 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; }