Show
Ignore:
Timestamp:
09/16/08 01:32:33 (4 months ago)
Author:
nowelium
Message:
 
Files:
1 modified

Legend:

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