public function __construct($BaseUrl = '') { AutoLoader::getNamespaceAutoLoader('Symfony\\Component', __DIR__ . '/../../../Vendor/'); AutoLoader::getNamespaceAutoLoader('Symfony\\Component', __DIR__ . '/../../../../../Core/HttpKernel/Vendor/'); $this->SymfonyRouteCollection = new RouteCollection(); $this->SymfonyRequestContext = new RequestContext(); $this->SymfonyRequestContext->setBaseUrl($BaseUrl); $this->SymfonyUrlMatcher = new UrlMatcher($this->SymfonyRouteCollection, $this->SymfonyRequestContext); $this->SymfonyEventDispatcher = new EventDispatcher(); $this->SymfonyEventDispatcher->addSubscriber(new RouterListener($this->SymfonyUrlMatcher)); $this->SymfonyHttpKernel = new HttpKernel($this->SymfonyEventDispatcher, new ControllerResolver()); }
protected function init(RouteCollection $routes) { $context = new RequestContext(); $context->fromRequest($this->request); $context->setBaseUrl($this->options['basepath']); $this->matcher = new UrlMatcher($routes, $context); $this->generator = new UrlGenerator($routes, $context); }
public function testComposeSchemaHostAndBasePathWithBackSplashOnResolve() { $requestContext = new RequestContext(); $requestContext->setScheme('theSchema'); $requestContext->setHost('thehost'); $requestContext->setBaseUrl('\\'); $resolver = new WebPathResolver($this->createFilesystemMock(), $requestContext, '/aWebRoot', 'aCachePrefix'); $this->assertEquals('theschema://thehost/aCachePrefix/aFilter/aPath', $resolver->resolve('aPath', 'aFilter')); }
/** * Generate a URL to a route * * @param string $route Name of the route to travel * @param array $params String or array of additional url parameters * @param bool $is_amp Is url using & (true) or & (false) * @param string|bool $session_id Possibility to use a custom session id instead of the global one * @param bool|string $reference_type The type of reference to be generated (one of the constants) * @return string The URL already passed through append_sid() */ public function route($route, array $params = array(), $is_amp = true, $session_id = false, $reference_type = UrlGeneratorInterface::ABSOLUTE_PATH) { $anchor = ''; if (isset($params['#'])) { $anchor = '#' . $params['#']; unset($params['#']); } $context = new RequestContext(); $context->fromRequest($this->symfony_request); if ($this->config['force_server_vars']) { $context->setHost($this->config['server_name']); $context->setScheme(substr($this->config['server_protocol'], 0, -3)); $context->setHttpPort($this->config['server_port']); $context->setHttpsPort($this->config['server_port']); $context->setBaseUrl(rtrim($this->config['script_path'], '/')); } $script_name = $this->symfony_request->getScriptName(); $page_name = substr($script_name, -1, 1) == '/' ? '' : utf8_basename($script_name); $base_url = $context->getBaseUrl(); // Append page name if base URL does not contain it if (!empty($page_name) && strpos($base_url, '/' . $page_name) === false) { $base_url .= '/' . $page_name; } // If enable_mod_rewrite is false we need to replace the current front-end by app.php, otherwise we need to remove it. $base_url = str_replace('/' . $page_name, empty($this->config['enable_mod_rewrite']) ? '/app.' . $this->php_ext : '', $base_url); // We need to update the base url to move to the directory of the app.php file if the current script is not app.php if ($page_name !== 'app.php' && !$this->config['force_server_vars']) { if (empty($this->config['enable_mod_rewrite'])) { $base_url = str_replace('/app.' . $this->php_ext, '/' . $this->phpbb_root_path . 'app.' . $this->php_ext, $base_url); } else { $base_url .= preg_replace(get_preg_expression('path_remove_dot_trailing_slash'), '$2', $this->phpbb_root_path); } } $base_url = $this->request->escape($this->filesystem->clean_path($base_url), true); $context->setBaseUrl($base_url); $this->router->setContext($context); $route_url = $this->router->generate($route, $params, $reference_type); if ($is_amp) { $route_url = str_replace(array('&', '&'), array('&', '&'), $route_url); } if ($reference_type === UrlGeneratorInterface::RELATIVE_PATH && empty($this->config['enable_mod_rewrite'])) { $route_url = 'app.' . $this->php_ext . '/' . $route_url; } return append_sid($route_url . $anchor, false, $is_amp, $session_id, true); }
/** * Call match on ChainRouter that has RequestMatcher in the chain. * * @dataProvider provideBaseUrl */ public function testMatchWithRequestMatchersAndContext($baseUrl) { $url = '//test'; list($low) = $this->createRouterMocks(); $high = $this->getMock('Symfony\\Cmf\\Component\\Routing\\Tests\\Routing\\RequestMatcher'); $high->expects($this->once())->method('matchRequest')->with($this->callback(function (Request $r) use($url, $baseUrl) { return true === $r->isSecure() && 'foobar.com' === $r->getHost() && 4433 === $r->getPort() && $baseUrl === $r->getBaseUrl() && $url === $r->getPathInfo(); }))->will($this->throwException(new \Symfony\Component\Routing\Exception\ResourceNotFoundException())); $low->expects($this->once())->method('match')->with($url)->will($this->returnValue(array('test'))); $this->router->add($low, 10); $this->router->add($high, 20); $requestContext = new RequestContext(); $requestContext->setScheme('https'); $requestContext->setHost('foobar.com'); $requestContext->setHttpsPort(4433); $requestContext->setBaseUrl($baseUrl); $this->router->setContext($requestContext); $result = $this->router->match($url); $this->assertEquals(array('test'), $result); }
/** * @param $url * @param RequestContext $context * @return array */ private function setUrlInContext($url, RequestContext $context) { $parts = parse_url($url); if (false === (bool) $parts) { throw new \RuntimeException('Invalid Application URL configured. Unable to generate links'); } if (isset($parts['schema'])) { $context->setScheme($parts['schema']); } if (isset($parts['host'])) { $context->setHost($parts['host']); } if (isset($parts['port'])) { $context->setHttpPort($parts['port']); $context->setHttpsPort($parts['port']); } if (isset($parts['path'])) { $context->setBaseUrl(rtrim($parts['path'], '/')); } if (isset($parts['query'])) { $context->setQueryString($parts['query']); } }
/** * Returns the install tool URL. * * @return string */ private function getInstallToolUrl() { $routes = new RouteCollection(); $routes->add('contao_install', new Route('/contao/install')); $context = new RequestContext(); $context->fromRequest($this->request); $context->setBaseUrl(''); return str_replace('/install.php/', '/', (new UrlGenerator($routes, $context))->generate('contao_install')); }
/** * @dataProvider providerGenerateWithSiteAccess * * @param string $urlGenerated The URL generated by the standard UrLGenerator * @param string $relevantUri The relevant URI part of the generated URL (without host and basepath) * @param string $expectedUrl The URL we're expecting to be finally generated, with siteaccess * @param string $saName The SiteAccess name * @param bool $isMatcherLexer True if the siteaccess matcher is URILexer * @param bool $absolute True if generated link needs to be absolute * @param string $routeName */ public function testGenerateWithSiteAccess($urlGenerated, $relevantUri, $expectedUrl, $saName, $isMatcherLexer, $absolute, $routeName) { $routeName = $routeName ?: __METHOD__; $nonSiteAccessAwareRoutes = array('_dontwantsiteaccess'); $generator = $this->getMock('Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface'); $generator->expects($this->once())->method('generate')->with($routeName)->will($this->returnValue($urlGenerated)); /** @var DefaultRouter|\PHPUnit_Framework_MockObject_MockObject $router */ $router = $this->generateRouter(array('getGenerator')); $router->expects($this->any())->method('getGenerator')->will($this->returnValue($generator)); // If matcher is URILexer, we make it act as it's supposed to, prepending the siteaccess. if ($isMatcherLexer) { $matcher = $this->getMock('eZ\\Publish\\Core\\MVC\\Symfony\\SiteAccess\\URILexer'); // Route is siteaccess aware, we're expecting analyseLink() to be called if (!in_array($routeName, $nonSiteAccessAwareRoutes)) { $matcher->expects($this->once())->method('analyseLink')->with($relevantUri)->will($this->returnValue("/{$saName}{$relevantUri}")); } else { $matcher->expects($this->never())->method('analyseLink'); } } else { $matcher = $this->getMock('eZ\\Publish\\Core\\MVC\\Symfony\\SiteAccess\\Matcher'); } $sa = new SiteAccess($saName, 'test', $matcher); $router->setSiteAccess($sa); $requestContext = new RequestContext(); $urlComponents = parse_url($urlGenerated); if (isset($urlComponents['host'])) { $requestContext->setHost($urlComponents['host']); $requestContext->setScheme($urlComponents['scheme']); if (isset($urlComponents['port']) && $urlComponents['scheme'] === 'http') { $requestContext->setHttpPort($urlComponents['port']); } else { if (isset($urlComponents['port']) && $urlComponents['scheme'] === 'https') { $requestContext->setHttpsPort($urlComponents['port']); } } } $requestContext->setBaseUrl(substr($urlComponents['path'], 0, strpos($urlComponents['path'], $relevantUri))); $router->setContext($requestContext); $router->setNonSiteAccessAwareRoutes($nonSiteAccessAwareRoutes); $this->assertSame($expectedUrl, $router->generate($routeName, array(), $absolute)); }