Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
177 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
|
||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters