summaryrefslogtreecommitdiff
path: root/src/Application.php
blob: ac69c20a52f8c454a1263a29bf6062fb9ed4ee3f (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php

namespace Engelsystem;

use Engelsystem\Config\Config;
use Engelsystem\Container\Container;
use Engelsystem\Container\ServiceProvider;
use Illuminate\Container\Container as IlluminateContainer;
use Psr\Container\ContainerInterface;
use Psr\Http\Server\MiddlewareInterface;

class Application extends Container
{
    /** @var string|null */
    protected $appPath = null;

    /** @var bool */
    protected $isBootstrapped = false;

    /** @var MiddlewareInterface[]|string[] */
    protected $middleware;

    /**
     * Registered service providers
     *
     * @var array
     */
    protected $serviceProviders = [];

    /**
     * Application constructor.
     *
     * @param string $appPath
     */
    public function __construct($appPath = null)
    {
        if (!is_null($appPath)) {
            $this->setAppPath($appPath);
        }

        $this->registerBaseBindings();
    }

    protected function registerBaseBindings()
    {
        static::setInstance($this);
        Container::setInstance($this);
        $this->instance('app', $this);
        $this->instance('container', $this);
        $this->instance(Container::class, $this);
        $this->instance(Application::class, $this);
        $this->instance(IlluminateContainer::class, $this);
        $this->bind(ContainerInterface::class, self::class);
    }

    /**
     * @param string|ServiceProvider $provider
     * @return ServiceProvider
     */
    public function register($provider)
    {
        if (is_string($provider)) {
            $provider = $this->make($provider);
        }

        $this->serviceProviders[] = $provider;

        $provider->register();

        if ($this->isBootstrapped) {
            $this->call([$provider, 'boot']);
        }

        return $provider;
    }

    /**
     * Boot service providers
     *
     * @param Config|null $config
     */
    public function bootstrap(Config $config = null)
    {
        if ($this->isBootstrapped) {
            return;
        }

        if ($config instanceof Config) {
            foreach ($config->get('providers', []) as $provider) {
                $this->register($provider);
            }

            $this->middleware = $config->get('middleware', []);
        }

        foreach ($this->serviceProviders as $provider) {
            $this->call([$provider, 'boot']);
        }

        $this->isBootstrapped = true;
    }

    protected function registerPaths()
    {
        $appPath = $this->appPath;

        $this->instance('path', $appPath);
        $this->instance('path.config', $appPath . DIRECTORY_SEPARATOR . 'config');
        $this->instance('path.resources', $appPath . DIRECTORY_SEPARATOR . 'resources');
        $this->instance('path.assets', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'assets');
        $this->instance('path.lang', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'lang');
        $this->instance('path.views', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'views');
        $this->instance('path.storage', $appPath . DIRECTORY_SEPARATOR . 'storage');
        $this->instance('path.cache', $this->get('path.storage') . DIRECTORY_SEPARATOR . 'cache');
        $this->instance('path.cache.routes', $this->get('path.cache') . DIRECTORY_SEPARATOR . 'routes.cache.php');
        $this->instance('path.cache.views', $this->get('path.cache') . DIRECTORY_SEPARATOR . 'views');
    }

    /**
     * Set app base path
     *
     * @param string $appPath
     * @return static
     */
    public function setAppPath($appPath)
    {
        $appPath = realpath($appPath);
        $appPath = rtrim($appPath, DIRECTORY_SEPARATOR);

        $this->appPath = $appPath;

        $this->registerPaths();

        return $this;
    }

    /**
     * @return string|null
     */
    public function path()
    {
        return $this->appPath;
    }

    /**
     * @return bool
     */
    public function isBooted()
    {
        return $this->isBootstrapped;
    }

    /**
     * @return MiddlewareInterface[]|string[]
     */
    public function getMiddleware()
    {
        return $this->middleware;
    }
}