Skip to content

Commit

Permalink
Service Container Base Class
Browse files Browse the repository at this point in the history
  • Loading branch information
tugrul committed Jun 24, 2016
1 parent 1ae493c commit f56d03c
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 142 deletions.
129 changes: 129 additions & 0 deletions app/Services.php
@@ -0,0 +1,129 @@
<?php

use Phalcon\Mvc\View;
use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\Url as UrlProvider;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Mvc\Model\Metadata\Memory as MetaData;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Flash\Session as FlashSession;
use Phalcon\Events\Manager as EventsManager;

class Services extends \Base\Services
{
/**
* We register the events manager
*/
protected function initDispatcher()
{
$eventsManager = new EventsManager;

/**
* Check if the user is allowed to access certain action using the SecurityPlugin
*/
$eventsManager->attach('dispatch:beforeDispatch', new SecurityPlugin);

/**
* Handle exceptions and not-found exceptions using NotFoundPlugin
*/
$eventsManager->attach('dispatch:beforeException', new NotFoundPlugin);

$dispatcher = new Dispatcher;
$dispatcher->setEventsManager($eventsManager);

return $dispatcher;
}

/**
* The URL component is used to generate all kind of urls in the application
*/
protected function initUrl()
{
$url = new UrlProvider();
$url->setBaseUri($this->get('config')->application->baseUri);
return $url;
}

protected function initView()
{
$view = new View();

$view->setViewsDir(APP_PATH . $this->get('config')->application->viewsDir);

$view->registerEngines(array(
".volt" => 'volt'
));

return $view;
}

/**
* Setting up volt
*/
protected function initSharedVolt($view, $di)
{
$volt = new VoltEngine($view, $di);

$volt->setOptions(array(
"compiledPath" => APP_PATH . "cache/volt/"
));

$compiler = $volt->getCompiler();
$compiler->addFunction('is_a', 'is_a');

return $volt;
}

/**
* Database connection is created based in the parameters defined in the configuration file
*/
protected function initDb()
{
$config = $this->get('config')->get('database')->toArray();

$dbClass = 'Phalcon\Db\Adapter\Pdo\\' . $config['adapter'];
unset($config['adapter']);

return new $dbClass($config);
}

/**
* If the configuration specify the use of metadata adapter use it or use memory otherwise
*/
protected function initModelsMetadata()
{
return new MetaData();
}

/**
* Start the session the first time some component request the session service
*/
protected function initSession()
{
$session = new SessionAdapter();
$session->start();
return $session;
}

/**
* Register the flash service with custom CSS classes
*/
protected function initFlash()
{
return new FlashSession(array(
'error' => 'alert alert-danger',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
'warning' => 'alert alert-warning'
));
}

/**
* Register a user component
*/
protected function initElements()
{
return new Elements();
}
}
20 changes: 11 additions & 9 deletions app/config/loader.php
Expand Up @@ -5,13 +5,15 @@
/**
* We're a registering a set of directories taken from the configuration file
*/
$loader->registerDirs(
array(
APP_PATH . $config->application->controllersDir,
APP_PATH . $config->application->pluginsDir,
APP_PATH . $config->application->libraryDir,
APP_PATH . $config->application->modelsDir,
APP_PATH . $config->application->formsDir,
)
)->register();
$loader->registerDirs([
APP_PATH . $config->application->controllersDir,
APP_PATH . $config->application->pluginsDir,
APP_PATH . $config->application->libraryDir,
APP_PATH . $config->application->modelsDir,
APP_PATH . $config->application->formsDir
])->register();

$loader->registerClasses([
'Services' => APP_PATH . 'app/Services.php'
]);

126 changes: 0 additions & 126 deletions app/config/services.php

This file was deleted.

34 changes: 34 additions & 0 deletions app/library/Base/Services.php
@@ -0,0 +1,34 @@
<?php

namespace Base;

class Services extends \Phalcon\DI\FactoryDefault
{
public function __construct($config)
{
parent::__construct();

$this->setShared('config', $config);
$this->bindServices();
}

protected function bindServices()
{
$reflection = new \ReflectionObject($this);
$methods = $reflection->getMethods();

foreach ($methods as $method) {

if ((strlen($method->name) > 10) && (strpos($method->name, 'initShared') === 0)) {
$this->set(lcfirst(substr($method->name, 10)), $method->getClosure($this));
continue;
}

if ((strlen($method->name) > 4) && (strpos($method->name, 'init') === 0)) {
$this->set(lcfirst(substr($method->name, 4)), $method->getClosure($this));
}

}

}
}
10 changes: 3 additions & 7 deletions public/index.php
Expand Up @@ -22,14 +22,10 @@
*/
require APP_PATH . 'app/config/loader.php';

/**
* Load application services
*/
require APP_PATH . 'app/config/services.php';

$application = new Application($di);
$application = new Application(new Services($config));

echo $application->handle()->getContent();
// NGINX - PHP-FPM already set PATH_INFO variable to handle route
echo $application->handle(!empty($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : null)->getContent();
} catch (Exception $e){
echo $e->getMessage() . '<br>';
echo '<pre>' . $e->getTraceAsString() . '</pre>';
Expand Down

0 comments on commit f56d03c

Please sign in to comment.