aboutsummaryrefslogtreecommitdiff
path: root/ratatoeskr/sys/Env.php
blob: cfe15987c35b81a56d81026b8d9bb777494d4b8a (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
34
35
36
37
<?php


namespace r7r\cms\sys;

use r7r\cms\sys\textprocessors\TextprocessorRepository;

/**
 * Env holds several global global objects. It's basically a DI container.
 */
class Env
{
    /** @var self|null  */
    private static $globalInstance = null;

    private $lazyLoaded = [];

    private function lazy(string $ident, callable $callback)
    {
        if (!isset($this->lazyLoaded[$ident])) {
            $this->lazyLoaded[$ident] = $callback();
        }
        return $this->lazyLoaded[$ident];
    }

    public static function getGlobal(): self
    {
        self::$globalInstance = self::$globalInstance ?? new self();

        return self::$globalInstance;
    }

    public function textprocessors(): TextprocessorRepository
    {
        return $this->lazy("textprocessors", [TextprocessorRepository::class, 'buildDefault']);
    }
}