/**
  * {@inheritdoc}
  */
 public function register(Container $app)
 {
     /**
      * Holds information about the current request
      *
      * @return RequestContext
      */
     $app['request_context'] = function () use($app) {
         $context = new RequestContext();
         // set default http & https ports if not set
         $context->setHttpPort(isset($app['request.http_port']) ? $app['request.http_port'] : 80);
         $context->setHttpsPort(isset($app['request.https_port']) ? $app['request.https_port'] : 443);
         return $context;
     };
     /**
      * Matches URL based on a set of routes.
      *
      * @return UrlMatcher
      */
     $app['matcher'] = function () use($app) {
         return new UrlMatcher($app['router'], $app['request_context']);
     };
     /**
      * Router
      */
     $options = array('cache_dir' => true === $app['use_cache'] ? __DIR__ . '/' . self::CACHE_DIRECTORY : null, 'debug' => true);
     $app['router'] = function () use($app, $options) {
         $router = new Router($app['config.loader'], sprintf(self::CONFIG_ROUTES_FILE, $app['env']), $options);
         return $router->getRouteCollection();
     };
 }
 /**
  * @dataProvider getPortData
  */
 public function testPort($defaultHttpPort, $defaultHttpsPort, $uri, $expectedHttpPort, $expectedHttpsPort)
 {
     $listener = new RouterListener($this->router, $defaultHttpPort, $defaultHttpsPort);
     $expectedContext = new RequestContext();
     $expectedContext->setHttpPort($expectedHttpPort);
     $expectedContext->setHttpsPort($expectedHttpsPort);
     $expectedContext->setScheme(0 === strpos($uri, 'https') ? 'https' : 'http');
     $this->router->expects($this->once())->method('setContext')->with($expectedContext);
     $event = $this->createGetResponseEventForUri($uri);
     $listener->onEarlyCoreRequest($event);
 }
 public function testNonStandHttpsRedirect()
 {
     $coll = new RouteCollection();
     $coll->add('foo', new Route('/foo', array(), array('_scheme' => 'https')));
     $context = new RequestContext();
     $context->setHttpsPort(9000);
     $context->setScheme('http');
     $matcher = new UrlMatcher($coll, $context);
     $result = $matcher->match('/foo');
     $this->assertArrayHasKey('url', $result);
     $this->assertSame($result['url'], 'https://localhost:9000/foo');
 }
 /**
  * @dataProvider getPortData
  */
 public function testPort($defaultHttpPort, $defaultHttpsPort, $uri, $expectedHttpPort, $expectedHttpsPort)
 {
     $urlMatcher = $this->getMockBuilder('Symfony\\Component\\Routing\\Matcher\\UrlMatcherInterface')->disableOriginalConstructor()->getMock();
     $context = new RequestContext();
     $context->setHttpPort($defaultHttpPort);
     $context->setHttpsPort($defaultHttpsPort);
     $urlMatcher->expects($this->any())->method('getContext')->will($this->returnValue($context));
     $routerListener = new RouterListener($urlMatcher, null, null, $this->requestStack);
     $request = $this->createRequestForUri($uri);
     $routerListener->match($request);
     $this->assertEquals($expectedHttpPort, $context->getHttpPort());
     $this->assertEquals($expectedHttpsPort, $context->getHttpsPort());
     $this->assertEquals(0 === strpos($uri, 'https') ? 'https' : 'http', $context->getScheme());
 }
