Changeset 19017 for lang/php

Show
Ignore:
Timestamp:
09/09/08 00:44:36 (2 months ago)
Author:
nowelium
Message:
 
Location:
lang/php/misc/Hermit
Files:
13 added
7 modified

Legend:

Unmodified
Added
Removed
  • lang/php/misc/Hermit/example/employee/hoge.php

    r18987 r19017  
    22 
    33require dirname(__FILE__) . '/../example.php'; 
     4HermitAutoloader::import(dirname(__FILE__)); 
     5HermitAutoloader::import(dirname(__FILE__) . '/dao'); 
    46 
    57class Hoge { 
     
    1618} 
    1719 
     20Hermit::bind('Hoge', 'EmployeeDao'); 
    1821$pdo = new PDO('sqlite:' . dirname(__FILE__) . '/resource/employee.db'); 
    1922$hoge = new Hoge; 
  • lang/php/misc/Hermit/src/Hermit/Hermit.php

    r18988 r19017  
    44 * @author nowelium 
    55 */ 
    6 class Hermit { 
    7     public static $classMap = array( 
    8         'Hoge' => 'EmployeeDao' 
    9     ); 
     6abstract class Hermit { 
     7    public static $classMap = array(); 
     8    public static function bind($targetClass, $dao){ 
     9        self::$classMap[$targetClass] = $dao; 
     10    } 
    1011    public static function create(PDO $pdo, $class = null){ 
    1112        if(is_null($class)){ 
     
    1516        } 
    1617        if(is_object($class)){ 
    17             return HermitProxy::delegateInstance($pdo, $class); 
     18            return HermitClassProxy::delegate($pdo, new ReflectionObject($class), $class); 
    1819        } 
    1920        if(isset(Hermit::$classMap[$class])){ 
     
    2728    protected static function createProxy(PDO $pdo, $targetClass){ 
    2829        $reflector = new ReflectionClass($targetClass); 
    29         /* 
    30         if($reflector->implementsInterface('HermitDao')){ 
    31             $instance = $reflector->newInstance(); 
    32             $instance->setPDO($pdo); 
    33             return HermitProxy::delegateInstance($pdo, $instance); 
    34         } 
    35         */ 
    3630        if($reflector->isInterface()){ 
    3731            return HermitInterfaceProxy::delegate($pdo, $reflector); 
    3832        } 
    39         return HermitFutureProxy::delegate($pdo, $targetClass); 
     33        return HermitFutureProxy::delegate($pdo, $reflector, $targetClass); 
    4034    } 
    4135} 
  • lang/php/misc/Hermit/src/Hermit/HermitAutoloader.php

    r18988 r19017  
    22 
    33class HermitAutoloader { 
     4    private static $classPath = array( 
     5        '/', 
     6        '/annote', 
     7        '/parameter', 
     8        '/proxy', 
     9        '/meta', 
     10        '/creator', 
     11        '/command' 
     12    ); 
     13    private static $userClassPath = array(); 
     14    private static $unmatch = array(); 
     15    private static $current; 
     16    public static function initialize(){ 
     17        self::$current = dirname(__FILE__); 
     18        spl_autoload_register(array(__CLASS__, 'autoload')); 
     19    } 
     20    public static function import($dir){ 
     21        self::$userClassPath[] = $dir; 
     22    } 
     23    public static function autoload($className){ 
     24        if(self::classload(self::$classPath, $className, self::$current)){ 
     25            return true; 
     26        } 
     27        if(self::classload(self::$userClassPath, $className)){ 
     28            return true; 
     29        } 
     30        return false; 
     31    } 
     32    protected static function classload(array $pathes, $className, $prefix = ''){ 
     33        $file = $className . '.php'; 
     34        foreach($pathes as $path){ 
     35            $filePath = $prefix . $path . DIRECTORY_SEPARATOR . $file; 
     36            if(isset(self::$unmatch[$filePath]) && self::$unmatch[$filePath]){ 
     37                continue; 
     38            } 
     39            if(file_exists($filePath)){ 
     40                require $filePath; 
     41                return true; 
     42            } 
     43            self::$unmatch[$filePath] = true; 
     44        } 
     45        return false; 
     46    } 
    447} 
     48 
     49HermitAutoloader::initialize(); 
  • lang/php/misc/Hermit/src/Hermit/HermitDao.php

    r18988 r19017  
    55 */ 
    66interface HermitDao { 
    7     public function setPDO(PDO $pdo); 
    87} 
  • lang/php/misc/Hermit/src/Hermit/proxy/HermitFutureProxy.php

    r18954 r19017  
    44 * @author nowelium 
    55 */ 
    6 class HermitFutureProxy { 
    7     protected $pdo; 
    8     protected $target; 
    9     public static function delegate(PDO $pdo, $target){ 
    10         $this->pdo = $pdo; 
    11         $this->target = $target; 
     6class HermitFutureProxy implements HermitProxy { 
     7    public static function delegate(PDO $pdo, ReflectionClass $reflector, $target = null){ 
    128    } 
    139} 
  • lang/php/misc/Hermit/src/Hermit/proxy/HermitInterfaceProxy.php

    r18954 r19017  
    44 * @author nowelium 
    55 */ 
    6 class HermitInterfaceProxy { 
     6class HermitInterfaceProxy implements HermitProxy { 
    77    protected $pdo; 
    88    protected $reflector; 
     
    1111        $this->pdo = $pdo; 
    1212    } 
    13     public static function delegate(PDO $pdo, ReflectionClass $reflector){ 
     13    public static function delegate(PDO $pdo, ReflectionClass $reflector, $instance = null){ 
    1414        return new self($reflector, $pdo); 
    1515    } 
     
    2323        switch(true){ 
    2424        case $annote->hasSql($name): 
    25             return $this->execute($method, $annote->getSql($name)); 
     25            return $this->execute($method, $params, $annote->getSql($name)); 
    2626        case $annote->hasQuery($name): 
    2727            $sql = 'SELECT * FROM ' . $annote->getTable() . ' WHERE ' . $annote->getQuery($name); 
    28             return $this->execute($method, $sql); 
     28            return $this->execute($method, $params, $sql); 
    2929        case $annote->hasFile($name): 
    30             return $this->execute($method, $annote->getFile($name)); 
     30            return $this->execute($method, $params, $annote->getFile($name)); 
    3131        case $annote->hasPath($name): 
    32             return $this->execute($method, $annote->getPath($name)); 
     32            return $this->execute($method, $params, $annote->getPath($name)); 
    3333        case $annote->hasDelegate($name); 
    3434            break; 
     
    3636        throw new BadMethodCallException($this->reflector->getName() . '::' . $name); 
    3737    } 
    38     protected static function execute(ReflectionMethod $method, $sql){ 
    39         $stmt = HermitSqlBuilder::prepare($this->pdo, $method, $sql); 
     38    protected function execute(ReflectionMethod $method, $params, $sql){ 
     39        $stmt = HermitStatementBuilder::prepare($this->pdo, $method, $sql); 
    4040        $stmt->execute($params); 
    4141        return HermitResultSet::create($stmt, $method); 
  • lang/php/misc/Hermit/src/Hermit/proxy/HermitProxy.php

    r18954 r19017  
    44 * @author nowelium 
    55 */ 
    6 class HermitProxy { 
    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 delegateInstance(PDO $pdo, $instance){ 
    15         return new self(new ReflectionObject($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         switch(true){ 
    24         case $annote->hasSql($name): 
    25             break; 
    26         case $annote->hasQuery($name): 
    27             break; 
    28         case $annote->hasFile($name): 
    29             break; 
    30         case $annote->hasPath($name): 
    31             break; 
    32         case $annote->hasDelegate($name): 
    33             break; 
    34         } 
    35         throw new BadMethodCallException(get_class($this->target) . '::' . $name); 
    36     } 
     6interface HermitProxy extends HermitDao { 
     7    public static function delegate(PDO $pdo, ReflectionClass $reflector, $instance = null); 
    378}