From e85d93573470c74cfe7ce7acedb520f4fdeb8708 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Mon, 18 Jun 2018 17:42:30 +0200 Subject: [PATCH] Create a Bootstrap class and move bootstrap code from public/index.php --- app/config/loader.php | 15 --------------- app/library/Bootstrap.php | 33 +++++++++++++++++++++++++++++++++ app/library/Services.php | 19 +++++++++++++++++++ public/index.php | 20 +++++++++----------- 4 files changed, 61 insertions(+), 26 deletions(-) delete mode 100644 app/config/loader.php create mode 100644 app/library/Bootstrap.php diff --git a/app/config/loader.php b/app/config/loader.php deleted file mode 100644 index 177824b..0000000 --- a/app/config/loader.php +++ /dev/null @@ -1,15 +0,0 @@ -registerNamespaces(array( - 'App\Controller' => $config->application->controllersDir, - 'App\Listener' => $config->application->listenersDir, - 'App\Model' => $config->application->modelsDir, - 'App\Form' => $config->application->formsDir, - 'Httpcb' => $config->application->libraryDir, -)); - -$loader->register(); - -require_once __DIR__ . '/../../vendor/autoload.php'; diff --git a/app/library/Bootstrap.php b/app/library/Bootstrap.php new file mode 100644 index 0000000..10b9810 --- /dev/null +++ b/app/library/Bootstrap.php @@ -0,0 +1,33 @@ +setDI($di); + } + + public function run() + { + $app = new Application($this->getDI()); + + // Force autoloader if registered. + // Should be needed by almost all other services. + $di = $this->getDI(); + if ($di->has('loader')) { + $di->get('loader'); + } + + return $app->handle()->getContent(); + } +} diff --git a/app/library/Services.php b/app/library/Services.php index 13af5e6..2edb1b4 100644 --- a/app/library/Services.php +++ b/app/library/Services.php @@ -69,6 +69,25 @@ class Services extends DiDefault return $config; } + protected function _initSharedLoader() + { + $config = $this->get('config'); + $loader = new \Phalcon\Loader(); + + $loader->registerNamespaces(array( + 'App\Controller' => $config->application->controllersDir, + 'App\Listener' => $config->application->listenersDir, + 'App\Model' => $config->application->modelsDir, + 'App\Form' => $config->application->formsDir, + 'Httpcb' => $config->application->libraryDir, + )); + + $loader->registerFiles(array(APP_PATH . '/vendor/autoload.php')); + $loader->register(); + + return $loader; + } + protected function _initSharedDispatcher() { $config = $this->get('config'); diff --git a/public/index.php b/public/index.php index 6862c0a..c4d2bc6 100644 --- a/public/index.php +++ b/public/index.php @@ -2,22 +2,20 @@ defined('APP_PATH') || define('APP_PATH', realpath('..')); -/** - * Read the configuration - */ -$config = include APP_PATH . "/app/config/config.php"; +require_once APP_PATH . "/app/library/Bootstrap.php"; +require_once APP_PATH . "/app/library/Services.php"; +/** + * Bootstrap the application. + */ +$bootstrap = new \Httpcb\Bootstrap(new Httpcb\Services()); + +$config = $bootstrap->getDI()->get('config'); if ($config->application->debug) { (new Phalcon\Debug)->listen(); } -/** - * Read auto-loader - */ -include APP_PATH . "/app/config/loader.php"; - /** * Handle the request */ -$app = new Phalcon\Mvc\Application(new Httpcb\Services()); -echo $app->handle()->getContent(); +echo $bootstrap->run();