Beispiel #5
0
 protected function initRouting()
 {
     $this['routes-collection'] = function () {
         return new RouteCollection();
     };
     $this['request-context'] = function ($container) {
         $context = new RequestContext();
         $context->setHttpPort($container['http_port']);
         $context->setHttpsPort($container['https_port']);
         return $context;
     };
     $this['url-matcher'] = function ($container) {
         return new UrlMatcher($container['routes-collection'], $container['request-context']);
     };
     $this['url-generator'] = function ($container) {
         return new UrlGenerator($container['routes-collection'], $container['request-context'], $container['logger.logger']);
     };
     $this['resolver'] = function ($container) {
         return new ControllerResolver($container, $container['logger.logger']);
     };
 }
 public function testFromRequest()
 {
     $request = Request::create('https://test.com:444/foo?bar=baz');
     $requestContext = new RequestContext();
     $requestContext->setHttpPort(123);
     $requestContext->fromRequest($request);
     $this->assertEquals('', $requestContext->getBaseUrl());
     $this->assertEquals('GET', $requestContext->getMethod());
     $this->assertEquals('test.com', $requestContext->getHost());
     $this->assertEquals('https', $requestContext->getScheme());
     $this->assertEquals('/foo', $requestContext->getPathInfo());
     $this->assertEquals('bar=baz', $requestContext->getQueryString());
     $this->assertSame(123, $requestContext->getHttpPort());
     $this->assertSame(444, $requestContext->getHttpsPort());
     $request = Request::create('http://test.com:8080/foo?bar=baz');
     $requestContext = new RequestContext();
     $requestContext->setHttpsPort(567);
     $requestContext->fromRequest($request);
     $this->assertSame(8080, $requestContext->getHttpPort());
     $this->assertSame(567, $requestContext->getHttpsPort());
 }
 public function register(Container $app)
 {
     $app['url_generator'] = function ($app) {
         return new UrlGenerator($app['routes'], $app['request_context']);
     };
     $app['request_matcher'] = function ($app) {
         return new RedirectableUrlMatcher($app['routes'], $app['request_context']);
     };
     $app['request_context'] = function ($app) {
         $context = new RequestContext();
         $context->setHttpPort(isset($app['request.http_port']) ? $app['request.http_port'] : 80);
         $context->setHttpsPort(isset($app['request.https_port']) ? $app['request.https_port'] : 443);
         return $context;
     };
     $app['routing.listener'] = function ($app) {
         $urlMatcher = new LazyRequestMatcher(function () use($app) {
             return $app['request_matcher'];
         });
         return new RouterListener($urlMatcher, $app['request_context'], $app['logger'], $app['request_stack']);
     };
 }
 public function register(Container $app)
 {
     $app['route_class'] = 'Silex\\Route';
     $app['route_factory'] = $app->factory(function ($app) {
         return new $app['route_class']();
     });
     $app['routes_factory'] = $app->factory(function () {
         return new RouteCollection();
     });
     $app['routes'] = function ($app) {
         return $app['routes_factory'];
     };
     $app['url_generator'] = function ($app) {
         return new UrlGenerator($app['routes'], $app['request_context']);
     };
     $app['request_matcher'] = function ($app) {
         return new RedirectableUrlMatcher($app['routes'], $app['request_context']);
     };
     $app['request_context'] = function ($app) {
         $context = new RequestContext();
         $context->setHttpPort(isset($app['request.http_port']) ? $app['request.http_port'] : 80);
         $context->setHttpsPort(isset($app['request.https_port']) ? $app['request.https_port'] : 443);
         return $context;
     };
     $app['controllers'] = function ($app) {
         return $app['controllers_factory'];
     };
     $app['controllers_factory'] = $app->factory(function ($app) {
         return new ControllerCollection($app['route_factory'], $app['routes_factory']);
     });
     $app['routing.listener'] = function ($app) {
         $urlMatcher = new LazyRequestMatcher(function () use($app) {
             return $app['request_matcher'];
         });
         if (Kernel::VERSION_ID >= 20800) {
             return new RouterListener($urlMatcher, $app['request_stack'], $app['request_context'], $app['logger']);
         }
         return new RouterListener($urlMatcher, $app['request_context'], $app['logger'], $app['request_stack']);
     };
 }
 /**
  * Instantiate a new Application.
  *
  * Objects and parameters can be passed as argument to the constructor.
  *
  * @param array $values The parameters or objects.
  */
 public function __construct(array $values = array())
 {
     parent::__construct();
     $app = $this;
     $this['logger'] = null;
     $this['routes'] = $this->share(function () {
         return new RouteCollection();
     });
     $this['controllers'] = $this->share(function () use($app) {
         return $app['controllers_factory'];
     });
     $this['controllers_factory'] = function () use($app) {
         return new ControllerCollection($app['route_factory']);
     };
     $this['route_class'] = 'Silex\\Route';
     $this['route_factory'] = function () use($app) {
         return new $app['route_class']();
     };
     $this['exception_handler'] = $this->share(function () use($app) {
         return new ExceptionHandler($app['debug']);
     });
     $this['dispatcher_class'] = 'Symfony\\Component\\EventDispatcher\\EventDispatcher';
     $this['dispatcher'] = $this->share(function () use($app) {
         /*
          * @var EventDispatcherInterface
          */
         $dispatcher = new $app['dispatcher_class']();
         $urlMatcher = new LazyUrlMatcher(function () use($app) {
             return $app['url_matcher'];
         });
         $dispatcher->addSubscriber(new RouterListener($urlMatcher, $app['request_context'], $app['logger'], $app['request_stack']));
         $dispatcher->addSubscriber(new LocaleListener($app, $urlMatcher, $app['request_stack']));
         if (isset($app['exception_handler'])) {
             $dispatcher->addSubscriber($app['exception_handler']);
         }
         $dispatcher->addSubscriber(new ResponseListener($app['charset']));
         $dispatcher->addSubscriber(new MiddlewareListener($app));
         $dispatcher->addSubscriber(new ConverterListener($app['routes'], $app['callback_resolver']));
         $dispatcher->addSubscriber(new StringToResponseListener());
         return $dispatcher;
     });
     $this['callback_resolver'] = $this->share(function () use($app) {
         return new CallbackResolver($app);
     });
     $this['resolver'] = $this->share(function () use($app) {
         return new ControllerResolver($app, $app['logger']);
     });
     $this['kernel'] = $this->share(function () use($app) {
         return new HttpKernel($app['dispatcher'], $app['resolver'], $app['request_stack']);
     });
     $this['request_stack'] = $this->share(function () use($app) {
         if (class_exists('Symfony\\Component\\HttpFoundation\\RequestStack')) {
             return new RequestStack();
         }
     });
     $this['request_context'] = $this->share(function () use($app) {
         $context = new RequestContext();
         $context->setHttpPort($app['request.http_port']);
         $context->setHttpsPort($app['request.https_port']);
         return $context;
     });
     $this['url_matcher'] = $this->share(function () use($app) {
         return new RedirectableUrlMatcher($app['routes'], $app['request_context']);
     });
     $this['request_error'] = $this->protect(function () {
         throw new \RuntimeException('Accessed request service outside of request scope. Try moving that call to a before handler or controller.');
     });
     $this['request'] = $this['request_error'];
     $this['request.http_port'] = 80;
     $this['request.https_port'] = 443;
     $this['debug'] = false;
     $this['charset'] = 'UTF-8';
     $this['locale'] = 'en';
     foreach ($values as $key => $value) {
         $this[$key] = $value;
     }
 }
Beispiel #10
0
 /**
  * Constructor.
  */
 public function __construct()
 {
     $app = $this;
     $this['autoloader'] = $this->share(function () {
         $loader = new UniversalClassLoader();
         $loader->register();
         return $loader;
     });
     $this['routes'] = $this->share(function () {
         return new RouteCollection();
     });
     $this['controllers'] = $this->share(function () use($app) {
         return new ControllerCollection();
     });
     $this['exception_handler'] = $this->share(function () {
         return new ExceptionHandler();
     });
     $this['dispatcher'] = $this->share(function () use($app) {
         $dispatcher = new EventDispatcher();
         $dispatcher->addSubscriber($app);
         if (isset($app['exception_handler'])) {
             $dispatcher->addSubscriber($app['exception_handler']);
         }
         $dispatcher->addSubscriber(new ResponseListener($app['charset']));
         $dispatcher->addSubscriber(new RouterListener($app['url_matcher']));
         return $dispatcher;
     });
     $this['resolver'] = $this->share(function () use($app) {
         return new ControllerResolver($app);
     });
     $this['kernel'] = $this->share(function () use($app) {
         return new HttpKernel($app['dispatcher'], $app['resolver']);
     });
     $this['request_context'] = $this->share(function () use($app) {
         $context = new RequestContext();
         $context->setHttpPort($app['request.http_port']);
         $context->setHttpsPort($app['request.https_port']);
         return $context;
     });
     $this['url_matcher'] = $this->share(function () use($app) {
         return new RedirectableUrlMatcher($app['routes'], $app['request_context']);
     });
     $this['request.http_port'] = 80;
     $this['request.https_port'] = 443;
     $this['debug'] = false;
     $this['charset'] = 'UTF-8';
 }
Beispiel #11
0
 /**
  * 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);
 }
Beispiel #12
0
 /**
  * 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);
 }
Beispiel #13
0
 /**
  * Sets the context from the domain.
  *
  * @param RequestContext $context
  * @param array          $parameters
  * @param string         $referenceType
  */
 private function addHostToContext(RequestContext $context, array $parameters, &$referenceType)
 {
     list($host, $port) = $this->getHostAndPort($parameters['_domain']);
     if ($context->getHost() === $host) {
         return;
     }
     $context->setHost($host);
     $referenceType = UrlGeneratorInterface::ABSOLUTE_URL;
     if (!$port) {
         return;
     }
     if (isset($parameters['_ssl']) && true === $parameters['_ssl']) {
         $context->setHttpsPort($port);
     } else {
         $context->setHttpPort($port);
     }
 }
 /**
  * @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));
 }
Beispiel #15
0
 /**
  * Constructor.
  */
 public function __construct()
 {
     $app = $this;
     $this['logger'] = null;
     $this['routes'] = $this->share(function () {
         return new RouteCollection();
     });
     $this['controllers'] = $this->share(function () use($app) {
         return new ControllerCollection();
     });
     $this['exception_handler'] = $this->share(function () {
         return new ExceptionHandler();
     });
     $this['dispatcher'] = $this->share(function () use($app) {
         $dispatcher = new EventDispatcher();
         $dispatcher->addSubscriber($app);
         $urlMatcher = new LazyUrlMatcher(function () use($app) {
             return $app['url_matcher'];
         });
         $dispatcher->addSubscriber(new RouterListener($urlMatcher, $app['logger']));
         return $dispatcher;
     });
     $this['resolver'] = $this->share(function () use($app) {
         return new ControllerResolver($app, $app['logger']);
     });
     $this['kernel'] = $this->share(function () use($app) {
         return new HttpKernel($app['dispatcher'], $app['resolver']);
     });
     $this['request_context'] = $this->share(function () use($app) {
         $context = new RequestContext();
         $context->setHttpPort($app['request.http_port']);
         $context->setHttpsPort($app['request.https_port']);
         return $context;
     });
     $this['url_matcher'] = $this->share(function () use($app) {
         return new RedirectableUrlMatcher($app['routes'], $app['request_context']);
     });
     $this['route_middlewares_trigger'] = $this->protect(function (KernelEvent $event) use($app) {
         $request = $event->getRequest();
         $routeName = $request->attributes->get('_route');
         if (!($route = $app['routes']->get($routeName))) {
             return;
         }
         foreach ((array) $route->getOption('_middlewares') as $callback) {
             $ret = call_user_func($callback, $request);
             if ($ret instanceof Response) {
                 $event->setResponse($ret);
                 return;
             } elseif (null !== $ret) {
                 throw new \RuntimeException(sprintf('Middleware for route "%s" returned an invalid response value. Must return null or an instance of Response.', $routeName));
             }
         }
     });
     $this['request.default_locale'] = 'en';
     $this['request_error'] = $this->protect(function () {
         throw new \RuntimeException('Accessed request service outside of request scope. Try moving that call to a before handler or controller.');
     });
     $this['request'] = $this['request_error'];
     $this['request.http_port'] = 80;
     $this['request.https_port'] = 443;
     $this['debug'] = false;
     $this['charset'] = 'UTF-8';
 }
