/** * Application constructor. * Parsing all registered modules and finds a route. After that it will execute the * requested module. * @param $loader - autoloader instance * @param $configurationFilePath */ public function __construct($loader, $configurationFilePath) { $this->translator = new Translator(); $this->response = new HtmlResponse(); $this->loader = $loader; $this->parser = new Parser(); $this->config = $this->parser->parse(file_get_contents($configurationFilePath)); $this->modules = array(); $this->routes = array(); $this->router = new Router(); if (array_key_exists('use_database', $this->config)) { if ($this->config['use_database'] && array_key_exists('Database', $this->config)) { $isDevMode = true; $config = Setup::createConfiguration($isDevMode); $driver = new AnnotationDriver(new AnnotationReader(), array(dirname(dirname(dirname(__DIR__))) . "/src/Application/Entity")); AnnotationRegistry::registerLoader('class_exists'); $config->setMetadataDriverImpl($driver); $conn = $this->config['Database']; $this->entityManager = EntityManager::create($conn, $config); } } foreach ($this->config['Modules'] as $module) { // $this->translator->addFolder('src/'.$module.'/language'); $path = 'src/' . $module . '/config/config.yml'; $langPath = dirname(dirname(dirname(__DIR__))) . '/src/' . $module . '/language'; if (file_exists($langPath)) { $this->translator->addFolder($langPath); } if (file_exists($path)) { $this->moduleSettings[$module] = $this->parser->parse(file_get_contents($path)); $this->router->assignRoute($this->moduleSettings[$module]['routes']); if (array_key_exists('invokable', $this->moduleSettings[$module])) { $this->getInvokablesFromModule($this->moduleSettings[$module]['invokable']); } if (array_key_exists('view', $this->moduleSettings[$module])) { $this->getViewsFromModule($this->moduleSettings[$module]['view']); } } } $this->translator->load(); new Autoloader($this->invokable, $this->config['Modules']); $this->router->addRoute('error_page', new Route(array('controller' => array_key_exists('error_page_controller', $this->config) ? $this->config['error_page_controller'] : '\\decoy\\base\\ErrorController', 'route' => 'error'))); $this->requestHeader = new HttpHeader(false); $this->requestHeader->parse($_SERVER); $this->currentRoute = $this->router->getRouteForURI($_SERVER['REQUEST_URI']); $this->parseRequestBody(); try { $this->execute(); } catch (\Exception $e) { Logger::Log('log/error.txt', $e->getMessage()); \decoy\base\ErrorController::$errors[] = $e->getMessage(); $this->toRoute('error_page'); } if (count(\decoy\base\ErrorController::$errors) > 0 && $this->config['debug'] == true) { Logger::Log('log/warn.txt', json_encode(\decoy\base\ErrorController::$errors, JSON_PRETTY_PRINT)); } }