# CallChain.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2008 by Garmonbozia Research Group                      *
    00004  *                                                                         *
    00005  *   This program is free software; you can redistribute it and/or modify  *
    00006  *   it under the terms of the GNU Lesser General Public License as        *
    00007  *   published by the Free Software Foundation; either version 3 of the    *
    00008  *   License, or (at your option) any later version.                       *
    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 ?>