Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class View{
- public function __construct( $source ){
- $this->GetSource( $source );
- $this->GetVariables();
- }
- public function __toString(){
- return $this->Render();
- }
- public function ClearVariables(){
- foreach( $this as $k => $v ){
- $this->$k = '';
- }
- }
- public function GetSource( $var = null ){
- static $enc = array();
- $id = GetInstanceId( $this );
- if( !empty( $var ) ){
- $enc[ $id ] = $var;
- }else{
- return $enc[ $id ];
- }
- }
- public function GetVariableDefault( $var = null ){
- static $enc = array();
- $id = GetInstanceId( $this );
- if( is_array( $var ) ){
- $enc[ $id ] = $var;
- }else if( is_scalar( $var ) ){
- return $enc[ $id ][ $var ];
- }else{
- return $enc[ $id ];
- }
- }
- public function GetVariableName( $var = null ){
- static $enc = array();
- $id = GetInstanceId( $this );
- if( is_array( $var ) ){
- $enc[ $id ] = $var;
- }else{
- return $enc[ $id ];
- }
- }
- public function Render(){
- ob_start();
- eval( '?' . '>' . $this->GetSource() );
- return ob_get_clean();
- }
- public function SetVariable( $k, $v ){
- if( isset( $this->$k ) ){
- $this->$k = $v;
- }
- }
- public function SetVariables( $obj ){
- if( is_array( $obj ) || is_object( $obj ) ){
- foreach( $obj as $k => $v ){
- $this->SetVariable( $k, $v );
- }
- }
- }
- public function SetVariableDefaults(){
- $defaults = $this->GetVariableDefault();
- foreach( $defaults as $k => $v ){
- $this->$k = $v;
- }
- }
- private function GetVariables(){
- $defaults = array();
- $variables = array();
- if( preg_match_all( '/\$this->(\w+)[^\(]/', $this->GetSource(), $matches, PREG_SET_ORDER ) ){
- while( list( $_, list( $_, $name ) ) = each( $matches ) ){
- $defaults[ $name ] = '';
- }
- $variables = array_keys( $defaults );
- }
- //Set the enclosure
- $this->GetVariableName( $variables );
- ob_start();
- @eval( '?' . '>' . $this->GetSource() );
- ob_end_clean();
- foreach( $defaults as $k => $v ){
- if( !empty( $this->$k ) ){
- $defaults[ $k ] = $this->$k;
- }
- }
- //Set the enclosure
- $this->GetVariableDefault( $defaults );
- }
- }
Add Comment
Please, Sign In to add comment