371 lines
9.7 KiB
PHP
371 lines
9.7 KiB
PHP
<?php
|
|
/**
|
|
* Services are globally registered in this file
|
|
*
|
|
* @var \Phalcon\Config $config
|
|
*/
|
|
|
|
|
|
|
|
use Phalcon\Di\FactoryDefault,
|
|
Phalcon\Mvc\View,
|
|
Phalcon\Assets\Manager as AssetsManager,
|
|
Phalcon\Mvc\Url as UrlResolver,
|
|
Phalcon\Mvc\View\Engine\Volt as VoltEngine,
|
|
Phalcon\Flash\Direct as FlashDirect,
|
|
Phalcon\Mvc\Model\Metadata\Memory as MemoryMetaData,
|
|
Phalcon\Mvc\Model\MetaData\Apc as ApcMetaData,
|
|
Phalcon\Cache\Frontend\Data as FrontendDataCache,
|
|
Phalcon\Cache\Backend\Apc as BackendApcCache,
|
|
Phalcon\Translate\Adapter\NativeArray as TranslateAdapter,
|
|
Phalcon\Logger,
|
|
Phalcon\Logger\Adapter\File as FileLogAdapter,
|
|
Phalcon\Session\Adapter\Files as SessionAdapter,
|
|
Phalcon\Mvc\Router;
|
|
|
|
use Httpcb\Auth,
|
|
Httpcb\OAuth\Client as OAuthClient,
|
|
Httpcb\OAuth\Adapter\League as OAuthAdapter,
|
|
Httpcb\Acl,
|
|
Httpcb\Navigation,
|
|
Httpcb\Menu;
|
|
|
|
use App\Listener\AclListener,
|
|
App\Listener\DispatchListener,
|
|
App\Listener\ActivityLog;
|
|
|
|
/**
|
|
* The FactoryDefault Dependency Injector automatically register the right services providing a full stack framework
|
|
*/
|
|
$di = new FactoryDefault();
|
|
|
|
/**
|
|
* Setup logging
|
|
*/
|
|
$di->setShared('logger', function() use ($config) {
|
|
|
|
//return new Phalcon\Logger\Adapter\Firephp("");
|
|
$path = $config->application->logDir;
|
|
return new FileLogAdapter($path . "app.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 DispatchListener());
|
|
}
|
|
|
|
$eventsManager->attach('dispatch:beforeExecuteRoute', new AclListener());
|
|
|
|
$dispatcher = new \Phalcon\Mvc\Dispatcher();
|
|
$dispatcher->setEventsManager($eventsManager);
|
|
|
|
$dispatcher->setDefaultNamespace('App\Controller');
|
|
|
|
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('/login/{strategy:([a-z]+)}/:params', array(
|
|
'controller' => 'auth',
|
|
'action' => 'oauth'
|
|
))->setName('oauth');
|
|
|
|
$router->add('/oauth/{provider:([a-z]+)}/disconnect', 'User::oauthdisconnect')->setName('oauth-disconnect');
|
|
$router->add('/oauth/{provider:([a-z]+)}/disconnect/{confirm}', 'User::oauthdisconnect')->setName('oauth-disconnect-confirm');
|
|
|
|
$router->add('/settings', array(
|
|
'controller' => 'user',
|
|
'action' => 'settings',
|
|
))->setName('user-settings');
|
|
|
|
return $router;
|
|
});
|
|
|
|
$di->setShared('viewHelper', 'Httpcb\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 Httpcb\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')
|
|
->addJs('fontawesome.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;
|
|
});
|
|
|
|
$di->setShared('eventsManager', function () {
|
|
|
|
$activityLog = new ActivityLog();
|
|
|
|
$eventsManager = new Phalcon\Events\Manager();
|
|
$eventsManager->attach('user', $activityLog);
|
|
$eventsManager->attach('auth', $activityLog);
|
|
|
|
return $eventsManager;
|
|
});
|
|
|
|
/**
|
|
* 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($provider) use ($config) {
|
|
|
|
if (isset($config->oauth->providers->{$provider})) {
|
|
$options = $config->oauth->providers->{$provider};
|
|
$options = $options->toArray();
|
|
} else {
|
|
$options = array();
|
|
}
|
|
|
|
$adapter = new OAuthAdapter($provider, $options);
|
|
|
|
return new OAuthClient($adapter);
|
|
});
|
|
|
|
$di->set('auth', function() use ($di, $config) {
|
|
$auth = new Auth($config);
|
|
$auth->setEventsManager($di->get('eventsManager'));
|
|
|
|
return $auth;
|
|
});
|
|
|
|
$di->set('acl', 'Httpcb\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::ROLE_USER);
|
|
} else {
|
|
$menu->setAclRole(Acl::ROLE_GUEST);
|
|
}
|
|
return $menu;
|
|
});
|