| 1 |
|
<?php |
| 2 |
|
/** |
| 3 |
|
* Xyster Framework |
| 4 |
|
* |
| 5 |
|
* LICENSE |
| 6 |
|
* |
| 7 |
|
* This source file is subject to the new BSD license that is bundled |
| 8 |
|
* with this package in the file LICENSE.txt. |
| 9 |
|
* It is also available through the world-wide-web at this URL: |
| 10 |
|
* http://www.opensource.org/licenses/bsd-license.php |
| 11 |
|
* If you did not receive a copy of the license and are unable to |
| 12 |
|
* obtain it through the world-wide-web, please send an email |
| 13 |
|
* to xyster@devweblog.org so we can send you a copy immediately. |
| 14 |
|
* |
| 15 |
|
* @category Xyster |
| 16 |
|
* @package Xyster_Orm |
| 17 |
|
* @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org) |
| 18 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 19 |
|
* @version $Id: Factory.php 74 2007-08-19 15:49:31Z doublecompile $ |
| 20 |
|
*/ |
| 21 |
|
/** |
| 22 |
|
* @see Xyster_Orm_Mapper_Factory_Abstract |
| 23 |
|
*/ |
| 24 |
1 |
require_once 'Xyster/Orm/Mapper/Factory/Abstract.php'; |
| 25 |
|
/** |
| 26 |
|
* A simple factory for creating mappers |
| 27 |
|
* |
| 28 |
|
* @category Xyster |
| 29 |
|
* @package Xyster_Orm |
| 30 |
|
* @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org) |
| 31 |
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License |
| 32 |
|
*/ |
| 33 |
|
class Xyster_Orm_Mapper_Factory extends Xyster_Orm_Mapper_Factory_Abstract |
| 34 |
|
{ |
| 35 |
|
/** |
| 36 |
|
* Default cache for meta information provided by the backend |
| 37 |
|
* |
| 38 |
|
* @var Zend_Cache_Core |
| 39 |
|
*/ |
| 40 |
|
protected $_defaultMetadataCache; |
| 41 |
|
|
| 42 |
|
/** |
| 43 |
|
* Gets the mapper for a given class |
| 44 |
|
* |
| 45 |
|
* @param string $className The name of the entity class |
| 46 |
|
* @return Xyster_Orm_Mapper The mapper object |
| 47 |
|
*/ |
| 48 |
|
public function get( $className ) |
| 49 |
|
{ |
| 50 |
23 |
if ( !isset($this->_mappers[$className]) ) { |
| 51 |
|
|
| 52 |
23 |
$mapperName = Xyster_Orm_Loader::loadMapperClass($className); |
| 53 |
23 |
$this->_mappers[$className] = new $mapperName($this, |
| 54 |
23 |
$this->getDefaultMetadataCache()); |
| 55 |
23 |
$this->_mappers[$className]->init(); |
| 56 |
|
|
| 57 |
23 |
} |
| 58 |
|
|
| 59 |
23 |
return $this->_mappers[$className]; |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
/** |
| 63 |
|
* Gets the default metadata cache for information returned by getFields() |
| 64 |
|
* |
| 65 |
|
* @return Zend_Cache_Core |
| 66 |
|
*/ |
| 67 |
|
public function getDefaultMetadataCache() |
| 68 |
|
{ |
| 69 |
26 |
return $this->_defaultMetadataCache; |
| 70 |
|
} |
| 71 |
|
|
| 72 |
|
/** |
| 73 |
|
* Sets the default metadata cache for information returned by getFields() |
| 74 |
|
* |
| 75 |
|
* If $defaultMetadataCache is null, then no metadata cache is used by |
| 76 |
|
* default. |
| 77 |
|
* |
| 78 |
|
* @param mixed $metadataCache Either a Cache object, or a string naming a Registry key |
| 79 |
|
*/ |
| 80 |
|
public function setDefaultMetadataCache($metadataCache = null) |
| 81 |
|
{ |
| 82 |
26 |
$this->_defaultMetadataCache = $this->_setupMetadataCache($metadataCache); |
| 83 |
|
} |
| 84 |
|
|
| 85 |
|
/** |
| 86 |
|
* @param mixed $metadataCache Either a Cache object, or a string naming a Registry key |
| 87 |
|
* @return Zend_Cache_Core |
| 88 |
|
* @throws Xyster_Orm_Mapper_Exception |
| 89 |
|
*/ |
| 90 |
|
protected function _setupMetadataCache($metadataCache) |
| 91 |
|
{ |
| 92 |
26 |
if (is_string($metadataCache)) { |
| 93 |
23 |
require_once 'Zend/Registry.php'; |
| 94 |
23 |
$metadataCache = Zend_Registry::get($metadataCache); |
| 95 |
22 |
} |
| 96 |
|
|
| 97 |
25 |
if ($metadataCache === null || $metadataCache instanceof Zend_Cache_Core) { |
| 98 |
24 |
return $metadataCache; |
| 99 |
0 |
} |
| 100 |
|
|
| 101 |
1 |
require_once 'Xyster/Orm/Mapper/Exception.php'; |
| 102 |
1 |
throw new Xyster_Orm_Mapper_Exception('Argument must be of type Zend_Cache_Core, or a Registry key where a Zend_Cache_Core object is stored'); |
| 103 |
|
} |
| 104 |
|
} |