Source for file jMethodSniffer.class.php

Documentation is available at jMethodSniffer.class.php

  1. <?php
  2. /**
  3. * @package    jelix
  4. * @subpackage utils
  5. * @author     Brice TencĂ©
  6. * @copyright  2012 Brice TencĂ©
  7. * @link       http://www.jelix.org
  8. * @licence    GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
  9. */
  10.  
  11. /**
  12.  * Utility class to "sniff" method calls to a class instance
  13.  * so that those calls may be repeated later on. The sniffed method should not use
  14.  * any of the magic methods used here (namely __get, __set, __call and __toString).
  15. * @package    jelix
  16. * @subpackage utils
  17. * @static
  18. */
  19. class jMethodSniffer {
  20.  
  21.     //use a single var to prevent colisions
  22.     protected $jMethodSnifferVars = null;
  23.     
  24.     public function __construct( $classInst, $instanceString='$classInstance', $notSniffed=array() ) {
  25.         $this->jMethodSnifferVars = new stdClass;
  26.         $this->jMethodSnifferVars->sniffedInstance = $classInst;
  27.         $this->jMethodSnifferVars->instanceString = $instanceString;
  28.         $this->jMethodSnifferVars->notSniffed = $notSniffed;
  29.         $this->jMethodSnifferVars->sniffed = array();
  30.     }
  31.  
  32.     public function __get( $propertyName ) {
  33.         trigger_error( "jMethodSniffer used : you should not access properties of this '".get_class($this->jMethodSnifferVars->sniffedInstance)."' instance !", E_USER_ERROR );
  34.     }
  35.  
  36.     public function __set( $propertyName, $value ) {
  37.         trigger_error( "jMethodSniffer used : you should not write properties of this '".get_class($this->jMethodSnifferVars->sniffedInstance)."' instance !", E_USER_ERROR );
  38.     }
  39.  
  40.     public function __call( $name , array $arguments ) {
  41.         if( !in_array( $name, $this->jMethodSnifferVars->notSniffed ) ) {
  42.             $this->jMethodSnifferVars->sniffed[] = array($name, $arguments);
  43.         }
  44.         return call_user_func_array( array($this->jMethodSnifferVars->sniffedInstance, $name), $arguments ); 
  45.     }
  46.  
  47.     public function __toString() {
  48.         $sniffedString = '';
  49.         foreach( $this->jMethodSnifferVars->sniffed as $sniffedItem ) {
  50.             $canUseJson = true;
  51.             foreach( $sniffedItem[1] as $methodParam ) {
  52.                 if( $canUseJson && !( is_bool($methodParam) || is_int($methodParam) ||
  53.                     is_double($methodParam) || is_float($methodParam) ||
  54.                     is_string($methodParam) ) ) {
  55.                     //json_encode / json_decode would be faster than serialize / unserialize, but this could lead to behaviour
  56.                     //differences if one (at least) of the arguments is e.g. an object ...
  57.                     $canUseJson = false;
  58.                     break;
  59.                 }
  60.             }
  61.             $encodedParams = '';
  62.             $decodingMethod = 'json_decode';
  63.             if( $canUseJson ) {
  64.                 $encodedParams = str_replace("'", "\\'", str_replace("\\", "\\\\", json_encode( $sniffedItem[1] )));
  65.             } else {
  66.                 $encodedParams = str_replace("'", "\\'", str_replace("\\", "\\\\", serialize( $sniffedItem[1] )));
  67.                 $decodingMethod = 'unserialize';
  68.             }
  69.             $sniffedString .= "call_user_func_array( array(". $this->jMethodSnifferVars->instanceString . ", '$sniffedItem[0]')" .
  70.                 ", ".$decodingMethod."('" . $encodedParams . "'));\n";
  71.         }
  72.         return $sniffedString;
  73.     }
  74. }

Documentation generated on Wed, 04 Jan 2017 22:55:57 +0100 by phpDocumentor 1.4.3