Changeset 20314 for lang/php

Show
Ignore:
Timestamp:
09/30/08 21:36:14 (2 months ago)
Author:
nowelium
Message:
 
Location:
lang/php/misc/Hermit
Files:
14 added
2 removed
6 modified

Legend:

Unmodified
Added
Removed
  • lang/php/misc/Hermit/src/Hermit/Hermit.php

    r20200 r20314  
    66class Hermit { 
    77    protected $proxy; 
    8     protected $listeners = array(); 
    9     protected $delegators = array(); 
    10     protected static $wrappers = array(); 
     8    protected $calls = array(); 
     9    protected static $behaviors = array(); 
    1110    public function __construct($class = null){ 
    1211        if(is_null($class)){ 
     
    2120    } 
    2221    public function __call($name, $parameters = array()){ 
    23         if(isset($this->delegators[$name])){ 
    24             $delegator = $this->delegators[$name]; 
    25             return self::__request($delegator, $name, $parameters); 
    26         } 
    27         if(isset($this->listeners[$name])){ 
    28             $response = self::__request($this->proxy, $name, $parameters); 
    29             $listeners = $this->listeners[$name]; 
    30             foreach($listeners as $listener){ 
    31                 self::__request($listener, $name, array($response)); 
     22        if(0 < count($this->calls)){ 
     23            foreach($this->calls as $call){ 
     24                if($call->has($name)){ 
     25                    return $call->execute($this->proxy, $name, $parameters); 
     26                } 
    3227            } 
    33             return $response; 
    3428        } 
    3529        return self::__request($this->proxy, $name, $parameters); 
     
    4943    } 
    5044    protected static function wrap(HermitProxy $proxy, $targetClass){ 
    51         if(HermitTransactionManager::has($targetClass)){ 
    52             return HermitTransactionManager::createProxy($proxy, $targetClass); 
     45        if(0 < count(self::$behaviors)){ 
     46            foreach(self::$behaviors as $behavior){ 
     47                if($behavior->has($targetClass)){ 
     48                    return $behavior->createProxy($proxy, $targetClass); 
     49                } 
     50            } 
    5351        } 
    5452        return $proxy; 
  • lang/php/misc/Hermit/src/Hermit/HermitTransactionManager.php

    r20152 r20314  
    44 * @author nowelium 
    55 */ 
    6 abstract class HermitTransactionManager { 
    7     private static $transactionScripts = array(); 
     6class HermitTransactionManager implements HermitBehaviorWrapper { 
     7    private static $instance; 
     8    private $transactionScripts = array(); 
    89    private function __construct(){ 
    910        // nop 
    1011    } 
    11     public static function set($targetClass, HermitTx $tx){ 
     12    protected static function getInstance(){ 
     13        if(null === self::$instance){ 
     14            self::$instance = new self; 
     15        } 
     16        if(!HermitRegister::hasBehavior(__CLASS__)){ 
     17            HermitRegister::putBehavior(__CLASS__, self::$instance); 
     18        } 
     19        return self::$instance; 
    1220    } 
    13     public static function get($targetClass){ 
     21    public function set($targetClass, HermitTx $tx){ 
     22        $instance = self::getInstance(); 
     23        $instance->transactionScripts[$targetClass] = $tx; 
    1424    } 
    15     public static function has($targetClass){ 
     25    public function get($targetClass){ 
     26        $instance = self::getInstance(); 
     27        if(isset($instance->transactionScripts[$targetClass])){ 
     28            return null; 
     29        } 
     30        return $instance->transactionScripts[$targetClass]; 
    1631    } 
    17     public static function createProxy(HermitProxy $proxy, $targetClass){ 
     32    public function has($targetClass){ 
     33        $instance = self::getInstance(); 
     34        return isset($instance->transactionScripts[$targetClass]); 
     35    } 
     36    public function createProxy(HermitProxy $proxy, $targetClass){ 
    1837        $tx = self::get($targetClass); 
    1938        return new HermitCallableProxy($proxy, array($tx, 'proceed')); 
  • lang/php/misc/Hermit/src/Hermit/command/HermitSqlCommandFactory.php

    r20200 r20314  
    1818    } 
    1919    public function create(PDO $pdo, $methodName){ 
     20        if(isset($this->createdCommands[$methodName])){ 
     21            return $this->createdCommands[$methodName]; 
     22        } 
    2023        $method = $this->annote->getMethod($methodName); 
    21         $methodId = spl_object_hash($method); 
    22         if(isset($this->createdCommands[$methodId])){ 
    23             return $this->createdCommands[$methodId]; 
    24         } 
    2524        $command = $this->createCommand($pdo, $method); 
    26         return $this->createdCommands[$methodId] = $command; 
     25        return $this->createdCommands[$methodName] = $command; 
    2726    } 
    2827    protected function createCommand(PDO $pdo, ReflectionMethod $method){ 
  • lang/php/misc/Hermit/src/Hermit/meta/HermitMySQLDatabaseMeta.php

    r20200 r20314  
    55 */ 
    66class HermitMySQLDatabaseMeta implements HermitDatabaseMeta { 
    7     const TABLE_INFO_SQL = 'SELECT * FROM :TABLE LIMIT 0'; 
     7    const USING_DB_NAME_SQL = 'SELECT database()'; 
     8    const TABLE_INFO_SQL = 'SELECT * FROM %s LIMIT 0'; 
     9    const PROCEDIRE_INFO_SQL = 'SELECT param_list, returns FROM mysql.proc WHERE db = :db AND name = :name'; 
    810    private $tables = array(); 
     11    private $procedures = array(); 
    912    private $pdo; 
     13    private $databaseName; 
    1014    public function __construct(PDO $pdo){ 
    1115        $this->pdo = $pdo; 
     16        $this->databaseName = $this->getUsingDatabaseName(); 
    1217    } 
    1318    public function getTableInfo($table){ 
     
    1520            return $this->tables[$table]; 
    1621        } 
    17  
    18         $stmt = $this->pdo->prepare(self::TABLE_INFO_SQL); 
    19         $stmt->execute(array(':TABLE' => $table)); 
     22         
     23        $sql = sprintf(self::TABLE_INFO_SQL, $table); 
     24        $stmt = $this->pdo->prepare($sql); 
     25        $stmt->execute(); 
    2026        $count = $stmt->columnCount(); 
    2127        $info = new HermitTableInfo; 
     
    3440    } 
    3541    public function getProcedureInfo($procedure){ 
    36         throw new RuntimeException('T.B.D'); 
     42        if(isset($this->procedures[$procedure])){ 
     43            return $this->procedures[$procedure]; 
     44        } 
     45        $stmt = $this->pdo->prepare(self::PROCEDIRE_INFO_SQL); 
     46        $stmt->execute(array(':db' => $this->databaseName, ':name' => $procedure)); 
     47        $paramList = $stmt->fetchColumn(0); 
     48 
     49        $info = new HermitProcedureInfo; 
     50        $chunk = array_map('trim', explode(',', $paramList)); 
     51        foreach($chunk as $field){ 
     52            $sp = preg_split('/\s+/', $field); 
     53 
     54            $inoutType = null; 
     55            $paramName = null; 
     56            $paramType = null; 
     57 
     58            $upper = strtoupper($sp[0]); 
     59            if(in_array($upper, array('IN', 'OUT', 'INOUT'), true)){ 
     60                $inoutType = $upper; 
     61                $paramName = $sp[1]; 
     62                $paramType = $sp[2]; 
     63            } else { 
     64                // default IN: http://dev.mysql.com/doc/refman/5.1/ja/create-procedure.html 
     65                $inoutType = 'IN'; 
     66                $paramName = $sp[0]; 
     67                $paramType = $sp[1]; 
     68            } 
     69            $info->addParamName($paramName); 
     70            $info->putParamType($paramName, $paramType); 
     71            if(strcmp('IN', $inoutType) === 0){ 
     72                $info->putInType($paramName); 
     73            } else if(strcmp('OUT', $inoutType) === 0){ 
     74                $info->putOutType($paramName); 
     75            } else { 
     76                $info->putInOutType($paramName); 
     77            } 
     78        } 
     79 
     80        return $this->procedures[$procedure] = $info; 
     81    } 
     82 
     83    protected function getUsingDatabaseName(){ 
     84        $stmt = $this->pdo->prepare(self::USING_DB_NAME_SQL); 
     85        $stmt->execute(); 
     86        return $stmt->fetchColumn(0); 
    3787    } 
    3888} 
  • lang/php/misc/Hermit/src/Hermit/valuetype/HermitValueTypeFactory.php

    r20200 r20314  
    1111    public static function create(HermitAnnote $annote, ReflectionMethod $method){ 
    1212        $value = $annote->getValueType($method); 
     13        if(null === $value){ 
     14          return new HermitNopValueType($annote, $method, null); 
     15        } 
    1316        foreach(self::$valueTypes as $type){ 
    1417            if(call_user_func(array($type, 'accept'), $value)){ 
     
    1619            } 
    1720        } 
    18         return new HermitNopValueType; 
     21        return new HermitNopValueType($annote, $method, $value); 
    1922    } 
    2023} 
  • lang/php/misc/Hermit/test/Hermit_TransactionManagerWrapTest.php

    r20200 r20314  
    11<?php 
    22require dirname(__FILE__) . '/setup.php'; 
    3