Show
Ignore:
Timestamp:
10/15/07 23:05:35 (15 months ago)
Author:
jwage
Message:

Moved static methods from Doctrine class to Doctrine_Facade and updated the tasks to use Doctrine_Facade.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/Doctrine.php

    r2866 r2872  
    190190     */ 
    191191    const LIMIT_ROWS       = 1; 
     192     
    192193    /** 
    193194     * constant for record limiting 
     
    204205     */ 
    205206    const FETCH_IMMEDIATE       = 0; 
     207     
    206208    /** 
    207209     * BATCH FETCHING 
     
    209211     */ 
    210212    const FETCH_BATCH           = 1; 
     213     
    211214    /** 
    212215     * LAZY FETCHING 
     
    214217     */ 
    215218    const FETCH_OFFSET          = 3; 
     219     
    216220    /** 
    217221     * LAZY OFFSET FETCHING 
     
    229233     */ 
    230234    const FETCH_VHOLDER         = 1; 
     235     
    231236    /** 
    232237     * FETCH RECORD 
     
    238243     */ 
    239244    const FETCH_RECORD          = 2; 
     245     
    240246    /** 
    241247     * FETCH ARRAY 
    242248     */ 
    243  
    244249    const FETCH_ARRAY           = 3; 
     250     
    245251    /** 
    246252     * PORTABILITY CONSTANTS 
     
    252258     */ 
    253259    const PORTABILITY_NONE          = 0; 
     260     
    254261    /** 
    255262     * Portability: convert names of tables and fields to case defined in the 
     
    264271     */ 
    265272    const PORTABILITY_RTRIM         = 2; 
     273     
    266274    /** 
    267275     * Portability: force reporting the number of rows deleted. 
     
    269277     */ 
    270278    const PORTABILITY_DELETE_COUNT  = 4; 
     279     
    271280    /** 
    272281     * Portability: convert empty values to null strings in data output by 
     
    275284     */ 
    276285    const PORTABILITY_EMPTY_TO_NULL = 8; 
     286     
    277287    /** 
    278288     * Portability: removes database/table qualifiers from associative indexes 
     
    280290     */ 
    281291    const PORTABILITY_FIX_ASSOC_FIELD_NAMES = 16; 
     292     
    282293    /** 
    283294     * Portability: makes Doctrine_Expression throw exception for unportable RDBMS expressions 
     
    285296     */ 
    286297    const PORTABILITY_EXPR          = 32; 
     298     
    287299    /** 
    288300     * Portability: turn on all portability features. 
     
    299311     */ 
    300312    const LOCK_OPTIMISTIC       = 0; 
     313     
    301314    /** 
    302315     * mode for pessimistic locking 
    303316     */ 
    304317    const LOCK_PESSIMISTIC      = 1; 
     318     
    305319    /** 
    306320     * EXPORT CONSTANTS 
     
    311325     */ 
    312326    const EXPORT_NONE               = 0; 
     327     
    313328    /** 
    314329     * export tables 
    315330     */ 
    316331    const EXPORT_TABLES             = 1; 
     332     
    317333    /** 
    318334     * export constraints 
    319335     */ 
    320336    const EXPORT_CONSTRAINTS        = 2; 
     337     
    321338    /** 
    322339     * export plugins 
    323340     */ 
    324341    const EXPORT_PLUGINS            = 4; 
     342     
    325343    /** 
    326344     * export all 
     
    352370     */ 
    353371    const IDENTIFIER_AUTOINC        = 1; 
     372     
    354373    /** 
    355374     * constant for sequence identifier 
    356375     */ 
    357376    const IDENTIFIER_SEQUENCE       = 2; 
     377     
    358378    /** 
    359379     * constant for normal identifier 
    360380     */ 
    361381    const IDENTIFIER_NATURAL        = 3; 
     382     
    362383    /** 
    363384     * constant for composite identifier 
    364385     */ 
    365386    const IDENTIFIER_COMPOSITE      = 4; 
    366     /** 
    367      * constructor 
     387     
     388    /** 
     389     * @var string $path            doctrine root directory 
     390     */ 
     391    private static $_path; 
     392     
     393    /** 
     394     * @var boolean $_debug 
     395     */ 
     396    private static $_debug = false; 
     397     
     398    /** 
     399     * __construct 
     400     * 
     401     * @return void 
     402     * @throws Doctrine_Exception 
    368403     */ 
    369404    public function __construct() 
     
    371406        throw new Doctrine_Exception('Doctrine is static class. No instances can be created.'); 
    372407    } 
    373     /** 
    374      * @var string $path            doctrine root directory 
    375      */ 
    376     private static $_path; 
    377     /** 
    378      * @var boolean $_debug 
    379      */ 
    380     private static $_debug = false; 
    381  
     408     
     409    /** 
     410     * debug 
     411     * 
     412     * @param string $bool  
     413     * @return void 
     414     */ 
    382415    public static function debug($bool = null) 
    383416    { 
     
    385418            self::$_debug = (bool) $bool; 
    386419        } 
     420         
    387421        return self::$_debug; 
    388422    } 
     423     
    389424    /** 
    390425     * getPath 
     
    398433            self::$_path = dirname(__FILE__); 
    399434        } 
     435         
    400436        return self::$_path; 
    401437    } 
     438     
    402439    /** 
    403440     * loadAll 
     
    408445    public static function loadAll() 
    409446    { 
    410         $classes = Doctrine_Compiler::getRuntimeClasses(); 
    411  
    412         foreach ($classes as $class) { 
    413             Doctrine::autoload($class); 
    414         } 
    415     } 
     447        return Doctrine_Facade::loadAllRuntimeClasses(); 
     448    } 
     449     
    416450    /** 
    417451     * loadModels 
     
    424458    public static function loadModels($directory) 
    425459    { 
    426         $declared = get_declared_classes(); 
    427          
    428         if ($directory !== null) { 
    429             foreach ((array) $directory as $dir) { 
    430                 $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), 
    431                                                         RecursiveIteratorIterator::LEAVES_ONLY); 
    432  
    433                 foreach ($it as $file) { 
    434                     $e = explode('.', $file->getFileName()); 
    435                     if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) { 
    436                         require_once $file->getPathName(); 
    437                     } 
    438                 } 
    439             } 
    440              
    441             $declared = array_diff(get_declared_classes(), $declared); 
    442         } 
    443          
    444         return self::getLoadedModels($declared); 
    445     } 
     460        return Doctrine_Facade::loadModels($directory); 
     461    } 
     462     
    446463    /** 
    447464     * getLoadedModels 
     
    454471    public static function getLoadedModels($classes = null) 
    455472    { 
    456         if ($classes === null) { 
    457             $classes = get_declared_classes(); 
    458         } 
    459          
    460         $parent = new ReflectionClass('Doctrine_Record'); 
    461          
    462         $loadedModels = array(); 
    463          
    464         // we iterate trhough the diff of previously declared classes 
    465         // and currently declared classes 
    466         foreach ($classes as $name) { 
    467             $class = new ReflectionClass($name); 
    468              
    469             // Skip the following classes 
    470             // - abstract classes 
    471             // - not a subclass of Doctrine_Record  
    472             // - don't have a setTableDefinition method 
    473             if ($class->isAbstract() ||  
    474                 !$class->isSubClassOf($parent) ||  
    475                 !$class->hasMethod('setTableDefinition')) { 
    476                 continue; 
    477             } 
    478              
    479             $loadedModels[] = $name; 
    480         } 
    481          
    482         return $loadedModels; 
    483     } 
     473        return Doctrine_Facade::getLoadedModels($classes); 
     474    } 
     475     
    484476    /** 
    485477     * getConnectionByTableName 
     
    492484    public static function getConnectionByTableName($tableName) 
    493485    { 
    494         $loadedModels = self::getLoadedModels(); 
    495          
    496         foreach ($loadedModels as $name) { 
    497             $model = new $name(); 
    498             $table = $model->getTable(); 
    499              
    500             if ($table->getTableName() == $tableName) { 
    501                return $table->getConnection();  
    502             } 
    503         } 
    504         return Doctrine_Manager::connection(); 
    505     } 
     486        return Doctrine_Facade::getConnectionByTableName($tableName); 
     487    } 
     488     
    506489    /** 
    507490     * importSchema 
     
    514497    public static function importSchema($directory, array $databases = array()) 
    515498    { 
    516         return Doctrine_Manager::connection()->import->importSchema($directory, $databases); 
    517     } 
    518     /** 
    519      * generateModelsFromDb 
    520      * 
    521      * Generate your model definitions from an existing database 
    522      * 
    523      * @param string $directory Directory to write your models to 
    524      * @param string $databases Array of databases to generate models for 
    525      * @return void 
    526      */ 
    527     public static function generateModelsFromDb($directory, array $databases = array()) 
    528     { 
    529         return self::importSchema($directory, $databases); 
    530     } 
    531     /** 
    532      * generateYamlFromDb 
    533      * 
    534      * Generates models from database to temporary location then uses those models to generate a yaml schema file. 
    535      * This should probably be fixed. We should write something to generate a yaml schema file directly from the database. 
    536      * 
    537      * @param string $yamlPath Path to write oyur yaml schema file to 
    538      * @return void 
    539      */ 
    540     public static function generateYamlFromDb($yamlPath) 
    541     { 
    542         $directory = '/tmp/tmp_doctrine_models'; 
    543  
    544         Doctrine::generateModelsFromDb($directory); 
    545  
    546         $export = new Doctrine_Export_Schema(); 
    547          
    548         return $export->exportSchema($yamlPath, 'yml', $directory); 
    549     } 
    550     /** 
    551      * generateModelsFromYaml 
    552      * 
    553      * Generate a yaml schema file from an existing directory of models 
    554      * 
    555      * @param string $yamlPath Path to your yaml schema files 
    556      * @param string $directory Directory to generate your models in 
    557      * @return void 
    558      */ 
    559     public static function generateModelsFromYaml($yamlPath, $directory) 
    560     { 
    561         $import = new Doctrine_Import_Schema(); 
    562         $import->generateBaseClasses(true); 
    563          
    564         return $import->importSchema($yamlPath, 'yml', $directory); 
    565     } 
     499        return Doctrine_Facade::generateModelsFromDb($directory, $databases); 
     500    } 
     501     
    566502    /** 
    567503     * exportSchema 
     
    573509    public static function exportSchema($directory = null) 
    574510    { 
    575         return Doctrine_Manager::connection()->export->exportSchema($directory); 
    576     } 
    577     /** 
    578      * createTablesFromModels 
    579      * 
    580      * Creates database tables for the models in the specified directory 
    581      * 
    582      * @param string $directory Directory containing your models 
    583      * @return void 
    584      */ 
    585     public static function createTablesFromModels($directory = null) 
    586     { 
    587         return self::exportSchema($directory); 
    588     } 
    589     /** 
    590      * generateYamlFromModels 
    591      * 
    592      * Generate yaml schema file for the models in the specified directory 
    593      * 
    594      * @param string $yamlPath Path to your yaml schema files 
    595      * @param string $directory Directory to generate your models in 
    596      * @return void 
    597      */ 
    598     public static function generateYamlFromModels($yamlPath, $directory) 
    599     { 
    600         $export = new Doctrine_Export_Schema(); 
    601          
    602         return $export->exportSchema($yamlPath, 'yml', $directory); 
    603     } 
    604     /** 
    605      * createDatabases 
    606      * 
    607      * Creates databases for connections 
    608      * 
    609      * @param string $specifiedConnections Array of connections you wish to create the database for 
    610      * @return void 
    611      */ 
    612     public static function createDatabases($specifiedConnections) 
    613     { 
    614         if (!is_array($specifiedConnections)) { 
    615             $specifiedConnections = (array) $specifiedConnections; 
    616         } 
    617          
    618         $connections = Doctrine_Manager::getInstance()->getConnections(); 
    619          
    620         foreach ($connections as $name => $connection) { 
    621             if (!empty($specifiedConnections) && !in_array($name, $specifiedConnections)) { 
    622                 continue; 
    623             } 
    624              
    625             $connection->export->createDatabase($name); 
    626         } 
    627     } 
    628     /** 
    629      * dropDatabases 
    630      * 
    631      * Drops databases for connections 
    632      * 
    633      * @param string $specifiedConnections Array of connections you wish to drop the database for 
    634      * @return void 
    635      */ 
    636     public static function dropDatabases($specifiedConnections = array()) 
    637     { 
    638         if (!is_array($specifiedConnections)) { 
    639             $specifiedConnections = (array) $specifiedConnections; 
    640         } 
    641          
    642         $connections = Doctrine_Manager::getInstance()->getConnections(); 
    643          
    644         foreach ($connections as $name => $connection) { 
    645             if (!empty($specifiedConnections) && !in_array($name, $specifiedConnections)) { 
    646                 continue; 
    647             } 
    648              
    649             $connection->export->dropDatabase($name); 
    650         } 
    651     } 
    652     /** 
    653      * dumpData 
    654      * 
    655      * Dump data to a yaml fixtures file 
    656      * 
    657      * @param string $yamlPath Path to write the yaml data fixtures to 
    658      * @param string $individualFiles Whether or not to dump data to individual fixtures files 
    659      * @return void 
    660      */ 
    661     public static function dumpData($yamlPath, $individualFiles = false) 
    662     { 
    663         $data = new Doctrine_Data(); 
    664          
    665         return $data->exportData($yamlPath, 'yml', array(), $individualFiles); 
    666     } 
    667     /** 
    668      * loadData 
    669      * 
    670      * Load data from a yaml fixtures file. 
    671      * The output of dumpData can be fed to loadData 
    672      * 
    673      * @param string $yamlPath Path to your yaml data fixtures 
    674      * @param string $append Whether or not to append the data 
    675      * @return void 
    676      */ 
    677     public static function loadData($yamlPath, $append = false) 
    678     { 
    679         $delete = isset($append) ? ($append ? false : true) : true; 
    680  
    681         if ($delete) 
    682         { 
    683           $models = Doctrine::getLoadedModels(); 
    684  
    685           foreach ($models as $model) 
    686           { 
    687             $model = new $model(); 
    688  
    689             $model->getTable()->createQuery()->delete($model)->execute(); 
    690           } 
    691         } 
    692  
    693         $data = new Doctrine_Data(); 
    694          
    695         return $data->importData($yamlPath, 'yml'); 
    696     } 
    697     /** 
    698      * loadDummyData 
    699      * 
    700      * Populdate your models with dummy data 
    701      * 
    702      * @param string $append Whether or not to append the data 
    703      * @param string $num Number of records to populate 
    704      * @return void 
    705      */ 
    706     public static function loadDummyData($append, $num = 5) 
    707     { 
    708         $delete = isset($append) ? ($append ? false : true) : true; 
    709  
    710         if ($delete) 
    711         { 
    712           $models = Doctrine::getLoadedModels(); 
    713  
    714           foreach ($models as $model) 
    715           { 
    716             $model = new $model(); 
    717  
    718             $model->getTable()->createQuery()->delete($model)->execute(); 
    719           } 
    720         } 
    721          
    722         $data = new Doctrine_Data(); 
    723          
    724         return $data->importDummyData($num); 
    725     } 
    726     /** 
    727      * migrate 
    728      *  
    729      * Migrate database to specified $to version. Migrates from current to latest if you do not specify. 
    730      * 
    731      * @param string $directory Directory which contains your migration classes 
    732      * @param string $to Version you wish to migrate to. 
    733      * @return void 
    734      */ 
    735     public static function migrate($directory, $to = null) 
    736     { 
    737         $migration = new Doctrine_Migration($directory); 
    738          
    739         return $migration->migrate($to); 
    740     } 
    741     /** 
    742      * generateMigrationClass 
    743      * 
    744      * Generate new migration class skeleton 
    745      * 
    746      * @param string $className Name of the Migration class to generate 
    747      * @param string $directory Directory which contains your migration classes 
    748      * @package default 
    749      */ 
    750     public static function generateMigrationClass($className, $directory) 
    751     { 
    752         $migration = new Doctrine_Migration($directory); 
    753         $next = (string) $migration->getNextVersion(); 
    754          
    755         $fileName = str_repeat('0', (3 - strlen($next))) . $next . '_' . Doctrine::tableize($className) . '.class.php'; 
    756         $path = $directory . DIRECTORY_SEPARATOR . $fileName; 
    757          
    758         $code  = '<?php' . PHP_EOL; 
    759         $code .= "// Automatically generated by the Doctrine ORM Framework\n"; 
    760         $code .= "class " . Doctrine::classify($className) . " extends Doctrine_Migration\n"; 
    761         $code .= "{\n"; 
    762         $code .= "\tpublic function up()\n\t{ }\n\n"; 
    763         $code .= "\tpublic function down()\n\t{ }\n"; 
    764         $code .= "}"; 
    765          
    766         file_put_contents($path, $code); 
    767     } 
     511        return Doctrine_Facade::createTablesFromModels($directory); 
     512    } 
     513     
    768514    /** 
    769515     * exportSql 
     
    774520    public static function exportSql($directory = null) 
    775521    { 
    776         return Doctrine_Manager::connection()->export->exportSql($directory); 
    777     } 
    778     /** 
    779      * generateSqlFromModels 
    780      * 
    781      * Generate sql for directory of models 
    782      * 
    783      * @param string $directory Directory where your models exist 
    784      * @return void 
    785      */ 
    786     public static function generateSqlFromModels($directory) 
    787     { 
    788         return self::exportSql($directory); 
    789     } 
     522        return Doctrine_Facade::generateSqlFromModels($directory); 
     523    } 
     524     
    790525    /** 
    791526     * compile 
     
    801536    public static function compile($target = null) 
    802537    { 
    803         Doctrine_Compiler::compile($target); 
    804     } 
     538        return Doctrine_Facade::compile($target); 
     539    } 
     540     
    805541    /** 
    806542     * simple autoload function 
     
    815551            return false; 
    816552        } 
     553         
    817554        if ( ! self::$_path) { 
    818555            self::$_path = dirname(__FILE__); 
    819556        } 
    820         $class = self::$_path . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR,$classname) . '.php'; 
     557         
     558        $class = self::$_path . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $classname) . '.php'; 
    821559 
    822560        if ( ! file_exists($class)) { 
     
    828566        return true; 
    829567    } 
     568     
    830569    /** 
    831570     * dump 
     
    859598        return implode("\n", $ret); 
    860599    } 
     600     
    861601    /** 
    862602     * returns table name from class name 
     
    869609         return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $classname)); 
    870610    } 
     611     
    871612    /** 
    872613     * returns class name from table name 
     
    879620        return preg_replace_callback('~(_?)(_)([\w])~', array("Doctrine", "classifyCallback"), ucfirst($tablename)); 
    880621    } 
    881  
     622     
    882623    /** 
    883624     * Callback function to classify a classname propperly.  
     
    890631        return $matches[1] . strtoupper($matches[3]); 
    891632    } 
     633     
    892634    /** 
    893635     * checks for valid class name (uses camel case and underscores)