diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2017-10-31 13:40:13 +0100 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2017-10-31 13:40:13 +0100 |
commit | 60fd72cd1a1e4e53b9af87e00a8c27687c6b5385 (patch) | |
tree | bcf250d05a49a848644af44f09fb897467e3ecb0 /src | |
parent | fb05a38a963784716c105e7f214615ad13d2f272 (diff) |
Added service providers
Diffstat (limited to 'src')
-rw-r--r-- | src/Application.php | 2 | ||||
-rw-r--r-- | src/Config/ConfigServiceProvider.php | 26 | ||||
-rw-r--r-- | src/Database/DatabaseServiceProvider.php | 31 | ||||
-rw-r--r-- | src/Exceptions/ExceptionsServiceProvider.php | 15 | ||||
-rw-r--r-- | src/Logger/LoggerServiceProvider.php | 18 | ||||
-rw-r--r-- | src/Renderer/RendererServiceProvider.php | 36 | ||||
-rw-r--r-- | src/Routing/RoutingServiceProvider.php | 14 |
7 files changed, 141 insertions, 1 deletions
diff --git a/src/Application.php b/src/Application.php index b62b28a9..c9023c7b 100644 --- a/src/Application.php +++ b/src/Application.php @@ -54,7 +54,7 @@ class Application extends Container public function register($provider) { if (is_string($provider)) { - $provider = $this->get($provider); + $provider = $this->make($provider); } $this->serviceProviders[] = $provider; diff --git a/src/Config/ConfigServiceProvider.php b/src/Config/ConfigServiceProvider.php new file mode 100644 index 00000000..01b648df --- /dev/null +++ b/src/Config/ConfigServiceProvider.php @@ -0,0 +1,26 @@ +<?php + +namespace Engelsystem\Config; + +use Engelsystem\Container\ServiceProvider; + +class ConfigServiceProvider extends ServiceProvider +{ + public function register() + { + $defaultConfigFile = config_path('config.default.php'); + $configFile = config_path('config.php'); + + $config = $this->app->make(Config::class); + $this->app->instance('config', $config); + + $config->set(require $defaultConfigFile); + + if (file_exists($configFile)) { + $config->set(array_replace_recursive( + $config->get(null), + require $configFile + )); + } + } +} diff --git a/src/Database/DatabaseServiceProvider.php b/src/Database/DatabaseServiceProvider.php new file mode 100644 index 00000000..364816cc --- /dev/null +++ b/src/Database/DatabaseServiceProvider.php @@ -0,0 +1,31 @@ +<?php + +namespace Engelsystem\Database; + +use Engelsystem\Container\ServiceProvider; +use Exception; +use PDO; + +class DatabaseServiceProvider extends ServiceProvider +{ + public function register() + { + $config = $this->app->get('config'); + Db::connect( + 'mysql:host=' . $config->get('database')['host'] . ';dbname=' . $config->get('database')['db'] . ';charset=utf8', + $config->get('database')['user'], + $config->get('database')['pw'] + ) || $this->exitOnError(); + + Db::getPdo()->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + Db::getPdo()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); + } + + /** + * @throws Exception + */ + protected function exitOnError() + { + throw new Exception('Error: Unable to connect to database'); + } +} diff --git a/src/Exceptions/ExceptionsServiceProvider.php b/src/Exceptions/ExceptionsServiceProvider.php new file mode 100644 index 00000000..7755e1e7 --- /dev/null +++ b/src/Exceptions/ExceptionsServiceProvider.php @@ -0,0 +1,15 @@ +<?php + +namespace Engelsystem\Exceptions; + +use Engelsystem\Container\ServiceProvider; +use Engelsystem\Exceptions\Handler as ExceptionHandler; + +class ExceptionsServiceProvider extends ServiceProvider +{ + public function register() + { + $errorHandler = $this->app->make(ExceptionHandler::class); + $this->app->instance('error.handler', $errorHandler); + } +} diff --git a/src/Logger/LoggerServiceProvider.php b/src/Logger/LoggerServiceProvider.php new file mode 100644 index 00000000..cf22f383 --- /dev/null +++ b/src/Logger/LoggerServiceProvider.php @@ -0,0 +1,18 @@ +<?php + +namespace Engelsystem\Logger; + +use Engelsystem\Container\ServiceProvider; +use Psr\Log\LoggerInterface; + +class LoggerServiceProvider extends ServiceProvider +{ + public function register() + { + $logger = $this->app->make(EngelsystemLogger::class); + $this->app->instance('logger', $logger); + + $this->app->bind(LoggerInterface::class, 'logger'); + $this->app->bind(EngelsystemLogger::class, 'logger'); + } +} diff --git a/src/Renderer/RendererServiceProvider.php b/src/Renderer/RendererServiceProvider.php new file mode 100644 index 00000000..3e8d69bc --- /dev/null +++ b/src/Renderer/RendererServiceProvider.php @@ -0,0 +1,36 @@ +<?php + +namespace Engelsystem\Renderer; + +use Engelsystem\Container\ServiceProvider; + +class RendererServiceProvider extends ServiceProvider +{ + public function register() + { + $this->registerRenderer(); + $this->registerHtmlEngine(); + } + + public function boot() + { + $renderer = $this->app->get('renderer'); + + foreach ($this->app->tagged('renderer.engine') as $engine) { + $renderer->addRenderer($engine); + } + } + + protected function registerRenderer() + { + $renderer = $this->app->make(Renderer::class); + $this->app->instance('renderer', $renderer); + } + + protected function registerHtmlEngine() + { + $htmlEngine = $this->app->make(HtmlEngine::class); + $this->app->instance('renderer.htmlEngine', $htmlEngine); + $this->app->tag('renderer.htmlEngine', ['renderer.engine']); + } +} diff --git a/src/Routing/RoutingServiceProvider.php b/src/Routing/RoutingServiceProvider.php new file mode 100644 index 00000000..b7db1383 --- /dev/null +++ b/src/Routing/RoutingServiceProvider.php @@ -0,0 +1,14 @@ +<?php + +namespace Engelsystem\Routing; + +use Engelsystem\Container\ServiceProvider; + +class RoutingServiceProvider extends ServiceProvider +{ + public function register() + { + $urlGenerator = $this->app->make(UrlGenerator::class); + $this->app->instance('routing.urlGenerator', $urlGenerator); + } +} |