public function testPersist() { $route = new Route(); $root = $this->getDm()->find(null, self::ROUTE_ROOT); $route->setContent($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'); $this->getDm()->persist($route); $this->getDm()->flush(); $this->assertEquals('/testroute', $route->getPath()); $this->getDm()->clear(); $route = $this->getDm()->find(null, self::ROUTE_ROOT . '/testroute'); $this->assertNotNull($route->getContent()); $this->assertEquals('/testroute', $route->getPath()); $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']); }
public function setUp() { parent::setUp(); $this->db('PHPCR')->createTestNode(); $this->createRoute(self::ROUTE_ROOT); $this->router = $this->getContainer()->get('router'); $root = $this->getDm()->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, 'testroute'); $route->setVariablePattern('/{slug}/{id}'); $route->setDefault('id', '0'); $route->setRequirement('id', '[0-9]+'); $route->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController'); //TODO options $this->getDm()->persist($route); $childroute = new Route(); $childroute->setPosition($route, 'child'); $childroute->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController'); $this->getDm()->persist($childroute); $formatroute = new Route(array('add_format_pattern' => true)); $formatroute->setPosition($root, 'format'); $formatroute->setVariablePattern('/{id}'); $formatroute->setRequirement('_format', 'html|json'); $formatroute->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController'); $this->getDm()->persist($formatroute); $format2jsonroute = new Route(array('add_format_pattern' => true)); $format2jsonroute->setPosition($root, 'format2.json'); $format2jsonroute->setDefault('_format', 'json'); $format2jsonroute->setRequirement('_format', 'json'); $format2jsonroute->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testJsonController'); $this->getDm()->persist($format2jsonroute); $format2route = new Route(array('add_format_pattern' => true)); $format2route->setPosition($root, 'format2'); $format2route->setDefault(RouteObjectInterface::CONTROLLER_NAME, 'testController'); $this->getDm()->persist($format2route); $this->getDm()->flush(); }
/** * 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 $manager \Doctrine\ODM\PHPCR\DocumentManager */ public function load(ObjectManager $manager) { if (!$manager instanceof DocumentManager) { $class = get_class($manager); throw new \RuntimeException("Fixture requires a PHPCR ODM DocumentManager instance, instance of '{$class}' given."); } $session = $manager->getPhpcrSession(); $basepath = $this->container->getParameter('cmf_routing.dynamic.persistence.phpcr.admin_basepath'); if ($session->itemExists($basepath)) { $session->removeItem($basepath); } NodeHelper::createPath($session, $basepath); $parent = $manager->find(null, $basepath); $content_path = $this->container->getParameter('cmf_content.persistence.phpcr.content_basepath'); $locales = $this->container->getParameter('locales'); foreach ($locales as $locale) { $home = new Route(); $home->setPosition($parent, $locale); $home->setDefault(RouteObjectInterface::TEMPLATE_NAME, 'homepage/index.html.twig'); $home->setContent($manager->find(null, "{$content_path}/home")); $manager->persist($home); $company = new Route(); $company->setPosition($home, 'company'); $company->setContent($manager->find(null, "{$content_path}/company")); $manager->persist($company); $team = new Route(); $team->setPosition($company, 'team'); $team->setContent($manager->find(null, "{$content_path}/team")); $manager->persist($team); $more = new Route(); $more->setPosition($company, 'more'); $more->setContent($manager->find(null, "{$content_path}/more")); $manager->persist($more); $projects = new Route(); $projects->setPosition($home, 'projects'); $projects->setContent($manager->find(null, "{$content_path}/projects")); $manager->persist($projects); $cmf = new Route(); $cmf->setPosition($projects, 'cmf'); $cmf->setContent($manager->find(null, "{$content_path}/cmf")); $manager->persist($cmf); $seo = new Route(); $seo->setPosition($home, 'simple-seo-example'); $seo->setContent($manager->find(null, "{$content_path}/simple-seo-example")); $manager->persist($seo); $seo = new Route(); $seo->setPosition($home, 'simple-seo-property'); $seo->setContent($manager->find(null, "{$content_path}/simple-seo-property")); $manager->persist($seo); $seo = new Route(); $seo->setPosition($home, 'demo-seo-extractor'); $seo->setContent($manager->find(null, "{$content_path}/demo-seo-extractor")); $manager->persist($seo); } // 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->setContent($manager->find(null, "{$content_path}/demo")); $demo->setDefault(RouteObjectInterface::TEMPLATE_NAME, 'demo/template_explicit.html.twig'); $manager->persist($demo); // explicit template $template = new Route(); $template->setPosition($demo, 'atemplate'); $template->setContent($manager->find(null, "{$content_path}/demo_template")); $template->setDefault(RouteObjectInterface::TEMPLATE_NAME, 'demo/template_explicit.html.twig'); $manager->persist($template); // explicit controller $controller = new Route(); $controller->setPosition($demo, 'controller'); $controller->setContent($manager->find(null, "{$content_path}/demo_controller")); $controller->setDefault('_controller', 'app.content_controller:specialAction'); $manager->persist($controller); // type to controller mapping $type = new Route(); $type->setPosition($demo, 'type'); $type->setContent($manager->find(null, "{$content_path}/demo_type")); $type->setDefault('type', 'demo_type'); $manager->persist($type); // class to controller mapping $class = new Route(); $class->setPosition($demo, 'class'); $class->setContent($manager->find(null, "{$content_path}/demo_class")); $manager->persist($class); // redirections // redirect to uri $redirect = new RedirectRoute(); $redirect->setPosition($parent, 'external'); $redirect->setUri('http://cmf.symfony.com'); $manager->persist($redirect); // redirect to other doctrine route $redirectRoute = new RedirectRoute(); $redirectRoute->setPosition($parent, 'short'); $redirectRoute->setRouteTarget($cmf); $manager->persist($redirectRoute); // redirect to Symfony route $redirectS = new RedirectRoute(); $redirectS->setPosition($parent, 'short1'); $redirectS->setRouteName('test'); $manager->persist($redirectS); // class to template mapping is used for all the rest $default_locale = $this->container->getParameter('locale'); $singlelocale = new Route(); $singlelocale->setPosition($manager->find(null, "{$basepath}/{$default_locale}"), 'singlelocale'); $singlelocale->setDefault('_locale', $default_locale); $singlelocale->setRequirement('_locale', $default_locale); $singlelocale->setContent($manager->find(null, "{$content_path}/singlelocale")); $manager->persist($singlelocale); // publication demos $publicationDemo = new Route(); $publicationDemo->setPosition($parent, 'publicationdemo'); $publicationDemo->setContent($manager->find(null, "{$content_path}/publication_demo")); $manager->persist($publicationDemo); $notPublished = new Route(); $notPublished->setPosition($publicationDemo, 'notpublished'); $notPublished->setContent($manager->find(null, "{$content_path}/not_published")); $manager->persist($notPublished); $publishedTomorrow = new Route(); $publishedTomorrow->setPosition($publicationDemo, 'publishedtomorrow'); $publishedTomorrow->setContent($manager->find(null, "{$content_path}/published_tomorrow")); $manager->persist($publishedTomorrow); $manager->flush(); }
/** * Update the locale of a route if $id starts with the prefix and has a * valid locale right after. * * @param Route $doc The route object * @param string $id The id (in move case, this is not the current * id of $route). * @param DocumentManager $dm The document manager to get locales from if * the setAvailableTranslations option is * enabled. * @param boolean $force Whether to update the locale even if the * route already has a locale. */ protected function updateLocale(Route $doc, $id, DocumentManager $dm, $force = false) { $matches = array(); // only update if the prefix matches, to allow for more than one // listener and more than one route root. if (!preg_match('#(' . implode('|', $this->getPrefixes()) . ')/([^/]+)(/|$)#', $id, $matches)) { return; } if (in_array($locale = $matches[2], $this->locales)) { if ($force || !$doc->getDefault('_locale')) { $doc->setDefault('_locale', $locale); } if ($force || !$doc->getRequirement('_locale')) { $doc->setRequirement('_locale', $locale); } } else { if ($this->addLocalePattern) { $doc->setOption('add_locale_pattern', true); } if ($this->updateAvailableTranslations && $dm->isDocumentTranslatable($doc) && !$doc->getRequirement('_locale')) { $locales = $dm->getLocalesFor($doc, true); $doc->setRequirement('_locale', implode('|', $locales)); } } }
/** * @expectedException \Symfony\Component\Routing\Exception\MethodNotAllowedException */ public function testNotAllowed() { $root = $this->getDm()->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'); $this->getDm()->persist($route); $this->getDm()->flush(); $this->router->matchRequest(Request::create('/notallowed', 'POST')); }