Commit 5d3b0952 authored by wernerm's avatar wernerm

Added isValidModelClass() static method and fixed getLoadedModels() in order...

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.
parent 929273a0
...@@ -510,7 +510,6 @@ final class Doctrine ...@@ -510,7 +510,6 @@ final class Doctrine
foreach ((array) $directory as $dir) { foreach ((array) $directory as $dir) {
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::LEAVES_ONLY); RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($it as $file) { foreach ($it as $file) {
$e = explode('.', $file->getFileName()); $e = explode('.', $file->getFileName());
if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) { if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) {
...@@ -531,7 +530,7 @@ final class Doctrine ...@@ -531,7 +530,7 @@ final class Doctrine
* Will filter through an array of classes and return the Doctrine_Records out of them. * 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 * 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 * @return array $loadedModels
*/ */
public static function getLoadedModels($classes = null) public static function getLoadedModels($classes = null)
...@@ -541,29 +540,71 @@ final class Doctrine ...@@ -541,29 +540,71 @@ final class Doctrine
$classes = array_merge($classes, array_keys(self::$_loadedModels)); $classes = array_merge($classes, array_keys(self::$_loadedModels));
} }
$parent = new ReflectionClass('Doctrine_Record');
$loadedModels = array(); $loadedModels = array();
foreach ((array) $classes as $name) { foreach ((array) $classes as $name) {
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); $class = new ReflectionClass($name);
if (self::isValidModelClass($class)) {
$loadedModels[] = $name;
}
}
}
} catch (Exception $e) {
continue;
}
}
}
}
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 // Skip the following classes
// - abstract classes // - abstract classes
// - not a subclass of Doctrine_Record // - not a subclass of Doctrine_Record
// - don't have a setTableDefinition method // - don't have a setTableDefinition method
if ($class->isAbstract() || if (!$class->isAbstract() &&
!$class->isSubClassOf($parent) || $class->isSubClassOf('Doctrine_Record') &&
!$class->hasMethod('setTableDefinition')) { $class->hasMethod('setTableDefinition')) {
continue; return true;
} }
$loadedModels[] = $name;
} }
return false;
return $loadedModels;
} }
/** /**
* getConnectionByTableName * getConnectionByTableName
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment