Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Homex;
- use Phalcon\Mvc\Application as PhalconApplication;
- use Phalcon\DI;
- use Phalcon\Events\Manager as EventsManager;
- use Phalcon\Config\Adapter\Ini as ConfigAdapter;
- use Phalcon\Session\Adapter\Files as SessionFiles;
- use Phalcon\Mvc\Model\Manager as ModelsManager;
- use Phalcon\Flash\Direct as FlashDirect;
- use Phalcon\Flash\Session as FlashSession;
- use Phalcon\Loader;
- class Application extends PhalconApplication
- {
- const SYSTEM_CONFIG_PATH = '/config/application.php';
- const MODE_PRODUCTION = 'prod';
- const MODE_TESTING = 'test';
- const MODE_DEVELOPING = 'dev';
- const DEFAULT_MODE = 'dev';
- /**
- * Set application mode and error reporting level.
- */
- public function __construct()
- {
- $di = new Di\FactoryDefault();
- parent::__construct($di);
- }
- public function run($mode = null)
- {
- $di = $this->getDI();
- $eventsManager = new EventsManager();
- $this->setEventsManager($eventsManager);
- $di->setShared('eventsManager', $eventsManager);
- $defaultMode = self::DEFAULT_MODE;
- $di['mode'] = function () use ($mode, $defaultMode) {
- return $mode ? $mode : $defaultMode;
- };
- $this->initConfig($di);
- $this->initLoader($di);
- $this->initRouter($di);
- $this->initSession($di);
- $this->initFlash($di);
- $this->initDatabase($di);
- $di->set('volt', function ($view, $di) {
- $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
- $volt->setOptions([
- 'compiledPath' => ROOT_PATH.'/cache/',
- ]);
- return $volt;
- }, true);
- $di->setShared('app', $this);
- $this->debug();
- $this->handle()->send();
- }
- protected function debug()
- {
- $di = $this->getDI();
- $config = $di['config'];
- if ((self::MODE_DEVELOPING === $di['mode']) || $config->application->debug) {
- $debug = new \Phalcon\Debug();
- $debug->listen();
- }
- }
- protected function initLoader($di)
- {
- $config = $di['config'];
- $modules = array_merge([
- ], $config->modules->toArray());
- $modules = array_filter($modules);
- $di->set('modules', function () use ($modules) {
- return $modules;
- });
- $modulesNamespaces = [
- 'Homex' => ROOT_PATH.'/app/',
- 'Core' => ROOT_PATH.'/app/Core/',
- ];
- $bootstraps = [];
- foreach ($di['modules'] as $module => $enabled) {
- $moduleName = ucfirst($module);
- $modulePath = $config->application->modulesDir.$moduleName.'/Module.php';
- if (file_exists($modulePath)) {
- $modulesNamespaces[ $moduleName ] = $config->application->modulesDir.$moduleName.'/';
- $bootstraps[ $module ] = [
- 'className' => $moduleName.'\Module',
- 'path' => $modulePath,
- ];
- }
- }
- $loader = new Loader();
- $loader->registerNamespaces($modulesNamespaces);
- $loader->register();
- $this->registerModules($bootstraps);
- $di->set('loader', $loader);
- return $loader;
- }
- protected function initRouter($di)
- {
- $modules = $di['modules'];
- $config = $di['config'];
- $router = new \Phalcon\Mvc\Router\Annotations(false);
- $router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI);
- $router->add('/:module/:controller/:action', [
- 'module' => 1,
- 'controller' => 2,
- 'action' => 3,
- ]);
- $router->notFound([
- 'namespace' => 'Homex\Controller',
- 'controller' => 'Errors',
- 'action' => 'show404'
- ]);
- // foreach module attach annotated and grouped routes
- foreach ($modules as $module => $moduleData) {
- $moduleName = ucfirst($module);
- $moduleDir = $config->application->modulesDir.$moduleName;
- if (!is_dir($moduleDir)) {
- continue;
- }
- $files = scandir($moduleDir.'/Controller');
- foreach ($files as $file) {
- if (!strpos($file, '.') || !strpos($file, 'Controller.php')) {
- continue;
- }
- $controller = sprintf(
- '%s\\Controller\\%s',
- $moduleName,
- str_replace('Controller.php', '', $file)
- );
- $router->addModuleResource($module, $controller);
- }
- $moduleRoute = '\\'.$moduleName.'\Routes';
- if (class_exists($moduleRoute)) {
- $router->mount(new $moduleRoute());
- }
- }
- $di->set('router', $router);
- return $router;
- }
- public function initConfig($di)
- {
- $config = include_once(ROOT_PATH . self::SYSTEM_CONFIG_PATH);
- if (file_exists($config->application->configDir.'settings.ini')) {
- $config->merge(
- new ConfigAdapter($config->application->configDir.'settings.ini')
- );
- }
- $mode = $di['mode'];
- if ($mode !== self::MODE_PRODUCTION) {
- $modeConfig = $config->application->configDir.'/settings.'.$mode.'.ini';
- if (file_exists($modeConfig)) {
- $config->merge(
- new ConfigAdapter($modeConfig)
- );
- }
- }
- $di->setShared('config', $config);
- }
- protected function initSession($di)
- {
- $di->set('session', function () {
- $session = new SessionFiles();
- $session->start();
- return $session;
- }, true);
- }
- /**
- * Init flash messages.
- *
- * @param DI $di Dependency Injection.
- *
- * @return void
- */
- protected function initFlash($di)
- {
- $di->set('flash', function () {
- $flash = new FlashDirect([
- 'error' => 'alert alert-error',
- 'success' => 'alert alert-success',
- 'notice' => 'alert alert-info',
- ]);
- return $flash;
- });
- $di->set('flashSession', function () {
- $flash = new FlashSession([
- 'error' => 'alert alert-error',
- 'success' => 'alert alert-success',
- 'notice' => 'alert alert-info',
- ]);
- return $flash;
- });
- }
- /**
- * Init database.
- *
- * @param DI $di Dependency Injection.
- *
- * @return Pdo
- */
- protected function initDatabase($di)
- {
- $config = $di['config'];
- $adapter = '\Phalcon\Db\Adapter\Pdo\\' . $config->database->adapter;
- /** @var Pdo $connection */
- $connection = new $adapter([
- 'host' => $config->database->host,
- 'username' => $config->database->username,
- 'password' => $config->database->password,
- 'dbname' => $config->database->dbname,
- 'options' => [
- \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
- \PDO::ATTR_CASE => \PDO::CASE_LOWER,
- ]
- ]);
- $di->set('db', $connection);
- $di->set('modelsManager', function () {
- $modelsManager = new ModelsManager();
- return $modelsManager;
- }, true);
- $di->set('modelsMetadata', function () use ($config) {
- if (isset($config->metadata)) {
- $metaDataConfig = $config->metadata;
- $metadataAdapter = '\Phalcon\Mvc\Model\Metadata\\' . $metaDataConfig->adapter;
- $metaData = new $metadataAdapter($config->metadata->toArray());
- } else {
- $metaData = new \Phalcon\Mvc\Model\MetaData\Memory();
- }
- return $metaData;
- }, true);
- return $connection;
- }
- private function testRoutes($router)
- {
- $testRoutes = array(
- '/',
- '/crm',
- '/crm/',
- '/crm/orders',
- '/crm/orders/',
- '/crm/orders/detail',
- '/crm/orders/detail?id=2',
- );
- //Testing each route
- foreach ($testRoutes as $testRoute) {
- //Handle the route
- $router->handle($testRoute);
- echo 'Testing ', $testRoute, '<br>';
- //Check if some route was matched
- if ($router->wasMatched()) {
- echo 'Module: ', $router->getModuleName(), '<br>';
- echo 'Controller: ', $router->getControllerName(), '<br>';
- echo 'Action: ', $router->getActionName(), '<br>';
- } else {
- echo 'The route wasn\'t matched by any route<br>';
- }
- echo '<br>';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement