aboutsummaryrefslogtreecommitdiff
path: root/ratatoeskr/sys/Env.php
blob: 63d079dc63c97232f455d1d43c6ccfe0d88dbe70 (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
38
39
40
41
42
43
44
45
46
<?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']);
    }

    public function database(): Database
    {
        return $this->lazy("database", static function () {
            global $config;

            return Database::fromConfig($config);
        });
    }
}