private function setUpDispatcher()
 {
     $this->dispatcher->setControllerName($this->router->getControllerName());
     $this->dispatcher->setActionName($this->router->getActionName());
     $this->dispatcher->setParams($this->router->getParams());
     $oDispatcherEventManager = new Manager();
     $oDispatcherEventManager->attach('dispatch:beforeDispatch', function (Event $oEvent, Dispatcher $oDispatcher, $data) {
         return false;
     });
     $this->dispatcher->setEventsManager($oDispatcherEventManager);
 }
Exemple #2
0
 /**
  * Tests the creation of the log file
  *
  * @author Nikos Dimopoulos <*****@*****.**>
  * @since  2012-11-30
  */
 public function testDbLoggerDefault()
 {
     $this->populateTable('customers', 10);
     $fileName = $this->getFileName('log', 'log');
     $evman = new PhEventsManager();
     $listener = new Listener(PATH_LOGS . $fileName);
     $evman->attach('db', $listener);
     $connection = $this->di->get('db');
     $connection->setEventsManager($evman);
     $connection->query("SELECT * FROM customers LIMIT 3");
     $connection->query("SELECT * FROM customers LIMIT 10");
     $connection->query("SELECT * FROM customers LIMIT 1");
     $this->assertTrue($connection->close());
     $listener->getLogger()->close();
     $lines = file(PATH_LOGS . $fileName);
     $this->assertEquals(count($lines), 3);
     $this->assertTrue(strpos($lines[0], '[DEBUG]') !== false);
     $this->assertTrue(strpos($lines[0], 'LIMIT 3') !== false);
     $this->assertTrue(strpos($lines[1], '[DEBUG]') !== false);
     $this->assertTrue(strpos($lines[1], 'LIMIT 10') !== false);
     $this->assertTrue(strpos($lines[2], '[DEBUG]') !== false);
     $this->assertTrue(strpos($lines[2], 'LIMIT 1') !== false);
     $this->cleanFile(PATH_LOGS, $fileName);
     //$this->assertTrue($actual, 'File was not correctly created');
 }
 public function registerServices($di)
 {
     /** Read configuration */
     $config = (include __DIR__ . "/../configs/config.php");
     $di['dispatcher'] = function () use($di) {
         $eventsManager = new EventsManager();
         $dispatcher = new \Phalcon\Mvc\Dispatcher();
         $dispatcher->setDefaultNamespace("Modules\\Users\\Controllers");
         $eventsManager->attach('dispatch:beforeException', new NotFoundPlugin($di));
         $dispatcher->setEventsManager($eventsManager);
         return $dispatcher;
     };
     /** Setting up the view component */
     $di['view'] = function () {
         $view = new \Phalcon\Mvc\View();
         $view->setViewsDir(__DIR__ . '/views/');
         $view->setLayoutsDir('../../common/layouts/');
         $view->setPartialsDir('../../common/partials/');
         $view->setTemplateAfter('users');
         return $view;
     };
     /** Database connection is created based in the parameters defined in the configuration file */
     $di['db'] = function () use($config) {
         return new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->name, "charset" => "utf8", "options" => array(\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")));
     };
 }
Exemple #4
0
 public function boot()
 {
     $dispatcher = di()->get('dispatcher');
     $event_manager = new EventsManager();
     $event_manager->attach('dispatch', new DispatcherEventListener());
     $dispatcher->setEventsManager($event_manager);
 }
 /**
  * Initialize Phalcon .
  */
 public function __construct()
 {
     $executionTime = -microtime(true);
     define('APP_PATH', realpath('..') . '/');
     $this->config = $config = new ConfigIni(APP_PATH . 'app/config/config.ini');
     $this->di = new FactoryDefault();
     $this->di->set('config', $config);
     $this->di->set('dispatcher', function () {
         $eventsManager = new EventsManager();
         $eventsManager->attach('dispatch:beforeExecuteRoute', new SecurityPlugin());
         $dispatcher = new Dispatcher();
         $dispatcher->setEventsManager($eventsManager);
         return $dispatcher;
     });
     $this->di->set('crypt', function () use($config) {
         $crypt = new Crypt();
         $crypt->setKey($config->application->phalconCryptKey);
         return $crypt;
     });
     $this->setDisplayErrors();
     $this->title = $config->application->title;
     $this->registerDirs();
     $this->setDB($config);
     $this->setViewProvider($config);
     $this->setURLProvider($config);
     $this->setSession();
     $this->application = new Application($this->di);
     $this->application->view->executionTime = $executionTime;
     $this->setCSSCollection();
     $this->setJSCollection();
     $this->setTitle();
 }
Exemple #6
0
 /**
  * 注册Profiler,监听DB
  * @param   $dblist 监听的db列表,默认监听db
  * @return [type] [description]
  */
 public static function registerDbprofiler($dblist = array('db'))
 {
     if (DI::getDefault()['request']->hasQuery('debug') && APP_ENV != 'product') {
         $profiler = new Profiler();
         $di = DI::getDefault();
         $di->setShared('profiler', function () use($profiler) {
             return $profiler;
         });
         foreach ($dblist as $value) {
             $eventsManager = new Manager();
             $connection = $di[$value];
             $eventsManager->attach('db', function ($event, $connection) use($profiler) {
                 //一条语句查询之前事件,profiler开始记录sql语句
                 if ($event->getType() == 'beforeQuery') {
                     $profiler->startProfile($connection->getSQLStatement());
                 }
                 //一条语句查询结束,结束本次记录,记录结果会保存在profiler对象中
                 if ($event->getType() == 'afterQuery') {
                     $profiler->stopProfile();
                 }
             });
             //将事件管理器绑定到db实例中
             $connection->setEventsManager($eventsManager);
         }
     }
 }
Exemple #7
0
 /**
  * Registers the module-only services
  *
  * @param Phalcon\DI $di
  */
 public function registerServices($di)
 {
     /**
      * Read configuration
      */
     $config = (include __DIR__ . "/config/config.php");
     $di['view']->setViewsDir(__DIR__ . '/views/');
     /**
      * Database connection is created based in the parameters defined in the configuration file
      */
     $di['db'] = function () use($config) {
         $connection = new DbAdapter(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname));
         $eventsManager = new EventsManager();
         $logger = new FileLogger(__DIR__ . "/logs/db.log");
         //Listen all the database events
         $eventsManager->attach('db:beforeQuery', function ($event, $connection) use($logger) {
             $sqlVariables = $connection->getSQLVariables();
             if (count($sqlVariables)) {
                 $logger->log($connection->getSQLStatement() . ' ' . join(', ', $sqlVariables), Logger::INFO);
             } else {
                 $logger->log($connection->getSQLStatement(), Logger::INFO);
             }
         });
         //Assign the eventsManager to the db adapter instance
         $connection->setEventsManager($eventsManager);
         return $connection;
     };
 }
