/**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $router = $this->getContainer()->get('router');
     $context = $router->getContext();
     if (null !== ($method = $input->getOption('method'))) {
         $context->setMethod($method);
     }
     if (null !== ($scheme = $input->getOption('scheme'))) {
         $context->setScheme($scheme);
     }
     if (null !== ($host = $input->getOption('host'))) {
         $context->setHost($host);
     }
     $matcher = new TraceableUrlMatcher($router->getRouteCollection(), $context);
     $traces = $matcher->getTraces($input->getArgument('path_info'));
     $matches = false;
     foreach ($traces as $trace) {
         if (TraceableUrlMatcher::ROUTE_ALMOST_MATCHES == $trace['level']) {
             $output->writeln(sprintf('<fg=yellow>Route "%s" almost matches but %s</>', $trace['name'], lcfirst($trace['log'])));
         } elseif (TraceableUrlMatcher::ROUTE_MATCHES == $trace['level']) {
             $output->writeln(sprintf('<fg=green>Route "%s" matches</>', $trace['name']));
             $routerDebugcommand = $this->getApplication()->find('router:debug');
             $output->writeln('');
             $routerDebugcommand->run(new ArrayInput(array('name' => $trace['name'])), $output);
             $matches = true;
         } elseif ($input->getOption('verbose')) {
             $output->writeln(sprintf('Route "%s" does not match: %s', $trace['name'], $trace['log']));
         }
     }
     if (!$matches) {
         $output->writeln('<fg=red>None of the routes match</>');
         return 1;
     }
 }
 /**
  * {@inheritdoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $io = new SymfonyStyle($input, $output);
     $router = $this->getContainer()->get('router');
     $context = $router->getContext();
     if (null !== ($method = $input->getOption('method'))) {
         $context->setMethod($method);
     }
     if (null !== ($scheme = $input->getOption('scheme'))) {
         $context->setScheme($scheme);
     }
     if (null !== ($host = $input->getOption('host'))) {
         $context->setHost($host);
     }
     $matcher = new TraceableUrlMatcher($router->getRouteCollection(), $context);
     $traces = $matcher->getTraces($input->getArgument('path_info'));
     $io->newLine();
     $matches = false;
     foreach ($traces as $trace) {
         if (TraceableUrlMatcher::ROUTE_ALMOST_MATCHES == $trace['level']) {
             $io->text(sprintf('Route <info>"%s"</> almost matches but %s', $trace['name'], lcfirst($trace['log'])));
         } elseif (TraceableUrlMatcher::ROUTE_MATCHES == $trace['level']) {
             $io->success(sprintf('Route "%s" matches', $trace['name']));
             $routerDebugCommand = $this->getApplication()->find('debug:router');
             $routerDebugCommand->run(new ArrayInput(array('name' => $trace['name'])), $output);
             $matches = true;
         } elseif ($input->getOption('verbose')) {
             $io->text(sprintf('Route "%s" does not match: %s', $trace['name'], $trace['log']));
         }
     }
     if (!$matches) {
         $io->error(sprintf('None of the routes match the path "%s"', $input->getArgument('path_info')));
         return 1;
     }
 }
 /**
  * Returns the routing traces associated to the given request.
  *
  * @param RequestDataCollector $request
  * @param string               $method
  *
  * @return array
  */
 private function getTraces(RequestDataCollector $request, $method)
 {
     $traceRequest = Request::create($request->getPathInfo(), $request->getRequestServer()->get('REQUEST_METHOD'), $request->getRequestAttributes()->all(), $request->getRequestCookies()->all(), array(), $request->getRequestServer()->all());
     $context = $this->matcher->getContext();
     $context->setMethod($method);
     $matcher = new TraceableUrlMatcher($this->routes, $context);
     return $matcher->getTracesForRequest($traceRequest);
 }
 /**
  * {@inheritdoc}
  */
 public function collect(Request $request, Response $response, \Exception $exception = null)
 {
     $this->data['path_info'] = $request->getPathInfo();
     if (!$this->router) {
         $this->data['traces'] = array();
     } else {
         $matcher = new TraceableUrlMatcher($this->router->getRouteCollection(), $this->router->getContext());
         $this->data['traces'] = $matcher->getTraces($request->getPathInfo());
     }
 }
 public function test()
 {
     $coll = new RouteCollection();
     $coll->add('foo', new Route('/foo', array(), array('_method' => 'POST')));
     $coll->add('bar', new Route('/bar/{id}', array(), array('id' => '\\d+')));
     $coll->add('bar1', new Route('/bar/{name}', array(), array('id' => '\\w+', '_method' => 'POST')));
     $coll->add('bar2', new Route('/foo', array(), array(), array(), 'baz'));
     $coll->add('bar3', new Route('/foo1', array(), array(), array(), 'baz'));
     $coll->add('bar4', new Route('/foo2', array(), array(), array(), 'baz', array(), array(), 'context.getMethod() == "GET"'));
     $context = new RequestContext();
     $context->setHost('baz');
     $matcher = new TraceableUrlMatcher($coll, $context);
     $traces = $matcher->getTraces('/babar');
     $this->assertEquals(array(0, 0, 0, 0, 0, 0), $this->getLevels($traces));
     $traces = $matcher->getTraces('/foo');
     $this->assertEquals(array(1, 0, 0, 2), $this->getLevels($traces));
     $traces = $matcher->getTraces('/bar/12');
     $this->assertEquals(array(0, 2), $this->getLevels($traces));
     $traces = $matcher->getTraces('/bar/dd');
     $this->assertEquals(array(0, 1, 1, 0, 0, 0), $this->getLevels($traces));
     $traces = $matcher->getTraces('/foo1');
     $this->assertEquals(array(0, 0, 0, 0, 2), $this->getLevels($traces));
     $context->setMethod('POST');
     $traces = $matcher->getTraces('/foo');
     $this->assertEquals(array(2), $this->getLevels($traces));
     $traces = $matcher->getTraces('/bar/dd');
     $this->assertEquals(array(0, 1, 2), $this->getLevels($traces));
     $traces = $matcher->getTraces('/foo2');
     $this->assertEquals(array(0, 0, 0, 0, 0, 1), $this->getLevels($traces));
 }
 public function testMatchRouteOnMultipleHosts()
 {
     $routes = new RouteCollection();
     $routes->add('first', new Route('/mypath/', array('_controller' => 'MainBundle:Info:first'), array(), array(), 'some.example.com'));
     $routes->add('second', new Route('/mypath/', array('_controller' => 'MainBundle:Info:second'), array(), array(), 'another.example.com'));
     $context = new RequestContext();
     $context->setHost('baz');
     $matcher = new TraceableUrlMatcher($routes, $context);
     $traces = $matcher->getTraces('/mypath/');
     $this->assertSame(array(TraceableUrlMatcher::ROUTE_ALMOST_MATCHES, TraceableUrlMatcher::ROUTE_ALMOST_MATCHES), $this->getLevels($traces));
 }
