Skip to content

Commit

Permalink
Introduced base module
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyklay committed Nov 20, 2016
1 parent 05c0923 commit 65afe15
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 18 deletions.
96 changes: 96 additions & 0 deletions core/common/Module.php
@@ -0,0 +1,96 @@
<?php
/**
* Phanbook : Delightfully simple forum software
*
* Licensed under The GNU License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @link http://phanbook.com Phanbook Project
* @since 1.0.0
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*/
namespace Phanbook\Common;

use Phalcon\DiInterface;
use Phalcon\Events\Manager;
use Phalcon\Events\ManagerInterface;

/**
* \Phanbook\Common\Module
*
* @package Phanbook\Common
*/
abstract class Module implements ModuleInterface
{
/**
* @var DiInterface
*/
protected $di;

/**
* @var Manager
*/
protected $eventsManager;

/**
* Module constructor.
*
* @param DiInterface $di
* @param Manager|null $manager
*/
public function __construct(DiInterface $di = null, Manager $manager = null)
{
$this->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;
}
}
36 changes: 36 additions & 0 deletions core/common/ModuleInterface.php
@@ -0,0 +1,36 @@
<?php
/**
* Phanbook : Delightfully simple forum software
*
* Licensed under The GNU License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @link http://phanbook.com Phanbook Project
* @since 1.0.0
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*/
namespace Phanbook\Common;

use Phalcon\Events\EventsAwareInterface;
use Phalcon\Mvc\ModuleDefinitionInterface;

/**
* \Phanbook\Common\ModuleInterface
*
* @package Phanbook\Common
*/
interface ModuleInterface extends ModuleDefinitionInterface, EventsAwareInterface
{
/**
* Initialize the module.
*/
public function initialize();

/**
* Gets controllers/tasks namespace.
*
* @return string
*/
public function getHandlersNamespace();
}
18 changes: 14 additions & 4 deletions core/modules/backend/Module.php
Expand Up @@ -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\AccessListener;
use Phanbook\Common\Library\Events\DispatcherListener;
Expand All @@ -25,8 +25,18 @@
*
* @package Phanbook\Backend
*/
class Module implements ModuleDefinitionInterface
class Module extends BaseModule
{
/**
* {@inheritdoc}
*
* @return string
*/
public function getHandlersNamespace()
{
return 'Phanbook\Backend\Controllers';
}

/**
* Registers an autoloader related to the module.
*
Expand All @@ -37,8 +47,8 @@ public function registerAutoloaders(DiInterface $di = null)
$loader = new Loader();

$namespaces = [
'Phanbook\Backend\Controllers' => __DIR__ . '/controllers/',
'Phanbook\Backend\Forms' => __DIR__ . '/forms/',
$this->getHandlersNamespace() => __DIR__ . '/controllers/',
'Phanbook\Backend\Forms' => __DIR__ . '/forms/',
];

$loader->registerNamespaces($namespaces);
Expand Down
22 changes: 16 additions & 6 deletions core/modules/cli/Module.php
Expand Up @@ -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.
*
Expand All @@ -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);
Expand All @@ -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());
}
}
18 changes: 14 additions & 4 deletions core/modules/frontend/Module.php
Expand Up @@ -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;

Expand All @@ -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.
*
Expand All @@ -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);
Expand Down
18 changes: 14 additions & 4 deletions core/modules/oauth/Module.php
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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);
Expand Down

0 comments on commit 65afe15

Please sign in to comment.