-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fariz Luqman
committed
Dec 14, 2016
1 parent
bc608e6
commit 35d2aeb
Showing
1 changed file
with
37 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
* @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); | ||
} | ||
|
||
} |