private static function createMatcher($sourceFolder, $reader, $context) { $matcher = null; $routeCacheFile = self::$CACHE_FOLDER . "/" . self::ROUTE_CACHE_FILE; if (!self::$DEBUG) { if (!file_exists($routeCacheFile)) { $routeLoader = new RouteAnnotationClassLoader($reader); $loader = new AnnotationDirectoryLoader(new FileLocator([$sourceFolder]), $routeLoader); $routes = $loader->load($sourceFolder); $dumper = new PhpMatcherDumper($routes); file_put_contents($routeCacheFile, $dumper->dump(["class" => self::ROUTE_CACHE_CLASS])); $matcher = new UrlMatcher($routes, $context); } else { $routes = (include self::$CACHE_FOLDER . "/" . self::ROUTE_CACHE_FILE); $className = self::ROUTE_CACHE_CLASS; $matcher = new $className($context); } } else { $routeLoader = new RouteAnnotationClassLoader($reader); $loader = new AnnotationDirectoryLoader(new FileLocator([$sourceFolder]), $routeLoader); $routes = $loader->load($sourceFolder); $matcher = new UrlMatcher($routes, $context); } return $matcher; }
/** * Loads from annotations from an array of directories. * * @param array $resource An array of directories prefixed with annotations: * * @return RouteCollection A RouteCollection instance * * @throws \InvalidArgumentException When route can't be parsed */ public function load($resource) { $collection = new RouteCollection(); foreach ($this->getAbsolutePaths(substr($resource, 12)) as $resource) { $collection->addCollection(parent::load('annotations:' . $resource)); } return $collection; }
/** * Loads from annotations from a directory glob pattern. * * @param string $glob A directory glob pattern containing "*" * @param string $type The resource type * * @return RouteCollection A RouteCollection instance * * @throws \InvalidArgumentException When route can't be parsed */ public function load($glob, $type = null) { $collection = new RouteCollection(); foreach ($this->getAbsolutePaths($glob) as $path) { $collection->addCollection(parent::load($path, $type)); } return $collection; }
public function testCollectRoutes() { $loader = new AnnotationDirectoryLoader(new FileLocator([__DIR__ . "/Stub"]), $this->annotClassLoader); $collections = $loader->load(__DIR__ . '/Stub'); $this->assertCount(1, $collections); $collections = $collections->all(); $route = array_pop($collections); $this->assertEquals("Corley\\Middleware\\Stub\\Sut", $route->getDefaults()["controller"]); }
public function setUp() { $loader = (include __DIR__ . '/../vendor/autoload.php'); AnnotationRegistry::registerLoader(array($loader, 'loadClass')); $container = new CompositeContainer(); $builder = new ContainerBuilder(); $builder->useAnnotations(true); $builder->wrapContainer($container); $diContainer = $builder->build(); $container->addContainer($diContainer); $reader = new AnnotationReader(); $routeLoader = new RouteAnnotationClassLoader($reader); $loader = new AnnotationDirectoryLoader(new FileLocator([__DIR__ . '/app']), $routeLoader); $routes = $loader->load(__DIR__ . '/app'); $context = new RequestContext(); $matcher = new UrlMatcher($routes, $context); $hookReader = new HookReader($reader); $executor = new AnnotExecutor($container, $hookReader); $this->app = new App($matcher, $executor); }
use Doctrine\Common\Annotations\AnnotationRegistry; use Doctrine\Common\Annotations\AnnotationReader; use Silex\Application; use Silex\Routing\Loader\AnnotatedRouteControllerLoader; use Symfony\Component\Config\FileLocator; use Symfony\Component\Finder\Finder; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader; /** @var $app Silex\Application */ $app['routes'] = $app->extend('routes', function (RouteCollection $routes) use($app) { AnnotationRegistry::registerAutoloadNamespace('Silex\\Routing\\Configuration', ROOT_DIR . '/src'); $loader = new AnnotationDirectoryLoader(new FileLocator(), new AnnotatedRouteControllerLoader(new AnnotationReader())); $finder = new Finder(); foreach ($finder->in(ROOT_DIR . '/src')->depth(0) as $namespace) { /** @var SplFileInfo $namespace */ if (is_dir($controllerDir = $namespace->getPathname() . '/Controller')) { $routes->addCollection($loader->load($controllerDir)); } } return $routes; }); $app->error(function (\Exception $e, $code) use($app) { if ($app['debug']) { return; } // 404.html, or 40x.html, or 4xx.html, or error.html $templates = array('errors/' . $code . '.html.twig', 'errors/default.html.twig'); return new Response($app['twig']->resolveTemplate($templates)->render(array('code' => $code)), $code); });