Ver Mensaje Individual
  #11 (permalink)  
Antiguo 11/02/2013, 15:57
chemajmb
 
Fecha de Ingreso: enero-2012
Ubicación: España
Mensajes: 150
Antigüedad: 14 años, 7 meses
Puntos: 0
Respuesta: Ejecutar siempre un mismo codigo al final de los metodos/funciones

Pues he hecho esto, no se si esta decente, mal o se puede mejorar, etc. pero asi lo he hecho.

Clase Vista
Código PHP:
abstract class ClaseVista{
    
    private 
$_peticion;
    private 
$_css;
    private 
$_themes;
    private 
$_js;
    private 
$_jsPlugin;
    private 
$_acl;
    private 
$_rutas;
    protected 
$_plantilla;
    protected 
$_marco;

    public function 
__construct( Peticion $peticion ) {
        
$this->_peticion = $peticion;
        
$this->_themes = array();
        
$this->_css = array();
        
$this->_js = array();
        
$this->_jsPlugin = array();
        
//$this->_acl = $_acl;
        
$this->_rutas = array();
        
$this->_plantilla = $peticion->getAccion();
        
$this->_marco = 'default';
        
$this->_mostrar = true;
    }

    public function 
renderizar()
    {
        if ( 
$this->_mostrar == null)
            exit();

        
$rutaPlantilla = DIR_VISTAS . $this->_peticion->getControlador() . DS . $this->_plantilla . '.phtml';

        if( 
is_readable( $rutaPlantilla ) ){

            
$class_vars = get_class_vars( get_class( $this ) );
            
$class_properties = get_object_vars( $this );
            
$class_data = array_diff_key ( $class_properties , $class_vars );
            foreach (
$class_data as $key => $value)
                $
$key = $value;

            
// incluyo las plantillas phtml
            
if ( !$this->_marco){
                include_once ( 
$rutaPlantilla );
                exit();
            }
            include_once ( 
DIR_VISTAS . 'layout' . DS . 'cabecera_html.phtml' );
            include_once ( 
DIR_VISTAS . 'layout' . DS . $this->_marco . DS . 'cabecera.phtml' );
            include_once ( 
$rutaPlantilla );
            include_once ( 
DIR_VISTAS . 'layout' . DS . $this->_marco . DS . 'pie.phtml' );
        }else {
            throw new 
MiExcepcion('Error de vista',array('rutaPlantilla' => $rutaPlantilla, 'plantilla' => $plantilla, 'marco' => $marco));
        }
    }
... 
Clase Lanzador o Bootstrap:
Código PHP:
<?php

class Lanzador
{
    public static function 
run( Peticion $peticion )
    {
        
$controlador = $peticion->getControlador() . 'Controlador';
        
$rutaControlador = DIR_CONTROLADORES . $controlador . '.php';
        
$accion = $peticion->getAccion();
        
$argumentos = $peticion->getArgumentos();

        if( 
is_readable( $rutaControlador ) ){
            require_once 
$rutaControlador;
            
//$registro = new claseRegistro();
            
$controlador = new $controlador( $peticion );

            if(
is_callable(array($controlador, $accion))){
                if( empty(
$argumentos) == true ){
                    
call_user_func(array($controlador, $accion));
                }
                else{
                    
call_user_func_array(array($controlador, $accion), array( $argumentos ) ) ;
                }
                
$controlador->getVista()->renderizar();
                exit();
            }
        }
        
header('Location: '.PAGINA_ERROR_404);
    }
}

?>
Lo único que tengo que hacer si en algún método no necesito mostrar la vista, es llamar el metodo $this->_vista->noMostrar() que me modifica la propiedad de $_mostrar a false.

Incluso si quiero rizar el rizo puedo optar por llamar a los metodos que no vayan a mostrar vista con un nombre que empiece o termine por una cadena de caracteres especifica a comprobar y si es igual a esa cadena, simplemente pongo la propiedad de $this->_mostrar a false.

Última edición por chemajmb; 11/02/2013 a las 16:11