/** * @param string $route * * @return string|null */ protected static function getAssetContentsByRoute($route) { $result = null; try { $routes = \Route::getList(); /* @var $routes \Symfony\Component\Routing\RouteCollection */ $context = new \Symfony\Component\Routing\RequestContext(); $request = \Request::getInstance(); $context->fromRequest($request); $matcher = new \Symfony\Component\Routing\Matcher\UrlMatcher($routes, $context); $matched = null; try { $matched = $matcher->match($route); } catch (\Exception $x) { $m = null; // Route matcher requires that paths ends with a slash if (preg_match('/^(.*[^\\/])($|\\?.*)$/', $route, $m)) { try { $matched = $matcher->match($m[1] . '/' . (isset($m[2]) ? $m[2] : '')); } catch (\Exception $x) { } } } if (isset($matched)) { $controller = $matched['_controller']; if (is_callable($controller)) { ob_start(); $r = call_user_func($controller, false); if ($r !== false) { $result = ob_get_contents(); } ob_end_clean(); } } } catch (\Exception $x) { } return $result; }
/** * Sets up Symfony 2 tests */ function setupSymfony2(Benchmark $benchmark, $routes, $args) { $argString = implode('/', array_map(function ($i) { return "{arg{$i}}"; }, range(1, $args))); $str = $firstStr = $lastStr = ''; $sfRoutes = new \Symfony\Component\Routing\RouteCollection(); $router = new \Symfony\Component\Routing\Matcher\UrlMatcher($sfRoutes, new \Symfony\Component\Routing\RequestContext()); for ($i = 0, $str = 'a'; $i < $routes; $i++, $str++) { list($pre, $post) = getRandomParts(); $str = '/' . $pre . '/' . $argString . '/' . $post; if (0 === $i) { $firstStr = str_replace(array('{', '}'), '', $str); } $lastStr = str_replace(array('{', '}'), '', $str); $sfRoutes->add($str, new \Symfony\Component\Routing\Route($str, array('controller' => 'handler' . $i))); } $benchmark->register(sprintf('Symfony2 - last route (%s routes)', $routes), function () use($router, $lastStr) { $route = $router->match($lastStr); }); $benchmark->register(sprintf('Symfony2 - unknown route (%s routes)', $routes), function () use($router) { try { $route = $router->match('/not-even-real'); } catch (\Symfony\Component\Routing\Exception\ResourceNotFoundException $e) { } }); }
/** * Sets up Symfony 2 tests */ function setupSymfony2(Benchmark $benchmark, $routes, $args) { $argString = implode('/', array_map(function ($i) { return "{arg{$i}}"; }, range(1, $args))); $str = $firstStr = $lastStr = ''; $sfRoutes = new \Symfony\Component\Routing\RouteCollection(); $router = new \Symfony\Component\Routing\Matcher\UrlMatcher($sfRoutes, new \Symfony\Component\Routing\RequestContext()); for ($i = 0, $str = 'a'; $i < $routes; $i++, $str++) { list($pre, $post) = getRandomParts(); $str = '/' . $pre . '/' . $argString . '/' . $post; if (0 === $i) { $firstStr = str_replace(array('{', '}'), '', $str); } $lastStr = str_replace(array('{', '}'), '', $str); $sfRoutes->add($str, new \Symfony\Component\Routing\Route($str, array('controller' => 'handler' . $i))); } $benchmark->register('Symfony2 - first route', function () use($router, $firstStr) { $route = $router->match($firstStr); }); }