|
| 1 | +<?php |
| 2 | + |
| 3 | +use MongoDB\Client; |
| 4 | +use Phalcon\Db\Adapter\Pdo\Mysql; |
| 5 | +use Phalcon\DI\FactoryDefault; |
| 6 | +use Phalcon\Exception as PhalconException; |
| 7 | +use Phalcon\Http\Request; |
| 8 | +use Phalcon\Incubator\MongoDB\Mvc\Collection\Manager as MongoDBCollectionManager; |
| 9 | +use Phalcon\Loader; |
| 10 | +use Phalcon\Mvc\Application; |
| 11 | +use Phalcon\Mvc\Model\MetaData\Apc; |
| 12 | +use Phalcon\Mvc\Model\MetaData\Memory; |
| 13 | +use Phalcon\Mvc\View; |
| 14 | +use Phalcon\Mvc\View\Engine\Volt; |
| 15 | + |
| 16 | +define('APP_PATH', realpath('..')); |
| 17 | +require APP_PATH . "/vendor/autoload.php"; |
| 18 | + |
| 19 | +try { |
| 20 | + // Load the config |
| 21 | + $config = include APP_PATH . "/app/config/config-mongo.php"; |
| 22 | + |
| 23 | + // Register an autoloader |
| 24 | + $loader = new Loader(); |
| 25 | + $loader->registerDirs([ |
| 26 | + $config->application->controllersDir, |
| 27 | + $config->application->modelsDir, |
| 28 | + $config->application->collectionsDir, |
| 29 | + ])->register(); |
| 30 | + |
| 31 | + // Create a DI |
| 32 | + $di = new FactoryDefault(); |
| 33 | + |
| 34 | + // Setting up the router |
| 35 | + $di->setShared('router', require APP_PATH . '/app/config/routes.php'); |
| 36 | + |
| 37 | + //MetaData |
| 38 | + $di->setShared('modelsMetadata', function () { |
| 39 | + if (function_exists('apc_store')) { |
| 40 | + return new Apc(); |
| 41 | + } |
| 42 | + |
| 43 | + return new Memory([ |
| 44 | + 'metaDataDir' => APP_PATH . "/app/compiled-templates/" |
| 45 | + ]); |
| 46 | + }); |
| 47 | + |
| 48 | + // Setting up the view component (seems to be required even when not used) |
| 49 | + $di->setShared('view', function () use ($config) { |
| 50 | + $view = new View(); |
| 51 | + $view->setViewsDir($config->application->viewsDir); |
| 52 | + $view->registerEngines([ |
| 53 | + ".volt" => function ($view) { |
| 54 | + $volt = new Volt($view); |
| 55 | + $volt->setOptions([ |
| 56 | + "path" => APP_PATH . "/app/compiled-templates/", |
| 57 | + "extension" => ".compiled", |
| 58 | + "separator" => '_', |
| 59 | + ]); |
| 60 | + |
| 61 | + return $volt; |
| 62 | + } |
| 63 | + ]); |
| 64 | + |
| 65 | + return $view; |
| 66 | + }); |
| 67 | + |
| 68 | + // Setting up the database connection |
| 69 | + $di->setShared('db', function () use ($config) { |
| 70 | + $database = $config->database; |
| 71 | + |
| 72 | + return new Mysql([ |
| 73 | + 'host' => $database->host, |
| 74 | + 'username' => $database->username, |
| 75 | + 'password' => $database->password, |
| 76 | + 'dbname' => $database->name, |
| 77 | + 'options' => [ |
| 78 | + PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', |
| 79 | + PDO::ATTR_PERSISTENT => true, |
| 80 | + ], |
| 81 | + ]); |
| 82 | + }); |
| 83 | + |
| 84 | + // Setting up the mongodb connection |
| 85 | + $di->setShared('mongo', function () use ($config) { |
| 86 | + $mongodbConfig = $config->mongodb; |
| 87 | + $mongo = new Client($mongodbConfig->url); |
| 88 | + |
| 89 | + return $mongo->selectDatabase($mongodbConfig->db); |
| 90 | + }); |
| 91 | + |
| 92 | + // Registering the mongoDB CollectionManager service |
| 93 | + $di->setShared('collectionsManager', function () { |
| 94 | + return new MongoDBCollectionManager(); |
| 95 | + }); |
| 96 | + |
| 97 | + // Handle the request |
| 98 | + $request = new Request(); |
| 99 | + $application = new Application(); |
| 100 | + $application->setDI($di); |
| 101 | + $application->handle($request->getURI())->send(); |
| 102 | +} catch (PhalconException $e) { |
| 103 | + echo "PhalconException: ", $e->getMessage(); |
| 104 | +} |
0 commit comments