一括実行系ななにか

分散系のこと考えてた時に出来たなにか

<?php
class LumpedCollection {                                                                                                                
  protected $_container = array();                                                                                                      
  protected $_method_cache = array();                                                                                                   
  protected $_class_name;                                                                                                               
                                                                                                                                        
  public function __construct(array $objs = null) {                                                                                     
    if($objs === null || count($objs) == 0) return;                                                                                      
    $this->_class_name = get_class($objs[0]);                                                                                           
    $this->_container = $objs;                                                                                                          
  }                                                                                                                                     
                                                                                                                                        
  public function append(stdClass $obj) {                                                                                               
    $obj_class = get_class($obj);                                                                                                       
                                                                                                                                        
    if($this->_class_name == null)                                                                                                      
      $this->_class_name = $obj_class;                                                                                                  
    else if ($obj_class !== $this->_class_name)                                                                                         
      throw new InvalidArgumentsException();                                                                                            
                                                                                                                                        
    $this->_container[] = $obj;                                                                                                         
  }                                                                                                                                     
                                                                                                                                        
  public function __call($name, $args) {                                                                                               
    $method = (isset($this->method_cache[$name]))                                                                                       
        ? $this->method_cache[$name]                                                                                                    
        : $this->method_cache[$name] = new ReflectionMethod($this->_class_name,$name);                                                  
                                                                                                                                        
    $results = array();                                                                                                                 
    foreach($this->_container as $obj) {                                                                                                
      $instance = ($method->isStatic()) ? null : $obj;                                                                                  
      $result = $method->invokeArgs($instance, $args);                                                                                  
      $results = $this->merge_result($result, $results);                                                                                
    }                                                                                                                                   
                                                                                                                                        
    return $results;                                                                                                                    
  }                                                                                                                                     
                                                                                                                                        
  protected function merge_result($result, $results) {                                                                                  
    if ($result !== null && is_array($result))                                                                                          
      $results = array_merge($results,$result);                                                                                         
    else                                                                                                                                
      $results[] = $result;                                                                                                             
                                                                                                                                        
    return $results;                                                                                                                    
  }
}                                                                                                                                       
                                                                                                                                        
class LumpedCollectionTest extends stdClass {                                                                                           
  private static $value = "aaa";                                                                                                        
  private $foo;                                                                                                                         
  public function setFoo($value) {                                                                                                      
    $this->foo = $value;                                                                                                                
    return true;                                                                                                                        
  }                                                                                                                                     
  public static function getValue() {                                                                                                   
    return self::$value;                                                                                                                
  }                                                                                                                                     
  public function getFoo() {                                                                                                            
    return $this->foo;                                                                                                                  
  }                                                                                                                                     
                                                                                                                                        
  public function getHash() {                                                                                                           
    return array('hoge' => 1,'hage' => 2, 'var' => array(1,2));                                                                         
  }                                                                                                                                     
                                                                                                                                        
  public function getArray() {                                                                                                          
    return array(1,2);                                                                                                                  
  }                                                                                                                                     
}                                                                                                                                       
                                                                                                                                        
$container = new LumpedCollection();                                                                                                    
$container->append(new LumpedCollectionTest());                                                                                         
$container->append(new LumpedCollectionTest());                                                                                         
$container->append(new LumpedCollectionTest());                                                                                         
$container->append(new LumpedCollectionTest());                                                                                         
                                                                                                                                        
$ret = $container->setFoo("foo");                                                                                                       
print_r($ret);                                                                                                                          
$ret = $container->getValue();                                                                                                          
print_r($ret);                                                                                                                          
$ret = $container->getFoo();                                                                                                            
print_r($ret);                                                                                                                          
$ret = $container->getArray();                                                                                                          
print_r($ret);                                                                                                                         
$ret = $container->getHash();                                                                                                          
print_r($ret);