Exemple #8
0
 public function boot()
 {
     $app = di()->get('application');
     $event_manager = new EventsManager();
     $event_manager->attach('application', new ApplicationEventListener());
     $app->setEventsManager($event_manager);
 }
Exemple #9
0
 /**
  * Create view instance.
  * If no events manager provided - events would not be attached.
  *
  * @param DIBehaviour  $di             DI.
  * @param Config       $config         Configuration.
  * @param string       $viewsDirectory Views directory location.
  * @param Manager|null $em             Events manager.
  *
  * @return View
  */
 public static function factory($di, $config, $viewsDirectory, $em = null)
 {
     $view = new PhView();
     $volt = new PhVolt($view, $di);
     $volt->setOptions(["compiledPath" => $config->global->view->compiledPath, "compiledExtension" => $config->global->view->compiledExtension, 'compiledSeparator' => $config->global->view->compiledSeparator, 'compileAlways' => $config->global->view->compileAlways]);
     $compiler = $volt->getCompiler();
     $compiler->addExtension(new EnViewExtension());
     $compiler->addFilter('floor', 'floor');
     $compiler->addFunction('range', 'range');
     $compiler->addFunction('in_array', 'in_array');
     $compiler->addFunction('count', 'count');
     $compiler->addFunction('str_repeat', 'str_repeat');
     $view->registerEngines(['.volt' => $volt])->setRenderLevel(PhView::LEVEL_ACTION_VIEW)->setViewsDir($viewsDirectory);
     // Attach a listener for type "view".
     if ($em) {
         $em->attach("view", function ($event, $view) use($di, $config) {
             if ($config->global->profiler && $di->has('profiler')) {
                 if ($event->getType() == 'beforeRender') {
                     $di->get('profiler')->start();
                 }
                 if ($event->getType() == 'afterRender') {
                     $di->get('profiler')->stop($view->getActiveRenderPath(), 'view');
                 }
             }
             if ($event->getType() == 'notFoundView') {
                 throw new Exception('View not found - "' . $view->getActiveRenderPath() . '"');
             }
         });
         $view->setEventsManager($em);
     }
     $di->set('view', $view);
     return $view;
 }
