Changeset 19344 for lang/php

Show
Ignore:
Timestamp:
09/16/08 01:32:33 (2 months ago)
Author:
nowelium
Message:
 
Location:
lang/php/misc/Hermit/src/Hermit
Files:
21 added
8 modified

Legend:

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

    r19017 r19344  
    44 * @author nowelium 
    55 */ 
    6 abstract class Hermit { 
    7     public static $classMap = array(); 
    8     public static function bind($targetClass, $dao){ 
    9         self::$classMap[$targetClass] = $dao; 
     6class Hermit { 
     7    protected $listeners = array(); 
     8    protected $delegaters = array(); 
     9 
     10    public function __construct($class){ 
     11        $this->proxy = self::__create($class); 
    1012    } 
    11     public static function create(PDO $pdo, $class = null){ 
    12         if(is_null($class)){ 
    13             $e = new Exception; 
    14             $trace = $e->getTrace(); 
    15             return Hermit::create($pdo, $trace[1]['class']); 
     13    public function __call($name, $parameters = array()){ 
     14        if(isset($this->delegater[$name])){ 
     15            return self::__request($this->delegater[$name], $name, $parameters); 
    1616        } 
     17        return self::__request($this->proxy, $name, $parameters); 
     18    } 
     19    protected static function __request(Hermit $hermit, $name, array $params){ 
     20        return $hermit->request($name, $params); 
     21    } 
     22    protected static function __create($class){ 
    1723        if(is_object($class)){ 
    18             return HermitClassProxy::delegate($pdo, new ReflectionObject($class), $class); 
     24            return HermitObjectProxy::delegate(new ReflectionObject($class), $class); 
    1925        } 
    2026        if(isset(Hermit::$classMap[$class])){ 
    21             return self::createProxy($pdo, Hermit::$classMap[$class]); 
     27            return self::__createProxy(Hermit::$classMap[$class]); 
    2228        } 
    2329        if(class_exists($class)){ 
    24             return self::create($pdo, $class); 
     30            return self::__createProxy($class); 
    2531        } 
    26         throw new InvalidArgumentException('nosuch class: ' . $class); 
     32        throw new RuntimeException('Hermit does not create: ' . $class); 
    2733    } 
    28     protected static function createProxy(PDO $pdo, $targetClass){ 
     34    protected static function __createProxy($targetClass){ 
    2935        $reflector = new ReflectionClass($targetClass); 
    3036        if($reflector->isInterface()){ 
    31             return HermitInterfaceProxy::delegate($pdo, $reflector); 
     37            return HermitInterfaceProxy::delegate($reflector); 
    3238        } 
    33         return HermitFutureProxy::delegate($pdo, $reflector, $targetClass); 
     39        return HermitClassProxy::delegate($reflector); 
    3440    } 
    3541} 
     42 
  • lang/php/misc/Hermit/src/Hermit/HermitAutoloader.php

    r19017 r19344  
    11<?php 
    22 
     3/** 
     4 * @author nowelium 
     5 */ 
    36class HermitAutoloader { 
    47    private static $classPath = array( 
     
    912        '/meta', 
    1013        '/creator', 
    11         '/command' 
     14        '/command', 
     15        '/statement', 
     16        '/resultset', 
     17        '/responder', 
     18        '/tx' 
    1219    ); 
    1320    private static $userClassPath = array(); 
  • lang/php/misc/Hermit/src/Hermit/HermitDao.php

    r19017 r19344  
    22 
    33/** 
    4  * @authro nowelium 
     4 * @author nowelium 
    55 */ 
    66interface HermitDao { 
  • lang/php/misc/Hermit/src/Hermit/command/HermitSqlCommandFactory.php

    r19017 r19344  
    11<?php 
    22 
     3/** 
     4 * @author nowelium 
     5 */ 
    36class HermitSqlCommandFactory { 
    47    protected $annote; 
    58    protected $reflector; 
    6     protected $hasCommand = array(); 
     9    protected $createdCommands = array(); 
    710    public function __construct(ReflectionClass $reflector){ 
    811        $this->annote = HermitAnnote::create($reflector); 
     
    1316    } 
    1417    public function getCommand($methodName){ 
    15         $command = new HermitSqlCommand; 
    16  
    17         return $command; 
     18        $method = $this->annote->getMethod($methodName); 
     19        $methodId = spl_object_hash($method); 
     20        if(isset($this->createdCommands[$methodId])){ 
     21            return $this->createdCommands[$methodId]; 
     22        } 
     23        $command = $this->createCommand($method); 
     24        return $this->createdCommands[$methodId] = $command; 
     25    } 
     26    protected function createCommand(ReflectionMethod $method){ 
     27        if($this->annote->isInsertMethod($methodName)){ 
     28            return $this->createInsertCommand($method); 
     29        } 
     30        if($this->annote->isUpdateMethod($methodName)){ 
     31            return $this->createUpdateCommand($method); 
     32        } 
     33        if($this->annote->isDeleteMethod($methodName)){ 
     34            return $this->createDeleteCommand($method); 
     35        } 
     36        return $this->createSelectCommand($method); 
     37    } 
     38    protected function createInsertCommand(ReflectionMethod $method){ 
     39    } 
     40    protected function createUpdateCommand(ReflectionMethod $method){ 
     41    } 
     42    protected function createSelectCommand(ReflectionMethod $method){ 
     43        $creator = $this->createSelectSqlCreator($method->getName()); 
     44        return new HermitSelectCommand($method, $creator); 
     45    } 
     46    protected function createSelectSqlCreator($name){ 
     47        switch(true){ 
     48        case $this->annote->hasSql($name): 
     49            return new HermitStaticSqlCreator($this->annote->getSql($name)); 
     50        case $this->annote->hasFile($name): 
     51            return new HermitStaticSqlCreator($this->annote->getFile($name)); 
     52        case $this->annote->hasPath($name): 
     53            return new HermitStaticSqlCreator($this->annote->getPath($name)); 
     54        case $this->annote->hasQuery($name): 
     55            $creator = new HermitAutoSelectCreator; 
     56            $creator->addQuery($this->annote->getQuery()); 
     57            return $creator; 
     58        } 
     59        throw new BadMethodCallException('invalid method:' . $name); 
    1860    } 
    1961} 
  • lang/php/misc/Hermit/src/Hermit/proxy/HermitClassProxy.php

    r19017 r19344  
    44 * @author nowelium 
    55 */ 
    6 class HermitClassProxy implements HermitDao { 
    7     protected $target; 
    8     protected $pdo; 
    9     protected function __construct(ReflectionClass $reflector, $targetClass, PDO $pdo = null){ 
    10         $this->target = $targetClass; 
    11         $this->reflector = $reflector; 
    12         $this->pdo = $pdo; 
    13     } 
    14     public static function delegate(PDO $pdo, ReflectionClass $reflector, $instance = null){ 
    15         return new self($reflector, $instance, $pdo); 
    16     } 
    17     public function __call($name, $params = array()){ 
    18         $annote = HermitAnnote::create($this->reflector); 
    19         if($annote->hasMethod($name, true)){ 
    20             $method = $annote->getMethod($name); 
    21             return $method->invokeArgs($this->target, $params); 
    22         } 
    23         throw new BadMethodCallException(get_class($this->target) . '::' . $name); 
     6class HermitClassProxy implements HermitProxy { 
     7    public function request($name, array $params){ 
    248    } 
    259} 
  • lang/php/misc/Hermit/src/Hermit/proxy/HermitFutureProxy.php

    r19017 r19344  
    44 * @author nowelium 
    55 */ 
    6 class HermitFutureProxy implements HermitProxy { 
    7     public static function delegate(PDO $pdo, ReflectionClass $reflector, $target = null){ 
    8     } 
     6interface HermitFutureProxy extends HermitProxy { 
     7    public static function delegate(PDO $pdo, ReflectionClass $reflector, $instance = null); 
    98} 
  • lang/php/misc/Hermit/src/Hermit/proxy/HermitInterfaceProxy.php

    r19017 r19344  
    44 * @author nowelium 
    55 */ 
    6 class HermitInterfaceProxy implements HermitProxy { 
    7     protected $pdo; 
     6class HermitInterfaceProxy implements HermitFutureProxy { 
    87    protected $reflector; 
    9     protected function __construct(ReflectionClass $reflector, $pdo){ 
     8    protected $commandFactory; 
     9    protected function __construct(ReflectionClass $reflector){ 
    1010        $this->reflector = $reflector; 
    11         $this->pdo = $pdo; 
     11        $this->commandFactory = new HermitSqlCommandFactory($reflector); 
    1212    } 
    13     public static function delegate(PDO $pdo, ReflectionClass $reflector, $instance = null){ 
    14         return new self($reflector, $pdo); 
     13    public static function delegate(ReflectionClass $reflector, $instance = null){ 
     14        return new self($reflector); 
    1515    } 
    16     public function __call($name, $params = array()){ 
    17         $annote = HermitAnnote::create($this->reflector); 
    18         if(!$annote->hasMethod($name)){ 
     16    public function request($name, array $params){ 
     17        if(!$this->commandFactory->hasCommand($name)){ 
    1918            throw new BadMethodCallException($this->reflector->getName() . '::' . $name); 
    2019        } 
    21  
    22         $method = $annote->getMethod($name); 
    23         switch(true){ 
    24         case $annote->hasSql($name): 
    25             return $this->execute($method, $params, $annote->getSql($name)); 
    26         case $annote->hasQuery($name): 
    27             $sql = 'SELECT * FROM ' . $annote->getTable() . ' WHERE ' . $annote->getQuery($name); 
    28             return $this->execute($method, $params, $sql); 
    29         case $annote->hasFile($name): 
    30             return $this->execute($method, $params, $annote->getFile($name)); 
    31         case $annote->hasPath($name): 
    32             return $this->execute($method, $params, $annote->getPath($name)); 
    33         case $annote->hasDelegate($name); 
    34             break; 
    35         } 
    36         throw new BadMethodCallException($this->reflector->getName() . '::' . $name); 
    37     } 
    38     protected function execute(ReflectionMethod $method, $params, $sql){ 
    39         $stmt = HermitStatementBuilder::prepare($this->pdo, $method, $sql); 
    40         $stmt->execute($params); 
    41         return HermitResultSet::create($stmt, $method); 
     20        $command = $this->commandFactory->getCommand($name); 
     21        return $command->execute($pdo, $params); 
    4222    } 
    4323} 
  • lang/php/misc/Hermit/src/Hermit/proxy/HermitProxy.php

    r19017 r19344  
    55 */ 
    66interface HermitProxy extends HermitDao { 
    7     public static function delegate(PDO $pdo, ReflectionClass $reflector, $instance = null); 
     7    public function request($name, array $parameters); 
    88}