aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/Commands/Checks/HelpTours.php11
-rw-r--r--lib/bootstrap-definitions.php4
-rw-r--r--lib/classes/StudipDispatcher.php30
-rw-r--r--lib/functions.php2
-rw-r--r--lib/helpers.php11
-rw-r--r--lib/modules/EvaluationsWidget.php2
-rw-r--r--lib/modules/NewsWidget.php5
-rw-r--r--lib/modules/TerminWidget.php5
-rw-r--r--lib/plugins/core/StudIPPlugin.class.php8
-rw-r--r--public/dispatch.php2
-rw-r--r--public/web_migrate.php3
11 files changed, 56 insertions, 27 deletions
diff --git a/cli/Commands/Checks/HelpTours.php b/cli/Commands/Checks/HelpTours.php
index 04d66c0..b4b2fa7 100644
--- a/cli/Commands/Checks/HelpTours.php
+++ b/cli/Commands/Checks/HelpTours.php
@@ -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])) {
diff --git a/lib/bootstrap-definitions.php b/lib/bootstrap-definitions.php
index 1b0fbc2..17f35d1 100644
--- a/lib/bootstrap-definitions.php
+++ b/lib/bootstrap-definitions.php
@@ -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);
+ }),
];
diff --git a/lib/classes/StudipDispatcher.php b/lib/classes/StudipDispatcher.php
index b93f9ae..af0ea48 100644
--- a/lib/classes/StudipDispatcher.php
+++ b/lib/classes/StudipDispatcher.php
@@ -1,5 +1,7 @@
<?php
+use Psr\Container\ContainerInterface;
+
/**
* StudipDispatcher.php - create the default Trails dispatcher
*
@@ -28,18 +30,25 @@
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]);
+ }
}
diff --git a/lib/functions.php b/lib/functions.php
index b084200..b47bcec 100644
--- a/lib/functions.php
+++ b/lib/functions.php
@@ -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) {
diff --git a/lib/helpers.php b/lib/helpers.php
index d851d73..6c7accd 100644
--- a/lib/helpers.php
+++ b/lib/helpers.php
@@ -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);
}
diff --git a/lib/modules/EvaluationsWidget.php b/lib/modules/EvaluationsWidget.php
index bfe7cd8..3c5cf6f 100644
--- a/lib/modules/EvaluationsWidget.php
+++ b/lib/modules/EvaluationsWidget.php
@@ -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;
diff --git a/lib/modules/NewsWidget.php b/lib/modules/NewsWidget.php
index f158631..73f4c3b 100644
--- a/lib/modules/NewsWidget.php
+++ b/lib/modules/NewsWidget.php
@@ -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;
diff --git a/lib/modules/TerminWidget.php b/lib/modules/TerminWidget.php
index c92f1d2..c9cf854 100644
--- a/lib/modules/TerminWidget.php
+++ b/lib/modules/TerminWidget.php
@@ -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;
diff --git a/lib/plugins/core/StudIPPlugin.class.php b/lib/plugins/core/StudIPPlugin.class.php
index 2fa9703..fafd583 100644
--- a/lib/plugins/core/StudIPPlugin.class.php
+++ b/lib/plugins/core/StudIPPlugin.class.php
@@ -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);
diff --git a/public/dispatch.php b/public/dispatch.php
index 3abad64..b6f6847 100644
--- a/public/dispatch.php
+++ b/public/dispatch.php
@@ -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);
diff --git a/public/web_migrate.php b/public/web_migrate.php
index 572c778..216a1fd 100644
--- a/public/web_migrate.php
+++ b/public/web_migrate.php
@@ -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}");