From 65afe1598f9daaafeff6c05768e8fe4870842cd1 Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Sun, 20 Nov 2016 04:32:02 +0200 Subject: [PATCH] Introduced base module --- core/common/Module.php | 96 ++++++++++++++++++++++++++++++++ core/common/ModuleInterface.php | 36 ++++++++++++ core/modules/backend/Module.php | 18 ++++-- core/modules/cli/Module.php | 22 ++++++-- core/modules/frontend/Module.php | 18 ++++-- core/modules/oauth/Module.php | 18 ++++-- 6 files changed, 190 insertions(+), 18 deletions(-) create mode 100644 core/common/Module.php create mode 100644 core/common/ModuleInterface.php diff --git a/core/common/Module.php b/core/common/Module.php new file mode 100644 index 0000000..f697dce --- /dev/null +++ b/core/common/Module.php @@ -0,0 +1,96 @@ +di = $di ?: container(); + $this->eventsManager = $manager; + } + + /** + * Initialize module. + */ + public function initialize() + { + $this->registerAutoloaders($this->di); + $this->registerServices($this->di); + } + + /** + * Returns the internal event manager. + * + * @return ManagerInterface + */ + public function getEventsManager() + { + $eventsManager = $this->eventsManager; + + if ($eventsManager instanceof ManagerInterface) { + return $eventsManager; + } + + if ($this->di->has('eventsManager')) { + $eventsManager = $this->di->getShared('eventsManager'); + } else { + $eventsManager = new Manager(); + $eventsManager->enablePriorities(true); + + $this->di->setShared('eventsManager', $eventsManager); + } + + $this->setEventsManager($eventsManager); + + return $eventsManager; + } + + /** + * Sets the events manager. + * + * @param ManagerInterface $eventsManager + * @return $this + */ + public function setEventsManager(ManagerInterface $eventsManager) + { + $this->eventsManager = $eventsManager; + + return $this; + } +} diff --git a/core/common/ModuleInterface.php b/core/common/ModuleInterface.php new file mode 100644 index 0000000..803e9c3 --- /dev/null +++ b/core/common/ModuleInterface.php @@ -0,0 +1,36 @@ + __DIR__ . '/controllers/', - 'Phanbook\Backend\Forms' => __DIR__ . '/forms/', + $this->getHandlersNamespace() => __DIR__ . '/controllers/', + 'Phanbook\Backend\Forms' => __DIR__ . '/forms/', ]; $loader->registerNamespaces($namespaces); diff --git a/core/modules/cli/Module.php b/core/modules/cli/Module.php index 6a2b187..5283e12 100644 --- a/core/modules/cli/Module.php +++ b/core/modules/cli/Module.php @@ -15,15 +15,25 @@ use Phalcon\Loader; use Phalcon\DiInterface; -use Phalcon\Mvc\ModuleDefinitionInterface; +use Phanbook\Common\Module as BaseModule; /** * \Phanbook\Cli\Module * * @package Phanbook\Cli */ -class Module implements ModuleDefinitionInterface +class Module extends BaseModule { + /** + * {@inheritdoc} + * + * @return string + */ + public function getHandlersNamespace() + { + return 'Phanbook\Cli\Tasks'; + } + /** * Registers an autoloader related to the module. * @@ -34,9 +44,9 @@ public function registerAutoloaders(DiInterface $di = null) $loader = new Loader(); $namespaces = [ - 'Phanbook\Cli\Tasks' => __DIR__ . '/tasks/', - 'Phanbook\Cli\Library' => __DIR__ . '/library/', - 'Phanbook\Seeder' => __DIR__ . '/seeders/', + $this->getHandlersNamespace() => __DIR__ . '/tasks/', + 'Phanbook\Cli\Library' => __DIR__ . '/library/', + 'Phanbook\Seeders' => __DIR__ . '/seeders/', ]; $loader->registerNamespaces($namespaces); @@ -52,6 +62,6 @@ public function registerAutoloaders(DiInterface $di = null) public function registerServices(DiInterface $di) { // Setting up the MVC Dispatcher - $di->getShared('dispatcher')->setDefaultNamespace('Phanbook\Cli\Tasks'); + $di->getShared('dispatcher')->setDefaultNamespace($this->getHandlersNamespace()); } } diff --git a/core/modules/frontend/Module.php b/core/modules/frontend/Module.php index 388e91a..c7a5813 100644 --- a/core/modules/frontend/Module.php +++ b/core/modules/frontend/Module.php @@ -15,7 +15,7 @@ use Phalcon\Loader; use Phalcon\DiInterface; -use Phalcon\Mvc\ModuleDefinitionInterface; +use Phanbook\Common\Module as BaseModule; use Phanbook\Common\Library\Events\ViewListener; use Phanbook\Common\Library\Events\DispatcherListener; @@ -24,8 +24,18 @@ * * @package Phanbook\Frontend */ -class Module implements ModuleDefinitionInterface +class Module extends BaseModule { + /** + * {@inheritdoc} + * + * @return string + */ + public function getHandlersNamespace() + { + return 'Phanbook\Frontend\Controllers'; + } + /** * Registers an autoloader related to the module. * @@ -36,8 +46,8 @@ public function registerAutoloaders(DiInterface $di = null) $loader = new Loader(); $namespaces = [ - 'Phanbook\Frontend\Controllers' => __DIR__ . '/controllers/', - 'Phanbook\Frontend\Forms' => __DIR__ . '/forms/', + $this->getHandlersNamespace() => __DIR__ . '/controllers/', + 'Phanbook\Frontend\Forms' => __DIR__ . '/forms/', ]; $loader->registerNamespaces($namespaces); diff --git a/core/modules/oauth/Module.php b/core/modules/oauth/Module.php index 8c09139..3a52ce1 100644 --- a/core/modules/oauth/Module.php +++ b/core/modules/oauth/Module.php @@ -15,7 +15,7 @@ use Phalcon\Loader; use Phalcon\DiInterface; -use Phalcon\Mvc\ModuleDefinitionInterface; +use Phanbook\Common\Module as BaseModule; use Phanbook\Common\Library\Events\UserLogins; use Phanbook\Common\Library\Events\ViewListener; use Phanbook\Common\Library\Events\DispatcherListener; @@ -25,8 +25,18 @@ * * @package Phanbook\Oauth */ -class Module implements ModuleDefinitionInterface +class Module extends BaseModule { + /** + * {@inheritdoc} + * + * @return string + */ + public function getHandlersNamespace() + { + return 'Phanbook\Oauth\Controllers'; + } + /** * Registers an autoloader related to the module. * @@ -37,8 +47,8 @@ public function registerAutoloaders(DiInterface $di = null) $loader = new Loader(); $namespaces = [ - 'Phanbook\Oauth\Controllers' => __DIR__ . '/controllers/', - 'Phanbook\Oauth\Forms' => __DIR__ . '/forms/', + $this->getHandlersNamespace() => __DIR__ . '/controllers/', + 'Phanbook\Oauth\Forms' => __DIR__ . '/forms/', ]; $loader->registerNamespaces($namespaces, true);