From 3c92b7393ca52be9d16ff1397ae8a3bb8c55b0ae Mon Sep 17 00:00:00 2001 From: coderkun Date: Fri, 22 May 2015 12:59:52 +0200 Subject: [PATCH 1/3] implement loading models for Controller Components --- core/Component.inc | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/core/Component.inc b/core/Component.inc index ae219970..38b550cd 100644 --- a/core/Component.inc +++ b/core/Component.inc @@ -80,6 +80,82 @@ return \nre\configs\AppConfig::$app['namespace']."controllers\\components\\$className"; } + + + + /** + * Construct a new (Controller) Component. + * + * @throws \nre\exceptions\DriverNotFoundException + * @throws \nre\exceptions\DriverNotValidException + * @throws \nre\exceptions\ModelNotValidException + * @throws \nre\exceptions\ModelNotFoundException + */ + protected function __construct() + { + // Load Models + $this->loadModels(); + } + + + + + /** + * Load the Models of this Component. + * + * @throws \nre\exceptions\DriverNotFoundException + * @throws \nre\exceptions\DriverNotValidException + * @throws \nre\exceptions\ModelNotValidException + * @throws \nre\exceptions\ModelNotFoundException + */ + protected function loadModels() + { + // Determine Models + $explicit = false; + $models = \nre\core\ClassLoader::stripClassType(\nre\core\ClassLoader::stripNamespace(get_class($this))); + if(property_exists($this, 'models')) + { + $models = $this->models; + $explicit = true; + } + if(!is_array($models)) { + $models = array($models); + } + // Models of parent classes + $parent = $this; + while($parent = get_parent_class($parent)) + { + $properties = get_class_vars($parent); + if(array_key_exists('models', $properties)) { + $models = array_merge($models, $properties['models']); + } + } + $models = array_unique($models); + + // Load Models + foreach($models as &$model) + { + try { + // Load class + Model::load($model); + + // Construct Model + $modelName = ucfirst(strtolower($model)); + $this->$modelName = Model::factory($model); + } + catch(\nre\exceptions\ModelNotValidException $e) { + if($explicit) { + throw $e; + } + } + catch(\nre\exceptions\ModelNotFoundException $e) { + if($explicit) { + throw $e; + } + } + } + } + } ?> From aa9df0f4818d5cb647fb14cdaa67ed1d65c8581a Mon Sep 17 00:00:00 2001 From: coderkun Date: Mon, 10 Aug 2015 20:21:30 +0200 Subject: [PATCH 2/3] detect localhost by HTTP_HOST instead of SERVER_ADDR --- core/Logger.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/Logger.inc b/core/Logger.inc index 0ea34757..6418980c 100644 --- a/core/Logger.inc +++ b/core/Logger.inc @@ -135,7 +135,7 @@ */ private function isLocalhost() { - return ($_SERVER['SERVER_ADDR'] == '127.0.0.1' || $_SERVER['SERVER_ADDR'] = '::1'); + return (strpos($_SERVER['HTTP_HOST'], '.') === false); } } From ce48e090735f40919e8379070a49620ff0c03f71 Mon Sep 17 00:00:00 2001 From: coderkun Date: Mon, 10 Aug 2015 20:24:52 +0200 Subject: [PATCH 3/3] change default log mode to AUTO --- core/Logger.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/Logger.inc b/core/Logger.inc index 6418980c..8f15caff 100644 --- a/core/Logger.inc +++ b/core/Logger.inc @@ -64,7 +64,7 @@ * @param string $message Message to log * @param int $logMode Log mode to use */ - public function log($message, $logMode=self::LOGMODE_SCREEN) + public function log($message, $logMode=self::LOGMODE_AUTO) { // Choose log mode automatically if($logMode == self::LOGMODE_AUTO) {