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

Add `DIContainer` class and `app` helper, refs #1684.

Merge request studip/studip!1092
parent 068f0971
No related branches found
No related tags found
No related merge requests found
<?php
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Psr\Log\LoggerInterface;
return [
LoggerInterface::class => DI\factory(function () {
return new Logger('studip', [
new StreamHandler(
$GLOBALS['TMP_PATH'] . '/studip.log',
\Studip\ENV === 'development' ? Logger::DEBUG : Logger::ERROR
),
]);
}),
];
......@@ -99,6 +99,7 @@ if ($GLOBALS['ASSETS_URL'][0] === '/') {
require 'config.inc.php';
require 'lib/helpers.php';
require 'lib/phplib/page_open.php';
require_once 'lib/functions.php';
require_once 'lib/language.inc.php';
......
<?php
namespace Studip;
use DI\ContainerBuilder;
use Psr\Container\ContainerInterface;
class DIContainer
{
/**
* The current globally available container.
*
* @var static
*/
protected static $instance;
/**
* Get the globally available instance of the container.
*
* @return static
*/
public static function getInstance()
{
if (is_null(static::$instance)) {
$builder = static::createBuilder();
static::$instance = $builder->build();
}
return static::$instance;
}
/**
* Set the instance of the container.
*
* @param \Psr\Container\ContainerInterface|null $container
* @return \Psr\Container\ContainerInterface|static
*/
public static function setInstance(ContainerInterface $container = null)
{
return static::$instance = $container;
}
/**
* Set up the ContainerBuilder.
*/
protected static function createBuilder(): ContainerBuilder
{
$builder = new ContainerBuilder();
if (\Studip\ENV == 'production') {
$builder->enableCompilation($GLOBALS['TMP_PATH']);
}
$builder->ignorePhpDocErrors(true);
$builder->addDefinitions('lib/bootstrap-definitions.php');
return $builder;
}
}
<?php
use Psr\Container\ContainerInterface;
/**
* This function returns the Dependency Injection container used.
*
* ```
* $container = app();
* ```
*
* You may pass a class or interface name to resolve it from the container:
*
* ```
* $logger = app(LoggerInterface::class);
* ```
*
* @param string|null $entryId
*
* @return ContainerInterface|mixed either the DI container or the entry associated to the $entryId
*/
function app($entryId = null)
{
$container = \Studip\DIContainer::getInstance();
if (is_null($entryId)) {
return $container;
}
return $container->get($entryId);
}
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