Beispiel #7
0
 /**
  * Renders the profiler panel for the given token.
  *
  * @param string $token The profiler token
  *
  * @return Response A Response instance
  */
 public function panelAction($token)
 {
     $profiler = $this->container->get('profiler');
     $profiler->disable();
     if (!$this->container->has('router')) {
         return new Response('The Router is not enabled.');
     }
     $router = $this->container->get('router');
     $profile = $profiler->loadProfile($token);
     $matcher = new TraceableUrlMatcher($router->getRouteCollection(), $router->getContext());
     $pathinfo = $profile->getCollector('request')->getPathInfo();
     return $this->container->get('templating')->renderResponse('WebProfilerBundle:Router:panel.html.twig', array('pathinfo' => $pathinfo, 'traces' => $matcher->getTraces($pathinfo)));
 }
Beispiel #8
0
 /**
  * Renders the profiler panel for the given token.
  *
  * @param string $token The profiler token
  *
  * @return Response A Response instance
  */
 public function panelAction($token)
 {
     $this->profiler->disable();
     if (null === $this->matcher || null === $this->routes) {
         return new Response('The Router is not enabled.');
     }
     $profile = $this->profiler->loadProfile($token);
     $context = $this->matcher->getContext();
     $context->setMethod($profile->getMethod());
     $matcher = new TraceableUrlMatcher($this->routes, $context);
     $request = $profile->getCollector('request');
     return new Response($this->twig->render('@WebProfiler/Router/panel.html.twig', array('request' => $request, 'router' => $profile->getCollector('router'), 'traces' => $matcher->getTraces($request->getPathInfo()))));
 }
 public function testRoutesWithConditions()
 {
     $routes = new RouteCollection();
     $routes->add('foo', new Route('/foo', array(), array(), array(), 'baz', array(), array(), "request.headers.get('User-Agent') matches '/firefox/i'"));
     $context = new RequestContext();
     $context->setHost('baz');
     $matcher = new TraceableUrlMatcher($routes, $context);
     $notMatchingRequest = Request::create('/foo', 'GET');
     $traces = $matcher->getTracesForRequest($notMatchingRequest);
     $this->assertEquals("Condition \"request.headers.get('User-Agent') matches '/firefox/i'\" does not evaluate to \"true\"", $traces[0]['log']);
     $matchingRequest = Request::create('/foo', 'GET', array(), array(), array(), array('HTTP_USER_AGENT' => 'Firefox'));
     $traces = $matcher->getTracesForRequest($matchingRequest);
     $this->assertEquals('Route matches!', $traces[0]['log']);
 }
Beispiel #10
0
 /**
  * Renders the profiler panel for the given token.
  *
  * @param string $token The profiler token
  *
  * @return Response A Response instance
  *
  * @throws NotFoundHttpException
  */
 public function panelAction($token)
 {
     if (null === $this->profiler) {
         throw new NotFoundHttpException('The profiler must be enabled.');
     }
     $this->profiler->disable();
     if (null === $this->matcher || null === $this->routes) {
         return new Response('The Router is not enabled.', 200, array('Content-Type' => 'text/html'));
     }
     $profile = $this->profiler->loadProfile($token);
     $context = $this->matcher->getContext();
     $context->setMethod($profile->getMethod());
     $matcher = new TraceableUrlMatcher($this->routes, $context);
     $request = $profile->getCollector('request');
     return Response::create($this->twig->render('@WebProfiler/Router/panel.html.twig', array('request' => $request, 'router' => $profile->getCollector('router'), 'traces' => $matcher->getTraces($request->getPathInfo()))), 200, array('Content-Type' => 'text/html'))->setCharset('UTF-8');
 }
Beispiel #11
0
 /**
  * @see Command
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $router = $this->getContainer()->get('router');
     $matcher = new TraceableUrlMatcher($router->getRouteCollection(), $router->getContext());
     $traces = $matcher->getTraces($input->getArgument('path_info'));
     $matches = false;
     foreach ($traces as $i => $trace) {
         if (TraceableUrlMatcher::ROUTE_ALMOST_MATCHES == $trace['level']) {
             $output->writeln(sprintf('<fg=yellow>Route "%s" almost matches but %s</>', $trace['name'], lcfirst($trace['log'])));
         } elseif (TraceableUrlMatcher::ROUTE_MATCHES == $trace['level']) {
             $output->writeln(sprintf('<fg=green>Route "%s" matches</>', $trace['name']));
             $matches = true;
         } elseif ($input->getOption('verbose')) {
             $output->writeln(sprintf('Route "%s" does not match: %s', $trace['name'], $trace['log']));
         }
     }
     if (!$matches) {
         $output->writeln('<fg=red>None of the routes matches</>');
     }
 }
 /**
  * Create a breadcrumbs node from path
  *
  * @param string $path
  * @param string $parent
  *
  * @return BreadcrumbsNode|bool
  */
 private function createBreadcrumbsNode($path, $parent)
 {
     // use $baseUrl for no prod environments e.g dev 'app_dev.php'
     $baseUrl = $this->getRequest()->getBaseUrl();
     $traces = $this->matcher->getTraces($path);
     foreach ($traces as $trace) {
         if (TraceableUrlMatcher::ROUTE_MATCHES == $trace['level']) {
             $label = $this->getLabel($path, $parent, $trace['name']);
             $node = new BreadcrumbsNode();
             $node->setLabel($label);
             $node->setPath($baseUrl . $path);
             return $node;
         }
     }
     return false;
 }
Beispiel #13
0
 protected function checkSSLPattern()
 {
     $trace = new TraceableUrlMatcher($this->routes, $this->context);
     $paths = $trace->getTraces($this->request->getPathInfo());
     foreach ($paths as $path) {
         if ($path['level'] == 1) {
             App::redirectUrl($this->generate($path['name'], true));
         }
     }
 }