Changeset 19426
- Timestamp:
- 09/17/08 13:55:47 (5 years ago)
- Location:
- lang/php/misc/Hermit/src/Hermit
- Files:
-
- 3 added
- 11 modified
-
Hermit.php (modified) (1 diff)
-
HermitAutoloader.php (modified) (1 diff)
-
annote/HermitAnnote.php (modified) (1 diff)
-
annote/HermitAnnoteConst.php (modified) (2 diffs)
-
command/HermitDeleteCommand.php (added)
-
command/HermitInsertCommand.php (added)
-
command/HermitSelectCommand.php (modified) (1 diff)
-
command/HermitSqlCommand.php (modified) (1 diff)
-
command/HermitSqlCommandFactory.php (modified) (3 diffs)
-
command/HermitUpdateCommand.php (added)
-
creator/HermitSqlCreator.php (modified) (1 diff)
-
creator/HermitStaticSqlCreator.php (modified) (1 diff)
-
proxy/HermitInterfaceProxy.php (modified) (1 diff)
-
statement/HermitStatementBuilder.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/php/misc/Hermit/src/Hermit/Hermit.php
r19344 r19426 7 7 protected $listeners = array(); 8 8 protected $delegaters = array(); 9 10 9 public function __construct($class){ 11 10 $this->proxy = self::__create($class); -
lang/php/misc/Hermit/src/Hermit/HermitAutoloader.php
r19344 r19426 4 4 * @author nowelium 5 5 */ 6 class HermitAutoloader {6 abstract class HermitAutoloader { 7 7 private static $classPath = array( 8 8 '/', -
lang/php/misc/Hermit/src/Hermit/annote/HermitAnnote.php
r18954 r19426 11 11 public abstract function hasMethod($name); 12 12 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); 23 22 public static final function create(ReflectionClass $reflector){ 24 23 return new HermitAnnoteConst($reflector); 25 24 } 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 } 26 40 } 41 -
lang/php/misc/Hermit/src/Hermit/annote/HermitAnnoteConst.php
r18954 r19426 10 10 const QUERY_SUFFIX = '_QUERY'; 11 11 const FILE_SUFFIX = '_FILE'; 12 const P ATH_SUFFIX = '_PATH';12 const PROCEDURE_SUFFIX = '_PROCEDURE'; 13 13 const DELEGATE_SUFFIX = '_DELEGATE'; 14 14 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 15 23 protected $reflector; 16 p rotectedfunction __construct(ReflectionClass $reflector){24 public function __construct(ReflectionClass $reflector){ 17 25 $this->reflector = $reflector; 18 26 } 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); 21 35 } 22 36 public function getTable(){ … … 39 53 return $this->reflector->getMethod($name); 40 54 } 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()); 43 57 } 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()); 46 60 } 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()); 49 63 } 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()); 52 66 } 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); 56 71 } 57 return file_exists(self::createFilePath($this->reflector->getFileName(), $name));72 return null; 58 73 } 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; 62 100 } 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); 66 105 } 67 return file_exists($this->getPath($name));106 return null; 68 107 } 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; 71 117 } 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){ 76 119 return $this->reflector->getConstant($name . self::DELEGATE_SUFFIX); 77 120 } 78 121 } 122 -
lang/php/misc/Hermit/src/Hermit/command/HermitSelectCommand.php
r19345 r19426 12 12 } 13 13 public function execute(PDO $pdo, array $parameters){ 14 $sql = $this->sqlCreator->createSql( );14 $sql = $this->sqlCreator->createSql($pdo); 15 15 $stmt = HermitStatementBuilder::prepare($pdo, $this->method, $sql); 16 16 $stmt->execute($parameters); -
lang/php/misc/Hermit/src/Hermit/command/HermitSqlCommand.php
r19017 r19426 2 2 3 3 interface HermitSqlCommand { 4 public function execute(PDO $pdo );4 public function execute(PDO $pdo, array $params); 5 5 } -
lang/php/misc/Hermit/src/Hermit/command/HermitSqlCommandFactory.php
r19344 r19426 12 12 $this->reflector = $reflector; 13 13 } 14 public function has Command($methodName){14 public function has($methodName){ 15 15 return $this->annote->hasMethod($methodName); 16 16 } 17 public function getCommand($methodName){17 public function create(PDO $pdo, $methodName){ 18 18 $method = $this->annote->getMethod($methodName); 19 19 $methodId = spl_object_hash($method); … … 25 25 } 26 26 protected function createCommand(ReflectionMethod $method){ 27 $methodName = $method->getName(); 28 if($this->annote->isProcedureMethod($methodName)){ 29 return $this->createProcedureCommand($method); 30 } 27 31 if($this->annote->isInsertMethod($methodName)){ 28 32 return $this->createInsertCommand($method); … … 36 40 return $this->createSelectCommand($method); 37 41 } 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 } 38 61 protected function createInsertCommand(ReflectionMethod $method){ 62 throw new RuntimeException('T.B.D'); 39 63 } 40 64 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'); 41 69 } 42 70 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 } 44 76 return new HermitSelectCommand($method, $creator); 45 77 } 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); 58 82 } 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; 60 88 } 61 89 } 90 -
lang/php/misc/Hermit/src/Hermit/creator/HermitSqlCreator.php
r19017 r19426 1 1 <?php 2 2 3 /** 4 * @author nowelium 5 */ 3 6 interface HermitSqlCreator { 4 public function createSql( );7 public function createSql(PDO $pdo); 5 8 } -
lang/php/misc/Hermit/src/Hermit/creator/HermitStaticSqlCreator.php
r19345 r19426 3 3 class HermitStaticSqlCreator implements HermitSqlCreator { 4 4 private $sql; 5 public function __construct( ReflectionMethod $method,$sql){5 public function __construct($sql){ 6 6 $this->sql = $sql; 7 7 } 8 public function createSql( ){8 public function createSql(PDO $pdo){ 9 9 return $this->sql; 10 10 } -
lang/php/misc/Hermit/src/Hermit/proxy/HermitInterfaceProxy.php
r19344 r19426 15 15 } 16 16 public function request($name, array $params){ 17 if(!$this->commandFactory->has Command($name)){17 if(!$this->commandFactory->has($name)){ 18 18 throw new BadMethodCallException($this->reflector->getName() . '::' . $name); 19 19 } 20 $command = $this->commandFactory->getCommand($name); 20 $pdo = HermitDataSourceManager::get($this->reflector->getName()); 21 $command = $this->commandFactory->create($pdo, $name); 21 22 return $command->execute($pdo, $params); 22 23 } -
lang/php/misc/Hermit/src/Hermit/statement/HermitStatementBuilder.php
r19344 r19426 4 4 * @author nowelium 5 5 */ 6 class HermitStatementBuilder {6 abstract class HermitStatementBuilder { 7 7 const REGEX = '/(\/\*(\w+)\*\/)((\'|")(\w+)(\'|"))/m'; 8 8 public static function prepare(PDO $pdo, ReflectionMethod $method, $sql){
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)