diff options
author | msquare <msquare@notrademark.de> | 2017-11-28 15:43:51 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-28 15:43:51 +0100 |
commit | 599f2fd264bfc7b1b6826fe206442806e317340f (patch) | |
tree | 50cf84d7d07d11bd65b45c2c17f37632f6cd8eff /src/Application.php | |
parent | a5fc5bd0979e8de1fce8a8addd351a6e7bd6aeb8 (diff) | |
parent | eda7f7788ea8012bd8be46405c56a666c11f3fa5 (diff) |
Merge pull request #365 from engelsystem/feature-igel-rewrite
Feature igel rewrite
Diffstat (limited to 'src/Application.php')
-rw-r--r-- | src/Application.php | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/src/Application.php b/src/Application.php new file mode 100644 index 00000000..c9023c7b --- /dev/null +++ b/src/Application.php @@ -0,0 +1,137 @@ +<?php + +namespace Engelsystem; + +use Engelsystem\Config\Config; +use Engelsystem\Container\Container; +use Engelsystem\Container\ServiceProvider; +use Psr\Container\ContainerInterface; + +class Application extends Container +{ + /** @var string|null */ + protected $appPath = null; + + /** @var bool */ + protected $isBootstrapped = false; + + /** + * 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->bind(ContainerInterface::class, Application::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); + } + } + + 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.lang', $appPath . DIRECTORY_SEPARATOR . 'locale'); + } + + /** + * 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; + } +} |