Files
sysPass/inc/Template.class.php
nuxsmin 601ce41212 * [ADD] 1st step moving to MVC model. Mostly views and code logic were rewritten and added a bunch of templates. Lot of work!!
* [ADD] New template "engine". A very basic engine to render php coded templates.
* [ADD] Cached config in session with expire time.
2015-06-07 03:42:50 +02:00

244 lines
6.8 KiB
PHP

<?php
/**
* sysPass
*
* @author nuxsmin
* @link http://syspass.org
* @copyright 2012-2015 Rubén Domínguez nuxsmin@syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*
*/
/**
* Clase Template para la manipulación de plantillas
*
* Motor de plantillas muy básico...
*
* Idea original de http://www.sitepoint.com/author/agervasio/
* publicada en http://www.sitepoint.com/flexible-view-manipulation-1/
*
*/
class SP_Template {
/**
* @var array Variable con los archivos de plantilla a cargar
*/
private $file = array();
/**
* @var array Variable con las variables a incluir en la plantilla
*/
private $vars = array();
/**
* @var null Variable con el controlador a usar
*/
private $controllers = array();
/**
* @param null $file Archivo de plantilla a añadir
* @param array $vars Variables a inicializar
*/
public function __construct($file = null, array $vars = array()) {
$this->addTemplate($file);
if(!empty($vars)){
$this->setVars($vars);
}
}
/**
* Overloading para añadir nuevas variables en al array de variables dela plantilla
* pasadas como atributos dinámicos de la clase
*
* @param string $name Nombre del atributo
* @param string $value Valor del atributo
* @return null
*/
public function __set($name, $value) {
$this->vars[$name] = $value;
return null;
}
/**
* Overloadig para controlar la devolución de atributos dinámicos.
*
* @param string $name Nombre del atributo
* @return null
* @throws InvalidArgumentException
*/
public function __get($name) {
if (!array_key_exists($name, $this->vars)) {
throw new InvalidArgumentException('No es posible obtener la variable "' . $name . '"');
}
return $this->vars[$name];
}
/**
* Overloading para comprobar si el atributo solicitado está declarado como variable
* en el array de variables de la plantilla.
*
* @param string $name Nombre del atributo
* @return bool
*/
public function __isset($name) {
return array_key_exists($name, $this->vars);
}
/**
* Overloading para eliminar una variable del array de variables de la plantilla pasado como
* atributo dinámico de la clase
*
* @param string $name Nombre del atributo
* @return $this
* @throws InvalidArgumentException
*/
public function __unset($name) {
if (!isset($this->vars[$name])) {
throw new InvalidArgumentException('No es posible destruir la variable "' . $name . '"');
}
unset($this->vars[$name]);
return $this;
}
/**
* Mostrar la plantilla solicitada.
* La salida se almacena en buffer y se devuelve el contenido
*
* @return string Con el contenido del buffer de salida
*/
public function render() {
extract($this->vars);
// Añadimos los controladores necesarios
foreach ( $this->controllers as $controller) {
// error_log('CTRL: ' . $controller->controller);
include_once $controller->controller;
}
ob_start();
// Añadimos las plantillas
foreach ( $this->file as $template) {
// error_log('TPL: ' . $template);
include_once $template;
}
return ob_get_clean();
}
/**
* Comprobar si un archivo de plantilla existe y se puede leer
*
* @param string $file Con el nombre del archivo
* @throws InvalidArgumentException
*/
private function checkTemplate($file){
$template = SP_Init::$SERVERROOT . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR . 'tpl' . DIRECTORY_SEPARATOR . $file . '.inc';
if (!is_file($template) || !is_readable($template)) {
throw new InvalidArgumentException('No es posible obtener la plantilla "' . $file .'"');
}
$this->setTemplate($template);
}
/**
* Añadir un nuevo archivo de plantilla al array de plantillas de la clase.
*
* @param string $file Con el nombre del archivo
*/
private function setTemplate($file){
$this->file[] = $file;
}
/**
* Establecer los atributos de la clase a partir de un array.
*
* @param array $vars Con los atributos de la clase
*/
private function setVars(&$vars){
foreach ($vars as $name => $value) {
$this->$name = $value;
}
}
/**
* Añadir una nueva plantilla al array de plantillas de la clase
*
* @param string $file Con el nombre del archivo de plantilla
*/
public function addTemplate($file){
if (!is_null($file) && $this->checkTemplate($file)){
$this->setTemplate($file);
}
}
public function setController(SP_Controller $controller){
$this->controllers[] = $controller;
}
/**
* Crear la variable y asignarle un valor en el array de variables
*
* @param $name string nombre de la variable
* @param $value mixed valor de la variable
* @param null $scope string ámbito de la variable
*/
public function assign($name, $value, $scope = null){
if(!is_null($scope)){
$name = $scope . '_' . $name;
}
// error_log('SET: ' . $name . ' -> ' . $value);
$this->vars[$name] = $value;
}
/**
* Anexar el valor de la variable al array de la misma en el array de variables
*
* @param $name string nombre de la variable
* @param $value mixed valor de la variable
* @param $index string índice del array
* @param null $scope string ámbito de la variable
*/
public function append($name, $value, $scope = null, $index = null){
if(!is_null($scope)){
$name = $scope . '_' . $name;
}
if (!is_null($index)){
$this->vars[$name][$index] = $value;
} else {
$this->vars[$name][] = $value;
}
}
/**
* Reset de las plantillas añadidas
*/
public function resetTemplates(){
$this->file = array();
}
/**
* Reset de las plantillas añadidas
*/
public function resetVariables(){
$this->vars = array();
}
}