PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/Engine/ProxyObject.php

https://github.com/shopaholiccompany/shopaholic
PHP | 110 lines | 48 code | 8 blank | 54 comment | 6 complexity | 04104df4295a98e565ba8a24025cce74 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Engine
  6. * @package Engine_ProxyObject
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: ProxyObject.php 7244 2010-09-01 01:49:53Z john $
  10. * @todo documentation
  11. */
  12. /**
  13. * @category Engine
  14. * @package Engine_ProxyObject
  15. * @copyright Copyright 2006-2010 Webligo Developments
  16. * @license http://www.socialengine.net/license/
  17. */
  18. class Engine_ProxyObject
  19. {
  20. /**
  21. * The object to request calls through.
  22. *
  23. * @var object
  24. */
  25. protected $_sender;
  26. /**
  27. * The object to receive calls
  28. *
  29. * @var object
  30. */
  31. protected $_receiver;
  32. /**
  33. * Constructor
  34. *
  35. * @param object $sender The object to request calls through.
  36. * @param object $receiver The object to receive calls
  37. */
  38. public function __construct($sender, $receiver)
  39. {
  40. if( !is_object($sender) || !is_object($receiver) )
  41. {
  42. throw new Engine_Exception('Sender and reciever must both be objects');
  43. }
  44. $this->_sender = $sender;
  45. $this->_receiver = $receiver;
  46. }
  47. /**
  48. * Proxy emulation
  49. *
  50. * @param string $method
  51. * @param array $arguments
  52. * @return mixed
  53. */
  54. public function __call($method, array $arguments)
  55. {
  56. // Requested method
  57. if( method_exists($this->_receiver, $method) )
  58. {
  59. $r = new ReflectionMethod($this->_receiver, $method);
  60. array_unshift($arguments, $this->_sender);
  61. $return = $r->invokeArgs($this->_receiver, $arguments);
  62. // Hack to make method chaining work
  63. if( $return === $this->_receiver )
  64. {
  65. return $this;
  66. }
  67. return $return;
  68. }
  69. // __call
  70. if( method_exists($this->_receiver, '__call') )
  71. {
  72. array_unshift($arguments, $this->_sender);
  73. $return = $this->_receiver->__call($method, $arguments);
  74. // Hack to make method chaining work
  75. if( $return === $this->_receiver )
  76. {
  77. return $this;
  78. }
  79. return $return;
  80. }
  81. // Whoops, method doesn't exist
  82. throw new Engine_Exception(sprintf('ProxyObject method "%s" does not exist and could not be trapped in __call().', $method));
  83. }
  84. /**
  85. * Gets the sender
  86. *
  87. * @return object
  88. */
  89. public function getSender()
  90. {
  91. return $this->_sender;
  92. }
  93. /**
  94. * Gets the receiver
  95. *
  96. * @return object
  97. */
  98. public function getReceiver()
  99. {
  100. return $this->_receiver;
  101. }
  102. }