Beispiel #16
0
 /**
  * Constructor.
  */
 public function __construct()
 {
     $app = $this;
     $this['logger'] = null;
     $this['autoloader'] = function () {
         throw new \RuntimeException('You tried to access the autoloader service. The autoloader has been removed from Silex. It is recommended that you use Composer to manage your dependencies and handle your autoloading. See http://getcomposer.org for more information.');
     };
     $this['routes'] = $this->share(function () {
         return new RouteCollection();
     });
     $this['controllers'] = $this->share(function () use($app) {
         return $app['controllers_factory'];
     });
     $this['controllers_factory'] = function () use($app) {
         return new ControllerCollection($app['route_factory']);
     };
     $this['route_class'] = 'Silex\\Route';
     $this['route_factory'] = function () use($app) {
         return new $app['route_class']();
     };
     $this['exception_handler'] = $this->share(function () {
         return new ExceptionHandler();
     });
     $this['dispatcher'] = $this->share(function () use($app) {
         $dispatcher = new EventDispatcher();
         $dispatcher->addSubscriber($app);
         $urlMatcher = new LazyUrlMatcher(function () use($app) {
             return $app['url_matcher'];
         });
         $dispatcher->addSubscriber(new RouterListener($urlMatcher, $app['request_context'], $app['logger']));
         $dispatcher->addSubscriber(new LocaleListener($app['locale'], $urlMatcher));
         return $dispatcher;
     });
     $this['resolver'] = $this->share(function () use($app) {
         return new ControllerResolver($app, $app['logger']);
     });
     $this['kernel'] = $this->share(function () use($app) {
         return new HttpKernel($app['dispatcher'], $app['resolver']);
     });
     $this['request_context'] = $this->share(function () use($app) {
         $context = new RequestContext();
         $context->setHttpPort($app['request.http_port']);
         $context->setHttpsPort($app['request.https_port']);
         return $context;
     });
     $this['url_matcher'] = $this->share(function () use($app) {
         return new RedirectableUrlMatcher($app['routes'], $app['request_context']);
     });
     $this['route_before_middlewares_trigger'] = $this->protect(function (GetResponseEvent $event) use($app) {
         $request = $event->getRequest();
         $routeName = $request->attributes->get('_route');
         if (!($route = $app['routes']->get($routeName))) {
             return;
         }
         foreach ((array) $route->getOption('_before_middlewares') as $callback) {
             $ret = call_user_func($callback, $request, $app);
             if ($ret instanceof Response) {
                 $event->setResponse($ret);
                 return;
             } elseif (null !== $ret) {
                 throw new \RuntimeException(sprintf('A before middleware for route "%s" returned an invalid response value. Must return null or an instance of Response.', $routeName));
             }
         }
     });
     $this['route_after_middlewares_trigger'] = $this->protect(function (FilterResponseEvent $event) use($app) {
         $request = $event->getRequest();
         $routeName = $request->attributes->get('_route');
         if (!($route = $app['routes']->get($routeName))) {
             return;
         }
         foreach ((array) $route->getOption('_after_middlewares') as $callback) {
             $response = call_user_func($callback, $request, $event->getResponse());
             if ($response instanceof Response) {
                 $event->setResponse($response);
             } elseif (null !== $response) {
                 throw new \RuntimeException(sprintf('An after middleware for route "%s" returned an invalid response value. Must return null or an instance of Response.', $routeName));
             }
         }
     });
     $this['request_error'] = $this->protect(function () {
         throw new \RuntimeException('Accessed request service outside of request scope. Try moving that call to a before handler or controller.');
     });
     $this['request'] = $this['request_error'];
     $this['request.http_port'] = 80;
     $this['request.https_port'] = 443;
     $this['debug'] = false;
     $this['charset'] = 'UTF-8';
     $this['locale'] = 'en';
 }
Beispiel #17
0
 public function testPort()
 {
     $requestContext = new RequestContext();
     $requestContext->setHttpPort('123');
     $requestContext->setHttpsPort('456');
     $this->assertSame(123, $requestContext->getHttpPort());
     $this->assertSame(456, $requestContext->getHttpsPort());
 }
