Changeset 19426

Show
Ignore:
Timestamp:
09/17/08 13:55:47 (4 months ago)
Author:
nowelium
Message:
 
Location:
lang/php/misc/Hermit/src/Hermit
Files:
3 added
11 modified

Legend:

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

    r19344 r19426  
    77    protected $listeners = array(); 
    88    protected $delegaters = array(); 
    9  
    109    public function __construct($class){ 
    1110        $this->proxy = self::__create($class); 
  • lang/php/misc/Hermit/src/Hermit/HermitAutoloader.php

    r19344 r19426  
    44 * @author nowelium 
    55 */ 
    6 class HermitAutoloader { 
     6abstract class HermitAutoloader { 
    77    private static $classPath = array( 
    88        '/', 
  • lang/php/misc/Hermit/src/Hermit/annote/HermitAnnote.php

    r18954 r19426  
    1111    public abstract function hasMethod($name); 
    1212    public abstract function getMethod($name); 
    13     public abstract function hasSql($name); 
    14     public abstract function getSql($name, $instance = null); 
    15     public abstract function hasQuery($name); 
    16     public abstract function getQuery($name, $instance = null); 
    17     public abstract function hasFile($name); 
    18     public abstract function getFile($name, $instance = null); 
    19     public abstract function hasPath($name); 
    20     public abstract function getPath($name, $instance = null); 
    21     public abstract function hasDelegate($name); 
    22     public abstract function getDelegate($name, $instance = null); 
     13    public abstract function isProcedureMethod(ReflectionMethod $method); 
     14    public abstract function isInsertMethod(ReflectionMethod $method); 
     15    public abstract function isUpdateMethod(ReflectionMethod $method); 
     16    public abstract function isDeleteMethod(ReflectionMethod $method); 
     17    public abstract function getProcedure(ReflectionMethod $method); 
     18    public abstract function getSql(ReflectionMethod $method, $suffix = null); 
     19    public abstract function getFile(ReflectionMethod $method); 
     20    public abstract function getQuery(ReflectionMethod $method); 
     21    public abstract function getDelegate(ReflectionMethod $method); 
    2322    public static final function create(ReflectionClass $reflector){ 
    2423        return new HermitAnnoteConst($reflector); 
    2524    } 
     25    public function isSelectMethod(ReflectionMethod $method){ 
     26        if($this->isProcedureMethod($method)){ 
     27            return false; 
     28        } 
     29        if($this->isInsertMethod($method)){ 
     30            return false; 
     31        } 
     32        if($this->isUpdateMethod($method)){ 
     33            return false; 
     34        } 
     35        if($this->isDeleteMethod($method)){ 
     36            return false; 
     37        } 
     38        return true; 
     39    } 
    2640} 
     41 
  • lang/php/misc/Hermit/src/Hermit/annote/HermitAnnoteConst.php

    r18954 r19426  
    1010    const QUERY_SUFFIX = '_QUERY'; 
    1111    const FILE_SUFFIX = '_FILE'; 
    12     const PATH_SUFFIX = '_PATH'; 
     12    const PROCEDURE_SUFFIX = '_PROCEDURE'; 
    1313    const DELEGATE_SUFFIX = '_DELEGATE'; 
    1414 
     15    const UNDERSCORE = '_'; 
     16    const SQL_FILE_EXTENSION = '.sql'; 
     17 
     18    const PROCEDURE_NAMES = '/^(proc|call)/i'; 
     19    const INSERT_NAMES = '/^(insert|create|add)/i'; 
     20    const UPDATE_NAMES = '/^(update|modify|store)/i'; 
     21    const DELETE_NAMES = '/^(delete|remove)/i'; 
     22 
    1523    protected $reflector; 
    16     protected function __construct(ReflectionClass $reflector){ 
     24    public function __construct(ReflectionClass $reflector){ 
    1725        $this->reflector = $reflector; 
    1826    } 
    19     protected static function createFilePath($pathOfClass, $suffix){ 
    20         return dirname($pathOfClass) . '_' . $suffix . '.sql'; 
     27    protected static function underscore(){ 
     28        $args = func_get_args(); 
     29        $buf = ''; 
     30        foreach($args as $arg){ 
     31            $buf .= $arg; 
     32            $buf .= self::UNDERSCORE; 
     33        } 
     34        return substr($buf, 0, -1); 
    2135    } 
    2236    public function getTable(){ 
     
    3953        return $this->reflector->getMethod($name); 
    4054    } 
    41     public function hasSql($name){ 
    42         return $this->reflector->hasConstant($name . self::SQL_SUFFIX); 
     55    public function isProcedureMethod(ReflectionMethod $method){ 
     56        return 1 === preg_match(self::PROCEDURE_NAMES, $method->getName()); 
    4357    } 
    44     public function getSql($name, $instance = null){ 
    45         return $this->reflector->getConstant($name . self::SQL_SUFFIX); 
     58    public function isInsertMethod(ReflectionMethod $method){ 
     59        return 1 === preg_match(self::INSERT_NAMES, $method->getName()); 
    4660    } 
    47     public function hasQuery($name){ 
    48         return $this->reflector->hasConstant($name . self::QUERY_SUFFIX); 
     61    public function isUpdateMethod(ReflectionMethod $method){ 
     62        return 1 === preg_match(self::UPDATE_NAMES, $method->getName()); 
    4963    } 
    50     public function getQuery($name, $instance = null){ 
    51         return $this->reflector->getConstant($name . self::QUERY_SUFFIX); 
     64    public function isDeleteMethod(ReflectionMethod $method){ 
     65        return 1 === preg_match(self::DELETE_NAMES, $method->getName()); 
    5266    } 
    53     public function hasFile($name){ 
    54         if(!$this->reflector->hasConstant($name . self::FILE_SUFFIX)){ 
    55             return false; 
     67    public function getProcedure(ReflectionMethod $method){ 
     68        $key = $method->getName . self::PROCEDURE_SUFFIX; 
     69        if($this->reflector->hasConstant($key)){ 
     70            return $this->reflector->getConstant($key); 
    5671        } 
    57         return file_exists(self::createFilePath($this->reflector->getFileName(), $name)); 
     72        return null; 
    5873    } 
    59     public function getFile($name, $instance = null){ 
    60         $path = self::createFilePath($this->reflector->getFileName(), $name); 
    61         return file_get_contents($path); 
     74    public function getSql(ReflectionMethod $method, $suffix = null){ 
     75        if(is_null($suffix)){ 
     76            $suffix = ''; 
     77        } 
     78        $methodName = $method->getName(); 
     79        $key1 = $methodName . self::UNDERSCORE . $suffix . self::SQL_SUFFIX; 
     80        if($this->reflector->hasConstant($key1)){ 
     81            return $this->reflector->getConstant($key1); 
     82        } 
     83        $key2 = $methodName . self::SQL_SUFFIX; 
     84        if($this->reflector->hasConstant($key2)){ 
     85            return $this->reflector->getConstant($key2); 
     86        } 
     87 
     88        $fileName = $this->reflector->getFileName(); 
     89        $className = $this->reflector->getName(); 
     90        $dirPath = dirname($fileName) . DIRECTORY_SEPARATOR; 
     91        $key3 = $dirPath . self::underscore($className, $methodName, $suffix) . self::SQL_FILE_EXTENSION; 
     92        if(file_exists($key3)){ 
     93            return file_get_contents($key3); 
     94        } 
     95        $key4 = $dirPath . self::underscore($className, $methodName) . self::SQL_FILE_EXTENSION; 
     96        if(file_exists($key4)){ 
     97            return file_get_contents($key4); 
     98        } 
     99        return null; 
    62100    } 
    63     public function hasPath($name){ 
    64         if(!$this->reflector->hasConstant($name . self::PATH_SUFFIX)){ 
    65             return false; 
     101    public function getQuery(ReflectionMethod $method){ 
     102        $key = $method->getName() . self::QUERY_SUFFIX; 
     103        if($this->reflector->hasConstant($key)){ 
     104            return $this->reflector->getConstant($key); 
    66105        } 
    67         return file_exists($this->getPath($name)); 
     106        return null; 
    68107    } 
    69     public function getPath($name, $instance = null){ 
    70         return file_get_contents($this->reflector->getConstant($name . self::PATH_SUFFIX)); 
     108    public function getFile(ReflectionMethod $method){ 
     109        $key = $method->getName() . self::FILE_SUFFIX; 
     110        if($this->reflector->hasConstant($key)){ 
     111            $path = $this->reflector->getConstant($key); 
     112            if(file_exists($path)){ 
     113                return file_get_contents($path); 
     114            } 
     115        } 
     116        return null; 
    71117    } 
    72     public function hasDelegate($name){ 
    73         return $this->reflector->hasConstant($name . self::DELEGATE_SUFFIX); 
    74     } 
    75     public function getDelegate($name, $instance = null){ 
     118    public function getDelegate(ReflectionMethod $method){ 
    76119        return $this->reflector->getConstant($name . self::DELEGATE_SUFFIX); 
    77120    } 
    78121} 
     122 
  • lang/php/misc/Hermit/src/Hermit/command/HermitSelectCommand.php

    r19345 r19426  
    1212    } 
    1313    public function execute(PDO $pdo, array $parameters){ 
    14         $sql = $this->sqlCreator->createSql(); 
     14        $sql = $this->sqlCreator->createSql($pdo); 
    1515        $stmt = HermitStatementBuilder::prepare($pdo, $this->method, $sql); 
    1616        $stmt->execute($parameters); 
  • lang/php/misc/Hermit/src/Hermit/command/HermitSqlCommand.php

    r19017 r19426  
    22 
    33interface HermitSqlCommand { 
    4     public function execute(PDO $pdo); 
     4    public function execute(PDO $pdo, array $params); 
    55} 
  • lang/php/misc/Hermit/src/Hermit/command/HermitSqlCommandFactory.php

    r19344 r19426  
    1212        $this->reflector = $reflector; 
    1313    } 
    14     public function hasCommand($methodName){ 
     14    public function has($methodName){ 
    1515        return $this->annote->hasMethod($methodName); 
    1616    } 
    17     public function getCommand($methodName){ 
     17    public function create(PDO $pdo, $methodName){ 
    1818        $method = $this->annote->getMethod($methodName); 
    1919        $methodId = spl_object_hash($method); 
     
    2525    } 
    2626    protected function createCommand(ReflectionMethod $method){ 
     27        $methodName = $method->getName(); 
     28        if($this->annote->isProcedureMethod($methodName)){ 
     29            return $this->createProcedureCommand($method); 
     30        } 
    2731        if($this->annote->isInsertMethod($methodName)){ 
    2832            return $this->createInsertCommand($method); 
     
    3640        return $this->createSelectCommand($method); 
    3741    } 
     42    protected function createProcedureCommand(ReflectionMethod $method){ 
     43        $creator = $this->createProcedureSqlCreator($method); 
     44        return new HermitProcedureCommand($method, $creator); 
     45    } 
     46    protected function createProcedureSqlCreator(ReflectionMethod $method){ 
     47        $sql = $this->annote->getProcedure($method); 
     48        if(null !== $sql){ 
     49            return new HermitStaticSqlCreator($sql); 
     50        } 
     51        $sql = $this->annote->getSql($method); 
     52        if(null !== $sql){ 
     53            return new HermitStaticSqlCreator($sql); 
     54        } 
     55        $sql = $this->annote->getFile($method); 
     56        if(null !== $sql){ 
     57            return new HermitStaticSqlCreator($sql); 
     58        } 
     59        throw new BadMethodCallException('method: "' . $method->getName() . '" was not apply to Procedure command'); 
     60    } 
    3861    protected function createInsertCommand(ReflectionMethod $method){ 
     62        throw new RuntimeException('T.B.D'); 
    3963    } 
    4064    protected function createUpdateCommand(ReflectionMethod $method){ 
     65        throw new RuntimeException('T.B.D'); 
     66    } 
     67    protected function createDeleteCommand(ReflectionMethod $method){ 
     68        throw new RuntimeException('T.B.D'); 
    4169    } 
    4270    protected function createSelectCommand(ReflectionMethod $method){ 
    43         $creator = $this->createSelectSqlCreator($method->getName()); 
     71        $creator = $this->createSelectSqlCreator($method); 
     72        $query = $this->annote->getQuery($method); 
     73        if(null !== $query){ 
     74            $creator->addQuery($query); 
     75        } 
    4476        return new HermitSelectCommand($method, $creator); 
    4577    } 
    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; 
     78    protected function createSelectSqlCreator(ReflectionMethod $method){ 
     79        $sql = $this->annote->getSql($method); 
     80        if(null !== $sql){ 
     81            return new HermitStaticSqlCreator($sql); 
    5882        } 
    59         throw new BadMethodCallException('invalid method:' . $name); 
     83        $sql = $this->annote->getFile($method); 
     84        if(null !== $sql){ 
     85            return new HermitStaticSqlCreator($sql); 
     86        } 
     87        return new HermitAutoSelectCreator; 
    6088    } 
    6189} 
     90 
  • lang/php/misc/Hermit/src/Hermit/creator/HermitSqlCreator.php

    r19017 r19426  
    11<?php 
    22 
     3/** 
     4 * @author nowelium 
     5 */ 
    36interface HermitSqlCreator { 
    4     public function createSql(); 
     7    public function createSql(PDO $pdo); 
    58} 
  • lang/php/misc/Hermit/src/Hermit/creator/HermitStaticSqlCreator.php

    r19345 r19426  
    33class HermitStaticSqlCreator implements HermitSqlCreator { 
    44    private $sql; 
    5     public function __construct(ReflectionMethod $method, $sql){ 
     5    public function __construct($sql){ 
    66        $this->sql = $sql; 
    77    } 
    8     public function createSql(){ 
     8    public function createSql(PDO $pdo){ 
    99        return $this->sql; 
    1010    } 
  • lang/php/misc/Hermit/src/Hermit/proxy/HermitInterfaceProxy.php

    r19344 r19426  
    1515    } 
    1616    public function request($name, array $params){ 
    17         if(!$this->commandFactory->hasCommand($name)){ 
     17        if(!$this->commandFactory->has($name)){ 
    1818            throw new BadMethodCallException($this->reflector->getName() . '::' . $name); 
    1919        } 
    20         $command = $this->commandFactory->getCommand($name); 
     20        $pdo = HermitDataSourceManager::get($this->reflector->getName()); 
     21        $command = $this->commandFactory->create($pdo, $name); 
    2122        return $command->execute($pdo, $params); 
    2223    } 
  • lang/php/misc/Hermit/src/Hermit/statement/HermitStatementBuilder.php

    r19344 r19426  
    44 * @author nowelium 
    55 */ 
    6 class HermitStatementBuilder { 
     6abstract class HermitStatementBuilder { 
    77    const REGEX = '/(\/\*(\w+)\*\/)((\'|")(\w+)(\'|"))/m'; 
    88    public static function prepare(PDO $pdo, ReflectionMethod $method, $sql){