Exemple #10
0
 /**
  * Prepares component
  *
  * @param stdClass $database
  */
 public static function setup($database)
 {
     if (!isset($database->adapter)) {
         throw new \Phalcon\Exception('Unspecified database Adapter in your configuration!');
     }
     $adapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . $database->adapter;
     if (!class_exists($adapter)) {
         throw new \Phalcon\Exception('Invalid database Adapter!');
     }
     $configArray = $database->toArray();
     unset($configArray['adapter']);
     self::$_connection = new $adapter($configArray);
     self::$_databaseConfig = $database;
     $profiler = new Profiler();
     $eventsManager = new EventsManager();
     $eventsManager->attach('db', function ($event, $connection) use($profiler) {
         if ($event->getType() == 'beforeQuery') {
             $profiler->startProfile($connection->getSQLStatement());
         }
         if ($event->getType() == 'afterQuery') {
             $profiler->stopProfile();
         }
     });
     self::$_connection->setEventsManager($eventsManager);
 }
Exemple #11
0
 /**
  * Create view instance.
  * If no events manager provided - events would not be attached.
  *
  * @param DIBehaviour  $di             DI.
  * @param Config       $config         Configuration.
  * @param string|null  $viewsDirectory Views directory location.
  * @param Manager|null $em             Events manager.
  *
  * @return View
  */
 public static function factory($di, $config, $viewsDirectory = null, $em = null)
 {
     $view = new View();
     $volt = new Volt($view, $di);
     $volt->setOptions(["compiledPath" => $config->application->view->compiledPath, "compiledExtension" => $config->application->view->compiledExtension, 'compiledSeparator' => $config->application->view->compiledSeparator, 'compileAlways' => $config->application->debug && $config->application->view->compileAlways]);
     $compiler = $volt->getCompiler();
     $compiler->addExtension(new Extension());
     $view->registerEngines([".volt" => $volt])->setRenderLevel(View::LEVEL_ACTION_VIEW)->restoreViewDir();
     if (!$viewsDirectory) {
         $view->setViewsDir($viewsDirectory);
     }
     // Attach a listener for type "view".
     if ($em) {
         $em->attach("view", function ($event, $view) use($di, $config) {
             if ($config->application->profiler && $di->has('profiler')) {
                 if ($event->getType() == 'beforeRender') {
                     $di->get('profiler')->start();
                 }
                 if ($event->getType() == 'afterRender') {
                     $di->get('profiler')->stop($view->getActiveRenderPath(), 'view');
                 }
             }
             if ($event->getType() == 'notFoundView') {
                 throw new Exception('View not found - "' . $view->getActiveRenderPath() . '"');
             }
         });
         $view->setEventsManager($em);
     }
     return $view;
 }
Exemple #12
0
 /**
  * Register the services here to make them general
  * or register in the ModuleDefinition to make them module-specific
  */
 public function registerServices(DiInterface $di)
 {
     //Read configuration
     $config = (include __DIR__ . "/config/config.php");
     // The URL component is used to generate all kind of urls in the application
     $di->set('url', function () use($config) {
         $url = new Url();
         $url->setBaseUri($config->application->baseUri);
         return $url;
     });
     //Registering a dispatcher
     $di->set('dispatcher', function () {
         //Create/Get an EventManager
         $eventsManager = new EventsManager();
         //Attach a listener
         $eventsManager->attach('dispatch', function ($event, $dispatcher, $exception) {
             //controller or action doesn't exist
             if ($event->getType() == 'beforeException') {
                 switch ($exception->getCode()) {
                     case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                     case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                         $dispatcher->forward(['module' => 'backend', 'controller' => 'errors', 'action' => 'notFound']);
                         return false;
                 }
             }
         });
         $dispatcher = new Dispatcher();
         $dispatcher->setDefaultNamespace("Phanbook\\Backend\\Controllers");
         $dispatcher->setEventsManager($eventsManager);
         return $dispatcher;
     });
     /**
      * Setting up the view component
      */
     $di->set('view', function () use($config) {
         $view = new View();
         $view->setViewsDir($config->application->viewsDir);
         $view->disableLevel([View::LEVEL_MAIN_LAYOUT => true, View::LEVEL_LAYOUT => true]);
         $view->registerEngines(['.volt' => 'volt']);
         // Create an event manager
         $eventsManager = new EventsManager();
         // Attach a listener for type 'view'
         $eventsManager->attach('view', function ($event, $view) {
             if ($event->getType() == 'notFoundView') {
                 throw new \Exception('View not found!!! (' . $view->getActiveRenderPath() . ')');
             }
         });
         // Bind the eventsManager to the view component
         $view->setEventsManager($eventsManager);
         return $view;
     });
     $configMenu = (include __DIR__ . "/config/config.menu.php");
     $di->setShared('menuStruct', function () use($configMenu) {
         // if structure received from db table instead getting from $config
         // we need to store it to cache for reducing db connections
         $struct = $configMenu->get('menuStruct')->toArray();
         return $struct;
     });
 }
Exemple #13
0
 /**
  * Override events manager default in Phalcon
  * @return \Phalex\Di\Di
  */
 protected function setEventsManager()
 {
     $ev = new EventsManager();
     $ev->enablePriorities(true);
     $ev->collectResponses(true);
     $this->set('eventsManager', $ev, true);
     return $this;
 }
Exemple #14
0
 /**
  * Registers the module-only services
  *
  * @param Phalcon\DI $di
  */
 public function registerServices(\Phalcon\DiInterface $di = NULL)
 {
     /**
      * Read configuration
      */
     $config = (include __DIR__ . "/config/config.php");
     $authConfig = (include __DIR__ . "/config/authConfig.php");
     /**
      * Setting up the view component
      */
     $di->set('dispatcher', function () {
         $eventsManager = new EventsManager();
         $eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
             //Handle 404 exceptions
             if ($exception instanceof DispatchException) {
                 $dispatcher->forward(array('controller' => 'public', 'action' => 'error404'));
                 return false;
             }
             //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('controller' => 'public', 'action' => 'error404'));
                         return false;
                 }
             }
         });
         $dispatcher = new \Phalcon\Mvc\Dispatcher();
         $dispatcher->setEventsManager($eventsManager);
         $dispatcher->setDefaultNamespace("Mall\\Mall\\Controllers");
         return $dispatcher;
     });
     $di['view'] = function () {
         $view = new View();
         $view->setViewsDir(__DIR__ . '/views/');
         return $view;
     };
     /**
      * Database connection is created based in the parameters defined in the configuration file
      */
     $di['db'] = function () use($config) {
         return new Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, "options" => array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'", \PDO::ATTR_CASE => \PDO::CASE_LOWER, \PDO::ATTR_EMULATE_PREPARES => false)));
     };
     $di->set('cookies', function () {
         $cookies = new \Phalcon\Http\Response\Cookies();
         $cookies->useEncryption(false);
         return $cookies;
     });
     $di->set('authConfig', function () use($authConfig) {
         return $authConfig;
     });
     $di->set('casLoginUrl', function () {
         $config = (include __DIR__ . "/../utils/cas/config/webpcConfig.php");
         $backurl = $config['domain'] . '/index/vali?forward=' . $config['domain'] . $_SERVER['REQUEST_URI'];
         return $config['loginUrl'] . '?siteid=' . $config['siteid'] . '&backurl=' . urlencode($backurl);
     });
 }
Exemple #15
0
 /**
  * Registers the module-only services
  *
  * @param Phalcon\DI $di
  */
 public function registerServices(\Phalcon\DiInterface $di = NULL)
 {
     /**
      * Read configuration
      */
     $config = (include __DIR__ . "/config/config.php");
     $sdkconfig = (include __DIR__ . "/config/sdkConfig.php");
     /**
      * Setting up the view component
      */
     $di['view'] = function () {
         $view = new View();
         $view->setViewsDir(__DIR__ . '/views/');
         return $view;
     };
     /**
      * Database connection is created based in the parameters defined in the configuration file
      */
     $di['db'] = function () use($config) {
         return new Mysql(array("host" => $config->database->host, "username" => $config->database->username, "password" => $config->database->password, "dbname" => $config->database->dbname, "options" => array(\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'", \PDO::ATTR_CASE => \PDO::CASE_LOWER)));
     };
     /**
      * Setting up the view component
      */
     $di->set('dispatcher', function () {
         $eventsManager = new EventsManager();
         $eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
             //Handle 404 exceptions
             if ($exception instanceof DispatchException) {
                 $dispatcher->forward(array('controller' => 'public', 'action' => 'error404'));
                 return false;
             }
             //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('controller' => 'public', 'action' => 'error404'));
                         return false;
                 }
             }
         });
         $dispatcher = new \Phalcon\Mvc\Dispatcher();
         $dispatcher->setEventsManager($eventsManager);
         $dispatcher->setDefaultNamespace("Ucenter\\Webpc\\Controllers");
         return $dispatcher;
     });
     $di['sdkconfig'] = function () use($sdkconfig) {
         return $sdkconfig;
     };
     $di->set('cookies', function () {
         $cookies = new \Phalcon\Http\Response\Cookies();
         $cookies->useEncryption(false);
         return $cookies;
     });
 }
