From c16c04428a7d1c837c11cc084f3eeb25f6911427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Audet?= Date: Mon, 29 Jan 2018 19:34:55 -0500 Subject: [PATCH] refactored the way initialisation is done --- src/Context.php | 26 +++++++++++++ src/Core.php | 38 ++++++------------- .../Core/AlreadyInitialisedException.php | 25 ++++++++++++ src/Exceptions/CoreException.php | 23 +++++++++++ 4 files changed, 85 insertions(+), 27 deletions(-) create mode 100644 src/Exceptions/Core/AlreadyInitialisedException.php create mode 100644 src/Exceptions/CoreException.php diff --git a/src/Context.php b/src/Context.php index e46589c..6c9eff6 100644 --- a/src/Context.php +++ b/src/Context.php @@ -67,6 +67,32 @@ public function register(string $providerClass): self return $this; } + public function route(): self + { + /** @var Events $events */ + $events = $this->getSingletons()->get(Events::class); + + /** @var Router $router */ + $router = $this->getSingletons()->get(Router::class); + + foreach ($this->getModulesManager()->getAllModules() as [$vendor_name, $module_name, $path]) { + $events->loadPath($path); + $router->loadPath($path); + } + + $events->fire('Cervo/System/Before'); + + $route = $router->dispatch(); + + $events->fire('Cervo/Route/Before'); + (new ControllerReflection($this, $route))(); + $events->fire('Cervo/Route/After'); + + $events->fire('Cervo/System/After'); + + return $this; + } + public function getConfig(): BaseConfig { return $this->config; diff --git a/src/Core.php b/src/Core.php index db7fe31..c863bdb 100644 --- a/src/Core.php +++ b/src/Core.php @@ -18,6 +18,8 @@ namespace Cervo; use Cervo\Config\BaseConfig; +use Cervo\Exceptions\ControllerReflection\AlreadyInitialisedException; + /** * Core class for Cervo. @@ -26,39 +28,21 @@ */ final class Core { - private $context = null; + private static $isInit = false; + private static $context = null; - public function __construct(?BaseConfig $config = null) + public static function init(?BaseConfig $config = null) { - $this->context = new Context($config); - } - - public function init() - { - /** @var Events $events */ - $events = $this->context->getSingletons()->get(Events::class); - - /** @var Router $router */ - $router = $this->context->getSingletons()->get(Router::class); - - foreach ($this->context->getModulesManager()->getAllModules() as [$vendor_name, $module_name, $path]) { - $events->loadPath($path); - $router->loadPath($path); + if (self::$isInit === true) { + throw new AlreadyInitialisedException; } - $events->fire('Cervo/System/Before'); - - $route = $router->dispatch(); - - $events->fire('Cervo/Route/Before'); - (new ControllerReflection($this->context, $route))(); - $events->fire('Cervo/Route/After'); - - $events->fire('Cervo/System/After'); + self::$isInit = true; + self::$context = new Context($config); } - public function getContext() + public static function getContext() { - return $this->context; + return self::$context; } } diff --git a/src/Exceptions/Core/AlreadyInitialisedException.php b/src/Exceptions/Core/AlreadyInitialisedException.php new file mode 100644 index 0000000..93e894d --- /dev/null +++ b/src/Exceptions/Core/AlreadyInitialisedException.php @@ -0,0 +1,25 @@ +. + * + * @package Cervo + * @author Marc André Audet + * @copyright 2010 - 2018 Nevraxe inc. & Marc André Audet + * @license See LICENSE.md BSD-2-Clauses + * @link https://github.com/Nevraxe/Cervo + * @since 5.0.0 + */ + +declare(strict_types=1); + +namespace Cervo\Exceptions\ControllerReflection; + +use Cervo\Exceptions\CoreException; + +class AlreadyInitialisedException extends CoreException +{ + +} diff --git a/src/Exceptions/CoreException.php b/src/Exceptions/CoreException.php new file mode 100644 index 0000000..5f376ff --- /dev/null +++ b/src/Exceptions/CoreException.php @@ -0,0 +1,23 @@ +. + * + * @package Cervo + * @author Marc André Audet + * @copyright 2010 - 2018 Nevraxe inc. & Marc André Audet + * @license See LICENSE.md BSD-2-Clauses + * @link https://github.com/Nevraxe/Cervo + * @since 5.0.0 + */ + +declare(strict_types=1); + +namespace Cervo\Exceptions; + +class CoreException extends \RuntimeException +{ + +}