Пример #1
0
 public function testRouteWithABasePath()
 {
     $router = new \Drest\Router();
     $router->setRouteBasePaths(array('v1', 'v2'));
     $routeMetaData = new \Drest\Mapping\RouteMetaData();
     $routeMetaData->setRoutePattern('/user/:id');
     $router->registerRoute($routeMetaData);
     $request = new \DrestCommon\Request\Request(\Symfony\Component\HttpFoundation\Request::create('/user/1'));
     $this->assertCount(0, $router->getMatchedRoutes($request));
     $request = new \DrestCommon\Request\Request(\Symfony\Component\HttpFoundation\Request::create('/v1/user/1'));
     $this->assertCount(1, $router->getMatchedRoutes($request));
     $request = new \DrestCommon\Request\Request(\Symfony\Component\HttpFoundation\Request::create('/v2/user/1'));
     $this->assertCount(1, $router->getMatchedRoutes($request));
 }
Пример #2
0
 public function testRemovingFromRegistryByRouteAndHavingActionUsedForOtherRoute()
 {
     $registry = new Registry();
     $className = 'DrestTests\\Entities\\Typical\\User';
     $className2 = 'DrestTests\\Entities\\Typical\\User';
     $routeName = 'route_name';
     $routeName2 = 'route_name2';
     $serviceAction = new \DrestTests\Action\Custom();
     $registry->register($serviceAction, [$className . '::' . $routeName, $className2 . '::' . $routeName2]);
     $routeMetaData = new \Drest\Mapping\RouteMetaData();
     $routeMetaData->setClassMetaData(new \Drest\Mapping\ClassMetaData(new \ReflectionClass($className)));
     $routeMetaData->setName($routeName);
     $routeMetaData2 = new \Drest\Mapping\RouteMetaData();
     $routeMetaData2->setClassMetaData(new \Drest\Mapping\ClassMetaData(new \ReflectionClass($className2)));
     $routeMetaData2->setName($routeName2);
     $registry->unregisterByRoute($routeMetaData);
     $this->assertFalse($registry->hasServiceAction($routeMetaData));
     $this->assertTrue($registry->hasServiceAction($routeMetaData2));
     $this->assertSame($registry->getServiceAction($routeMetaData2), $serviceAction);
     $registry->unregisterByRoute($routeMetaData2);
     $this->assertFalse($registry->hasServiceAction($routeMetaData));
     $this->assertFalse($registry->hasServiceAction($routeMetaData2));
 }
Пример #3
0
 /**
  * Process all routes defined
  * @param array $routes
  * @param Mapping\ClassMetaData $metadata
  * @throws DrestException
  */
 protected function processRoutes(array $routes, \Drest\Mapping\ClassMetaData $metadata)
 {
     $originFound = false;
     foreach ($routes as $route) {
         $routeMetaData = new \Drest\Mapping\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 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 (isset($route['origin']) && !is_null($route['origin'])) {
             if ($originFound) {
                 throw DrestException::resourceCanOnlyHaveOneRouteSetAsOrigin();
             }
             $metadata->originRouteName = $route['name'];
             $originFound = true;
         }
         $metadata->addRouteMetaData($routeMetaData);
     }
 }