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

Removed Facade and moved all static methods to Doctrine class.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/Doctrine.php

    r2902 r2939  
    255255    /** 
    256256     * Portability: turn off all portability features. 
    257      * @see Doctrine::ATTR_PORTABILITY 
     257     * @see self::ATTR_PORTABILITY 
    258258     */ 
    259259    const PORTABILITY_NONE          = 0; 
     
    262262     * Portability: convert names of tables and fields to case defined in the 
    263263     * "field_case" option when using the query*(), fetch*() methods. 
    264      * @see Doctrine::ATTR_PORTABILITY 
     264     * @see self::ATTR_PORTABILITY 
    265265     */ 
    266266    const PORTABILITY_FIX_CASE      = 1; 
     
    268268    /** 
    269269     * Portability: right trim the data output by query*() and fetch*(). 
    270      * @see Doctrine::ATTR_PORTABILITY 
     270     * @see self::ATTR_PORTABILITY 
    271271     */ 
    272272    const PORTABILITY_RTRIM         = 2; 
     
    274274    /** 
    275275     * Portability: force reporting the number of rows deleted. 
    276      * @see Doctrine::ATTR_PORTABILITY 
     276     * @see self::ATTR_PORTABILITY 
    277277     */ 
    278278    const PORTABILITY_DELETE_COUNT  = 4; 
     
    281281     * Portability: convert empty values to null strings in data output by 
    282282     * query*() and fetch*(). 
    283      * @see Doctrine::ATTR_PORTABILITY 
     283     * @see self::ATTR_PORTABILITY 
    284284     */ 
    285285    const PORTABILITY_EMPTY_TO_NULL = 8; 
     
    287287    /** 
    288288     * Portability: removes database/table qualifiers from associative indexes 
    289      * @see Doctrine::ATTR_PORTABILITY 
     289     * @see self::ATTR_PORTABILITY 
    290290     */ 
    291291    const PORTABILITY_FIX_ASSOC_FIELD_NAMES = 16; 
     
    293293    /** 
    294294     * Portability: makes Doctrine_Expression throw exception for unportable RDBMS expressions 
    295      * @see Doctrine::ATTR_PORTABILITY 
     295     * @see self::ATTR_PORTABILITY 
    296296     */ 
    297297    const PORTABILITY_EXPR          = 32; 
     
    299299    /** 
    300300     * Portability: turn on all portability features. 
    301      * @see Doctrine::ATTR_PORTABILITY 
     301     * @see self::ATTR_PORTABILITY 
    302302     */ 
    303303    const PORTABILITY_ALL           = 63; 
     
    445445    public static function loadAll() 
    446446    { 
    447         return Doctrine_Facade::loadAllRuntimeClasses(); 
     447        return self::loadAllRuntimeClasses(); 
     448    } 
     449     
     450    /** 
     451     * importSchema 
     452     * method for importing existing schema to Doctrine_Record classes 
     453     * 
     454     * @param string $directory Directory to write your models to 
     455     * @param array $databases Array of databases to generate models for 
     456     * @return boolean 
     457     */ 
     458    public static function importSchema($directory, array $databases = array()) 
     459    { 
     460        return self::generateModelsFromDb($directory, $databases); 
     461    } 
     462     
     463    /** 
     464     * exportSchema 
     465     * method for exporting Doctrine_Record classes to a schema 
     466     * 
     467     * @param string $directory Directory containing your models 
     468     * @return void 
     469     */ 
     470    public static function exportSchema($directory = null) 
     471    { 
     472        return self::createTablesFromModels($directory); 
     473    } 
     474     
     475    /** 
     476     * exportSql 
     477     * method for exporting Doctrine_Record classes to a schema 
     478     * 
     479     * @param string $directory 
     480     */ 
     481    public static function exportSql($directory = null) 
     482    { 
     483        return self::generateSqlFromModels($directory); 
     484    } 
     485     
     486    /** 
     487     * loadAllRuntimeClasses 
     488     * 
     489     * loads all runtime classes 
     490     * 
     491     * @return void 
     492     */ 
     493    public static function loadAllRuntimeClasses() 
     494    { 
     495        $classes = Doctrine_Compiler::getRuntimeClasses(); 
     496 
     497        foreach ($classes as $class) { 
     498            self::autoload($class); 
     499        } 
    448500    } 
    449501     
     
    458510    public static function loadModels($directory) 
    459511    { 
    460         return Doctrine_Facade::loadModels($directory); 
     512        $declared = get_declared_classes(); 
     513         
     514        if ($directory !== null) { 
     515            foreach ((array) $directory as $dir) { 
     516                $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), 
     517                                                        RecursiveIteratorIterator::LEAVES_ONLY); 
     518 
     519                foreach ($it as $file) { 
     520                    $e = explode('.', $file->getFileName()); 
     521                    if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) { 
     522                        require_once $file->getPathName(); 
     523                    } 
     524                } 
     525            } 
     526             
     527            $declared = array_diff(get_declared_classes(), $declared); 
     528        } 
     529         
     530        return self::getLoadedModels($declared); 
    461531    } 
    462532     
     
    471541    public static function getLoadedModels($classes = null) 
    472542    { 
    473         return Doctrine_Facade::getLoadedModels($classes); 
     543        if ($classes === null) { 
     544            $classes = get_declared_classes(); 
     545        } 
     546         
     547        $parent = new ReflectionClass('Doctrine_Record'); 
     548         
     549        $loadedModels = array(); 
     550         
     551        // we iterate trhough the diff of previously declared classes 
     552        // and currently declared classes 
     553        foreach ($classes as $name) { 
     554            $class = new ReflectionClass($name); 
     555             
     556            // Skip the following classes 
     557            // - abstract classes 
     558            // - not a subclass of Doctrine_Record  
     559            // - don't have a setTableDefinition method 
     560            if ($class->isAbstract() ||  
     561                !$class->isSubClassOf($parent) ||  
     562                !$class->hasMethod('setTableDefinition')) { 
     563                continue; 
     564            } 
     565             
     566            $loadedModels[] = $name; 
     567        } 
     568         
     569        return $loadedModels; 
    474570    } 
    475571     
     
    484580    public static function getConnectionByTableName($tableName) 
    485581    { 
    486         return Doctrine_Facade::getConnectionByTableName($tableName); 
    487     } 
    488      
    489     /** 
    490      * importSchema 
     582        $loadedModels = self::getLoadedModels(); 
     583         
     584        foreach ($loadedModels as $name) { 
     585            $model = new $name(); 
     586            $table = $model->getTable(); 
     587             
     588            if ($table->getTableName() == $tableName) { 
     589               return $table->getConnection();  
     590            } 
     591        } 
     592         
     593        return Doctrine_Manager::connection(); 
     594    } 
     595     
     596    /** 
     597     * generateModelsFromDb 
     598     * 
    491599     * method for importing existing schema to Doctrine_Record classes 
    492600     * 
     
    495603     * @return boolean 
    496604     */ 
    497     public static function importSchema($directory, array $databases = array()) 
    498     { 
    499         return Doctrine_Facade::generateModelsFromDb($directory, $databases); 
    500     } 
    501      
    502     /** 
    503      * exportSchema 
    504      * method for exporting Doctrine_Record classes to a schema 
     605    public static function generateModelsFromDb($directory, array $databases = array()) 
     606    { 
     607        return Doctrine_Manager::connection()->import->importSchema($directory, $databases); 
     608    } 
     609     
     610    /** 
     611     * generateYamlFromDb 
     612     * 
     613     * Generates models from database to temporary location then uses those models to generate a yaml schema file. 
     614     * This should probably be fixed. We should write something to generate a yaml schema file directly from the database. 
     615     * 
     616     * @param string $yamlPath Path to write oyur yaml schema file to 
     617     * @return void 
     618     */ 
     619    public static function generateYamlFromDb($yamlPath) 
     620    { 
     621        $directory = '/tmp/tmp_doctrine_models'; 
     622 
     623        Doctrine::generateModelsFromDb($directory); 
     624 
     625        $export = new Doctrine_Export_Schema(); 
     626         
     627        $result = $export->exportSchema($yamlPath, 'yml', $directory); 
     628         
     629        exec('rm -rf ' . $directory); 
     630         
     631        return $result; 
     632    } 
     633    /** 
     634     * generateModelsFromYaml 
     635     * 
     636     * Generate a yaml schema file from an existing directory of models 
     637     * 
     638     * @param string $yamlPath Path to your yaml schema files 
     639     * @param string $directory Directory to generate your models in 
     640     * @return void 
     641     */ 
     642    public static function generateModelsFromYaml($yamlPath, $directory) 
     643    { 
     644        $import = new Doctrine_Import_Schema(); 
     645        $import->generateBaseClasses(true); 
     646         
     647        return $import->importSchema($yamlPath, 'yml', $directory); 
     648    } 
     649     
     650    /** 
     651     * createTablesFromModels 
     652     * 
     653     * Creates database tables for the models in the specified directory 
    505654     * 
    506655     * @param string $directory Directory containing your models 
    507656     * @return void 
    508657     */ 
    509     public static function exportSchema($directory = null) 
    510     { 
    511         return Doctrine_Facade::createTablesFromModels($directory); 
    512     } 
    513      
    514     /** 
    515      * exportSql 
    516      * method for exporting Doctrine_Record classes to a schema 
    517      * 
    518      * @param string $directory 
    519      */ 
    520     public static function exportSql($directory = null) 
    521     { 
    522         return Doctrine_Facade::generateSqlFromModels($directory); 
     658    public static function createTablesFromModels($directory = null) 
     659    { 
     660        return Doctrine_Manager::connection()->export->exportSchema($directory); 
     661    } 
     662     
     663    /** 
     664     * generateSqlFromModels 
     665     * 
     666     * @param string $directory  
     667     * @return void 
     668     */ 
     669    public static function generateSqlFromModels($directory = null) 
     670    { 
     671        $sql = Doctrine_Manager::connection()->export->exportSql($directory); 
     672         
     673        $build = ''; 
     674        foreach ($sql as $query) { 
     675            $build .= $query.";\n"; 
     676        } 
     677         
     678        return $build; 
     679    } 
     680 
     681    /** 
     682     * generateYamlFromModels 
     683     * 
     684     * Generate yaml schema file for the models in the specified directory 
     685     * 
     686     * @param string $yamlPath Path to your yaml schema files 
     687     * @param string $directory Directory to generate your models in 
     688     * @return void 
     689     */ 
     690    public static function generateYamlFromModels($yamlPath, $directory) 
     691    { 
     692        $export = new Doctrine_Export_Schema(); 
     693         
     694        return $export->exportSchema($yamlPath, 'yml', $directory); 
     695    } 
     696     
     697    /** 
     698     * createDatabases 
     699     * 
     700     * Creates databases for connections 
     701     * 
     702     * @param string $specifiedConnections Array of connections you wish to create the database for 
     703     * @return void 
     704     */ 
     705    public static function createDatabases($specifiedConnections) 
     706    { 
     707        if (!is_array($specifiedConnections)) { 
     708            $specifiedConnections = (array) $specifiedConnections; 
     709        } 
     710         
     711        $connections = Doctrine_Manager::getInstance()->getConnections(); 
     712         
     713        foreach ($connections as $name => $connection) { 
     714            if (!empty($specifiedConnections) && !in_array($name, $specifiedConnections)) { 
     715                continue; 
     716            } 
     717             
     718            $connection->export->createDatabase($name); 
     719        } 
     720    } 
     721     
     722    /** 
     723     * dropDatabases 
     724     * 
     725     * Drops databases for connections 
     726     * 
     727     * @param string $specifiedConnections Array of connections you wish to drop the database for 
     728     * @return void 
     729     */ 
     730    public static function dropDatabases($specifiedConnections = array()) 
     731    { 
     732        if (!is_array($specifiedConnections)) { 
     733            $specifiedConnections = (array) $specifiedConnections; 
     734        } 
     735         
     736        $connections = Doctrine_Manager::getInstance()->getConnections(); 
     737         
     738        foreach ($connections as $name => $connection) { 
     739            if (!empty($specifiedConnections) && !in_array($name, $specifiedConnections)) { 
     740                continue; 
     741            } 
     742             
     743            $connection->export->dropDatabase($name); 
     744        } 
     745    } 
     746     
     747    /** 
     748     * dumpData 
     749     * 
     750     * Dump data to a yaml fixtures file 
     751     * 
     752     * @param string $yamlPath Path to write the yaml data fixtures to 
     753     * @param string $individualFiles Whether or not to dump data to individual fixtures files 
     754     * @return void 
     755     */ 
     756    public static function dumpData($yamlPath, $individualFiles = false) 
     757    { 
     758        $data = new Doctrine_Data(); 
     759         
     760        return $data->exportData($yamlPath, 'yml', array(), $individualFiles); 
     761    } 
     762     
     763    /** 
     764     * loadData 
     765     * 
     766     * Load data from a yaml fixtures file. 
     767     * The output of dumpData can be fed to loadData 
     768     * 
     769     * @param string $yamlPath Path to your yaml data fixtures 
     770     * @param string $append Whether or not to append the data 
     771     * @return void 
     772     */ 
     773    public static function loadData($yamlPath, $append = false) 
     774    { 
     775        $delete = isset($append) ? ($append ? false : true) : true; 
     776 
     777        if ($delete) 
     778        { 
     779            $models = Doctrine::getLoadedModels(); 
     780 
     781            foreach ($models as $model) 
     782            { 
     783                $model = new $model(); 
     784 
     785                $model->getTable()->createQuery()->delete($model)->execute(); 
     786            } 
     787        } 
     788 
     789        $data = new Doctrine_Data(); 
     790         
     791        return $data->importData($yamlPath, 'yml'); 
     792    } 
     793     
     794    /** 
     795     * loadDummyData 
     796     * 
     797     * Populdate your models with dummy data 
     798     * 
     799     * @param string $append Whether or not to append the data 
     800     * @param string $num Number of records to populate 
     801     * @return void 
     802     */ 
     803    public static function loadDummyData($append, $num = 5) 
     804    { 
     805        $delete = isset($append) ? ($append ? false : true) : true; 
     806 
     807        if ($delete) 
     808        { 
     809          $models = Doctrine::getLoadedModels(); 
     810 
     811          foreach ($models as $model) 
     812          { 
     813            $model = new $model(); 
     814 
     815            $model->getTable()->createQuery()->delete($model)->execute(); 
     816          } 
     817        } 
     818         
     819        $data = new Doctrine_Data(); 
     820         
     821        return $data->importDummyData($num); 
     822    } 
     823     
     824    /** 
     825     * migrate 
     826     *  
     827     * Migrate database to specified $to version. Migrates from current to latest if you do not specify. 
     828     * 
     829     * @param string $directory Directory which contains your migration classes 
     830     * @param string $to Version you wish to migrate to. 
     831     * @return void 
     832     */ 
     833    public static function migrate($directory, $to = null) 
     834    { 
     835        $migration = new Doctrine_Migration($directory); 
     836         
     837        return $migration->migrate($to); 
     838    } 
     839     
     840    /** 
     841     * generateMigrationClass 
     842     * 
     843     * Generate new migration class skeleton 
     844     * 
     845     * @param string $className Name of the Migration class to generate 
     846     * @param string $directory Directory which contains your migration classes 
     847     * @package default 
     848     */ 
     849    public static function generateMigrationClass($className, $directory) 
     850    { 
     851        $migration = new Doctrine_Migration($directory); 
     852        $next = (string) $migration->getNextVersion(); 
     853         
     854        $fileName = str_repeat('0', (3 - strlen($next))) . $next . '_' . self::tableize($className) . '.class.php'; 
     855        $path = $directory . DIRECTORY_SEPARATOR . $fileName; 
     856         
     857        $code  = '<?php' . PHP_EOL; 
     858        $code .= "// Automatically generated by the Doctrine ORM Framework\n"; 
     859        $code .= "class " . self::classify($className) . " extends Doctrine_Migration\n"; 
     860        $code .= "{\n"; 
     861        $code .= "\tpublic function up()\n\t{ }\n\n"; 
     862        $code .= "\tpublic function down()\n\t{ }\n"; 
     863        $code .= "}"; 
     864         
     865        file_put_contents($path, $code); 
    523866    } 
    524867     
     
    536879    public static function compile($target = null, $includedDrivers = array()) 
    537880    { 
    538         return Doctrine_Facade::compile($target, $includedDrivers); 
     881        return Doctrine_Compiler::compile($target, $includedDrivers); 
    539882    } 
    540883     
     
    583926                $ret[] = 'Array('; 
    584927                foreach ($var as $k => $v) { 
    585                     $ret[] = $k . ' : ' . Doctrine::dump($v, false); 
     928                    $ret[] = $k . ' : ' . self::dump($v, false); 
    586929                } 
    587930                $ret[] = ")";