Skip to content
Snippets Groups Projects
Commit d08d1b67 authored by Marcus Eibrink-Lunzenauer's avatar Marcus Eibrink-Lunzenauer
Browse files

Use dependency injection in trails, closes #1714.

Closes #1714

Merge request studip/studip!1118
parent e814af9c
No related branches found
No related tags found
No related merge requests found
......@@ -59,11 +59,10 @@ class HelpTours extends Command
$plugin = new $plugin_info['class']();
if ($result[1]) {
$dispatcher = new \Trails_Dispatcher(
$GLOBALS['ABSOLUTE_PATH_STUDIP'] . $plugin->getPluginPath(),
rtrim(\PluginEngine::getLink($plugin, [], null, true), '/'),
'index'
);
$dispatcher = app(\Trails_Dispatcher::class);
$dispatcher->trails_root = $GLOBALS['ABSOLUTE_PATH_STUDIP'] . $plugin->getPluginPath();
$dispatcher->trails_uri = rtrim(\PluginEngine::getLink($plugin, [], null, true), '/');
$dispatcher->default_controller = 'index';
$dispatcher->current_plugin = $plugin;
$parsed = $dispatcher->parse($result[1]);
$controller = $dispatcher->load_controller($parsed[0]);
......@@ -72,7 +71,7 @@ class HelpTours extends Command
}
}
} elseif (match_route('dispatch.php/*', $step->route)) {
$dispatcher = new \StudipDispatcher();
$dispatcher = app(\Trails_Dispatcher::class);
$parsed = $dispatcher->parse(substr($step->route, strlen('dispatch.php') + 1));
$controller = $dispatcher->load_controller($parsed[0]);
if ($parsed[1] && !$controller->has_action($parsed[1])) {
......
......@@ -2,6 +2,7 @@
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
return [
......@@ -19,4 +20,7 @@ return [
StudipPDO::class => DI\factory(function () {
return DBManager::get();
}),
Trails_Dispatcher::class => DI\factory(function (ContainerInterface $container) {
return new \StudipDispatcher($container);
}),
];
<?php
use Psr\Container\ContainerInterface;
/**
* StudipDispatcher.php - create the default Trails dispatcher
*
......@@ -27,19 +29,26 @@
*/
class StudipDispatcher extends Trails_Dispatcher {
/**
* This variable contains the DI-Container.
* @var ContainerInterface
*/
protected $container;
/**
* Create a new Trails_Dispatcher with Stud.IP specific parameters
* for: trails_root is "$STUDIP_BASE_PATH/app", trails_uri is
* "dispatch.php" and default_controller is "default" (which does
* not map to anything).
*/
public function __construct()
public function __construct(ContainerInterface $container)
{
global $STUDIP_BASE_PATH, $ABSOLUTE_URI_STUDIP;
$trails_root = $STUDIP_BASE_PATH . DIRECTORY_SEPARATOR . 'app';
$trails_uri = rtrim($ABSOLUTE_URI_STUDIP, '/') . '/dispatch.php';
$default_controller = 'default';
$this->container = $container;
parent::__construct($trails_root, $trails_uri, $default_controller);
}
......@@ -55,4 +64,23 @@ class StudipDispatcher extends Trails_Dispatcher {
{
throw $exception;
}
/**
* Loads the controller file for a given controller path and return an
* instance of that controller. If an error occures, an exception will be
* thrown.
*
* @param string the relative controller path
*
* @return TrailsController an instance of that controller
*/
function load_controller($controller) {
require_once "{$this->trails_root}/controllers/{$controller}.php";
$class = Trails_Inflector::camelize($controller) . 'Controller';
if (!class_exists($class)) {
throw new Trails_UnknownController("Controller missing: '$class'");
}
return $this->container->make($class, ['dispatcher' => $this]);
}
}
......@@ -1425,7 +1425,7 @@ function get_route($route = '')
$route = 'plugins.php/' . $pieces[0] . (!empty($pieces[1]) ? '/' . $pieces[1] : '') . (!empty($pieces[2]) ? '/' . $pieces[2] : '');
} elseif (mb_strpos($route, 'dispatch.php/') !== false) {
$trails = explode('dispatch.php/', $route);
$dispatcher = new StudipDispatcher();
$dispatcher = app(\Trails_Dispatcher::class);
$pieces = explode('/', $trails[1]);
$trail = '';
foreach ($pieces as $index => $piece) {
......
......@@ -9,22 +9,25 @@ use Psr\Container\ContainerInterface;
* $container = app();
* ```
*
* You may pass a class or interface name to resolve it from the container:
* You may pass an entry name, a class or interface name to resolve it from the container:
*
* ```
* $logger = app(LoggerInterface::class);
* ```
*
* @param string|null $entryId
* @param string|null $entryId entry name or a class name
* @param array $parameters Optional parameters to use to build the entry.
* Use this to force specific parameters to specific values.
* Parameters not defined in this array will be resolved using the container.
*
* @return ContainerInterface|mixed either the DI container or the entry associated to the $entryId
*/
function app($entryId = null)
function app($entryId = null, $parameters = [])
{
$container = \Studip\DIContainer::getInstance();
if (is_null($entryId)) {
return $container;
}
return $container->get($entryId);
return $container->make($entryId, $parameters);
}
......@@ -43,7 +43,7 @@ class EvaluationsWidget extends CorePlugin implements PortalPlugin
}
// include and show votes and tests
$controller = new AuthenticatedController(new StudipDispatcher());
$controller = app(AuthenticatedController::class, ['dispatcher' => app(\Trails_Dispatcher::class)]);
$controller->suppress_empty_output = true;
$response = $controller->relay('evaluation/display/studip')->body;
......
......@@ -9,8 +9,6 @@
* the License, or (at your option) any later version.
*/
require_once 'app/controllers/news.php';
class NewsWidget extends CorePlugin implements PortalPlugin
{
public function getPluginName()
......@@ -27,8 +25,7 @@ class NewsWidget extends CorePlugin implements PortalPlugin
function getPortalTemplate()
{
$dispatcher = new StudipDispatcher();
$controller = new NewsController($dispatcher);
$controller = app(\Trails_Dispatcher::class)->load_controller('news');
$response = $controller->relayWithRedirect('news/display/studip');
$template = $GLOBALS['template_factory']->open('shared/string');
$template->content = $response->body;
......
......@@ -10,8 +10,6 @@
* the License, or (at your option) any later version.
*/
require_once 'app/controllers/calendar/contentbox.php';
class TerminWidget extends CorePlugin implements PortalPlugin
{
public function getPluginName()
......@@ -28,8 +26,7 @@ class TerminWidget extends CorePlugin implements PortalPlugin
public function getPortalTemplate()
{
$dispatcher = new StudipDispatcher();
$controller = new Calendar_ContentboxController($dispatcher);
$controller = app(\Trails_Dispatcher::class)->load_controller('calendar/contentbox');
$response = $controller->relay('calendar/contentbox/display/'.$GLOBALS['user']->id);
$template = $GLOBALS['template_factory']->open('shared/string');
$template->content = $response->body;
......
......@@ -157,10 +157,10 @@ abstract class StudIPPlugin
$action = $args[0] !== '' ? array_shift($args).'_action' : 'show_action';
if (!method_exists($this, $action)) {
$trails_root = $this->getPluginPath();
$trails_uri = rtrim(PluginEngine::getLink($this, [], null, true), '/');
$dispatcher = new Trails_Dispatcher($trails_root, $trails_uri, 'index');
$dispatcher = app(\Trails_Dispatcher::class);
$dispatcher->trails_root = $this->getPluginPath();
$dispatcher->trails_uri = rtrim(PluginEngine::getLink($this, [], null, true), '/');
$dispatcher->default_controller = 'index';
$dispatcher->current_plugin = $this;
try {
$dispatcher->dispatch($unconsumed_path);
......
......@@ -23,5 +23,5 @@ URLHelper::setBaseUrl($GLOBALS['ABSOLUTE_URI_STUDIP']);
$request_uri = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '/';
$dispatcher = new StudipDispatcher();
$dispatcher = app(\Trails_Dispatcher::class);
$dispatcher->dispatch($request_uri);
......@@ -36,5 +36,6 @@ $GLOBALS['template_factory'] = new Flexi_TemplateFactory('../templates/');
# get plugin class from request
$dispatch_to = $_SERVER['PATH_INFO'] ?: '';
$dispatcher = new Trails_Dispatcher( '../app', $_SERVER['SCRIPT_NAME'], 'web_migrate');
$dispatcher = app(\Trails_Dispatcher::class);
$dispatcher->trails_uri = $_SERVER['SCRIPT_NAME'];
$dispatcher->dispatch("web_migrate/{$dispatch_to}");
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment