Inject a Router to the Application via constructor

This commit is contained in:
Dmitriy Simushev 2014-07-02 10:44:16 +00:00
parent 5337d899d4
commit 1a599a6af3
3 changed files with 34 additions and 17 deletions

View File

@ -18,10 +18,16 @@
// Initialize libraries // Initialize libraries
require_once(dirname(__FILE__) . '/libs/init.php'); require_once(dirname(__FILE__) . '/libs/init.php');
use Symfony\Component\HttpFoundation\Request;
use Mibew\Application; use Mibew\Application;
use Mibew\Routing\RouteCollectionLoader;
use Mibew\Routing\Router;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Config\FileLocator;
$application = new Application(); $file_locator = new FileLocator(array(MIBEW_FS_ROOT));
$router = new Router(new RouteCollectionLoader($file_locator));
$application = new Application($router);
// Process request // Process request
$request = Request::createFromGlobals(); $request = Request::createFromGlobals();

View File

@ -27,9 +27,8 @@ use Mibew\Http\Exception\HttpException;
use Mibew\Http\Exception\MethodNotAllowedException as MethodNotAllowedHttpException; use Mibew\Http\Exception\MethodNotAllowedException as MethodNotAllowedHttpException;
use Mibew\Http\Exception\NotFoundException as NotFoundHttpException; use Mibew\Http\Exception\NotFoundException as NotFoundHttpException;
use Mibew\Routing\Router; use Mibew\Routing\Router;
use Mibew\Routing\RouteCollectionLoader; use Mibew\Routing\RouterAwareInterface;
use Mibew\Routing\Exception\AccessDeniedException as AccessDeniedRoutingException; use Mibew\Routing\Exception\AccessDeniedException as AccessDeniedRoutingException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
@ -40,18 +39,13 @@ use Symfony\Component\Routing\Exception\ResourceNotFoundException as ResourceNot
/** /**
* Incapsulates whole application * Incapsulates whole application
*/ */
class Application class Application implements RouterAwareInterface
{ {
/** /**
* @var Router|null * @var Router|null
*/ */
protected $router = null; protected $router = null;
/**
* @var FileLocator|null
*/
protected $fileLocator = null;
/** /**
* @var ControllerResolver|null * @var ControllerResolver|null
*/ */
@ -69,11 +63,12 @@ class Application
/** /**
* Class constructor. * Class constructor.
*
* @param Router $router Appropriate router instance.
*/ */
public function __construct() public function __construct(Router $router)
{ {
$this->fileLocator = new FileLocator(array(MIBEW_FS_ROOT)); $this->router = $router;
$this->router = new Router(new RouteCollectionLoader($this->fileLocator));
$this->authenticationManager = new AuthenticationManager(); $this->authenticationManager = new AuthenticationManager();
$this->controllerResolver = new ControllerResolver( $this->controllerResolver = new ControllerResolver(
$this->router, $this->router,
@ -93,7 +88,7 @@ class Application
// Actualize request context in the internal router instance // Actualize request context in the internal router instance
$context = new RequestContext(); $context = new RequestContext();
$context->fromRequest($request); $context->fromRequest($request);
$this->router->setContext($context); $this->getRouter()->setContext($context);
// Actualize cookie factory in the authentication manager. // Actualize cookie factory in the authentication manager.
$cookie_factory = CookieFactory::fromRequest($request); $cookie_factory = CookieFactory::fromRequest($request);
@ -104,7 +99,7 @@ class Application
// Try to match a route, check if the client can access it and add // Try to match a route, check if the client can access it and add
// extra data to the request. // extra data to the request.
try { try {
$parameters = $this->router->matchRequest($request); $parameters = $this->getRouter()->matchRequest($request);
$request->attributes->add($parameters); $request->attributes->add($parameters);
// Check if the user can access the page // Check if the user can access the page
@ -152,6 +147,22 @@ class Application
return $response; return $response;
} }
/**
* {@inheritdoc}
*/
public function setRouter(Router $router)
{
$this->router = $router;
}
/**
* {@inheritdoc}
*/
public function getRouter()
{
return $this->router;
}
/** /**
* Builds response for pages with denied access * Builds response for pages with denied access
* *
@ -188,7 +199,7 @@ class Application
// Operator is not logged in. Redirect him to the login page. // Operator is not logged in. Redirect him to the login page.
$_SESSION['backpath'] = $request->getUri(); $_SESSION['backpath'] = $request->getUri();
$response = new RedirectResponse($this->router->generate('login')); $response = new RedirectResponse($this->getRouter()->generate('login'));
return $response; return $response;
} }

View File

@ -56,7 +56,7 @@ class Router implements RouterInterface, RequestMatcherInterface
/** /**
* Class constructor. * Class constructor.
* *
* @ param RouteLoader $loader An instance of route loader. * @param RouteLoader $loader An instance of route loader.
* @param RequestContext $context The context of the request. * @param RequestContext $context The context of the request.
*/ */
public function __construct(RouteCollectionLoader $loader, RequestContext $context = null) public function __construct(RouteCollectionLoader $loader, RequestContext $context = null)