| 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: Loader.php 74 2007-08-19 15:49:31Z doublecompile $ |
| 20 |
|
*/ |
| 21 |
|
/** |
| 22 |
|
* @see Zend_Loader |
| 23 |
|
*/ |
| 24 |
1 |
require_once 'Zend/Loader.php'; |
| 25 |
|
/** |
| 26 |
|
* The entity/mapper/collection class loader for the Orm layer |
| 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_Loader |
| 34 |
|
{ |
| 35 |
|
/** |
| 36 |
|
* Paths where entities, mappers, and sets are stored |
| 37 |
|
* |
| 38 |
|
* @var array |
| 39 |
|
*/ |
| 40 |
|
static protected $_paths = array(); |
| 41 |
|
|
| 42 |
|
/** |
| 43 |
|
* Adds a path to where class files for entities can be found |
| 44 |
|
* |
| 45 |
|
* @param string $path |
| 46 |
|
*/ |
| 47 |
|
public static function addPath( $path ) |
| 48 |
|
{ |
| 49 |
234 |
$path = rtrim($path, '/'); |
| 50 |
234 |
$path = rtrim($path, '\\'); |
| 51 |
234 |
$path .= DIRECTORY_SEPARATOR; |
| 52 |
|
|
| 53 |
234 |
if ( @!is_dir($path) ) { |
| 54 |
1 |
require_once 'Xyster/Orm/Exception.php'; |
| 55 |
1 |
throw new Xyster_Orm_Exception("The path '$path' does not exist'"); |
| 56 |
|
} |
| 57 |
|
|
| 58 |
234 |
self::$_paths[$path] = $path; // no need for dups |
| 59 |
|
} |
| 60 |
|
|
| 61 |
|
/** |
| 62 |
|
* Tries to load the class in one of the paths defined for entities |
| 63 |
|
* |
| 64 |
|
* @param string $className |
| 65 |
|
* @return string the class name loaded |
| 66 |
|
* @throws Xyster_Orm_Exception if the class file cannot be loaded |
| 67 |
|
* @throws Xyster_Orm_Exception if the file is found but no class with the name given is defined |
| 68 |
|
*/ |
| 69 |
|
public static function loadClass( $className ) |
| 70 |
|
{ |
| 71 |
228 |
if ( class_exists($className, false) ) { |
| 72 |
226 |
return $className; |
| 73 |
0 |
} |
| 74 |
|
|
| 75 |
8 |
$dirs = self::$_paths; |
| 76 |
8 |
$file = $className . '.php'; |
| 77 |
|
|
| 78 |
|
try { |
| 79 |
8 |
require_once 'Zend/Loader.php'; |
| 80 |
8 |
Zend_Loader::loadFile($file, $dirs, true); |
| 81 |
8 |
} catch (Zend_Exception $e) { |
| 82 |
0 |
require_once 'Xyster/Orm/Exception.php'; |
| 83 |
0 |
throw new Xyster_Orm_Exception('Cannot load class "' . $className . '"'); |
| 84 |
|
} |
| 85 |
|
|
| 86 |
8 |
if (!class_exists($className,false)) { |
| 87 |
1 |
require_once 'Xyster/Orm/Exception.php'; |
| 88 |
1 |
throw new Xyster_Orm_Exception('Invalid class ("' . $className . '")'); |
| 89 |
0 |
} |
| 90 |
|
|
| 91 |
7 |
return $className; |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
/** |
| 95 |
|
* Loads the class and makes sure it's a Xyster_Orm_Entity |
| 96 |
|
* |
| 97 |
|
* @param string $className |
| 98 |
|
* @throws Xyster_Orm_Exception if the class loaded is not a derivitive of Xyster_Orm_Entity |
| 99 |
|
*/ |
| 100 |
|
public static function loadEntityClass( $className ) |
| 101 |
|
{ |
| 102 |
225 |
self::loadClass($className); |
| 103 |
|
|
| 104 |
225 |
if (!($className instanceof Xyster_Orm_Entity) && |
| 105 |
225 |
!is_subclass_of($className, 'Xyster_Orm_Entity')) { |
| 106 |
1 |
require_once 'Xyster/Orm/Exception.php'; |
| 107 |
1 |
throw new Xyster_Orm_Exception("'" . $className . "' is not a subclass of Xyster_Orm_Entity"); |
| 108 |
0 |
} |
| 109 |
|
} |
| 110 |
|
|
| 111 |
|
/** |
| 112 |
|
* Loads the mapper class for the entity class given |
| 113 |
|
* |
| 114 |
|
* To load the 'PersonMapper' class, the $className parameter should just be |
| 115 |
|
* 'Person'. |
| 116 |
|
* |
| 117 |
|
* @param string $className the name of the entity class |
| 118 |
|
* @return string the class name |
| 119 |
|
* @throws Xyster_Orm_Exception if the class loaded is not a derivitive of Xyster_Orm_Mapper_Interface |
| 120 |
|
*/ |
| 121 |
|
public static function loadMapperClass( $className ) |
| 122 |
|
{ |
| 123 |
223 |
self::loadEntityClass($className); |
| 124 |
|
|
| 125 |
223 |
$mapper = $className . 'Mapper'; |
| 126 |
|
|
| 127 |
223 |
self::loadClass($mapper); |
| 128 |
|
|
| 129 |
223 |
if (!($mapper instanceof Xyster_Orm_Mapper_Interface) && |
| 130 |
223 |
!is_subclass_of($mapper, 'Xyster_Orm_Mapper_Abstract')) { |
| 131 |
1 |
require_once 'Xyster/Orm/Exception.php'; |
| 132 |
1 |
throw new Xyster_Orm_Exception("'" . $mapper . "' is not a subclass of Xyster_Orm_Mapper_Interface"); |
| 133 |
0 |
} |
| 134 |
|
|
| 135 |
222 |
return $mapper; |
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
/** |
| 139 |
|
* Loads the set class for the entity class given |
| 140 |
|
* |
| 141 |
|
* @param string $className the name of the set class |
| 142 |
|
* @param boolean $autoSuffix whether to append 'Set' to the end of the class |
| 143 |
|
* @throws Xyster_Orm_Exception if the class loaded is not a derivitive of Xyster_Orm_Set |
| 144 |
|
* @return string the class name |
| 145 |
|
*/ |
| 146 |
|
public static function loadSetClass( $className, $autoSuffix = true ) |
| 147 |
|
{ |
| 148 |
223 |
$set = ( $autoSuffix ) ? $className . 'Set' : $className; |
| 149 |
223 |
self::loadClass($set); |
| 150 |
|
|
| 151 |
223 |
if (!($set instanceof Xyster_Orm_Set) && |
| 152 |
223 |
!is_subclass_of($set, 'Xyster_Orm_Set')) { |
| 153 |
1 |
require_once 'Xyster/Orm/Exception.php'; |
| 154 |
1 |
throw new Xyster_Orm_Exception("'" . $set . "' is not a subclass of Xyster_Orm_Set"); |
| 155 |
0 |
} |
| 156 |
|
|
| 157 |
222 |
return $set; |
| 158 |
|
} |
| 159 |
|
} |