Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00015 final class CallChain
00016 {
00017 private $chain = array();
00018
00022 public static function create()
00023 {
00024 return new self;
00025 }
00026
00030 public function add($object)
00031 {
00032 $this->chain[] = $object;
00033
00034 return $this;
00035 }
00036
00037 public function call($method, $args = null )
00038 {
00039 if (!$this->chain)
00040 throw new WrongStateException();
00041
00042 $args = func_get_args();
00043 array_shift($args);
00044
00045 if (count($args)) {
00046 $result = $args;
00047 foreach ($this->chain as $object)
00048 $result = call_user_func_array(
00049 array($object, $method),
00050 is_array($result)
00051 ? $result
00052 : array($result)
00053 );
00054 } else {
00055 foreach ($this->chain as $object)
00056 $result = call_user_func(array($object, $method));
00057 }
00058
00059 return $result;
00060 }
00061
00062 public function __call($method, $args = null)
00063 {
00064 return call_user_func_array(
00065 array($this, 'call'),
00066 array_merge(array($method), $args)
00067 );
00068 }
00069 }
00070 ?>