2017-09-19 14:50:20 +02:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2017-09-19 14:50:20 +02:00
|
|
|
namespace Engelsystem;
|
|
|
|
|
2017-09-22 14:02:02 +02:00
|
|
|
use Engelsystem\Config\Config;
|
2017-09-19 14:50:20 +02:00
|
|
|
use Engelsystem\Container\Container;
|
2017-09-22 14:02:02 +02:00
|
|
|
use Engelsystem\Container\ServiceProvider;
|
2018-01-14 01:48:50 +01:00
|
|
|
use Illuminate\Container\Container as IlluminateContainer;
|
2021-06-29 00:27:57 +02:00
|
|
|
use Illuminate\Contracts\Container\Container as IlluminateContainerContract;
|
2017-09-19 14:50:20 +02:00
|
|
|
use Psr\Container\ContainerInterface;
|
2018-08-12 00:08:52 +02:00
|
|
|
use Psr\Http\Server\MiddlewareInterface;
|
2017-09-19 14:50:20 +02:00
|
|
|
|
|
|
|
class Application extends Container
|
|
|
|
{
|
2022-12-15 19:50:56 +01:00
|
|
|
protected ?string $appPath = null;
|
2017-09-21 20:52:19 +02:00
|
|
|
|
2022-12-15 19:50:56 +01:00
|
|
|
protected bool $isBootstrapped = false;
|
2017-09-22 14:02:02 +02:00
|
|
|
|
2018-08-12 00:08:52 +02:00
|
|
|
/** @var MiddlewareInterface[]|string[] */
|
2022-12-15 19:50:56 +01:00
|
|
|
protected array $middleware;
|
2018-08-12 00:08:52 +02:00
|
|
|
|
2017-09-22 14:02:02 +02:00
|
|
|
/**
|
|
|
|
* Registered service providers
|
|
|
|
*/
|
2022-12-15 19:50:56 +01:00
|
|
|
protected array $serviceProviders = [];
|
2017-09-22 14:02:02 +02:00
|
|
|
|
2017-09-21 20:52:19 +02:00
|
|
|
/**
|
|
|
|
* Application constructor.
|
|
|
|
*/
|
2022-12-14 00:28:34 +01:00
|
|
|
public function __construct(string $appPath = null)
|
2017-09-19 14:50:20 +02:00
|
|
|
{
|
2017-09-21 20:52:19 +02:00
|
|
|
if (!is_null($appPath)) {
|
|
|
|
$this->setAppPath($appPath);
|
|
|
|
}
|
|
|
|
|
2017-09-19 14:50:20 +02:00
|
|
|
$this->registerBaseBindings();
|
|
|
|
}
|
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function registerBaseBindings(): void
|
2017-09-19 14:50:20 +02:00
|
|
|
{
|
2017-09-21 18:37:37 +02:00
|
|
|
static::setInstance($this);
|
2017-09-19 14:50:20 +02:00
|
|
|
Container::setInstance($this);
|
|
|
|
$this->instance('app', $this);
|
|
|
|
$this->instance('container', $this);
|
|
|
|
$this->instance(Container::class, $this);
|
|
|
|
$this->instance(Application::class, $this);
|
2018-01-14 01:48:50 +01:00
|
|
|
$this->instance(IlluminateContainer::class, $this);
|
2021-06-29 00:27:57 +02:00
|
|
|
$this->instance(IlluminateContainerContract::class, $this);
|
2019-04-24 13:50:01 +02:00
|
|
|
$this->bind(ContainerInterface::class, self::class);
|
2017-09-19 14:50:20 +02:00
|
|
|
}
|
2017-09-21 20:52:19 +02:00
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
public function register(string|ServiceProvider $provider): ServiceProvider
|
2017-09-22 14:02:02 +02:00
|
|
|
{
|
|
|
|
if (is_string($provider)) {
|
2017-10-31 13:40:13 +01:00
|
|
|
$provider = $this->make($provider);
|
2017-09-22 14:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->serviceProviders[] = $provider;
|
|
|
|
|
|
|
|
$provider->register();
|
|
|
|
|
|
|
|
if ($this->isBootstrapped) {
|
|
|
|
$this->call([$provider, 'boot']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $provider;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Boot service providers
|
|
|
|
*
|
|
|
|
* @param Config|null $config
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function bootstrap(Config $config = null): void
|
2017-09-22 14:02:02 +02:00
|
|
|
{
|
|
|
|
if ($this->isBootstrapped) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($config instanceof Config) {
|
|
|
|
foreach ($config->get('providers', []) as $provider) {
|
|
|
|
$this->register($provider);
|
|
|
|
}
|
2018-08-12 00:08:52 +02:00
|
|
|
|
|
|
|
$this->middleware = $config->get('middleware', []);
|
2017-09-22 14:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->serviceProviders as $provider) {
|
|
|
|
$this->call([$provider, 'boot']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->isBootstrapped = true;
|
|
|
|
}
|
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
protected function registerPaths(): void
|
2017-09-22 14:02:02 +02:00
|
|
|
{
|
|
|
|
$appPath = $this->appPath;
|
|
|
|
|
|
|
|
$this->instance('path', $appPath);
|
|
|
|
$this->instance('path.config', $appPath . DIRECTORY_SEPARATOR . 'config');
|
2018-09-10 17:22:05 +02:00
|
|
|
$this->instance('path.resources', $appPath . DIRECTORY_SEPARATOR . 'resources');
|
2023-05-21 12:40:36 +02:00
|
|
|
$this->instance('path.resources.api', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'api');
|
2021-08-10 11:19:58 +02:00
|
|
|
$this->instance('path.public', $appPath . DIRECTORY_SEPARATOR . 'public');
|
2018-09-10 17:22:05 +02:00
|
|
|
$this->instance('path.assets', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'assets');
|
2021-08-10 11:19:58 +02:00
|
|
|
$this->instance('path.assets.public', $this->get('path.public') . DIRECTORY_SEPARATOR . 'assets');
|
2018-09-10 17:22:05 +02:00
|
|
|
$this->instance('path.lang', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'lang');
|
2018-10-28 12:59:49 +01:00
|
|
|
$this->instance('path.views', $this->get('path.resources') . DIRECTORY_SEPARATOR . 'views');
|
|
|
|
$this->instance('path.storage', $appPath . DIRECTORY_SEPARATOR . 'storage');
|
2019-07-21 02:34:52 +02:00
|
|
|
$this->instance('path.storage.app', $this->get('path.storage') . DIRECTORY_SEPARATOR . 'app');
|
2018-10-28 12:59:49 +01:00
|
|
|
$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');
|
2017-09-22 14:02:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set app base path
|
|
|
|
*
|
2017-09-21 20:52:19 +02:00
|
|
|
* @return static
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function setAppPath(string $appPath): static
|
2017-09-21 20:52:19 +02:00
|
|
|
{
|
2017-09-22 14:02:02 +02:00
|
|
|
$appPath = realpath($appPath);
|
2017-09-21 20:52:19 +02:00
|
|
|
$appPath = rtrim($appPath, DIRECTORY_SEPARATOR);
|
|
|
|
|
|
|
|
$this->appPath = $appPath;
|
2017-09-22 14:02:02 +02:00
|
|
|
|
|
|
|
$this->registerPaths();
|
2017-09-21 20:52:19 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
public function path(): ?string
|
2017-09-21 20:52:19 +02:00
|
|
|
{
|
|
|
|
return $this->appPath;
|
|
|
|
}
|
2017-09-22 14:02:02 +02:00
|
|
|
|
2022-12-14 19:15:20 +01:00
|
|
|
public function isBooted(): bool
|
2017-09-22 14:02:02 +02:00
|
|
|
{
|
|
|
|
return $this->isBootstrapped;
|
|
|
|
}
|
2018-08-12 00:08:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return MiddlewareInterface[]|string[]
|
|
|
|
*/
|
2022-12-14 19:15:20 +01:00
|
|
|
public function getMiddleware(): array
|
2018-08-12 00:08:52 +02:00
|
|
|
{
|
|
|
|
return $this->middleware;
|
|
|
|
}
|
2017-09-19 14:50:20 +02:00
|
|
|
}
|