From d49e49c364c1b73e4e4e3b52dc10ee9d0150e447 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Fri, 22 Sep 2017 14:02:02 +0200 Subject: Implemented service provider functionality --- src/Application.php | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 79 insertions(+), 1 deletion(-) (limited to 'src/Application.php') diff --git a/src/Application.php b/src/Application.php index 80538396..b62b28a9 100644 --- a/src/Application.php +++ b/src/Application.php @@ -2,7 +2,9 @@ namespace Engelsystem; +use Engelsystem\Config\Config; use Engelsystem\Container\Container; +use Engelsystem\Container\ServiceProvider; use Psr\Container\ContainerInterface; class Application extends Container @@ -10,6 +12,16 @@ class Application extends Container /** @var string|null */ protected $appPath = null; + /** @var bool */ + protected $isBootstrapped = false; + + /** + * Registered service providers + * + * @var array + */ + protected $serviceProviders = []; + /** * Application constructor. * @@ -36,15 +48,73 @@ class Application extends Container } /** + * @param string|ServiceProvider $provider + * @return ServiceProvider + */ + public function register($provider) + { + if (is_string($provider)) { + $provider = $this->get($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->instance('path', $appPath); + + $this->registerPaths(); return $this; } @@ -56,4 +126,12 @@ class Application extends Container { return $this->appPath; } + + /** + * @return bool + */ + public function isBooted() + { + return $this->isBootstrapped; + } } -- cgit v1.2.3-54-g00ecf