Exemple #16
0
 /**
  * Registers services related to the module
  *
  * @param DiInterface $dependencyInjector
  */
 public function registerServices(DiInterface $dependencyInjector)
 {
     /**
      * Read configuration
      */
     $config = (include __DIR__ . "/config/config.php");
     /**
      * Registering a dispatcher
      */
     $dependencyInjector->set('dispatcher', function () {
         $dispatcher = new Dispatcher();
         $dispatcher->setDefaultNamespace('Frontend\\Controllers');
         /**
          * Not-found action or handler
          */
         $eventsManager = new EventsManager();
         $eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
             switch ($exception->getCode()) {
                 case Dispatcher::EXCEPTION_CYCLIC_ROUTING:
                 case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                 case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                     $dispatcher->forward(['controller' => 'about', 'action' => 'error']);
                     return false;
             }
         });
         $dispatcher->setEventsManager($eventsManager);
         return $dispatcher;
     });
     /**
      * Setting up the view component
      */
     $dependencyInjector->set('view', function () {
         $view = new View();
         $view->registerEngines(array('.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
         $view->setViewsDir(__DIR__ . '/views/');
         return $view;
     });
     $dependencyInjector->set('viewCache', function () use($config) {
         //Cache data for one day by default
         $frontCache = new OutputFrontend(array("lifetime" => 86400));
         //File connection settings
         $cache = new FileBackend($frontCache, array('cacheDir' => STATIC_PATH . '/'));
         return $cache;
     });
     $dependencyInjector->set('cookies', function () {
         $cookies = new Cookies();
         $cookies->useEncryption(false);
         return $cookies;
     });
     /**
      * Database connection is created based in the parameters defined in the configuration file
      */
     $dependencyInjector->set('db', function () use($config) {
         return new DbAdapter($config->database->toArray());
     });
 }
Exemple #17
0
 protected function initialize()
 {
     $eventsManager = new EventsManager();
     $eventsManager->attach("dispatch", function ($event, $dispatcher) {
     });
     //Bind the eventsManager to the view component
     $this->setEventsManager($eventsManager);
     $this->setDefaultNamespace('HashTag\\Controllers');
     return $this;
 }
Exemple #18
0
 /**
  *
  * @param Di $diFactory
  */
 public function __construct(Di $diFactory)
 {
     $this->diFactory = $diFactory;
     $this->loader = new Loader();
     $ev = new EventsManager();
     $ev->enablePriorities(true);
     $ev->collectResponses(true);
     $diFactory->set('autoloaderEventsManager', $ev, true);
     $this->loader->setEventsManager($ev);
 }
 /**
  * Registers the Dispatcher in the DI Container.
  * 
  * @return void
  */
 public function register()
 {
     $this->di->setShared('dispatcher', function () {
         $dispatcher = new MVCDispatcher();
         $this->setDefaultNamespace($dispatcher);
         $dispatcher->setEventsManager($eventsManager = new EventsManager());
         $eventsManager->attach('dispatch', new DispatcherListener($this->di->get('router')));
         return $dispatcher;
     });
 }
 public function boot()
 {
     $event_manager = new Manager();
     $event_manager->attach('view:afterRender', function (Event $event, View $dispatcher, $exception) {
         $flash = $dispatcher->getDI()->get('flash');
         $flash->destroy();
     });
     di()->get('view')->setEventsManager($event_manager);
     return $this;
 }
 /**
  * {@inheridoc}.
  */
 public function boot()
 {
     $dispatcher = di()->get('dispatcher');
     $event_manager = new EventsManager();
     $event_manager->attach('dispatch:beforeException', function ($event, $dispatcher, $exception) {
         if ($exception instanceof DispatchException) {
             throw new ControllerNotFoundException($exception->getMessage());
         }
     });
     $dispatcher->setEventsManager($event_manager);
 }
Exemple #22
0
 public function __construct(Di $di)
 {
     parent::__construct();
     $this->di = $di;
     $eventsManager = new EventsManager();
     $eventsManager->enablePriorities(true);
     $eventsManager->collectResponses(true);
     $di->set('eventsManager', $eventsManager, true);
     $this->setEventsManager($eventsManager);
     $this->setDI($di);
     $this->setDefaultAction('index');
     $this->setDefaultController('index');
 }
Exemple #23
0
 /**
  * Register the services here to make them general
  * or register in the ModuleDefinition to make them module-specific
  */
 public function registerServices(DiInterface $di)
 {
     /**
      * Read configuration
      */
     //$config = include __DIR__ . "/config/config.php";
     //Registering a dispatcher
     $di->set('dispatcher', function () {
         $eventsManager = new EventsManager();
         $eventsManager->attach("dispatch", function ($event, $dispatcher, $exception) {
             //controller or action doesn't exist
             $object = $event->getData();
             if ($event->getType() == 'beforeException') {
                 switch ($exception->getCode()) {
                     case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                     case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                         $dispatcher->forward(['controller' => 'errors', 'action' => 'index']);
                         return false;
                     case Dispatcher::EXCEPTION_CYCLIC_ROUTING:
                         $dispatcher->forward(['controller' => 'errors', 'action' => 'show404']);
                         return false;
                 }
             }
         });
         $dispatcher = new Dispatcher();
         $dispatcher->setDefaultNamespace("Phanbook\\Frontend\\Controllers");
         $dispatcher->setEventsManager($eventsManager);
         return $dispatcher;
     });
     /**
      * Setting up the view component
      */
     $di->set('view', function () use($di) {
         $config = $di->get('config');
         $view = new View();
         $view->setViewsDir(ROOT_DIR . 'content/themes/' . $config->theme);
         $view->disableLevel([View::LEVEL_MAIN_LAYOUT => true, View::LEVEL_LAYOUT => true]);
         $view->registerEngines(['.volt' => 'volt']);
         // Create an event manager
         $eventsManager = new EventsManager();
         $eventsManager->attach('view', function ($event, $view) {
             if ($event->getType() == 'notFoundView') {
                 throw new \Exception('View not found!!! (' . $view->getActiveRenderPath() . ')');
             }
         });
         // Bind the eventsManager to the view component
         $view->setEventsManager($eventsManager);
         return $view;
     });
 }
 public function initialize()
 {
     $eventsManager = new EventsManager();
     // Attach an anonymous function as a listener for "model" events
     $eventsManager->attach('model', function ($event, $robot) {
         if ($event->getType() == 'beforeCreate') {
             $uuid = $this->getDI()->getUtils()->uuid();
             $this->uuid = hex2bin($uuid);
         }
         return true;
     });
     // Attach the events manager to the event
     $this->setEventsManager($eventsManager);
 }
Exemple #25
0
 public function register()
 {
     $event_manager = new EventsManager();
     $event_manager->attach('dispatch:beforeException', function ($event, $dispatcher, $exception) {
         if ($exception instanceof DispatchException) {
             throw new ControllerNotFoundException($exception->getMessage());
             return false;
         }
     });
     $dispatcher = new MvcDispatcher();
     $dispatcher->setEventsManager($event_manager);
     $dispatcher->setDefaultNamespace('App\\Controllers');
     return $dispatcher;
 }
 public function initialize()
 {
     $eventsManager = new EventsManager();
     //Attach an anonymous function as a listener for "model" events
     $eventsManager->attach('model', function ($event, $robot) {
         if ($event->getType() == 'beforeSave') {
             if ($robot->name == 'Scooby Doo') {
                 echo "Scooby Doo isn't a robot!";
                 return false;
             }
         }
         return true;
     });
     //Attach the events manager to the event
     $this->setEventsManager($eventsManager);
 }
Exemple #27
0
 public function initPersistentDB($di)
 {
     // Setup the database service
     $di->set('db', function () {
         $eventsManager = new EventsManager();
         $logger = new FileLogger(__DIR__ . '/' . date('Y-m-d') . '.sql.log');
         $eventsManager->attach('db', function ($event, $connection) use($logger) {
             if ($event->getType() == 'beforeQuery') {
                 $logger->info($connection->getSQLStatement());
             }
         });
         $db = new \Phalcon\Db\Adapter\Pdo\Mysql(array("host" => "127.0.0.1", "username" => "root", "password" => "123456", "dbname" => "rookie", "charset" => "utf8", "persistent" => true));
         $db->setEventsManager($eventsManager);
         return $db;
     });
 }
Exemple #28
0
 /**
  * Register all event listener from config.
  *
  * @return mixed
  */
 public function registerEvents()
 {
     $listeners = $this->config['event'];
     foreach ($listeners as $name => $listener) {
         $listener = '\\' . $listener;
         $this->eventManager->attach($name, new $listener());
     }
     return;
 }
Exemple #29
0
 /**
  * Registers services related to the module
  *
  * @param DiInterface $dependencyInjector
  */
 public function registerServices(DiInterface $dependencyInjector)
 {
     /**
      * Read configuration
      */
     $config = (include __DIR__ . "/config/config.php");
     /**
      * Registering a dispatcher
      */
     $dependencyInjector->set('dispatcher', function () {
         $dispatcher = new Dispatcher();
         $dispatcher->setDefaultNamespace('Api\\Controllers');
         /**
          * Not-found action or handler
          */
         $eventsManager = new EventsManager();
         $eventsManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
             switch ($exception->getCode()) {
                 case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
                 case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
                     $dispatcher->forward(['controller' => 'error', 'action' => 'error404']);
                     return false;
             }
         });
         $dispatcher->setEventsManager($eventsManager);
         return $dispatcher;
     });
     /**
      * Setting up the view component
      */
     $dependencyInjector->set('view', function () {
         $view = new View();
         $view->registerEngines(array('.phtml' => 'Phalcon\\Mvc\\View\\Engine\\Php'));
         $view->setViewsDir(__DIR__ . '/views/');
         return $view;
     });
     /**
      * Database connection is created based in the parameters defined in the configuration file
      */
     $dependencyInjector->set('db', function () use($config) {
         return new DbAdapter($config->database->toArray());
     });
 }
Exemple #30
0
 /**
  * Register the services here to make them general
  * or register in the ModuleDefinition to make them module-specific
  */
 public function registerServices(DiInterface $di)
 {
     $di->set('view', function () {
         $view = new View();
         $view->setViewsDir(__DIR__ . '/views/');
         $view->disableLevel([View::LEVEL_MAIN_LAYOUT => true, View::LEVEL_LAYOUT => true]);
         $view->registerEngines(['.volt' => 'volt']);
         // Create an event manager
         $eventsManager = new EventsManager();
         $eventsManager->attach('view', function ($event, $view) {
             if ($event->getType() == 'notFoundView') {
                 throw new \Exception('View not found!!! (' . $view->getActiveRenderPath() . ')');
             }
         });
         // Bind the eventsManager to the view component
         $view->setEventsManager($eventsManager);
         return $view;
     });
 }