Changeset 537

Show
Ignore:
Timestamp:
2008年10月04日 16時15分43秒 (2 months ago)
Author:
iteman
Message:

- Refactored.

Location:
trunk
Files:
3 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/Piece/ORM/Config/Reader.php

    r526 r537  
    4545require_once 'spyc.php5'; 
    4646 
    47 // {{{ Piece::ORM::Config::ConfigFactory 
     47// {{{ Piece::ORM::Config::Reader 
    4848 
    4949/** 
     
    5656 * @since      Class available since Release 0.1.0 
    5757 */ 
    58 class ConfigFactory 
     58class Reader 
    5959{ 
    6060 
     
    7777     */ 
    7878 
     79    private $_configDirectory; 
     80 
    7981    /**#@-*/ 
    8082 
     
    8486 
    8587    // }}} 
    86     // {{{ factory() 
     88    // {{{ __construct() 
     89 
     90    /** 
     91     * @param string $configDirectory 
     92     */ 
     93    public function __construct($configDirectory = null) 
     94    { 
     95        $this->_configDirectory = $configDirectory; 
     96    } 
     97 
     98    // }}} 
     99    // {{{ read() 
    87100 
    88101    /** 
    89102     * Creates a Piece::ORM::Config object from a configuration file or a cache. 
    90103     * 
    91      * @param string $configDirectory 
    92104     * @return Piece::ORM::Config 
    93105     * @throws Piece::ORM::Exception 
    94106     */ 
    95     public static function factory($configDirectory = null) 
    96     { 
    97         if (is_null($configDirectory)) { 
     107    public function read() 
     108    { 
     109        if (is_null($this->_configDirectory)) { 
    98110            return new Config(); 
    99111        } 
    100112 
    101         if (!file_exists($configDirectory)) { 
    102             throw new Exception("The configuration directory [ $configDirectory ] is not found."); 
    103         } 
    104  
    105         $dslFile = "$configDirectory/piece-orm-config.yaml"; 
     113        if (!file_exists($this->_configDirectory)) { 
     114            throw new Exception("The configuration directory [ {$this->_configDirectory} ] is not found."); 
     115        } 
     116 
     117        $dslFile = "{$this->_configDirectory}/piece-orm-config.yaml"; 
    106118        if (!file_exists($dslFile)) { 
    107119            throw new Exception("The configuration file [ $dslFile ] is not found."); 
     
    113125 
    114126        if (is_null(Registry::getContext()->getCacheDirectory())) { 
    115             return self::_createConfigurationFromFile($dslFile); 
     127            return $this->_createConfigurationFromFile($dslFile); 
    116128        } 
    117129 
     
    122134                          E_USER_WARNING 
    123135                          ); 
    124             return self::_createConfigurationFromFile($dslFile); 
     136            return $this->_createConfigurationFromFile($dslFile); 
    125137        } 
    126138 
     
    132144                          E_USER_WARNING 
    133145                          ); 
    134             return self::_createConfigurationFromFile($dslFile); 
    135         } 
    136  
    137         return self::_getConfiguration($dslFile); 
     146            return $this->_createConfigurationFromFile($dslFile); 
     147        } 
     148 
     149        return $this->_getConfiguration($dslFile); 
    138150    } 
    139151 
     
    183195                          E_USER_WARNING 
    184196                          ); 
    185             return self::_createConfigurationFromFile($dslFile); 
     197            return $this->_createConfigurationFromFile($dslFile); 
    186198        } 
    187199 
    188200        if (!$config) { 
    189             $config = self::_createConfigurationFromFile($dslFile); 
     201            $config = $this->_createConfigurationFromFile($dslFile); 
    190202            $result = $cache->save($config); 
    191203            if (::PEAR::isError($result)) { 
  • trunk/tests/Piece/ORM/Config/ReaderTest.php

    r526 r537  
    3838namespace Piece::ORM::Config; 
    3939 
    40 use Piece::ORM::Config::ConfigFactory; 
     40use Piece::ORM::Config::Reader; 
    4141use Piece::ORM::Context; 
    4242use Piece::ORM::Context::Registry; 
     
    4444require_once 'spyc.php5'; 
    4545 
    46 // {{{ Piece::ORM::Config::ConfigFactoryTest 
     46// {{{ Piece::ORM::Config::ReaderTest 
    4747 
    4848/** 
    49  * Some tests for Piece::ORM::Config::ConfigFactory. 
     49 * Some tests for Piece::ORM::Config::Reader. 
    5050 * 
    5151 * @package    Piece_ORM 
     
    5555 * @since      Class available since Release 0.1.0 
    5656 */ 
    57 class ConfigFactoryTest extends ::PHPUnit_Framework_TestCase 
     57class ReaderTest extends ::PHPUnit_Framework_TestCase 
    5858{ 
    5959 
     
    102102    public function testShouldCreateAnObjectWithoutConfigurationFile() 
    103103    { 
    104         $this->assertType('Piece::ORM::Config', ConfigFactory::factory()); 
     104        $reader = new Reader(); 
     105 
     106        $this->assertType('Piece::ORM::Config', $reader->read()); 
    105107    } 
    106108 
     
    110112    public function testShouldRaiseAnExceptionWhenAGivenConfigurationDirectoryIsNotFound() 
    111113    { 
    112         ConfigFactory::factory(dirname(__FILE__) . '/foo', $this->_cacheDirectory); 
     114        $reader = new Reader(dirname(__FILE__) . '/foo'); 
     115        $reader->read(); 
    113116    } 
    114117 
     
    118121    public function testShouldRaiseAnExceptionWhenAGivenConfigurationFileIsNotFound() 
    119122    { 
    120         ConfigFactory::factory(dirname(__FILE__), $this->_cacheDirectory); 
     123        $reader = new Reader(dirname(__FILE__)); 
     124        $reader->read(); 
    121125    } 
    122126 
    123127    public function testShouldCreateAnObjectEvenThoughAGivenCacheDirectoryIsNotFound() 
    124128    { 
    125         $this->assertType('Piece::ORM::Config', 
    126                           @ConfigFactory::factory($this->_cacheDirectory, 
    127                                                   dirname(__FILE__) . '/foo') 
    128                           ); 
     129        $reader = new Reader($this->_cacheDirectory); 
     130 
     131        $this->assertType('Piece::ORM::Config', $reader->read()); 
    129132    } 
    130133 
     
    132135    { 
    133136        $dsl = ::Spyc::YAMLLoad("{$this->_cacheDirectory}/piece-orm-config.yaml"); 
    134         $config = ConfigFactory::factory($this->_cacheDirectory, $this->_cacheDirectory); 
     137        $reader = new Reader($this->_cacheDirectory); 
     138        $config = $reader->read(); 
    135139 
    136140        $this->assertEquals(2, count($dsl['databases'])); 
     
    151155        $oldDirectory = getcwd(); 
    152156        chdir("{$this->_cacheDirectory}/CacheIDsShouldBeUniqueInOneCacheDirectory1"); 
    153         ConfigFactory::factory('.', $this->_cacheDirectory); 
     157        $reader = new Reader('.'); 
     158        $reader->read(); 
    154159 
    155160        $this->assertEquals(1, $this->_getCacheFileCount($this->_cacheDirectory)); 
    156161 
    157162        chdir("{$this->_cacheDirectory}/CacheIDsShouldBeUniqueInOneCacheDirectory2"); 
    158         ConfigFactory::factory('.', $this->_cacheDirectory); 
     163        $reader = new Reader('.'); 
     164        $reader->read(); 
    159165 
    160166        $this->assertEquals(2, $this->_getCacheFileCount($this->_cacheDirectory));