Last active
December 17, 2015 09:19
-
-
Save phalcon/5586731 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <?php | |
| /** | |
| * PhalconEye | |
| * | |
| * LICENSE | |
| * | |
| * This source file is subject to the new BSD license that is bundled | |
| * with this package in the file LICENSE.txt. | |
| * | |
| * If you did not receive a copy of the license and are unable to | |
| * obtain it through the world-wide-web, please send an email | |
| * to phalconeye@gmail.com so we can send you a copy immediately. | |
| * | |
| */ | |
| namespace Engine; | |
| abstract class Bootstrap implements BootstrapInterface | |
| { | |
| /** | |
| * @var string | |
| */ | |
| protected $_moduleName = ""; | |
| /** | |
| * @var \Phalcon\Config | |
| */ | |
| protected $_config; | |
| /** | |
| * @var \Phalcon\DiInterface | |
| */ | |
| protected $_di; | |
| public function __construct() | |
| { | |
| $this->_di = \Phalcon\DI::getDefault(); | |
| $this->_config = $this->_di->get('config'); | |
| } | |
| public static function dependencyInjection(\Phalcon\DiInterface $di){ | |
| } | |
| public function registerAutoloaders() | |
| { | |
| } | |
| /** | |
| * Register the services here to make them general or register in the ModuleDefinition to make them module-specific | |
| */ | |
| public function registerServices($di) | |
| { | |
| if (empty($this->_moduleName)) { | |
| $class = new \ReflectionClass($this); | |
| throw new \Engine\Exception('Bootstrap has no module name: ' . $class->getFileName()); | |
| } | |
| $moduleDirectory = $this->getModuleDirectory(); | |
| //Create an event manager | |
| $eventsManager = new EventsManager($this->_config); | |
| /*************************************************/ | |
| // Initialize view | |
| /*************************************************/ | |
| $di->set('view', function () use ($moduleDirectory, $eventsManager) { | |
| $view = new \Phalcon\Mvc\View(); | |
| $view->setViewsDir($moduleDirectory . '/View/'); | |
| $view->registerEngines(array( | |
| ".volt" => function ($view, $di) { | |
| $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di); | |
| $config = $di->get('config'); | |
| $volt->setOptions(array( | |
| "compiledPath" => $config->application->view->compiledPath, | |
| "compiledExtension" => $config->application->view->compiledExtension, | |
| 'compiledSeparator' => '_', | |
| 'compileAlways' => $config->application->debug | |
| )); | |
| $compiler = $volt->getCompiler(); | |
| //register helper | |
| $compiler->addFunction('helper', function ($resolvedArgs) use ($di) { | |
| return '(new \Engine\Helper(' . $resolvedArgs . '))'; | |
| }); | |
| // register translation filter | |
| $compiler->addFilter('trans', function ($resolvedArgs) { | |
| return '$this->getDI()->get("trans")->query(' . $resolvedArgs . ')'; | |
| }); | |
| return $volt; | |
| } | |
| )); | |
| //Attach a listener for type "view" | |
| if (!$config->application->debug) { | |
| $eventsManager->attach("view", function ($event, $view) { | |
| if ($event->getType() == 'notFoundView') { | |
| \Phalcon\DI::getDefault()->get('logger')->error('View not found - "' . $view->getActiveRenderPath().'"'); | |
| } | |
| }); | |
| $view->setEventsManager($eventsManager); | |
| } | |
| return $view; | |
| }); | |
| /*************************************************/ | |
| // Initialize dispatcher | |
| /*************************************************/ | |
| if (!$config->application->debug) { | |
| $eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) { | |
| //Alternative way, controller or action doesn't exist | |
| if ($event->getType() == 'beforeException') { | |
| switch ($exception->getCode()) { | |
| case \Phalcon\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: | |
| case \Phalcon\Dispatcher::EXCEPTION_ACTION_NOT_FOUND: | |
| $dispatcher->forward(array( | |
| 'module' => Application::$defaultModule, | |
| 'namespace' => ucfirst(Application::$defaultModule) . '\Controller', | |
| 'controller' => 'Error', | |
| 'action' => 'show404' | |
| )); | |
| return false; | |
| } | |
| } | |
| }); | |
| //This is only in debug mode? | |
| $eventsManager->attach('dispatch', new \Engine\Plugin\CacheAnnotation()); | |
| } | |
| /** | |
| * Listening to events in the dispatcher using the | |
| * Acl plugin | |
| */ | |
| $eventsManager->attach('dispatch', $di->get(Application::$defaultModule)->acl()); | |
| // Create dispatcher | |
| $dispatcher = new \Phalcon\Mvc\Dispatcher(); | |
| $dispatcher->setEventsManager($eventsManager); | |
| $di->set('dispatcher', $dispatcher); | |
| } | |
| public function getModuleName() | |
| { | |
| return $this->_moduleName; | |
| } | |
| public function getModuleDirectory() | |
| { | |
| return $this->_config->application->modulesDir . $this->_moduleName; | |
| } | |
| } |
This file contains hidden or 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
| <?php | |
| /** | |
| * PhalconEye | |
| * | |
| * LICENSE | |
| * | |
| * This source file is subject to the new BSD license that is bundled | |
| * with this package in the file LICENSE.txt. | |
| * | |
| * If you did not receive a copy of the license and are unable to | |
| * obtain it through the world-wide-web, please send an email | |
| * to lantian.ivan@gmail.com so we can send you a copy immediately. | |
| * | |
| */ | |
| namespace Engine; | |
| class EventsManager extends \Phalcon\Events\Manager | |
| { | |
| public function __construct($config) | |
| { | |
| // Attach modules plugins events | |
| $modules = $config->get('events')->toArray(); | |
| $loadedModules = $config->modules->toArray(); | |
| if (!empty($modules)){ | |
| foreach ($modules as $module => $events) { | |
| if (!in_array($module, $loadedModules)) { | |
| continue; | |
| } | |
| foreach ($events as $event) { | |
| $pluginClass = $event['namespace'] . '\\' . $event['class']; | |
| $this->attach($event['type'], new $pluginClass()); | |
| } | |
| } | |
| } | |
| // Attach plugins events | |
| $plugins = $config->get('plugins'); | |
| if (!empty($plugins)) { | |
| foreach($plugins as $pluginName => $plugin) { | |
| if (!$plugin['enabled'] || empty($plugin['events']) || !is_array($plugin['events'])) { | |
| continue; | |
| } | |
| $pluginClass = '\Plugin\\'.ucfirst($pluginName) . '\\' .ucfirst($pluginName); | |
| foreach ($plugin['events'] as $event) { | |
| $this->attach($event, new $pluginClass()); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment