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
...@@ -37,7 +37,7 @@ final class Doctrine ...@@ -37,7 +37,7 @@ final class Doctrine
* VERSION * VERSION
*/ */
const VERSION = '0.1.0'; const VERSION = '0.1.0';
/** /**
* ERROR CONSTANTS * ERROR CONSTANTS
*/ */
...@@ -361,7 +361,7 @@ final class Doctrine ...@@ -361,7 +361,7 @@ final class Doctrine
* HYDRATE_ARRAY * HYDRATE_ARRAY
*/ */
const HYDRATE_ARRAY = 3; const HYDRATE_ARRAY = 3;
/** /**
* HYDRATE_NONE * HYDRATE_NONE
*/ */
...@@ -435,16 +435,16 @@ final class Doctrine ...@@ -435,16 +435,16 @@ final class Doctrine
* @var boolean $_debug * @var boolean $_debug
*/ */
private static $_debug = false; private static $_debug = false;
/** /**
* _loadedModels * _loadedModels
* *
* Array of all the loaded models and the path to each one for autoloading * Array of all the loaded models and the path to each one for autoloading
* *
* @var array * @var array
*/ */
private static $_loadedModels = array(); private static $_loadedModels = array();
/** /**
* _validators * _validators
* *
...@@ -467,7 +467,7 @@ final class Doctrine ...@@ -467,7 +467,7 @@ final class Doctrine
/** /**
* debug * debug
* *
* @param string $bool * @param string $bool
* @return void * @return void
*/ */
public static function debug($bool = null) public static function debug($bool = null)
...@@ -475,7 +475,7 @@ final class Doctrine ...@@ -475,7 +475,7 @@ final class Doctrine
if ($bool !== null) { if ($bool !== null) {
self::$_debug = (bool) $bool; self::$_debug = (bool) $bool;
} }
return self::$_debug; return self::$_debug;
} }
...@@ -490,7 +490,7 @@ final class Doctrine ...@@ -490,7 +490,7 @@ final class Doctrine
if ( ! self::$_path) { if ( ! self::$_path) {
self::$_path = dirname(__FILE__); self::$_path = dirname(__FILE__);
} }
return self::$_path; return self::$_path;
} }
...@@ -498,19 +498,18 @@ final class Doctrine ...@@ -498,19 +498,18 @@ final class Doctrine
* loadModels * loadModels
* *
* Recursively load all models from a directory or array of directories * 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 * @return array $loadedModels
*/ */
public static function loadModels($directory) public static function loadModels($directory)
{ {
if ($directory !== null) { if ($directory !== null) {
$manager = Doctrine_Manager::getInstance(); $manager = Doctrine_Manager::getInstance();
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) {
...@@ -527,12 +526,12 @@ final class Doctrine ...@@ -527,12 +526,12 @@ final class Doctrine
* getLoadedModels * getLoadedModels
* *
* Get all the loaded models, you can provide an array of classes or it will use get_declared_classes() * Get all the loaded models, you can provide an array of classes or it will use get_declared_classes()
* *
* 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)
{ {
...@@ -540,51 +539,93 @@ final class Doctrine ...@@ -540,51 +539,93 @@ final class Doctrine
$classes = get_declared_classes(); $classes = get_declared_classes();
$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) {
$class = new ReflectionClass($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);
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
* *
* Get the connection object for a table by the actual table name * Get the connection object for a table by the actual table name
* *
* @param string $tableName * @param string $tableName
* @return object Doctrine_Connection * @return object Doctrine_Connection
*/ */
public static function getConnectionByTableName($tableName) public static function getConnectionByTableName($tableName)
{ {
$loadedModels = self::getLoadedModels(); $loadedModels = self::getLoadedModels();
foreach ($loadedModels as $name) { foreach ($loadedModels as $name) {
$model = new $name(); $model = new $name();
$table = $model->getTable(); $table = $model->getTable();
if ($table->getTableName() == $tableName) { if ($table->getTableName() == $tableName) {
return $table->getConnection(); return $table->getConnection();
} }
} }
return Doctrine_Manager::connection(); return Doctrine_Manager::connection();
} }
...@@ -619,11 +660,11 @@ final class Doctrine ...@@ -619,11 +660,11 @@ final class Doctrine
Doctrine::generateModelsFromDb($directory); Doctrine::generateModelsFromDb($directory);
$export = new Doctrine_Export_Schema(); $export = new Doctrine_Export_Schema();
$result = $export->exportSchema($yamlPath, 'yml', $directory); $result = $export->exportSchema($yamlPath, 'yml', $directory);
exec('rm -rf ' . $directory); exec('rm -rf ' . $directory);
return $result; return $result;
} }
...@@ -641,7 +682,7 @@ final class Doctrine ...@@ -641,7 +682,7 @@ final class Doctrine
{ {
$import = new Doctrine_Import_Schema(); $import = new Doctrine_Import_Schema();
$import->setOptions($options); $import->setOptions($options);
return $import->importSchema($yamlPath, 'yml', $directory); return $import->importSchema($yamlPath, 'yml', $directory);
} }
...@@ -674,18 +715,18 @@ final class Doctrine ...@@ -674,18 +715,18 @@ final class Doctrine
/** /**
* generateSqlFromModels * generateSqlFromModels
* *
* @param string $directory * @param string $directory
* @return string $build String of sql queries. One query per line * @return string $build String of sql queries. One query per line
*/ */
public static function generateSqlFromModels($directory = null) public static function generateSqlFromModels($directory = null)
{ {
$sql = Doctrine_Manager::connection()->export->exportSql($directory); $sql = Doctrine_Manager::connection()->export->exportSql($directory);
$build = ''; $build = '';
foreach ($sql as $query) { foreach ($sql as $query) {
$build .= $query.";\n"; $build .= $query.";\n";
} }
return $build; return $build;
} }
...@@ -701,7 +742,7 @@ final class Doctrine ...@@ -701,7 +742,7 @@ final class Doctrine
public static function generateYamlFromModels($yamlPath, $directory) public static function generateYamlFromModels($yamlPath, $directory)
{ {
$export = new Doctrine_Export_Schema(); $export = new Doctrine_Export_Schema();
return $export->exportSchema($yamlPath, 'yml', $directory); return $export->exportSchema($yamlPath, 'yml', $directory);
} }
...@@ -718,43 +759,43 @@ final class Doctrine ...@@ -718,43 +759,43 @@ final class Doctrine
if ( ! is_array($specifiedConnections)) { if ( ! is_array($specifiedConnections)) {
$specifiedConnections = (array) $specifiedConnections; $specifiedConnections = (array) $specifiedConnections;
} }
$manager = Doctrine_Manager::getInstance(); $manager = Doctrine_Manager::getInstance();
$connections = $manager->getConnections(); $connections = $manager->getConnections();
$results = array(); $results = array();
foreach ($connections as $name => $connection) { foreach ($connections as $name => $connection) {
if ( ! empty($specifiedConnections) && !in_array($name, $specifiedConnections)) { if ( ! empty($specifiedConnections) && !in_array($name, $specifiedConnections)) {
continue; continue;
} }
$info = $manager->parsePdoDsn($connection->getOption('dsn')); $info = $manager->parsePdoDsn($connection->getOption('dsn'));
$username = $connection->getOption('username'); $username = $connection->getOption('username');
$password = $connection->getOption('password'); $password = $connection->getOption('password');
// Make connection without database specified so we can create it // Make connection without database specified so we can create it
$connect = $manager->openConnection(new PDO($info['scheme'] . ':host=' . $info['host'], $username, $password), 'tmp_connection', false); $connect = $manager->openConnection(new PDO($info['scheme'] . ':host=' . $info['host'], $username, $password), 'tmp_connection', false);
try { try {
// Create database // Create database
$connect->export->createDatabase($name); $connect->export->createDatabase($name);
// Close the tmp connection with no database // Close the tmp connection with no database
$manager->closeConnection($connect); $manager->closeConnection($connect);
// Close original connection // Close original connection
$manager->closeConnection($connection); $manager->closeConnection($connection);
// Reopen original connection with newly created database // Reopen original connection with newly created database
$manager->openConnection(new PDO($info['dsn'], $username, $password), $name, true); $manager->openConnection(new PDO($info['dsn'], $username, $password), $name, true);
$results[$name] = true; $results[$name] = true;
} catch (Exception $e) { } catch (Exception $e) {
$results[$name] = false; $results[$name] = false;
} }
} }
return $results; return $results;
} }
...@@ -771,27 +812,27 @@ final class Doctrine ...@@ -771,27 +812,27 @@ final class Doctrine
if ( ! is_array($specifiedConnections)) { if ( ! is_array($specifiedConnections)) {
$specifiedConnections = (array) $specifiedConnections; $specifiedConnections = (array) $specifiedConnections;
} }
$manager = Doctrine_Manager::getInstance(); $manager = Doctrine_Manager::getInstance();
$connections = $manager->getConnections(); $connections = $manager->getConnections();
$results = array(); $results = array();
foreach ($connections as $name => $connection) { foreach ($connections as $name => $connection) {
if ( ! empty($specifiedConnections) && !in_array($name, $specifiedConnections)) { if ( ! empty($specifiedConnections) && !in_array($name, $specifiedConnections)) {
continue; continue;
} }
try { try {
$connection->export->dropDatabase($name); $connection->export->dropDatabase($name);
$results[$name] = true; $results[$name] = true;
} catch (Exception $e) { } catch (Exception $e) {
$results[$name] = false; $results[$name] = false;
} }
} }
return $results; return $results;
} }
...@@ -807,7 +848,7 @@ final class Doctrine ...@@ -807,7 +848,7 @@ final class Doctrine
public static function dumpData($yamlPath, $individualFiles = false) public static function dumpData($yamlPath, $individualFiles = false)
{ {
$data = new Doctrine_Data(); $data = new Doctrine_Data();
return $data->exportData($yamlPath, 'yml', array(), $individualFiles); return $data->exportData($yamlPath, 'yml', array(), $individualFiles);
} }
...@@ -824,17 +865,17 @@ final class Doctrine ...@@ -824,17 +865,17 @@ final class Doctrine
public static function loadData($yamlPath, $append = false) public static function loadData($yamlPath, $append = false)
{ {
$data = new Doctrine_Data(); $data = new Doctrine_Data();
if ( ! $append) { if ( ! $append) {
$data->purge(); $data->purge();
} }
return $data->importData($yamlPath, 'yml'); return $data->importData($yamlPath, 'yml');
} }
/** /**
* migrate * migrate
* *
* Migrate database to specified $to version. Migrates from current to latest if you do not specify. * Migrate database to specified $to version. Migrates from current to latest if you do not specify.
* *
* @param string $migrationsPath Path to migrations directory which contains your migration classes * @param string $migrationsPath Path to migrations directory which contains your migration classes
...@@ -845,7 +886,7 @@ final class Doctrine ...@@ -845,7 +886,7 @@ final class Doctrine
public static function migrate($migrationsPath, $to = null) public static function migrate($migrationsPath, $to = null)
{ {
$migration = new Doctrine_Migration($migrationsPath); $migration = new Doctrine_Migration($migrationsPath);
return $migration->migrate($to); return $migration->migrate($to);
} }
...@@ -860,42 +901,42 @@ final class Doctrine ...@@ -860,42 +901,42 @@ final class Doctrine
public static function generateMigrationClass($className, $migrationsPath) public static function generateMigrationClass($className, $migrationsPath)
{ {
$builder = new Doctrine_Migration_Builder($migrationsPath); $builder = new Doctrine_Migration_Builder($migrationsPath);
return $builder->generateMigrationClass($className); return $builder->generateMigrationClass($className);
} }
/** /**
* generateMigrationsFromDb * generateMigrationsFromDb
* *
* @param string $migrationsPath * @param string $migrationsPath
* @return void * @return void
* @throws new Doctrine_Migration_Exception * @throws new Doctrine_Migration_Exception
*/ */
public static function generateMigrationsFromDb($migrationsPath) public static function generateMigrationsFromDb($migrationsPath)
{ {
$builder = new Doctrine_Migration_Builder($migrationsPath); $builder = new Doctrine_Migration_Builder($migrationsPath);
return $builder->generateMigrationsFromDb(); return $builder->generateMigrationsFromDb();
} }
/** /**
* generateMigrationsFromModels * generateMigrationsFromModels
* *
* @param string $migrationsPath * @param string $migrationsPath
* @param string $modelsPath * @param string $modelsPath
* @return void * @return void
*/ */
public static function generateMigrationsFromModels($migrationsPath, $modelsPath = null) public static function generateMigrationsFromModels($migrationsPath, $modelsPath = null)
{ {
$builder = new Doctrine_Migration_Builder($migrationsPath); $builder = new Doctrine_Migration_Builder($migrationsPath);
return $builder->generateMigrationsFromModels($modelsPath); return $builder->generateMigrationsFromModels($modelsPath);
} }
/** /**
* getTable * getTable
* *
* @param string $tableName * @param string $tableName
* @return void * @return void
*/ */
public static function getTable($tableName) public static function getTable($tableName)
...@@ -906,7 +947,7 @@ final class Doctrine ...@@ -906,7 +947,7 @@ final class Doctrine
/** /**
* fileFinder * fileFinder
* *
* @param string $type * @param string $type
* @return void * @return void
*/ */
public static function fileFinder($type) public static function fileFinder($type)
...@@ -945,24 +986,24 @@ final class Doctrine ...@@ -945,24 +986,24 @@ final class Doctrine
if (class_exists($className, false)) { if (class_exists($className, false)) {
return false; return false;
} }
if ( ! self::$_path) { if ( ! self::$_path) {
self::$_path = dirname(__FILE__); self::$_path = dirname(__FILE__);
} }
$class = self::$_path . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; $class = self::$_path . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
if (file_exists($class)) { if (file_exists($class)) {
require_once($class); require_once($class);
return true; return true;
} }
$loadedModels = self::$_loadedModels; $loadedModels = self::$_loadedModels;
if (isset($loadedModels[$className]) && file_exists($loadedModels[$className])) { if (isset($loadedModels[$className]) && file_exists($loadedModels[$className])) {
require_once($loadedModels[$className]); require_once($loadedModels[$className]);
return true; return true;
} }
...@@ -987,7 +1028,7 @@ final class Doctrine ...@@ -987,7 +1028,7 @@ final class Doctrine
$ret[] = 'Array('; $ret[] = 'Array(';
$indent .= " "; $indent .= " ";
foreach ($var as $k => $v) { foreach ($var as $k => $v) {
$ret[] = $indent . $k . ' : ' . self::dump($v, false, $indent); $ret[] = $indent . $k . ' : ' . self::dump($v, false, $indent);
} }
$indent = substr($indent,0, -4); $indent = substr($indent,0, -4);
...@@ -1034,10 +1075,10 @@ final class Doctrine ...@@ -1034,10 +1075,10 @@ final class Doctrine
/** /**
* classifyCallback * classifyCallback
* *
* Callback function to classify a classname properly. * Callback function to classify a classname properly.
* *
* @param array $matches An array of matches from a pcre_replace call * @param array $matches An array of matches from a pcre_replace call
* @return string A string with matches 1 and mathces 3 in upper case. * @return string A string with matches 1 and mathces 3 in upper case.
*/ */
public static function classifyCallback($matches) public static function classifyCallback($matches)
{ {
...@@ -1060,13 +1101,13 @@ final class Doctrine ...@@ -1060,13 +1101,13 @@ final class Doctrine
return true; return true;
} }
/** /**
* makeDirectories * makeDirectories
* *
* Makes the directories for a path recursively * Makes the directories for a path recursively
* *
* @param string $path * @param string $path
* @return void * @return void
*/ */
public static function makeDirectories($path, $mode = 0777) public static function makeDirectories($path, $mode = 0777)
...@@ -1074,18 +1115,18 @@ final class Doctrine ...@@ -1074,18 +1115,18 @@ final class Doctrine
if (!$path) { if (!$path) {
return false; return false;
} }
if (is_dir($path) || is_file($path)) { if (is_dir($path) || is_file($path)) {
return true; return true;
} }
return mkdir($path, $mode, true); return mkdir($path, $mode, true);
} }
/** /**
* removeDirectories * removeDirectories
* *
* @param string $folderPath * @param string $folderPath
* @return void * @return void
*/ */
public static function removeDirectories($folderPath) public static function removeDirectories($folderPath)
...@@ -1111,7 +1152,7 @@ final class Doctrine ...@@ -1111,7 +1152,7 @@ final class Doctrine
return false; return false;
} }
} }
/** /**
* getValidators * getValidators
* *
...@@ -1123,19 +1164,19 @@ final class Doctrine ...@@ -1123,19 +1164,19 @@ final class Doctrine
{ {
if (empty(self::$_validators)) { if (empty(self::$_validators)) {
$dir = Doctrine::getPath() . DIRECTORY_SEPARATOR . 'Doctrine' . DIRECTORY_SEPARATOR . 'Validator'; $dir = Doctrine::getPath() . DIRECTORY_SEPARATOR . 'Doctrine' . DIRECTORY_SEPARATOR . 'Validator';
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY); $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($files as $file) { foreach ($files as $file) {
$e = explode('.', $file->getFileName()); $e = explode('.', $file->getFileName());
if (end($e) == 'php') { if (end($e) == 'php') {
$name = strtolower($e[0]); $name = strtolower($e[0]);
self::$_validators[$name] = $name; self::$_validators[$name] = $name;
} }
} }
} }
return self::$_validators; return self::$_validators;
} }
} }
\ No newline at end of file
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