From 35d2aeb796901fc964a3f3c3a9e065772aeffa20 Mon Sep 17 00:00:00 2001 From: Fariz Luqman Date: Thu, 15 Dec 2016 02:51:49 +0800 Subject: [PATCH] Update Viewer.php --- src/Viewer.php | 63 +++++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/src/Viewer.php b/src/Viewer.php index 8c5b83e..9b112b8 100644 --- a/src/Viewer.php +++ b/src/Viewer.php @@ -4,29 +4,29 @@ * * Copyright (c) 2016 Studio Nexus * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included + * + * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS - * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY - * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, - * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * + * * @package Damn Stupid Simple * @author Studio Nexus * @copyright 2016 Studio Nexus * @license MIT - * @version Release: 0.2.0 + * @version Release: 0.3.0 * @link https://www.studionexus.co/php/damnstupidsimple */ namespace Core; @@ -35,14 +35,14 @@ * The Viewer * ----------------------------------------------------------------------- * - * Reads and render the template file. Responsible for injecting + * Reads and render the template file. Responsible for injecting * dependencies from both Container and the Core\Sharer * */ class Viewer { - + /** - * Finds, renders and displays a template file. Reports a 404 error in + * Finds, renders and displays a template file. Reports a 404 error in * case of missing files. * * @param string $file file name / path to the file @@ -58,7 +58,18 @@ static function file($file, array &$data = []){ if($file === 'index' || $file === 'index.php'){ Debugger::report(404, true); }else{ - if(file_exists(DSS_PATH.$file)){ + /** + * Get the path of the calling script and get it's containing Directory + * to enable include() style of accessing files + */ + $callingScriptPath = debug_backtrace()[0]['file']; + $callingScriptDirectory = realpath(dirname($callingScriptPath)); + + if(file_exists($callingScriptDirectory.'/'.$file)){ + self::render($callingScriptDirectory.'/'.$file, $data); + }else if(file_exists($callingScriptDirectory.'/'.$file.'.php')){ + self::render($callingScriptDirectory.'/'.$file.'.php', $data); + }else if(file_exists(DSS_PATH.$file)){ self::render($file, $data); }else if(file_exists(DSS_PATH.$file.'.php')){ self::render(DSS_PATH.$file.'.php'); @@ -67,9 +78,9 @@ static function file($file, array &$data = []){ } } } - + /** - * Renders a template file. Inject dependencies from the Application + * Renders a template file. Inject dependencies from the Application * Container and the Core\Sharer before viewing the file. Also, * extracts &$data into variables usable from the template files * @@ -83,20 +94,20 @@ static function file($file, array &$data = []){ static private function render($file, &$data = []){ // Extract the variable $data passed to the Core\Viewer::file() extract ($data); - + // We don't want duplicated data unset($data); - + // Extract data retreived from the Sharer if(Sharer::get() !== null){ extract(Sharer::get()); } - + ob_start(); include_once($file); $output = ob_get_contents(); ob_end_clean(); echo ($output); } - + }