Show
Ignore:
Timestamp:
09/17/08 13:55:47 (4 months ago)
Author:
nowelium
Message:
 
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • 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