Added isValidModelClass() static method and fixed getLoadedModels() in order to resort back to the (classical) approach of class inclusion as a fallback when record classes have different names than their file names. The fallback behaviour of getLoadedModels() is now similar to what is was before the changes introduced in rev 3002.
This commit is contained in:
parent
929273a0f1
commit
5d3b09524c
@ -499,7 +499,7 @@ final class Doctrine
|
||||
*
|
||||
* Recursively load all models from a directory or array of directories
|
||||
*
|
||||
* @param string $directory Path to directory of models or array of directory paths
|
||||
* @param string $directory Path to directory of models or array of directory paths
|
||||
* @return array $loadedModels
|
||||
*/
|
||||
public static function loadModels($directory)
|
||||
@ -510,7 +510,6 @@ final class Doctrine
|
||||
foreach ((array) $directory as $dir) {
|
||||
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
|
||||
RecursiveIteratorIterator::LEAVES_ONLY);
|
||||
|
||||
foreach ($it as $file) {
|
||||
$e = explode('.', $file->getFileName());
|
||||
if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) {
|
||||
@ -531,8 +530,8 @@ final class Doctrine
|
||||
* Will filter through an array of classes and return the Doctrine_Records out of them.
|
||||
* If you do not specify $classes it will return all of the currently loaded Doctrine_Records
|
||||
*
|
||||
* @param $classes Array of classes to filter through, otherwise uses get_declared_classes()
|
||||
* @return array $loadedModels
|
||||
* @param classes Array of classes to filter through, otherwise uses get_declared_classes()
|
||||
* @return array $loadedModels
|
||||
*/
|
||||
public static function getLoadedModels($classes = null)
|
||||
{
|
||||
@ -541,29 +540,71 @@ final class Doctrine
|
||||
$classes = array_merge($classes, array_keys(self::$_loadedModels));
|
||||
}
|
||||
|
||||
$parent = new ReflectionClass('Doctrine_Record');
|
||||
|
||||
$loadedModels = array();
|
||||
|
||||
foreach ((array) $classes as $name) {
|
||||
$class = new ReflectionClass($name);
|
||||
|
||||
// Skip the following classes
|
||||
// - abstract classes
|
||||
// - not a subclass of Doctrine_Record
|
||||
// - don't have a setTableDefinition method
|
||||
if ($class->isAbstract() ||
|
||||
!$class->isSubClassOf($parent) ||
|
||||
!$class->hasMethod('setTableDefinition')) {
|
||||
continue;
|
||||
try {
|
||||
$class = new ReflectionClass($name);
|
||||
if (self::isValidModelClass($class)) {
|
||||
$loadedModels[] = $name;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
// Determine class names by the actual inclusion of the model file
|
||||
// The possibility exists that the class name(s) contained in the model
|
||||
// file is not the same as the actual model file name itself
|
||||
if (isset(self::$_loadedModels[$name])) {
|
||||
$declaredBefore = get_declared_classes();
|
||||
try {
|
||||
require_once self::$_loadedModels[$name];
|
||||
$declaredAfter = get_declared_classes();
|
||||
// Using array_slice since array_diff is broken is some versions
|
||||
$foundClasses = array_slice($declaredAfter, count($declaredBefore)-1);
|
||||
if ($foundClasses) {
|
||||
foreach ($foundClasses as $name) {
|
||||
$class = new ReflectionClass($name);
|
||||
if (self::isValidModelClass($class)) {
|
||||
$loadedModels[] = $name;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$loadedModels[] = $name;
|
||||
}
|
||||
|
||||
return $loadedModels;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* isValidModelClass
|
||||
*
|
||||
* Checks whether a reflection class is a valid Doctrine model class
|
||||
*
|
||||
* @param class A reflection class to validate
|
||||
* @return boolean
|
||||
*/
|
||||
public static function isValidModelClass($class)
|
||||
{
|
||||
if ($class instanceof ReflectionClass) {
|
||||
// Skip the following classes
|
||||
// - abstract classes
|
||||
// - not a subclass of Doctrine_Record
|
||||
// - don't have a setTableDefinition method
|
||||
if (!$class->isAbstract() &&
|
||||
$class->isSubClassOf('Doctrine_Record') &&
|
||||
$class->hasMethod('setTableDefinition')) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* getConnectionByTableName
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user