1
0
mirror of synced 2025-01-06 00:57:10 +03:00

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:
wernerm 2007-11-22 13:40:22 +00:00
parent 929273a0f1
commit 5d3b09524c

View File

@ -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,7 +530,7 @@ 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()
* @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')) {
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
*