Beispiel #18
0
 /**
  * Constructor.
  */
 public function __construct()
 {
     $app = $this;
     $this['logger'] = null;
     $this['autoloader'] = function () {
         throw new \RuntimeException('You tried to access the autoloader service. The autoloader has been removed from Silex. It is recommended that you use Composer to manage your dependencies and handle your autoloading. See http://getcomposer.org for more information.');
     };
     $this['routes'] = $this->share(function () {
         return new RouteCollection();
     });
     $this['controllers'] = $this->share(function () use($app) {
         return $app['controllers_factory'];
     });
     $this['controllers_factory'] = function () use($app) {
         return new ControllerCollection($app['route_factory']);
     };
     $this['route_class'] = 'Silex\\Route';
     $this['route_factory'] = function () use($app) {
         return new $app['route_class']();
     };
     $this['exception_handler'] = $this->share(function () use($app) {
         return new ExceptionHandler($app['debug']);
     });
     $this['dispatcher_class'] = 'Symfony\\Component\\EventDispatcher\\EventDispatcher';
     $this['dispatcher'] = $this->share(function () use($app) {
         $dispatcher = new $app['dispatcher_class']();
         $urlMatcher = new LazyUrlMatcher(function () use($app) {
             return $app['url_matcher'];
         });
         $dispatcher->addSubscriber(new RouterListener($urlMatcher, $app['request_context'], $app['logger']));
         $dispatcher->addSubscriber(new LocaleListener($app, $urlMatcher));
         if (isset($app['exception_handler'])) {
             $dispatcher->addSubscriber($app['exception_handler']);
         }
         $dispatcher->addSubscriber(new ResponseListener($app['charset']));
         $dispatcher->addSubscriber(new MiddlewareListener($app));
         $dispatcher->addSubscriber(new ConverterListener($app['routes']));
         $dispatcher->addSubscriber(new StringToResponseListener());
         return $dispatcher;
     });
     $this['resolver'] = $this->share(function () use($app) {
         return new ControllerResolver($app, $app['logger']);
     });
     $this['kernel'] = $this->share(function () use($app) {
         return new HttpKernel($app['dispatcher'], $app['resolver']);
     });
     $this['request_context'] = $this->share(function () use($app) {
         $context = new RequestContext();
         $context->setHttpPort($app['request.http_port']);
         $context->setHttpsPort($app['request.https_port']);
         return $context;
     });
     $this['url_matcher'] = $this->share(function () use($app) {
         return new RedirectableUrlMatcher($app['routes'], $app['request_context']);
     });
     $this['request_error'] = $this->protect(function () {
         throw new \RuntimeException('Accessed request service outside of request scope. Try moving that call to a before handler or controller.');
     });
     $this['request'] = $this['request_error'];
     $this['request.http_port'] = 80;
     $this['request.https_port'] = 443;
     $this['debug'] = false;
     $this['charset'] = 'UTF-8';
     $this['locale'] = 'en';
 }
 /**
  * @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']);
     }
 }
 public function testComposeSchemaHttpsAndCustomPortAndFileUrlOnResolve()
 {
     $requestContext = new RequestContext();
     $requestContext->setScheme('https');
     $requestContext->setHost('thehost');
     $requestContext->setHttpsPort(444);
     $resolver = new WebPathResolver($this->createFilesystemMock(), $requestContext, '/aWebRoot', 'aCachePrefix');
     $this->assertEquals('https://thehost:444/aCachePrefix/aFilter/aPath', $resolver->resolve('aPath', 'aFilter'));
 }
 public function __construct($container, $config, array $values = array())
 {
     $this->container = $container;
     $defaultConfig = ['config.path' => null, 'view.path' => null, 'http.cache.path' => null, 'view.cache.path' => null, 'view.compile.path' => null, 'routes.yml' => 'routes.yml', 'services.yml' => 'services.yml'];
     $realConfig = array_merge($defaultConfig, $config);
     $this->container['config'] = $realConfig;
     $this->container['callback_resolver'] = function ($c) {
         return new \Vicus\Component\CallbackResolver($c);
     };
     $this->container['controller_resolver'] = function ($c) {
         return new HttpKernel\Controller\ControllerResolver();
     };
     $this->container['resolver'] = function ($c) {
         return new \Vicus\Component\HttpKernel\Controller\ServiceControllerResolver($c['controller_resolver'], $c['callback_resolver']);
     };
     $this->container['routes'] = function ($c) {
         return new RouteCollection();
     };
     $this->container['routes'] = $this->container->extend('routes', function (RouteCollection $routes, $c) {
         $collection = $c['routing_yaml_file_loader']->load($c['config']['routes.yml']);
         $routes->addCollection($collection);
         return $routes;
     });
     $this->container['config_file_locator'] = function ($c) {
         return new FileLocator($c['config']['config.path']);
     };
     $this->container['routing_yaml_file_loader'] = function ($c) {
         $loader = new RoutingYamlFileLoader($c['config_file_locator']);
         return $loader;
     };
     $this->container['container_builder'] = function ($c) {
         return new ContainerBuilder($c);
     };
     $this->container['container_yaml_file_loader'] = function ($c) {
         return new ContainerYamlFileLoader($c['container_builder'], $c['config_file_locator']);
     };
     $this->container['services'] = function ($c) {
         return $c['container_yaml_file_loader']->load($c['config']['services.yml']);
     };
     $this->container['context'] = function ($c) {
         $context = new Routing\RequestContext();
         $context->setHttpPort($c['request.http_port']);
         $context->setHttpsPort($c['request.https_port']);
         return $context;
     };
     $this->container['matcher'] = function ($c) {
         return new Routing\Matcher\UrlMatcher($c['routes'], $c['context']);
     };
     $this->container['event_dispatcher'] = function ($c) {
         return new EventDispatcher();
     };
     $this->container['exception_handler'] = function ($c) {
         return new ExceptionHandler($c['debug']);
     };
     $this->container['event_dispatcher_add_listeners'] = function ($c) {
         $exceptionController = $c['exception_controller'] ? $c['exception_controller'] : '\\\\Vicus\\Controller\\ErrorController::exceptionAction';
         $c['event_dispatcher']->addSubscriber(new HttpKernel\EventListener\RouterListener($c['matcher']));
         $c['event_dispatcher']->addSubscriber(new \Vicus\Listener\StringResponseListener());
         $c['event_dispatcher']->addSubscriber(new \Vicus\Listener\ContentLengthListener());
         $c['event_dispatcher']->addSubscriber(new HttpKernel\EventListener\StreamedResponseListener());
         $c['event_dispatcher']->addSubscriber(new HttpKernel\EventListener\RouterListener($c['matcher']));
         $listener = new HttpKernel\EventListener\ExceptionListener($exceptionController);
         $c['event_dispatcher']->addSubscriber($listener);
         if (isset($c['exception_handler'])) {
             $c['event_dispatcher']->addSubscriber($c['exception_handler']);
         }
     };
     $this->container['kernel'] = function ($c) {
         return new \Vicus\Kernel($c['event_dispatcher'], $c['resolver']);
     };
     $this->container->extends['kernel'] = function ($c) {
         return new HttpCache($c['kernel'], new Store($c['config']['http.cache.path']));
     };
     $this->container['request_error'] = $this->container->protect(function () {
         throw new \RuntimeException('Accessed request service outside of request scope. Try moving that call to a before handler or controller.');
     });
     //request is already used in bootstrap. Any silex documentations that askes for request replace with request_state (for now)
     $this->container['request'] = $this->container['request_error'];
     $this->container['request.http_port'] = 80;
     $this->container['request.https_port'] = 443;
     $this->container['debug'] = false;
     $this->container['charset'] = 'UTF-8';
     $this->container['locale'] = 'en';
     //Build services list in container
     $this->container['services'];
     foreach ($values as $key => $value) {
         $this->container[$key] = $value;
     }
 }
Beispiel #22
0
 /**
  * Constructor.
  */
 public function __construct()
 {
     $app = $this;
     $this['autoloader'] = $this->share(function () {
         $loader = new UniversalClassLoader();
         $loader->register();
         return $loader;
     });
     $this['routes'] = $this->share(function () {
         return new RouteCollection();
     });
     $this['controllers'] = $this->share(function () use($app) {
         return new ControllerCollection();
     });
     $this['exception_handler'] = $this->share(function () {
         return new ExceptionHandler();
     });
     $this['dispatcher'] = $this->share(function () use($app) {
         $dispatcher = new EventDispatcher();
         $dispatcher->addSubscriber($app);
         $urlMatcher = new LazyUrlMatcher(function () use($app) {
             return $app['url_matcher'];
         });
         $dispatcher->addSubscriber(new RouterListener($urlMatcher));
         return $dispatcher;
     });
     $this['resolver'] = $this->share(function () use($app) {
         return new ControllerResolver($app);
     });
     $this['kernel'] = $this->share(function () use($app) {
         return new HttpKernel($app['dispatcher'], $app['resolver']);
     });
     $this['request_context'] = $this->share(function () use($app) {
         $context = new RequestContext();
         $context->setHttpPort($app['request.http_port']);
         $context->setHttpsPort($app['request.https_port']);
         return $context;
     });
     $this['url_matcher'] = $this->share(function () use($app) {
         return new RedirectableUrlMatcher($app['routes'], $app['request_context']);
     });
     $this['request.default_locale'] = 'en';
     $this['request'] = function () {
         throw new \RuntimeException('Accessed request service outside of request scope. Try moving that call to a before handler or controller.');
     };
     $this['request.http_port'] = 80;
     $this['request.https_port'] = 443;
     $this['debug'] = false;
     $this['charset'] = 'UTF-8';
 }