From 09a31ace997116f6d2baf6f9462ba6751e19b721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Pe=CC=81rez=20Crespo?= <jaime.perez@uninett.no> Date: Mon, 15 Oct 2018 12:46:15 +0200 Subject: [PATCH] Start using dependency injection. --- lib/SimpleSAML/ModuleControllerResolver.php | 34 ++++++++++++++- lib/SimpleSAML/Router.php | 47 ++++++++++++++++++++- modules/core/lib/Controller.php | 20 ++++++--- modules/core/routes.yaml | 4 +- 4 files changed, 93 insertions(+), 12 deletions(-) diff --git a/lib/SimpleSAML/ModuleControllerResolver.php b/lib/SimpleSAML/ModuleControllerResolver.php index 97ebc9798..bd0135387 100644 --- a/lib/SimpleSAML/ModuleControllerResolver.php +++ b/lib/SimpleSAML/ModuleControllerResolver.php @@ -5,6 +5,7 @@ namespace SimpleSAML; use SimpleSAML\Error\Exception; use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException; use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface; use Symfony\Component\HttpKernel\Controller\ControllerResolver; @@ -30,6 +31,9 @@ class ModuleControllerResolver extends ControllerResolver implements ArgumentRes /** @var ArgumentMetadataFactory */ protected $argFactory; + /** @var ContainerBuilder */ + protected $container; + /** @var string */ protected $module; @@ -55,6 +59,7 @@ class ModuleControllerResolver extends ControllerResolver implements ArgumentRes ); $this->argFactory = new ArgumentMetadataFactory(); + $this->container = new ContainerBuilder(); try { $this->routes = $loader->load('routes.yaml'); @@ -92,7 +97,10 @@ class ModuleControllerResolver extends ControllerResolver implements ArgumentRes try { $matcher = new UrlMatcher($this->routes, $ctxt); $this->params = $matcher->match($ctxt->getPathInfo()); - return self::createController($this->params['_controller']); + list($class, $method) = explode('::', $this->params['_controller']); + $this->container->register($class, $class)->setAutowired(true); + $this->container->compile(); + return [$this->container->get($class), $method]; } catch (ResourceNotFoundException $e) { // no route defined matching this request } @@ -151,4 +159,28 @@ class ModuleControllerResolver extends ControllerResolver implements ArgumentRes return $args; } + + + /** + * Set the configuration to use by the controllers. + * + * @param \SimpleSAML\Configuration $config + */ + public function setConfiguration(Configuration $config) + { + $this->container->set(Configuration::class, $config); + $this->container->register(Configuration::class)->setSynthetic(true)->setAutowired(true); + } + + + /** + * Set the session to use by the controllers. + * + * @param \SimpleSAML\Session $session + */ + public function setSession(Session $session) + { + $this->container->set(Session::class, $session); + $this->container->register(Session::class)->setSynthetic(true)->setAutowired(true); + } } diff --git a/lib/SimpleSAML/Router.php b/lib/SimpleSAML/Router.php index fa9346896..fe6e56883 100644 --- a/lib/SimpleSAML/Router.php +++ b/lib/SimpleSAML/Router.php @@ -20,14 +20,25 @@ class Router protected $arguments; + /** @var \SimpleSAML\Configuration */ + protected $config; + /** @var RequestContext */ protected $context; - /** @var ModuleControllerResolver */ - protected $resolver; + /** @var EventDispatcher */ protected $dispatcher; + + /** @var Request */ protected $request; + /** @var \SimpleSAML\ModuleControllerResolver */ + protected $resolver; + + /** @var \SimpleSAML\Session */ + protected $session; + + /** @var RequestStack */ protected $stack; @@ -48,6 +59,8 @@ class Router /** * Process a given request. * + * If no specific arguments are given, the default instances will be used (configuration, session, etc). + * * @param Request|null $request The request to process. Defaults to the current one. * * @return Response A response suitable for the given request. @@ -56,6 +69,12 @@ class Router */ public function process(Request $request = null) { + if ($this->config === null) { + $this->setConfiguration(Configuration::getInstance()); + } + if ($this->session === null) { + $this->setSession(Session::getSessionFromRequest()); + } $this->request = $request; if ($request === null) { $this->request = Request::createFromGlobals(); @@ -78,4 +97,28 @@ class Router $response->prepare($this->request); $response->send(); } + + + /** + * Set the configuration to use by the controller. + * + * @param \SimpleSAML\Configuration $config + */ + public function setConfiguration(Configuration $config) + { + $this->config = $config; + $this->resolver->setConfiguration($config); + } + + + /** + * Set the session to use by the controller. + * + * @param \SimpleSAML\Session $session + */ + public function setSession(Session $session) + { + $this->session = $session; + $this->resolver->setSession($session); + } } diff --git a/modules/core/lib/Controller.php b/modules/core/lib/Controller.php index ab43c003b..5cd712cb3 100644 --- a/modules/core/lib/Controller.php +++ b/modules/core/lib/Controller.php @@ -19,6 +19,9 @@ class Controller /** @var \SimpleSAML\Configuration */ protected $config; + /** @var \SimpleSAML\Session */ + protected $session; + /** @var array */ protected $sources; @@ -28,12 +31,16 @@ class Controller * * It initializes the global configuration and auth source configuration for the controllers implemented here. * + * @param \SimpleSAML\Configuration $config The configuration to use by the controllers. + * @param \SimpleSAML\Session $session The session to use by the controllers. + * * @throws \Exception */ - public function __construct() + public function __construct(\SimpleSAML\Configuration $config, \SimpleSAML\Session $session) { - $this->config = \SimpleSAML\Configuration::getInstance(); - $this->sources = \SimpleSAML\Configuration::getOptionalConfig('authsources.php')->toArray(); + $this->config = $config; + $this->sources = $config::getOptionalConfig('authsources.php')->toArray(); + $this->session = $session; } @@ -48,7 +55,7 @@ class Controller * * @return \SimpleSAML\XHTML\Template|RedirectResponse An HTML template or a redirect response. * - * @throws Exception + * @throws \SimpleSAML\Error\Exception * @throws \SimpleSAML\Error\CriticalConfigurationError */ public function login(Request $request, $as = null) @@ -114,7 +121,7 @@ class Controller * @return \SimpleSAML\XHTML\Template|RedirectResponse An HTML template or a redirection if we are not * authenticated. * - * @throws Exception An exception in case the auth source specified is invalid. + * @throws \SimpleSAML\Error\Exception An exception in case the auth source specified is invalid. */ public function account($as) { @@ -129,7 +136,6 @@ class Controller } $attributes = $auth->getAttributes(); - $session = \SimpleSAML\Session::getSessionFromRequest(); $t = new \SimpleSAML\XHTML\Template($this->config, 'auth_status.php', 'attributes'); $t->data['header'] = '{status:header_saml20_sp}'; @@ -138,7 +144,7 @@ class Controller ? $auth->getAuthData('saml:sp:NameID') : false; $t->data['logouturl'] = \SimpleSAML\Module::getModuleURL('core/logout/'.urlencode($as)); - $t->data['remaining'] = $session->getAuthData($as, 'Expire') - time(); + $t->data['remaining'] = $this->session->getAuthData($as, 'Expire') - time(); $t->setStatusCode(200); return $t; diff --git a/modules/core/routes.yaml b/modules/core/routes.yaml index 2eea68a23..95da497c4 100644 --- a/modules/core/routes.yaml +++ b/modules/core/routes.yaml @@ -1,7 +1,7 @@ core-login: path: /login/{as} - defaults: { _controller: '\SimpleSAML\Module\core\Controller::login', as: null } + defaults: { _controller: 'SimpleSAML\Module\core\Controller::login', as: null } core-account: path: /account/{as} - defaults: { _controller: '\SimpleSAML\Module\core\Controller::account' } + defaults: { _controller: 'SimpleSAML\Module\core\Controller::account' } -- GitLab