|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Shared configuration service |
| 5 | + */ |
| 6 | +/** @var Phalcon\Di $di */ |
| 7 | +$di->setShared('router', function () { |
| 8 | + $config = $this->getShared('config'); |
| 9 | + |
| 10 | + $router = new \Phalcon\Mvc\Router(false); |
| 11 | + $router->removeExtraSlashes(true) |
| 12 | + ->setDefaultModule($config->application->moduleDefault); |
| 13 | + |
| 14 | + // mount module routes |
| 15 | + foreach (array_keys($config->application->modules->toArray()) as $module) { |
| 16 | + if (!is_file(APP_PATH . '/modules/' . $module . '/config/routers.php')) { |
| 17 | + continue; |
| 18 | + } |
| 19 | + |
| 20 | + $routers = require_once APP_PATH . '/modules/' . $module . '/config/routers.php'; |
| 21 | + |
| 22 | + if (!empty($routers) && is_array($routers)) { |
| 23 | + foreach ($routers as $route) { |
| 24 | + $router->mount(new $route()); |
| 25 | + } |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + return $router; |
| 30 | +}); |
| 31 | + |
| 32 | +/** |
| 33 | + * The URL component is used to generate all kind of urls in the application |
| 34 | + */ |
| 35 | +$di->setShared('url', function () { |
| 36 | + $config = $this->getShared('config'); |
| 37 | + $url = new \Phalcon\Mvc\Url(); |
| 38 | + |
| 39 | + if (isset($config->application->baseUri)) { |
| 40 | + $url->setBaseUri($config->application->baseUri); |
| 41 | + } |
| 42 | + |
| 43 | + return $url; |
| 44 | +}); |
| 45 | + |
| 46 | +$di->setShared('voltService', function ($view) { |
| 47 | + $volt = new \Phalcon\Mvc\View\Engine\Volt($view, $this); |
| 48 | + $directory = $this->getShared('config')->application->cacheDir . '/volt'; |
| 49 | + |
| 50 | + if (!is_dir($directory)) { |
| 51 | + mkdir($directory, 0755, true); |
| 52 | + } |
| 53 | + |
| 54 | + $volt->setOptions([ |
| 55 | + "compiledPath" => $directory, |
| 56 | + "compiledSeparator" => "-", |
| 57 | + "compileAlways" => true, |
| 58 | + ]); |
| 59 | + |
| 60 | + return $volt; |
| 61 | +}); |
| 62 | + |
| 63 | +/** |
| 64 | + * Database connection is created based in the parameters defined in the configuration file |
| 65 | + */ |
| 66 | +$di->setShared('db', function () { |
| 67 | + $config = $this->getShared('config'); |
| 68 | + |
| 69 | + $class = 'Phalcon\Db\Adapter\Pdo\\' . $config->database->adapter; |
| 70 | + $params = [ |
| 71 | + 'host' => $config->database->host, |
| 72 | + 'username' => $config->database->username, |
| 73 | + 'password' => $config->database->password, |
| 74 | + 'dbname' => $config->database->dbname, |
| 75 | + 'charset' => $config->database->charset, |
| 76 | + ]; |
| 77 | + |
| 78 | + if ($config->database->adapter == 'Postgresql') { |
| 79 | + unset($params['charset']); |
| 80 | + } |
| 81 | + |
| 82 | + $connection = new $class($params); |
| 83 | + |
| 84 | + return $connection; |
| 85 | +}); |
| 86 | + |
| 87 | + |
| 88 | +/** |
| 89 | + * If the configuration specify the use of metadata adapter use it or use memory otherwise |
| 90 | + */ |
| 91 | +$di->setShared('modelsMetadata', function () { |
| 92 | + return new \Phalcon\Mvc\Model\Metadata\Memory(); |
| 93 | +}); |
| 94 | + |
| 95 | +/** |
| 96 | + * Register the session flash service with the Twitter Bootstrap classes |
| 97 | + */ |
| 98 | +$di->setShared('flash', function () { |
| 99 | + return new \Phalcon\Flash\Direct([ |
| 100 | + 'error' => 'alert alert-danger', |
| 101 | + 'success' => 'alert alert-success', |
| 102 | + 'notice' => 'alert alert-info', |
| 103 | + 'warning' => 'alert alert-warning', |
| 104 | + ]); |
| 105 | +}); |
| 106 | + |
| 107 | +/** |
| 108 | + * Start the session the first time some component request the session service |
| 109 | + */ |
| 110 | +$di->setShared('session', function () { |
| 111 | + $session = new \Phalcon\Session\Adapter\Files(); |
| 112 | + $session->start(); |
| 113 | + |
| 114 | + return $session; |
| 115 | +}); |
0 commit comments