Re: guardar datos en memoria
| From: | Carlos Medina | Date: | Fri, 19 Dec 2008 18:13:58 +0000 |
| Subject: | Re: guardar datos en memoria | ||
| References: | 1 2 3 4 5 6 | Groups: | php.general.es |
| Request: | Send a blank email to general-es+get-63623@lists.php.net to get a copy of this message | ||
Hola chicos que les parece esta?
<?php
abstract class componentClass{
/**
* Singleton Instances
* @var Array $a_instances
*/
private static $a_instances = array();
/**
* Configuration Object
*/
protected $o_conf = null;
/**
* Values for this Component
* @var Array $a_values
*/
public $a_values = array();
/**
* The View Object
* @var Object $o_view
*/
protected $o_view;
/**
* Data of the Component
*
* @var object $o_data
*/
protected $o_data;
/**
* Do not Accept the clone Funcion for the Compoentns
*
*/
private function __clone(){}
/**
* Add a value to the Component
*
* @param string $s_key
* @param string $s_value
*/
public function setValue( $s_key, $s_value){
$this->a_values[ $s_key ] = $s_value;
}
/**
* Get a value from Component
*
* @param string $s_key
* @return mixed
*/
public function getValue( $s_key ){
if( true == isset( $this->a_values[ $s_key ])){
return $this->a_values[ $s_key ];
}
}
/**
* Get all Values
*
*/
public function getAllValues(){
if( true == is_array( $this->a_values ) &&
count( $this->a_values ) > 0 ){
return $this->a_values;
}
}
/**
* Get the View Object
*
*/
public function getViewObject(){
return $this->o_view;
}
/**
* Set the View Object
*
* @param Zend_view $viewObject
*/
public function setViewObject( Zend_view $viewObject ){
$this->o_view = $viewObject;
}
/**
* Implements Singleton to manage the Components
*
* @param string $s_className
* @param array $a_params
*/
public static function register( $s_className, $a_params = NULL ){
// Get the Same Object of All
if( false == Zend_Registry::isRegistered( $s_className ) ){
// Load The Class
Zend_Loader::loadClass( $s_className );
// Register a Instance of this Object
$instance = new $s_className( $a_params );
// set the Configuration
self::setConfiguration( $s_className, $instance );
}
// Set the View for this Component
$instance->o_view = new Zend_View();
// Registry the Component
Zend_Registry::set( strval( $s_className ), $instance );
}
/**
* Load the Configuration Object for this component
*
* @param String $s_className
* @param componentClass $o_componentObject
*/
public static function setConfiguration( $s_className, componentClass $o_componentObject ){
// Set the Configuration path
$s_configurationPath = $s_className . '_' . 'Configuration';
// Load the Component Class
try{
Zend_Loader::loadClass( $s_configurationPath );
// Get the Configuration Object
$config = new $s_configurationPath( array(), true );
$config->addConfig();
// Set the Configuration object to the Component Object
$o_componentObject->o_conf = $config;
} catch( Exeption $e ){
echo $e;
}
}
public function getConfiguration( $key ){
if( true == isset( $this->o_conf->$key ) ){
return $this->o_conf->$key;
}
}
/**
* Init the Component
*
*/
public function init(){}
/**
* Run the Component
*
*/
public function run(){}
/**
* Render a Component from anywhere in the code
*
* @param componentClass $instance
* @param mixed $o_render
* @param mixed $s_className
*/
public static function render( componentClass $instance, $o_render = null, $return = false ){
// call the Init method
$instance->init();
// Run the Component
$instance->run();
// Add the Template Path from Config
$templatePath = $instance->getConfiguration( 'templatePath' );
// Add the Template Name form Config
$templateFile = $instance->getConfiguration( 'templateFile' );
// Get the Template Path from given Values
if( $instance->getValue( 'templatePath' ) ){
$templatePath = $instance->getValue( 'templatePath' );
}
// Get the Template Name from Given Values
if( $instance->getValue( 'templateFile' ) ){
$templateFile = $instance->getValue( 'templateFile' );
}
// Set the Data from Method Parameter
if( true == is_object( $o_render ) ){
$instance->o_view->component = $o_render;
}else{
// Add the Data to the View to render
$instance->o_view->component = $instance->o_data;
}
// set the Script Path
$instance->o_view->setScriptPath( $templatePath );
// Render the Component
if( true == isset( $templateFile ) )
{
if( true == $return ){
return $instance->o_view->render( $templateFile . '.tpl' );
}else{
echo $instance->o_view->render( $templateFile . '.tpl' );
}
}else{
throw new Exception( 'no template File selected ' );
}
}
}
?>