public function testPersist()
 {
     $route = new Route();
     $root = self::$dm->find(null, self::ROUTE_ROOT);
     $route->setRouteContent($root);
     // this happens to be a referenceable node
     $route->setPosition($root, 'testroute');
     $route->setDefault('x', 'y');
     $route->setRequirement('testreq', 'testregex');
     $route->setOptions(array('test' => 'value'));
     $route->setOption('another', 'value2');
     self::$dm->persist($route);
     self::$dm->flush();
     $this->assertEquals('/testroute', $route->getPattern());
     self::$dm->clear();
     $route = self::$dm->find(null, self::ROUTE_ROOT . '/testroute');
     $this->assertNotNull($route->getRouteContent());
     $this->assertEquals('/testroute', $route->getPattern());
     $this->assertEquals('y', $route->getDefault('x'));
     $defaults = $route->getDefaults();
     $this->assertArrayHasKey('x', $defaults);
     $this->assertEquals('y', $defaults['x']);
     $requirements = $route->getRequirements();
     $this->assertArrayHasKey('testreq', $requirements);
     $this->assertEquals('testregex', $requirements['testreq']);
     $options = $route->getOptions();
     $this->assertArrayHasKey('test', $options);
     $this->assertEquals('value', $options['test']);
     $this->assertArrayHasKey('another', $options);
     $this->assertEquals('value2', $options['another']);
 }
 protected function updateLocale(Route $doc, $id)
 {
     $matches = array();
     // only update route objects and only if the prefix can match, to allow
     // for more than one listener and more than one route root
     if (!preg_match('#' . $this->idPrefix . '/([^/]+)(/|$)#', $id, $matches)) {
         return;
     }
     if (in_array($matches[1], $this->locales) && !$doc->getDefault('_locale') && !$doc->getRequirement('_locale')) {
         $doc->setDefault('_locale', $matches[1]);
         $doc->setRequirement('_locale', $matches[1]);
     }
 }
Пример #3
0
 /**
  * Load routing data into the document manager.
  *
  * NOTE: We demo all possibilities. Of course, you should try to be
  * consistent in what you use and only use different things for special
  * cases.
  *
  * @param $dm
  */
 public function load(ObjectManager $dm)
 {
     $session = $dm->getPhpcrSession();
     $basepath = $this->container->getParameter('symfony_cmf_routing_extra.routing_repositoryroot');
     $content_path = $this->container->getParameter('symfony_cmf_content.static_basepath');
     if ($session->itemExists($basepath)) {
         $session->removeItem($basepath);
     }
     NodeHelper::createPath($session, dirname($basepath));
     $root = $dm->find(null, dirname($basepath));
     $locales = array('en', 'fr', 'de');
     //TODO: can we get this from phpcr-odm in a sane way?
     $parent = new MultilangLanguageSelectRoute();
     $parent->setPosition($root, basename($basepath));
     $dm->persist($parent);
     foreach ($locales as $locale) {
         $home = new Route();
         $home->setPosition($parent, $locale);
         $home->setDefault('_locale', $locale);
         $home->setRouteContent($dm->find(null, "{$content_path}/home"));
         $dm->persist($home);
         $company = new Route();
         $company->setPosition($home, 'company');
         $company->setDefault('_locale', $locale);
         $company->setRouteContent($dm->find(null, "{$content_path}/company"));
         $dm->persist($company);
         $team = new Route();
         $team->setPosition($company, 'team');
         $team->setDefault('_locale', $locale);
         $team->setRouteContent($dm->find(null, "{$content_path}/team"));
         $dm->persist($team);
         $more = new Route();
         $more->setPosition($company, 'more');
         $more->setDefault('_locale', $locale);
         $more->setRouteContent($dm->find(null, "{$content_path}/more"));
         $dm->persist($more);
         $projects = new Route();
         $projects->setPosition($home, 'projects');
         $projects->setDefault('_locale', $locale);
         $projects->setRouteContent($dm->find(null, "{$content_path}/projects"));
         $dm->persist($projects);
         $cmf = new Route();
         $cmf->setPosition($projects, 'cmf');
         $cmf->setDefault('_locale', $locale);
         $cmf->setRouteContent($dm->find(null, "{$content_path}/cmf"));
         $dm->persist($cmf);
     }
     // demo features of routing
     // we can create routes without locales, but will lose the language context of course
     $demo = new Route();
     $demo->setPosition($parent, 'demo');
     $demo->setRouteContent($dm->find(null, "{$content_path}/demo"));
     $demo->setDefault(RouteObjectInterface::TEMPLATE_NAME, 'SandboxMainBundle:Demo:template_explicit.html.twig');
     $dm->persist($demo);
     // explicit template
     $template = new Route();
     $template->setPosition($demo, 'atemplate');
     $template->setRouteContent($dm->find(null, "{$content_path}/demo_template"));
     $template->setDefault(RouteObjectInterface::TEMPLATE_NAME, 'SandboxMainBundle:Demo:template_explicit.html.twig');
     $dm->persist($template);
     // explicit controller
     $controller = new Route();
     $controller->setPosition($demo, 'controller');
     $controller->setRouteContent($dm->find(null, "{$content_path}/demo_controller"));
     $controller->setDefault('_controller', 'sandbox_main.controller:specialAction');
     $dm->persist($controller);
     // alias to controller mapping
     $alias = new Route();
     $alias->setPosition($demo, 'alias');
     $alias->setRouteContent($dm->find(null, "{$content_path}/demo_alias"));
     $alias->setDefault(RouteObjectInterface::CONTROLLER_ALIAS, 'demo_alias');
     $dm->persist($alias);
     // class to controller mapping
     $class = new Route();
     $class->setPosition($demo, 'class');
     $class->setRouteContent($dm->find(null, "{$content_path}/demo_class"));
     $dm->persist($class);
     // redirections
     // redirect to uri
     $redirect = new RedirectRoute();
     $redirect->setPosition($parent, 'external');
     $redirect->setUri('http://cmf.symfony.com');
     $dm->persist($redirect);
     // redirect to other doctrine route
     $redirectRoute = new RedirectRoute();
     $redirectRoute->setPosition($parent, 'short');
     $redirectRoute->setRouteTarget($cmf);
     $dm->persist($redirectRoute);
     // redirect to Symfony route
     $redirectS = new RedirectRoute();
     $redirectS->setPosition($parent, 'short1');
     $redirectS->setRouteName('test');
     $dm->persist($redirectS);
     // class to template mapping is used for all the rest
     $dm->flush();
 }
 /**
  * @expectedException \Symfony\Component\Routing\Exception\MethodNotAllowedException
  */
 public function testNotAllowed()
 {
     $root = self::$dm->find(null, self::ROUTE_ROOT);
     // do not set a content here, or we need a valid request and so on...
     $route = new Route();
     $route->setPosition($root, 'notallowed');
     $route->setRequirement('_method', 'GET');
     $route->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController');
     self::$dm->persist($route);
     self::$dm->flush();
     self::$router->matchRequest(Request::create('/notallowed', 'POST'));
 }