aboutsummaryrefslogtreecommitdiff
path: root/lib/helpers.php
blob: a44f10b9b97ebb2d5f5b4a69a83e7562d4b0146c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

use Psr\Container\ContainerInterface;

/**
 * This function returns the Dependency Injection container used.
 *
 * ```
 * $container = app();
 * ```
 *
 * You may pass an entry name, a class or interface name to resolve it from the container:
 *
 * ```
 * $logger = app(LoggerInterface::class);
 * ```
 * @template T
 * @param class-string<T>|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 T|ContainerInterface|mixed either the DI container or the entry associated to the $entryId
 */
function app($entryId = null, $parameters = [])
{
    $container = \Studip\DIContainer::getInstance();
    if (is_null($entryId)) {
        return $container;
    }

    return $container->make($entryId, $parameters);
}