public function get(\Phalcon\Mvc\Model\Query\Builder $builder, $page) { $config = $this->di->getConfig()->pager; // TODO: obkumać dlaczego przestało działać, może pojawi się nowa wersja inkubatora. $adapter = new \Phalcon\Paginator\Adapter\QueryBuilder(array('builder' => $builder, 'limit' => $config->limit, 'page' => $page)); $adapter = new \Phalcon\Paginator\Adapter\Model(array('data' => $builder->getQuery()->execute(), 'limit' => intval($config->limit), 'page' => $page)); return new \Phalcon\Paginator\Pager($adapter, array('layoutClass' => 'Phalcon\\Paginator\\Pager\\Layout\\Bootstrap', 'rangeLength' => intval($config->length), 'urlMask' => '?page={%page_number}')); }
/** * Registers the module-only services * * @param \Phalcon\DiInterface $di */ public function registerServices($di) { /** * Read application wide and module only configurations */ $appConfig = $di->get('config'); $moduleConfig = (include __DIR__ . '/config/config.php'); $di->set('moduleConfig', $moduleConfig); /** * The URL component is used to generate all kind of urls in the application */ $di->set('url', function () use($appConfig) { $url = new UrlResolver(); $url->setBaseUri($appConfig->application->baseUri); return $url; }); /** * Module specific dispatcher */ $di->set('dispatcher', function () use($di) { $dispatcher = new Dispatcher(); $dispatcher->setEventsManager($di->getShared('eventsManager')); $dispatcher->setDefaultNamespace('App\\Modules\\Frontend\\'); return $dispatcher; }); $di->setShared('request', function () use($appConfig) { return new \Phalcon\Http\Request(); }); /** * Include config per environment */ include __DIR__ . '/config/config_' . $appConfig->application->environment . '.php'; $database = $di->getConfig()->application->site . $di->get('request')->getQuery("country_code"); /** * Simple database connection to localhost */ $di->setShared('mongo', function ($config, $database) { $mongo = new \Mongo(); return $mongo->selectDb($config->{$database}->dbname); }, true); $di->setShared('collectionManager', function () { return new \Phalcon\Mvc\Collection\Manager(); }, true); }
/** * 注册全局的服务,需在 app 或者 其他 module 中的 registerServices 手动调用。 * * @param \Phalcon\DiInterface $di */ public static function registerGlobalServices($di) { $config = $di->getConfig(); // 当用户系统和 if (!empty($config->user->dbAdapter)) { $di->set('userDbMaster', function () use($di, $config) { $slaves = $config->user->dbAdapter->slave; $slaveKey = array_rand($slaves->toArray()); if (!isset($slaves->{$slaveKey}) || count($slaves) < 1) { throw new Exception\RuntimeException(sprintf('No DB slave options found')); } return Engine::diDbAdapter($slaves->{$slaveKey}->adapter, $slaves->{$slaveKey}->toArray(), $di); }); $di->set('userDbSlave', function () use($di, $config) { $slaves = $config->user->dbAdapter->slave; $slaveKey = array_rand($slaves->toArray()); if (!isset($slaves->{$slaveKey}) || count($slaves) < 1) { throw new Exception\RuntimeException(sprintf('No DB slave options found')); } return Engine::diDbAdapter($slaves->{$slaveKey}->adapter, $slaves->{$slaveKey}->toArray(), $di); }); } }
/** * Mount the module specific routes before the module is loaded. * Add ModuleRoutes Group and annotated controllers for parsing their routing information. * * @param \Phalcon\DiInterface $di */ public static function initRoutes(DiInterface $di) { $loader = new Loader(); $loader->registerNamespaces(['App\\Modules\\Backend\\Controllers' => __DIR__ . '/Controllers/', 'App\\Modules\\Backend\\Controllers\\API' => __DIR__ . '/Controllers/api/', 'App\\Modules\\Backend\\Models' => __DIR__ . '/Models/', 'App\\Modules\\Backend\\Library' => __DIR__ . '/Lib/', 'App\\Modules\\Frontend\\Controllers' => __DIR__ . '/../Frontend/Controllers/', 'App\\Modules\\Frontend\\Models' => __DIR__ . '/../Frontend/Models/'], TRUE)->register(); /** * Read application wide and module only configurations */ $appConfig = $di->get('config'); $moduleConfig = (include __DIR__ . '/config/config.php'); $di->setShared('moduleConfig', $moduleConfig); /** * The URL component is used to generate all kind of urls in the application */ $di->setShared('url', function () use($appConfig) { $url = new UrlResolver(); $url->setBaseUri($appConfig->application->baseUri); return $url; }); $di->setShared('request', function () use($appConfig) { return new \Phalcon\Http\Request(); }); /** * Read configuration */ include __DIR__ . "/../../config/env/" . $appConfig->application->environment . ".php"; $database = $di->getConfig()->application->site . $di->get('request')->getQuery("countryCode"); /** * Module specific database connection */ $di->set('db', function () use($config, $database) { $eventsManager = new \Phalcon\Events\Manager(); //Create a database listener $dbListener = new MyDBListener(); //Listen all the database events $eventsManager->attach('db', $dbListener); $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(['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'")]); //Assign the eventsManager to the db adapter instance $connection->setEventsManager($eventsManager); return $connection; }); /** * Simple database connection to localhost */ $di->set('mongo', function () use($config, $database) { $mongo = new \MongoClient(); return $mongo->selectDb($config->{$database}->dbname); }, true); $di->set('collectionManager', function () { return new \Phalcon\Mvc\Collection\Manager(); }, true); /** * Include composer autoloader */ require __DIR__ . "/../../../vendor/autoload.php"; /** * Module specific dispatcher */ $di->set('dispatcher', function () use($di) { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace('App\\Modules\\Backend\\Controllers\\'); return $dispatcher; }); $di->set('utils', function () { require __DIR__ . "/../../Common/Lib/Application/Plugins/Utils.php"; $utils = new Utils(); return $utils; }); /** * If our request contains a body, it has to be valid JSON. This parses the * body into a standard Object and makes that available from the DI. If this service * is called from a function, and the request body is not valid JSON or is empty, * the program will throw an Exception. */ $di->setShared('requestBody', function () { parse_str(file_get_contents("php://input"), $in); // JSON body could not be parsed, throw exception if ($in === null) { throw new HTTPException('There was a problem understanding the data sent to the server by the application.', 409, array('dev' => 'The JSON body sent to the server was unable to be parsed.', 'internalCode' => 'REQ1000', 'more' => '')); } return $in; }); /** * This means we can create listeners that run when an event is triggered. */ $di->setShared('modelsManager', function () use($di, $config, $database) { $eventsManager = new \Phalcon\Events\Manager(); $customModelsManager = new CustomModelsManager(); /** * Attach an anonymous function as a listener for "model" events */ $eventsManager->attach('model', $customModelsManager); /** * Setting a default EventsManager */ $customModelsManager->setEventsManager($eventsManager); return $customModelsManager; }); }
/** * 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"); $configGlobal = $di->getConfig(); $di->set('url', function () use($config, $configGlobal) { $url = new Url(); if (APPLICATION_ENV == 'production') { $url->setStaticBaseUri($configGlobal->application->production->staticBaseUri); } else { $url->setStaticBaseUri($configGlobal->application->development->staticBaseUri); } $url->setBaseUri($config->application->baseUri); return $url; }); //Registering a dispatcher $di->set('dispatcher', function () { $eventsManager = $this->getEventsManager(); $eventsManager->attach("dispatch", function ($event, $dispatcher, $exception) { //controller or action doesn't exist if ($event->getType() == 'beforeException') { $message = $exception->getMessage(); $response = $this->getResponse(); switch ($exception->getCode()) { case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: $response->redirect(); return false; case Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $response->redirect('action-not-found?msg=' . $message); return false; case Dispatcher::EXCEPTION_CYCLIC_ROUTING: $response->redirect('cyclic-routing?msg=' . $message); 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 = $this->getEventsManager(); $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; }); }
/** * 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"); $configGlobal = $di->getConfig(); // The URL component is used to generate all kind of urls in the application $di->set('url', function () use($config, $configGlobal) { $url = new Url(); if (APPLICATION_ENV == 'production') { $url->setStaticBaseUri($configGlobal->application->production->staticBaseUri); } else { $url->setStaticBaseUri($configGlobal->application->development->staticBaseUri); } $url->setBaseUri($config->application->baseUri); return $url; }); //Registering a dispatcher $di->set('dispatcher', function () use($di) { //Create/Get an EventManager $eventsManager = new EventsManager(); //Attach a listener $eventsManager->attach('dispatch', function ($event, $dispatcher, $exception) use($di) { //controller or action doesn't exist if ($event->getType() == 'beforeException') { $message = $exception->getMessage(); $response = $di->getResponse(); switch ($exception->getCode()) { case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND: $response->redirect(); return false; case Dispatcher::EXCEPTION_ACTION_NOT_FOUND: $response->redirect('action-not-found?msg=' . $message); return false; case Dispatcher::EXCEPTION_CYCLIC_ROUTING: $response->redirect('cyclic-routing?msg=' . $message); 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; }); }
/** * Mount the module specific routes before the module is loaded. * Add ModuleRoutes Group and annotated controllers for parsing their routing information. * * @param \Phalcon\DiInterface $di */ public static function initRoutes(DiInterface $di) { $loader = new Loader(); $loader->registerNamespaces(['App\\Modules\\Admin' => __DIR__, 'App\\Modules\\Admin\\Models' => __DIR__ . '/Models/', 'App\\Modules\\Admin\\Controllers' => __DIR__ . '/Controllers/', 'App\\Modules\\Admin\\Controllers\\API' => __DIR__ . '/Controllers/api/'], TRUE)->register(); /** * Read application wide and module only configurations */ $appConfig = $di->get('config'); $moduleConfig = (include __DIR__ . '/config/config.php'); $di->set('moduleConfig', $moduleConfig); /** * The URL component is used to generate all kind of urls in the application */ $di->set('url', function () use($appConfig) { $url = new UrlResolver(); $url->setBaseUri($appConfig->application->baseUri); return $url; }); $di->setShared('request', function () use($appConfig) { return new \Phalcon\Http\Request(); }); /** * Read configuration */ include __DIR__ . "/../../config/env/" . $appConfig->application->environment . ".php"; $database = $di->getConfig()->application->site . $di->get('request')->getQuery("countryCode"); /** * Module specific database connection */ $di->set('dbMysql', function () use($config, $database) { $eventsManager = new \Phalcon\Events\Manager(); $logger = new FileLogger(__DIR__ . "/../../Common/logs/admin/debug.log"); //Listen all the database events $eventsManager->attach('dbMysql', function ($event, $connection) use($logger) { if ($event->getType() == 'beforeQuery') { $logger->log($connection->getSQLStatement(), \Phalcon\Logger::INFO); } }); $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(['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'")]); //Assign the eventsManager to the db adapter instance $connection->setEventsManager($eventsManager); return $connection; }); /** * Module specific dispatcher */ $di->setShared('dispatcher', function () use($di) { $dispatcher = new Dispatcher(); $dispatcher->setDefaultNamespace('App\\Modules\\Admin\\Controllers\\'); return $dispatcher; }); $di->set('utils', function () { require __DIR__ . "/../../Common/Lib/Application/Plugins/Utils.php"; $utils = new Utils(); return $utils; }); /** * If our request contains a body, it has to be valid JSON. This parses the * body into a standard Object and makes that available from the DI. If this service * is called from a function, and the request body is not valid JSON or is empty, * the program will throw an Exception. */ $di->setShared('requestBody', function () { parse_str(file_get_contents("php://input"), $in); // JSON body could not be parsed, throw exception if ($in === null) { throw new HTTPException('There was a problem understanding the data sent to the server by the application.', 409, array('dev' => 'The JSON body sent to the server was unable to be parsed.', 'internalCode' => 'REQ1000', 'more' => '')); } return $in; }); }