Archived
1
0
Fork 0
This repository has been archived on 2026-04-03. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
httpcb/app/config/services.php

330 lines
8.5 KiB
PHP

<?php
/**
* Services are globally registered in this file
*
* @var \Phalcon\Config $config
*/
use Phalcon\Di\FactoryDefault;
use Phalcon\Mvc\View;
use Phalcon\Assets\Manager as AssetsManager;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Navigation\Navigation;
use Phalcon\Flash\Direct as FlashDirect;
use Phalcon\Mvc\Model\Metadata\Memory as MemoryMetaData;
use Phalcon\Mvc\Model\MetaData\Apc as ApcMetaData;
use Phalcon\Cache\Frontend\Data as FrontendDataCache;
use Phalcon\Cache\Backend\Apc as BackendApcCache;
use Phalcon\Translate\Adapter\NativeArray as TranslateAdapter;
use Phalcon\Logger;
use Phalcon\Logger\Adapter\Firephp as FirephpAdapter;
use Phalcon\Session\Adapter\Files as SessionAdapter;
use Phalcon\Mvc\Router;
/**
* The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
*/
$di = new FactoryDefault();
/**
* Setup logging
*/
$di->setShared('logger', function() {
//return new Phalcon\Logger\Adapter\Firephp("");
return new Phalcon\Logger\Adapter\File(APP_PATH . "/app/log.txt");
});
$di->setShared('dispatcher', function() use ($di, $config) {
$eventsManager = new Phalcon\Events\Manager();
$eventsManager->attach("dispatch", function($event, $dispatcher) {
$actionName = lcfirst(Phalcon\Text::camelize($dispatcher->getActionName(), '-_'));
$dispatcher->setActionName($actionName);
});
if ($config->application->debug == false) {
$eventsManager->attach('dispatch:beforeException', new \ExceptionHandlerPlugin());
}
$eventsManager->attach('dispatch:beforeExecuteRoute', new \AclPlugin());
$dispatcher = new \Phalcon\Mvc\Dispatcher();
$dispatcher->setEventsManager($eventsManager);
return $dispatcher;
});
/**
* The URL component is used to generate all kind of urls in the application
*/
$di->setShared('url', function() use ($config) {
$url = new UrlResolver();
$url->setBaseUri($config->application->baseUri);
return $url;
});
$di->setShared('router', function() {
// Create the router
$router = new Router();
$router->removeExtraSlashes(true);
$router->add('/', array(
'controller' => 'index',
'action' => 'index'
))->setName('home-route');
// Route about page to index controller
$router->add('/about', array(
'controller' => 'index',
'action' => 'about'
))->setName('about-route');
$router->add('/callback/created/{id}', array(
'controller' => 'callback',
'action' => 'created',
))->setName('cb-created');
// Route callback endpoints to callback controller.
$router->add('/cb/{id}', array(
'controller' => 'callback',
'action' => 'endpoint',
))->setName('cb-endpoint');
// Login routes.
$router->add('/login', array(
'controller' => 'auth',
'action' => 'index',
))->setName('login');
$router->add('/logout', array(
'controller' => 'auth',
'action' => 'logout',
))->setName('logout');
$router->add('/oauth/{strategy:([a-z]+)}/:params', array(
'controller' => 'auth',
'action' => 'oauth'
))->setName('oauth');
$router->add('/settings', array(
'controller' => 'user',
'action' => 'settings',
))->setName('user-settings');
return $router;
});
$di->setShared('viewHelper', '\ViewHelper\Service');
/**
* Setting up the view component
*/
$di->setShared('view', function () use ($di, $config) {
$view = new View();
$view->setViewsDir($config->application->viewsDir);
$view->setLayoutsDir('_layouts/');
$view->setPartialsDir('_partials/');
$view->registerEngines(array(
'.volt' => function ($view, $di) use ($config) {
$volt = new VoltEngine($view, $di);
$volt->setOptions(array(
'compiledPath' => $config->application->viewCacheDir,
'compiledSeparator' => '_',
'compileAlways' => true,
));
// Register view helpers
$compiler = $volt->getCompiler();
$compiler->addExtension(new \ViewHelper\Volt\Extension($di));
return $volt;
},
'.phtml' => 'Phalcon\Mvc\View\Engine\Php'
));
// Set default main layout.
$view->setMainView('layout');
return $view;
});
$di->setShared('assets', function () {
$manager = new AssetsManager();
// CSS
$manager->collection('css')
->setPrefix('css/')
->addCss('application.min.css');
// Javascript
$manager->collection('js')
->setPrefix('js/')
->addJs('jquery-3.0.0.min.js')
->addJs('bootstrap.min.js');
return $manager;
});
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->setShared('db', function () use ($di, $config) {
$dbConfig = $config->database->toArray();
$adapter = $dbConfig['adapter'];
unset($dbConfig['adapter']);
$class = 'Phalcon\Db\Adapter\Pdo\\' . $adapter;
$db = new $class($dbConfig);
$logger = $di->get('logger');
$eventsManager = new Phalcon\Events\Manager();
$eventsManager->attach('db', function ($event, $connection) use ($di, $logger) {
if ($event->getType() == 'beforeQuery') {
$logger->log($connection->getRealSQLStatement(), Logger::INFO);
}
});
$db->setEventsManager($eventsManager);
return $db;
});
/**
* If the configuration specify the use of metadata adapter use it or use memory otherwise
*/
$di->setShared('modelsMetadata', function () use ($config) {
// Use adapter and options from config if defined.
if (isset($config->cache->metadata)) {
$mdConfig = $config->cache->metadata->toArray();
$options = $mdConfig['options'];
$adapter = $mdConfig['adapter'];
$class = 'Phalcon\Mvc\Model\MetaData\\' . $adapter;
$metadata = new $class($options);
}
// Otherwise, default to Memory.
else {
$metadata = new Phalcon\Mvc\Model\MetaData\Memory();
}
return $metadata;
});
// Set the models cache service
/*
$di->set('modelsCache', function () {
// Cache data for one day by default
$frontCache = new FrontendDataCache(
array(
"lifetime" => 86400
)
);
// Memcached connection settings
$cache = new BackendApcCache(
$frontCache,
array(
'prefix' => 'httpcb_'
)
);
return $cache;
});*/
/**
* Start the session the first time some component request the session service
*/
$di->set('session', function () use ($config) {
// Set session directory if defined.
if (isset($config->application->sessionDir)) {
session_save_path($config->application->sessionDir);
}
// Create and start session.
$session = new SessionAdapter();
$session->start();
return $session;
});
$di->setShared('flash', function () {
return new Phalcon\Flash\Session();
});
$di->set('oauth', function() use ($config) {
return new OAuth($config);
});
$di->set('auth', function() use ($config) {
return new Auth\Auth($config);
});
$di->set('acl', function() {
return new Acl\Acl();
});
$di->set('menu', function() use ($di) {
$config = array(
'home' => array(
'caption' => 'Home',
'route' => 'home-route',
'controller' => 'index',
'action' => 'index',
),
'create-new ' => array(
'caption' => 'Create new',
'resource' => 'callback',
'controller' => 'callback',
'action' => 'new',
),
'my-callbacks' => array(
'caption' => 'List callbacks',
'resource' => 'callback',
'controller' => 'callback',
'action' => 'list',
'children' => array(
'show' => array(
'resource' => 'callback',
'controller' => 'callback',
'action' => 'show',
),
),
),
'about' => array(
'route' => 'about-route',
'caption' => 'About',
'controller' => 'index',
'action' => 'about'
),
);
$navigation = new Navigation($config);
$menu = new Menu($navigation);
$menu->setMenuClass(null);
if ($di->get('auth')->hasIdentity()) {
$menu->setAclRole(Acl\Acl::ROLE_USER);
} else {
$menu->setAclRole(Acl\Acl::ROLE_GUEST);
}
return $menu;
});