aboutsummaryrefslogtreecommitdiff
path: root/ratatoeskr/sys/Env.php
blob: 612418efff73b7979331cd96dfd76c379f90753b (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
47
48
49
50
51
52
53
54
55
56
57
<?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);
        });
    }

    /**
     * The Base path of this ratatoeskr site.
     * @return string
     */
    public function siteBasePath(): string
    {
        return $this->lazy("siteBasePath", static function () {
            return dirname(dirname(dirname(__FILE__)));
        });
    }
}