Ei kuvausta

services.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. declare(strict_types=1);
  3. use Phalcon\Escaper;
  4. use Phalcon\Flash\Direct as Flash;
  5. use Phalcon\Mvc\Model\Metadata\Memory as MetaDataAdapter;
  6. use Phalcon\Mvc\View;
  7. use Phalcon\Mvc\View\Engine\Php as PhpEngine;
  8. use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
  9. use Phalcon\Session\Adapter\Stream as SessionAdapter;
  10. use Phalcon\Session\Manager as SessionManager;
  11. use Phalcon\Url as UrlResolver;
  12. /**
  13. * Shared configuration service
  14. */
  15. $di->setShared('config', function () {
  16. return include APP_PATH . "/config/config.php";
  17. });
  18. /**
  19. * The URL component is used to generate all kind of urls in the application
  20. */
  21. $di->setShared('url', function () {
  22. $config = $this->getConfig();
  23. $url = new UrlResolver();
  24. $url->setBaseUri($config->application->baseUri);
  25. return $url;
  26. });
  27. /**
  28. * Setting up the view component
  29. */
  30. $di->setShared('view', function () {
  31. $config = $this->getConfig();
  32. $view = new View();
  33. $view->setDI($this);
  34. $view->setViewsDir($config->application->viewsDir);
  35. $view->registerEngines([
  36. '.volt' => function ($view) {
  37. $config = $this->getConfig();
  38. $volt = new VoltEngine($view, $this);
  39. $volt->setOptions([
  40. 'path' => $config->application->cacheDir,
  41. 'separator' => '_'
  42. ]);
  43. return $volt;
  44. },
  45. '.phtml' => PhpEngine::class
  46. ]);
  47. return $view;
  48. });
  49. /**
  50. * Database connection is created based in the parameters defined in the configuration file
  51. */
  52. $di->setShared('db', function () {
  53. $config = $this->getConfig();
  54. $class = 'Phalcon\Db\Adapter\Pdo\\' . $config->database->adapter;
  55. $params = [
  56. 'host' => $config->database->host,
  57. 'username' => $config->database->username,
  58. 'password' => $config->database->password,
  59. 'dbname' => $config->database->dbname,
  60. 'charset' => $config->database->charset
  61. ];
  62. if ($config->database->adapter == 'Postgresql') {
  63. unset($params['charset']);
  64. }
  65. return new $class($params);
  66. });
  67. /**
  68. * If the configuration specify the use of metadata adapter use it or use memory otherwise
  69. */
  70. $di->setShared('modelsMetadata', function () {
  71. return new MetaDataAdapter();
  72. });
  73. /**
  74. * Register the session flash service with the Twitter Bootstrap classes
  75. */
  76. $di->set('flash', function () {
  77. $escaper = new Escaper();
  78. $flash = new Flash($escaper);
  79. $flash->setImplicitFlush(false);
  80. $flash->setCssClasses([
  81. 'error' => 'alert alert-danger',
  82. 'success' => 'alert alert-success',
  83. 'notice' => 'alert alert-info',
  84. 'warning' => 'alert alert-warning'
  85. ]);
  86. return $flash;
  87. });
  88. /**
  89. * Start the session the first time some component request the session service
  90. */
  91. $di->setShared('session', function () {
  92. $session = new SessionManager();
  93. $files = new SessionAdapter([
  94. 'savePath' => sys_get_temp_dir(),
  95. ]);
  96. $session->setAdapter($files);
  97. $session->start();
  98. return $session;
  99. });