getMethods(\ReflectionMethod::IS_PROTECTED); foreach($methods as $method) { if (substr($method->getName(),0, 11) == '_initShared') { $service = lcfirst(substr($method->getName(), 11)); $this->setShared($service, $method->getClosure($this)); } else if (substr($method->getName(),0, 5) == '_init') { $service = lcfirst(substr($method->getName(), 5)); $this->set($service, $method->getClosure($this)); } } } protected function _initSharedConfig() { $config = new Config(APP_PATH . '/app/config/conf.app.yml'); try { $local = new Config(APP_PATH . '/app/config/conf.local.yml'); $config->merge($local); } catch(Phalcon\Config\Exception $e) { // Sometime went wrong. } return $config; } protected function _initSharedLoader() { $config = $this->get('config'); $loader = new \Phalcon\Loader(); $loader->registerNamespaces(array( 'App\Controller' => $config->application->controllersDir, 'App\Listener' => $config->application->listenersDir, 'App\Model' => $config->application->modelsDir, 'App\Form' => $config->application->formsDir, 'Httpcb' => $config->application->libraryDir, )); $loader->registerFiles(array(APP_PATH . '/vendor/autoload.php')); $loader->register(); return $loader; } protected function _initSharedDispatcher() { $config = $this->get('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; } /** * Database connection is created based in the parameters defined in the configuration file */ protected function _initSharedDb() { $dbConfig = $this->get('config')->database->toArray(); $logger = $this->get('logger'); $adapter = $dbConfig['adapter']; unset($dbConfig['adapter']); $class = 'Phalcon\Db\Adapter\Pdo\\' . $adapter; $db = new $class($dbConfig); $eventsManager = new \Phalcon\Events\Manager(); $eventsManager->attach('db', function ($event, $connection) use ($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. */ protected function _initSharedModelsMetadata() { $config = $this->get('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; } protected function _initSession() { $config = $this->get('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; } /** * Setting up the view component */ protected function _initSharedView() { $config = $this->get('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 ViewHelperVoltExtension($di)); return $volt; }, '.phtml' => 'Phalcon\Mvc\View\Engine\Php' )); // Set default main layout. $view->setMainView('layout'); return $view; } protected function _initSharedFlash() { return new \Phalcon\Flash\Session(); } protected function _initSharedAssets() { $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; } /** * Menu system * * @return \Httpcb\Menu */ protected function _initMenu() { $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 ($this->get('auth')->hasIdentity()) { $menu->setAclRole(Acl::ROLE_USER); } else { $menu->setAclRole(Acl::ROLE_GUEST); } return $menu; } /** * The URL component is used to generate all kind of urls in the application */ protected function _initSharedUrl() { $config = $this->get('config'); $url = new UrlResolver(); $url->setBaseUri($config->application->baseUri); return $url; } protected function _initSharedRouter() { // 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'); $router->add('/act/{link}', array( 'controller' => 'user', 'action' => 'activationlink', ))->setName('activation-link'); return $router; } protected function _initSharedEventsManager() { $activityLog = new ActivityLog(); $eventsManager = new \Phalcon\Events\Manager(); $eventsManager->attach('user', $activityLog); $eventsManager->attach('auth', $activityLog); return $eventsManager; } protected function _initAuth() { $auth = new Auth($this->get('config')); $auth->setEventsManager($this->get('eventsManager')); return $auth; } protected function _initAcl() { return new Acl(); } protected function _initSharedLogger() { $path = $this->get('config')->application->logDir; return new FileLogAdapter($path . "app.txt"); } protected function _initTemplate() { $config = $this->get('config'); $view = new Phalcon\Mvc\View\Simple(); $view->setViewsDir($config->application->templateDir); $view->registerEngines([ '.volt' => function ($view, $di) use ($config) { $volt = new VoltEngine($view, $di); $volt->setOptions(array( 'compiledPath' => $config->application->viewCacheDir, 'compiledSeparator' => '_', )); return $volt; }, '.phtml' => 'Phalcon\Mvc\View\Engine\Php' ]); return $view; } protected function _initSharedSendgrid() { return new SendGrid($this->get('config')->sendgrid->key); } protected function _initOauth($provider) { $config = $this->get('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); } }