Guest User

Untitled

a guest
May 20th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. <?php
  2. class View{
  3. public function __construct( $source ){
  4. $this->GetSource( $source );
  5. $this->GetVariables();
  6. }
  7.  
  8. public function __toString(){
  9. return $this->Render();
  10. }
  11.  
  12. public function ClearVariables(){
  13. foreach( $this as $k => $v ){
  14. $this->$k = '';
  15. }
  16. }
  17.  
  18. public function GetSource( $var = null ){
  19. static $enc = array();
  20. $id = GetInstanceId( $this );
  21.  
  22. if( !empty( $var ) ){
  23. $enc[ $id ] = $var;
  24. }else{
  25. return $enc[ $id ];
  26. }
  27. }
  28.  
  29. public function GetVariableDefault( $var = null ){
  30. static $enc = array();
  31. $id = GetInstanceId( $this );
  32.  
  33. if( is_array( $var ) ){
  34. $enc[ $id ] = $var;
  35. }else if( is_scalar( $var ) ){
  36. return $enc[ $id ][ $var ];
  37. }else{
  38. return $enc[ $id ];
  39. }
  40. }
  41.  
  42. public function GetVariableName( $var = null ){
  43. static $enc = array();
  44. $id = GetInstanceId( $this );
  45.  
  46. if( is_array( $var ) ){
  47. $enc[ $id ] = $var;
  48. }else{
  49. return $enc[ $id ];
  50. }
  51. }
  52.  
  53. public function Render(){
  54. ob_start();
  55. eval( '?' . '>' . $this->GetSource() );
  56. return ob_get_clean();
  57. }
  58.  
  59. public function SetVariable( $k, $v ){
  60. if( isset( $this->$k ) ){
  61. $this->$k = $v;
  62. }
  63. }
  64.  
  65. public function SetVariables( $obj ){
  66. if( is_array( $obj ) || is_object( $obj ) ){
  67. foreach( $obj as $k => $v ){
  68. $this->SetVariable( $k, $v );
  69. }
  70. }
  71. }
  72.  
  73. public function SetVariableDefaults(){
  74. $defaults = $this->GetVariableDefault();
  75. foreach( $defaults as $k => $v ){
  76. $this->$k = $v;
  77. }
  78. }
  79.  
  80. private function GetVariables(){
  81. $defaults = array();
  82. $variables = array();
  83. if( preg_match_all( '/\$this->(\w+)[^\(]/', $this->GetSource(), $matches, PREG_SET_ORDER ) ){
  84. while( list( $_, list( $_, $name ) ) = each( $matches ) ){
  85. $defaults[ $name ] = '';
  86. }
  87. $variables = array_keys( $defaults );
  88. }
  89.  
  90. //Set the enclosure
  91. $this->GetVariableName( $variables );
  92.  
  93. ob_start();
  94. @eval( '?' . '>' . $this->GetSource() );
  95. ob_end_clean();
  96.  
  97. foreach( $defaults as $k => $v ){
  98. if( !empty( $this->$k ) ){
  99. $defaults[ $k ] = $this->$k;
  100. }
  101. }
  102.  
  103. //Set the enclosure
  104. $this->GetVariableDefault( $defaults );
  105. }
  106. }
Add Comment
Please, Sign In to add comment