Skip to content
Snippets Groups Projects
Unverified Commit 09a31ace authored by Jaime Pérez Crespo's avatar Jaime Pérez Crespo
Browse files

Start using dependency injection.

parent b2748b36
No related branches found
No related tags found
No related merge requests found
...@@ -5,6 +5,7 @@ namespace SimpleSAML; ...@@ -5,6 +5,7 @@ namespace SimpleSAML;
use SimpleSAML\Error\Exception; use SimpleSAML\Error\Exception;
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException; use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface; use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Symfony\Component\HttpKernel\Controller\ControllerResolver; use Symfony\Component\HttpKernel\Controller\ControllerResolver;
...@@ -30,6 +31,9 @@ class ModuleControllerResolver extends ControllerResolver implements ArgumentRes ...@@ -30,6 +31,9 @@ class ModuleControllerResolver extends ControllerResolver implements ArgumentRes
/** @var ArgumentMetadataFactory */ /** @var ArgumentMetadataFactory */
protected $argFactory; protected $argFactory;
/** @var ContainerBuilder */
protected $container;
/** @var string */ /** @var string */
protected $module; protected $module;
...@@ -55,6 +59,7 @@ class ModuleControllerResolver extends ControllerResolver implements ArgumentRes ...@@ -55,6 +59,7 @@ class ModuleControllerResolver extends ControllerResolver implements ArgumentRes
); );
$this->argFactory = new ArgumentMetadataFactory(); $this->argFactory = new ArgumentMetadataFactory();
$this->container = new ContainerBuilder();
try { try {
$this->routes = $loader->load('routes.yaml'); $this->routes = $loader->load('routes.yaml');
...@@ -92,7 +97,10 @@ class ModuleControllerResolver extends ControllerResolver implements ArgumentRes ...@@ -92,7 +97,10 @@ class ModuleControllerResolver extends ControllerResolver implements ArgumentRes
try { try {
$matcher = new UrlMatcher($this->routes, $ctxt); $matcher = new UrlMatcher($this->routes, $ctxt);
$this->params = $matcher->match($ctxt->getPathInfo()); $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) { } catch (ResourceNotFoundException $e) {
// no route defined matching this request // no route defined matching this request
} }
...@@ -151,4 +159,28 @@ class ModuleControllerResolver extends ControllerResolver implements ArgumentRes ...@@ -151,4 +159,28 @@ class ModuleControllerResolver extends ControllerResolver implements ArgumentRes
return $args; 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);
}
} }
...@@ -20,14 +20,25 @@ class Router ...@@ -20,14 +20,25 @@ class Router
protected $arguments; protected $arguments;
/** @var \SimpleSAML\Configuration */
protected $config;
/** @var RequestContext */ /** @var RequestContext */
protected $context; protected $context;
/** @var ModuleControllerResolver */ /** @var EventDispatcher */
protected $resolver;
protected $dispatcher; protected $dispatcher;
/** @var Request */
protected $request; protected $request;
/** @var \SimpleSAML\ModuleControllerResolver */
protected $resolver;
/** @var \SimpleSAML\Session */
protected $session;
/** @var RequestStack */
protected $stack; protected $stack;
...@@ -48,6 +59,8 @@ class Router ...@@ -48,6 +59,8 @@ class Router
/** /**
* Process a given request. * 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. * @param Request|null $request The request to process. Defaults to the current one.
* *
* @return Response A response suitable for the given request. * @return Response A response suitable for the given request.
...@@ -56,6 +69,12 @@ class Router ...@@ -56,6 +69,12 @@ class Router
*/ */
public function process(Request $request = null) 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; $this->request = $request;
if ($request === null) { if ($request === null) {
$this->request = Request::createFromGlobals(); $this->request = Request::createFromGlobals();
...@@ -78,4 +97,28 @@ class Router ...@@ -78,4 +97,28 @@ class Router
$response->prepare($this->request); $response->prepare($this->request);
$response->send(); $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);
}
} }
...@@ -19,6 +19,9 @@ class Controller ...@@ -19,6 +19,9 @@ class Controller
/** @var \SimpleSAML\Configuration */ /** @var \SimpleSAML\Configuration */
protected $config; protected $config;
/** @var \SimpleSAML\Session */
protected $session;
/** @var array */ /** @var array */
protected $sources; protected $sources;
...@@ -28,12 +31,16 @@ class Controller ...@@ -28,12 +31,16 @@ class Controller
* *
* It initializes the global configuration and auth source configuration for the controllers implemented here. * 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 * @throws \Exception
*/ */
public function __construct() public function __construct(\SimpleSAML\Configuration $config, \SimpleSAML\Session $session)
{ {
$this->config = \SimpleSAML\Configuration::getInstance(); $this->config = $config;
$this->sources = \SimpleSAML\Configuration::getOptionalConfig('authsources.php')->toArray(); $this->sources = $config::getOptionalConfig('authsources.php')->toArray();
$this->session = $session;
} }
...@@ -48,7 +55,7 @@ class Controller ...@@ -48,7 +55,7 @@ class Controller
* *
* @return \SimpleSAML\XHTML\Template|RedirectResponse An HTML template or a redirect response. * @return \SimpleSAML\XHTML\Template|RedirectResponse An HTML template or a redirect response.
* *
* @throws Exception * @throws \SimpleSAML\Error\Exception
* @throws \SimpleSAML\Error\CriticalConfigurationError * @throws \SimpleSAML\Error\CriticalConfigurationError
*/ */
public function login(Request $request, $as = null) public function login(Request $request, $as = null)
...@@ -114,7 +121,7 @@ class Controller ...@@ -114,7 +121,7 @@ class Controller
* @return \SimpleSAML\XHTML\Template|RedirectResponse An HTML template or a redirection if we are not * @return \SimpleSAML\XHTML\Template|RedirectResponse An HTML template or a redirection if we are not
* authenticated. * 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) public function account($as)
{ {
...@@ -129,7 +136,6 @@ class Controller ...@@ -129,7 +136,6 @@ class Controller
} }
$attributes = $auth->getAttributes(); $attributes = $auth->getAttributes();
$session = \SimpleSAML\Session::getSessionFromRequest();
$t = new \SimpleSAML\XHTML\Template($this->config, 'auth_status.php', 'attributes'); $t = new \SimpleSAML\XHTML\Template($this->config, 'auth_status.php', 'attributes');
$t->data['header'] = '{status:header_saml20_sp}'; $t->data['header'] = '{status:header_saml20_sp}';
...@@ -138,7 +144,7 @@ class Controller ...@@ -138,7 +144,7 @@ class Controller
? $auth->getAuthData('saml:sp:NameID') ? $auth->getAuthData('saml:sp:NameID')
: false; : false;
$t->data['logouturl'] = \SimpleSAML\Module::getModuleURL('core/logout/'.urlencode($as)); $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); $t->setStatusCode(200);
return $t; return $t;
......
core-login: core-login:
path: /login/{as} path: /login/{as}
defaults: { _controller: '\SimpleSAML\Module\core\Controller::login', as: null } defaults: { _controller: 'SimpleSAML\Module\core\Controller::login', as: null }
core-account: core-account:
path: /account/{as} path: /account/{as}
defaults: { _controller: '\SimpleSAML\Module\core\Controller::account' } defaults: { _controller: 'SimpleSAML\Module\core\Controller::account' }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment