Spaces:
Sleeping
Sleeping
File size: 6,873 Bytes
07c3cdd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | <?php
namespace Maveriks\Pattern\Mvc;
/**
* View
*
* This is the parent class to support view at MVC Pattern
* (Adapted version from PhpAlchemy project)
*
* @version 1.0
* @author Erik Amaru Ortiz <aortiz.erik@gmail.com>
* @link https://github.com/phpalchemy/phpalchemy
* @copyright Copyright 2012 Erik Amaru Ortiz
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @package Maveriks\Pattern\Mvc
*/
abstract class View
{
/**
* Contains all variables that are available on template file
*
* @var array
*/
protected $data = array();
/**
* Contains the absolute paths where templates are stored
*
* @var array
*/
protected $templateDir = array();
/**
* Contains the absolute path where the engine store the cache files
*
* @var string
*/
protected $cacheDir = '';
/**
* String to store the output string that is sent by http response
*
* @var string
*/
protected $content = '';
/**
* Relative path of template file
*
* @var string
*/
protected $tpl = '';
/**
* Cache flag to specify if templating cache is enabled or not
*
* @var string
*/
protected $cache = false;
/**
* Charset Encodig
*
* @var string
*/
protected $charset = 'UTF-8';
/**
* Debug flag to indicate to template engine is a debug environment
* Defaults false
*
* @var bool
*/
public $debug = false;
/**
* @param string $tpl template file
*/
public function __construct($tpl = '')
{
$this->tpl = $tpl;
}
/**
* Sets template file path (absolute or partial path)
*
* @param string $tpl contains the path of template file
*/
public function setTpl($tpl)
{
$this->tpl = $tpl;
}
/**
* Gets template file
*/
public function getTpl()
{
return $this->tpl;
}
/**
* Sets the base path where the engine can be find all templates
*
* @param string $path contains the absolute path where templates are stored
*/
public function setTemplateDir($path)
{
$this->templateDir[] = $path;
}
/**
* Gets the templates files base path
*
* @return array returns the templates base path
*/
public function getTemplateDir()
{
return $this->templateDir;
}
/**
* Sets cache directory path
*
* @param string $path cache directory path
*/
public function setCacheDir($dir)
{
$this->cacheDir = $dir;
if (! is_dir($this->cacheDir)) {
$this->createDir($this->cacheDir);
}
}
/**
* Gets cache directory path
*
* @return string contains cache directory path
*/
public function getCacheDir()
{
return $this->cacheDir;
}
/**
* Enable/Disable templating cache
*
* @param bool $value boolean value to enable or not cache
*/
public function enableCache($value)
{
$this->cache = $value ? true: false;
}
/**
* Enable/Disable debug mode of template engine
*
* @param bool $value boolean value to enable or not debug mode
*/
public function enableDebug($value)
{
$this->debug = $value ? true: false;
}
/**
* Sets charset encoding for template engine
* @param string $charset conatins a valid charset like UTF-8, ISO-8859-1 (latin), etc.
*/
public function setCharset($charset)
{
$this->charset = $charset;
}
/**
* Gets charset encoding
*
* @return string charset encoding
*/
public function getCharset()
{
return $this->charset;
}
/**
* Alias of method assign()
*
* @param string $name name or key to store teh value passed
* @param string $value variable value
*/
public function set($name, $value = null)
{
$this->assign($name, $value);
}
/**
* Assings a variable to the template file
*
* @param string $name name or key to store teh value passed
* @param string $value variable value
*/
public function assign($name, $value = null)
{
if (is_string($name)) {
return $this->data[$name] = $value;
}
if (is_array($name)) {
$this->assignFromArray($name);
return null;
}
throw new \InvalidArgumentException("Invalid data type for key, '" .gettype($name) . "' given.");
}
/**
* Gets a variable that was previously assigned
*
* @param string $name name or key to store teh value passed
* @param string $value variable value
*/
public function get($name)
{
if (!isset($this->data[$name])) {
throw new \InvalidArgumentException("Variable '$name' doesn't exist.");
}
return $this->data[$name];
}
public function exists($name)
{
return array_key_exists($name, $this->data);
}
/**
* Gets final template parsed output string
*
* @return string parsed output
*/
public function getOutput()
{
$output = '';
\ob_start();
$this->render();
$output = \ob_get_contents();
\ob_end_clean();
return $output;
}
/**
* Render the output string (To override by child class)
*/
abstract public function render();
/**
* Multiple variable assignment
*
* @param array $data associative array conatining variables, the keys are used as variables names
*/
protected function assignFromArray($data)
{
if (! is_array($data)) {
throw new \InvalidArgumentException(
"Invalid data type: argument should be array, '" .gettype($data) . "' given."
);
}
foreach ($data as $key => $value) {
$this->assign($key, $value);
}
}
protected function createDir($strPath, $rights = 0777)
{
$folderPath = array($strPath);
$oldumask = umask(0);
while (!@is_dir(dirname(end($folderPath)))
&& dirname(end($folderPath)) != '/'
&& dirname(end($folderPath)) != '.'
&& dirname(end($folderPath)) != ''
) {
array_push($folderPath, dirname(end($folderPath)));
}
while ($parentFolderPath = array_pop($folderPath)) {
if (! @is_dir($parentFolderPath)) {
if (! @mkdir($parentFolderPath, $rights)) {
throw new \Exception("Templating Engine Error: Can't create folder '$parentFolderPath'");
}
}
}
umask($oldumask);
}
}
|