PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/jelix-legacy/utils/jMethodSniffer.class.php

https://github.com/gmarrot/jelix
PHP | 74 lines | 48 code | 7 blank | 19 comment | 6 complexity | 54ad7f85d8e67a94d16e3ceb2f73f7e4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  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. * Utility class to "sniff" method calls to a class instance
  12. * so that those calls may be repeated later on. The sniffed method should not use
  13. * any of the magic methods used here (namely __get, __set, __call and __toString).
  14. * @package jelix
  15. * @subpackage utils
  16. * @static
  17. */
  18. class jMethodSniffer {
  19. //use a single var to prevent colisions
  20. protected $jMethodSnifferVars = null;
  21. public function __construct( $classInst, $instanceString='$classInstance', $notSniffed=array() ) {
  22. $this->jMethodSnifferVars = new stdClass;
  23. $this->jMethodSnifferVars->sniffedInstance = $classInst;
  24. $this->jMethodSnifferVars->instanceString = $instanceString;
  25. $this->jMethodSnifferVars->notSniffed = $notSniffed;
  26. $this->jMethodSnifferVars->sniffed = array();
  27. }
  28. public function __get( $propertyName ) {
  29. trigger_error( "jMethodSniffer used : you should not access properties of this '".get_class($this->jMethodSnifferVars->sniffedInstance)."' instance !", E_USER_ERROR );
  30. }
  31. public function __set( $propertyName, $value ) {
  32. trigger_error( "jMethodSniffer used : you should not write properties of this '".get_class($this->jMethodSnifferVars->sniffedInstance)."' instance !", E_USER_ERROR );
  33. }
  34. public function __call( $name , array $arguments ) {
  35. if( !in_array( $name, $this->jMethodSnifferVars->notSniffed ) ) {
  36. $this->jMethodSnifferVars->sniffed[] = array($name, $arguments);
  37. }
  38. return call_user_func_array( array($this->jMethodSnifferVars->sniffedInstance, $name), $arguments );
  39. }
  40. public function __toString() {
  41. $sniffedString = '';
  42. foreach( $this->jMethodSnifferVars->sniffed as $sniffedItem ) {
  43. $canUseJson = true;
  44. foreach( $sniffedItem[1] as $methodParam ) {
  45. if( $canUseJson && !( is_bool($methodParam) || is_int($methodParam) ||
  46. is_double($methodParam) || is_float($methodParam) ||
  47. is_string($methodParam) ) ) {
  48. //json_encode / json_decode would be faster than serialize / unserialize, but this could lead to behaviour
  49. //differences if one (at least) of the arguments is e.g. an object ...
  50. $canUseJson = false;
  51. break;
  52. }
  53. }
  54. $encodedParams = '';
  55. $decodingMethod = 'json_decode';
  56. if( $canUseJson ) {
  57. $encodedParams = str_replace("'", "\\'", str_replace("\\", "\\\\", json_encode( $sniffedItem[1] )));
  58. } else {
  59. $encodedParams = str_replace("'", "\\'", str_replace("\\", "\\\\", serialize( $sniffedItem[1] )));
  60. $decodingMethod = 'unserialize';
  61. }
  62. $sniffedString .= "call_user_func_array( array(". $this->jMethodSnifferVars->instanceString . ", '$sniffedItem[0]')" .
  63. ", ".$decodingMethod."('" . $encodedParams . "'));\n";
  64. }
  65. return $sniffedString;
  66. }
  67. }