Skip to content

Instantly share code, notes, and snippets.

@hinzundcode
Created July 28, 2012 20:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hinzundcode/3194587 to your computer and use it in GitHub Desktop.
Save hinzundcode/3194587 to your computer and use it in GitHub Desktop.
Handle ControllerCollection without HttpKernel and Events
<?php
error_reporting(E_ALL + E_STRICT);
require_once __DIR__.'/../vendor/autoload.php';
use Silex\Application;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Silex\ControllerCollection;
$app = new Application();
$app['debug'] = true;
class SimpleControllerCollectionHandler {
protected $resolver;
protected $request;
protected $request_context;
public function __construct(ControllerResolverInterface $resolver, Request $request, RequestContext $request_context) {
$this->resolver = $resolver;
$this->request = $request;
$this->request_context = $request_context;
}
public function handle(ControllerCollection $controllers, $path) {
$request = $this->request->duplicate();
$routes = new RouteCollection();
$routes->addCollection($controllers->flush(''), '');
$matcher = new UrlMatcher($routes, $this->request_context);
try {
$parameters = $matcher->match($path);
} catch (ResourceNotFoundException $e) {
throw new NotFoundHttpException('route not found', $e);
} catch (MethodNotAllowedException $e) {
throw new MethodNotAllowedHttpException($e->getAllowedMethods(), 'method not allowed', $e);
}
// attach parameters to request
$request->attributes->add($parameters);
unset($parameters['_route']);
unset($parameters['_controllers']);
$request->attributes->set('_route_params', $parameters);
if (false == $controller = $this->resolver->getController($request)) {
throw new NotFoundHttpException('controller not found');
}
// convert attributes
$route = $routes->get($request->attributes->get('_route'));
if ($route && $converters = $route->getOption('_converters')) {
foreach ($converters as $name => $callback) {
$request->attributes->set($name, $callback($request->attributes->get($name, null), $request));
}
}
$arguments = $this->resolver->getArguments($request, $controller);
return call_user_func_array($controller, $arguments);
}
}
$app['controllers_handler'] = $app->share(function () use ($app) {
return new SimpleControllerCollectionHandler($app['resolver'], $app['request'], $app['request_context']);
});
$app->get('/{page_name}/{slug}', function (Application $app, $page_name, $slug) {
$page = $app['pages']->get($page_name);
$controllers = $page->getControllers($app['controllers_factory']);
// for example /, /edit, /comments/page-{pageNo}, ...
return $app['controllers_handler']->handle($controllers, $slug);
})->assert('slug', '^.*')->convert('slug', function ($slug) {
return '/'.$slug;
});
$app->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment