Changeset 3889

Show
Ignore:
Timestamp:
02/23/08 21:43:57 (17 months ago)
Author:
jwage
Message:

fixes #627 #791 This also fixes an issue where the class tables do not extend the correct class when dealing with inheritance. Also new tests to cover this functionality.

Location:
branches/0.10
Files:
2 removed
4 modified
1 copied
1 moved

Legend:

Unmodified
Added
Removed
  • branches/0.10/lib/Doctrine/Import/Builder.php

    r3887 r3889  
    4040{ 
    4141    /** 
    42      * Path 
     42     * _path 
    4343     *  
    4444     * the path where imported files are being generated 
     
    4949     
    5050    /** 
    51      * packagesPrefix 
     51     * _packagesPrefix 
    5252     * 
    5353     * @var string 
     
    5656 
    5757    /** 
    58      * packagesPath 
     58     * _packagesPath 
    5959     * 
    6060     * @var string 
     
    6363     
    6464    /** 
    65      * suffix 
     65     * _suffix 
    6666     *  
    6767     * File suffix to use when writing class definitions 
     
    7272 
    7373    /** 
    74      * generateBaseClasses 
     74     * _generateBaseClasses 
    7575     *  
    7676     * Bool true/false for whether or not to generate base classes 
     
    8181 
    8282    /** 
    83      * baseClassesDirectory 
     83     * _base 
     84     * 
     85     * Prefix to use for generated base classes 
     86     * 
     87     * @var string 
     88     */ 
     89    protected $_baseClassPrefix = 'Base'; 
     90 
     91    /** 
     92     * _baseClassesDirectory 
    8493     *  
    8594     * Directory to put the generate base classes in 
     
    9099     
    91100    /** 
    92      * baseClassName 
     101     * _baseClassName 
    93102     * 
    94103     * @var string 
     
    97106     
    98107    /** 
    99      * tpl 
     108     * _tpl 
    100109     * 
    101110     * Class template used for writing classes 
     
    123132    public function setTargetPath($path) 
    124133    { 
    125         if ( ! $this->_packagesPath) { 
    126             $this->setPackagesPath($path . DIRECTORY_SEPARATOR . 'packages'); 
    127         } 
    128  
    129         $this->_path = $path; 
     134        if ($path) { 
     135            if ( ! $this->_packagesPath) { 
     136                $this->setPackagesPath($path . DIRECTORY_SEPARATOR . 'packages'); 
     137            } 
     138 
     139            $this->_path = $path; 
     140        } 
    130141    } 
    131142     
     
    149160    public function setPackagesPath($packagesPath) 
    150161    { 
    151         $this->_packagesPath = $packagesPath; 
     162        if ($packagesPath) { 
     163            $this->_packagesPath = $packagesPath; 
     164        } 
    152165    } 
    153166     
     
    168181        return $this->_generateBaseClasses; 
    169182    } 
    170  
     183     
     184    /** 
     185     * setBaseClassPrefix 
     186     * 
     187     * @param string $prefix  
     188     * @return void 
     189     */ 
     190    public function setBaseClassPrefix($prefix) 
     191    { 
     192        $this->_baseClassPrefix = $prefix; 
     193    } 
     194     
     195    /** 
     196     * getBaseClassPrefix 
     197     * 
     198     * @return void 
     199     */ 
     200    public function getBaseClassPrefix() 
     201    { 
     202        return $this->_baseClassPrefix; 
     203    } 
     204     
    171205    /** 
    172206     * setBaseClassesDirectory 
     
    358392 
    359393                if (isset($relation['refClass'])) { 
    360                     $a[] = '\'refClass\' => ' . var_export($relation['refClass'], true); 
     394                    $a[] = '\'refClass\' => ' . $this->varExport($relation['refClass']); 
    361395                } 
    362396             
    363397                if (isset($relation['deferred']) && $relation['deferred']) { 
    364                     $a[] = '\'default\' => ' . var_export($relation['deferred'], true); 
     398                    $a[] = '\'default\' => ' . $this->varExport($relation['deferred']); 
    365399                } 
    366400             
    367401                if (isset($relation['local']) && $relation['local']) { 
    368                     $a[] = '\'local\' => ' . var_export($relation['local'], true); 
     402                    $a[] = '\'local\' => ' . $this->varExport($relation['local']); 
    369403                } 
    370404             
    371405                if (isset($relation['foreign']) && $relation['foreign']) { 
    372                     $a[] = '\'foreign\' => ' . var_export($relation['foreign'], true); 
     406                    $a[] = '\'foreign\' => ' . $this->varExport($relation['foreign']); 
    373407                } 
    374408             
    375409                if (isset($relation['onDelete']) && $relation['onDelete']) { 
    376                     $a[] = '\'onDelete\' => ' . var_export($relation['onDelete'], true); 
     410                    $a[] = '\'onDelete\' => ' . $this->varExport($relation['onDelete']); 
    377411                } 
    378412             
    379413                if (isset($relation['onUpdate']) && $relation['onUpdate']) { 
    380                     $a[] = '\'onUpdate\' => ' . var_export($relation['onUpdate'], true); 
     414                    $a[] = '\'onUpdate\' => ' . $this->varExport($relation['onUpdate']); 
    381415                } 
    382416             
    383417                if (isset($relation['equal']) && $relation['equal']) {  
    384                     $a[] = '\'equal\' => ' . var_export($relation['equal'], true);  
     418                    $a[] = '\'equal\' => ' . $this->varExport($relation['equal']);  
    385419                } 
    386420             
     
    446480             
    447481            if (is_array($options) && !empty($options)) { 
    448                 $build .= ', ' . var_export($options, true); 
     482                $build .= ', ' . $this->varExport($options); 
    449483            } 
    450484     
     
    453487         
    454488        return $build; 
     489    } 
     490 
     491    /** 
     492     * varExport 
     493     * 
     494     * Special function for var_export() 
     495     * The normal code which is returned is malformed and does not follow Doctrine standards 
     496     * So we do some string replacing to clean it up 
     497     * 
     498     * @param string $var  
     499     * @return void 
     500     */ 
     501    public function varExport($var) 
     502    { 
     503        $export = var_export($var, true); 
     504        $export = str_replace("\n ", '', $export); 
     505        $export = str_replace('array ( ', 'array(', $export); 
     506        $export = str_replace(",\n", "", $export); 
     507         
     508        return $export; 
    455509    } 
    456510 
     
    502556             
    503557            if (is_array($options) && !empty($options)) { 
    504                 $optionsPhp = var_export($options, true); 
     558                $optionsPhp = $this->varExport($options); 
    505559             
    506560                $build .= "    \$this->loadTemplate('" . $name . "', " . $optionsPhp . ");\n"; 
     
    528582        foreach ($actAs as $name => $options) { 
    529583            if (is_array($options) && !empty($options)) { 
    530                 $optionsPhp = var_export($options, true); 
     584                $optionsPhp = $this->varExport($options); 
    531585                 
    532586                $build .= "    \$this->actAs('" . $name . "', " . $optionsPhp . ");\n"; 
     
    587641        $build = ''; 
    588642        foreach ($options as $name => $value) { 
    589             $build .= "    \$this->option('$name', " . var_export($value, true) . ");\n"; 
     643            $build .= "    \$this->option('$name', " . $this->varExport($value) . ");\n"; 
    590644        } 
    591645         
     
    605659      foreach ($indexes as $indexName => $definitions) { 
    606660          $build .= "\n    \$this->index('" . $indexName . "'"; 
    607           $build .= ', ' . var_export($definitions, true); 
     661          $build .= ', ' . $this->varExport($definitions); 
    608662          $build .= ');'; 
    609663      } 
     
    665719            throw new Doctrine_Import_Builder_Exception('Missing class name.'); 
    666720        } 
    667  
     721         
     722        $definition['topLevelClassName'] = $definition['className']; 
     723         
    668724        if ($this->generateBaseClasses()) { 
    669725            $definition['is_package'] = (isset($definition['package']) && $definition['package']) ? true:false; 
     
    674730                unset($e[0]); 
    675731                 
    676                 $definition['package_path'] = implode(DIRECTORY_SEPARATOR, $e); 
     732                $definition['package_path'] = ! empty($e) ? implode(DIRECTORY_SEPARATOR, $e):$definition['package_name']; 
    677733            } 
    678734             
     
    701757                $packageLevel['is_package_class'] = true; 
    702758                unset($packageLevel['connection']); 
     759                 
     760                $packageLevel['tableClassName'] = $packageLevel['className'] . 'Table'; 
     761                $packageLevel['inheritance']['tableExtends'] = isset($definition['inheritance']['extends']) ? $definition['inheritance']['extends'] . 'Table':'Doctrine_Table'; 
     762                 
     763                $topLevel['tableClassName'] = $topLevel['topLevelClassName'] . 'Table'; 
     764                $topLevel['inheritance']['tableExtends'] = $packageLevel['className'] . 'Table'; 
     765            } else { 
     766                $topLevel['tableClassName'] = $topLevel['className'] . 'Table'; 
     767                $topLevel['inheritance']['tableExtends'] = isset($definition['inheritance']['extends']) ? $definition['inheritance']['extends'] . 'Table':'Doctrine_Table'; 
    703768            } 
    704769 
     
    711776            $this->writeDefinition($baseClass); 
    712777             
    713             if (!empty($packageLevel)) { 
     778            if ( ! empty($packageLevel)) { 
    714779                $this->writeDefinition($packageLevel); 
    715780            } 
     
    718783        } else { 
    719784            $this->writeDefinition($definition); 
     785        } 
     786    } 
     787 
     788    /** 
     789     * writeTableDefinition 
     790     * 
     791     * @return void 
     792     */ 
     793    public function writeTableDefinition($className, $path, $options = array()) 
     794    { 
     795        $content  = '<?php' . PHP_EOL; 
     796        $content .= sprintf(self::$_tpl, false, 
     797                                       $className, 
     798                                       isset($options['extends']) ? $options['extends']:'Doctrine_Table', 
     799                                       null, 
     800                                       null, 
     801                                       null 
     802                                       ); 
     803 
     804        Doctrine_Lib::makeDirectories($path); 
     805 
     806        $writePath = $path . DIRECTORY_SEPARATOR . $className . $this->_suffix; 
     807 
     808        if ( ! file_exists($writePath)) { 
     809            file_put_contents($writePath, $content); 
    720810        } 
    721811    } 
     
    750840                $writePath = $this->_path; 
    751841            } 
    752         } 
    753  
     842 
     843            $this->writeTableDefinition($definition['tableClassName'], $writePath, array('extends' => $definition['inheritance']['tableExtends'])); 
     844        } 
    754845        // If is the package class then we need to make the path to the complete package 
    755         if (isset($definition['is_package_class']) && $definition['is_package_class']) { 
    756             $path = str_replace('.', DIRECTORY_SEPARATOR, trim($definition['package'])); 
    757              
    758             $writePath = $packagesPath . DIRECTORY_SEPARATOR . $path; 
    759         } 
    760          
     846        else if (isset($definition['is_package_class']) && $definition['is_package_class']) { 
     847            $writePath = $packagesPath . DIRECTORY_SEPARATOR . $definition['package_path']; 
     848 
     849            $this->writeTableDefinition($definition['tableClassName'], $writePath, array('extends' => $definition['inheritance']['tableExtends'])); 
     850        } 
    761851        // If it is the base class of the doctrine record definition 
    762         if (isset($definition['is_base_class']) && $definition['is_base_class']) { 
     852        else if (isset($definition['is_base_class']) && $definition['is_base_class']) { 
    763853            // If it is a part of a package then we need to put it in a package subfolder 
    764854            if (isset($definition['is_package']) && $definition['is_package']) { 
    765                 $writePath  = $this->_path . DIRECTORY_SEPARATOR . $definition['package_name'] . DIRECTORY_SEPARATOR . $this->_baseClassesDirectory; 
     855                $basePath = $this->_path . DIRECTORY_SEPARATOR . $definition['package_name']; 
     856                $writePath = $basePath . DIRECTORY_SEPARATOR . $this->_baseClassesDirectory; 
    766857            // Otherwise lets just put it in the root generated folder 
    767858            } else { 
     
    770861        } 
    771862 
     863        // If we have a writePath from the if else conditionals above then use it 
    772864        if (isset($writePath)) { 
    773865            Doctrine_Lib::makeDirectories($writePath); 
    774              
     866 
    775867            $writePath .= DIRECTORY_SEPARATOR . $fileName; 
     868        // Otherwise none of the conditions were met and we aren't generating base classes 
    776869        } else { 
    777870            Doctrine_Lib::makeDirectories($this->_path); 
    778              
     871 
    779872            $writePath = $this->_path . DIRECTORY_SEPARATOR . $fileName; 
    780873        } 
     
    789882        $code .= PHP_EOL . $definitionCode; 
    790883 
     884        $bytes = file_put_contents($writePath, $code); 
     885        return; 
     886         
    791887        if (isset($definition['generate_once']) && $definition['generate_once'] === true) { 
    792888            if ( ! file_exists($writePath)) { 
  • branches/0.10/tests/BaseTestCase.php

    r3888 r3889  
    3535    public function testAggressiveModelLoading() 
    3636    { 
    37         $path = realpath('../models/ModelLoadingTest/Aggressive'); 
     37        $path = realpath('ModelLoadingTest/Aggressive'); 
    3838         
    3939        $models = Doctrine::loadModels($path, Doctrine::MODEL_LOADING_AGGRESSIVE); 
     
    5555    public function testConservativeModelLoading() 
    5656    { 
    57         $path = realpath('../models/ModelLoadingTest/Conservative'); 
     57        $path = realpath('ModelLoadingTest/Conservative'); 
    5858 
    5959        $models = Doctrine::loadModels($path, Doctrine::MODEL_LOADING_CONSERVATIVE); 
  • branches/0.10/tests/Import/BuilderTestCase.php

    r3888 r3889  
    4646        $schemaTestInheritanceChild2 = new ReflectionClass('SchemaTestInheritanceChild2'); 
    4747 
    48         /* 
    4948        $schemaTestInheritanceParentTable = new ReflectionClass('SchemaTestInheritanceParentTable'); 
    5049        $schemaTestInheritanceChild1Table = new ReflectionClass('SchemaTestInheritanceChild1Table'); 
    5150        $schemaTestInheritanceChild2Table = new ReflectionClass('SchemaTestInheritanceChild2Table'); 
    52         */ 
    5351 
     52        $this->assertTrue($schemaTestInheritanceParent->isSubClassOf('Doctrine_Record')); 
    5453        $this->assertTrue($schemaTestInheritanceParent->isSubClassOf('BaseSchemaTestInheritanceParent')); 
     54        $this->assertTrue($schemaTestInheritanceParent->isSubClassOf('PackageSchemaTestInheritanceParent')); 
    5555        $this->assertTrue($schemaTestInheritanceChild1->isSubClassOf('BaseSchemaTestInheritanceChild1')); 
    5656        $this->assertTrue($schemaTestInheritanceChild2->isSubClassOf('BaseSchemaTestInheritanceChild2')); 
     
    6363        $this->assertTrue($schemaTestInheritanceChild2->isSubClassOf('SchemaTestInheritanceChild1')); 
    6464        $this->assertTrue($schemaTestInheritanceChild2->isSubClassOf('BaseSchemaTestInheritanceChild1')); 
     65        $this->assertTrue($schemaTestInheritanceChild2->isSubClassOf('PackageSchemaTestInheritanceParent')); 
    6566 
    66         /* 
    6767        $this->assertTrue($schemaTestInheritanceParentTable->isSubClassOf('Doctrine_Table')); 
    6868        $this->assertTrue($schemaTestInheritanceChild1Table->isSubClassOf('SchemaTestInheritanceParentTable')); 
    6969        $this->assertTrue($schemaTestInheritanceChild1Table->isSubClassOf('PackageSchemaTestInheritanceParentTable')); 
    70         */ 
    71          
     70 
     71        $this->assertTrue($schemaTestInheritanceChild2Table->isSubClassOf('SchemaTestInheritanceParentTable')); 
     72        $this->assertTrue($schemaTestInheritanceChild2Table->isSubClassOf('PackageSchemaTestInheritanceParentTable')); 
     73        $this->assertTrue($schemaTestInheritanceChild2Table->isSubClassOf('SchemaTestInheritanceChild1Table')); 
     74        $this->assertTrue($schemaTestInheritanceChild2Table->isSubClassOf('PackageSchemaTestInheritanceChild1Table')); 
     75 
    7276        Doctrine_Lib::removeDirectories($path); 
    7377    } 
  • branches/0.10/tests/run.php

    r3888 r3889  
    9090$export->addTestCase(new Doctrine_Export_Mysql_TestCase()); 
    9191$export->addTestCase(new Doctrine_Export_Sqlite_TestCase()); 
     92$export->addTestCase(new Doctrine_Export_Schema_TestCase()); 
    9293$test->addTestCase($export); 
    9394 
     
    104105$import->addTestCase(new Doctrine_Import_Oracle_TestCase()); 
    105106$import->addTestCase(new Doctrine_Import_Sqlite_TestCase()); 
     107$import->addTestCase(new Doctrine_Import_Builder_TestCase()); 
     108$import->addTestCase(new Doctrine_Import_Schema_TestCase()); 
    106109$test->addTestCase($import); 
    107110 
     
    228231$record->addTestCase(new Doctrine_Record_Inheritance_TestCase()); 
    229232$record->addTestCase(new Doctrine_Record_Synchronize_TestCase()); 
    230 $record->addTestCase(new Doctrine_Import_Builder_TestCase()); 
    231233$test->addTestCase($record); 
    232234 
     
    279281$test->addTestCase($parser); 
    280282 
    281 $schemaFiles = new GroupTest('Schema files', 'schema_files'); 
    282 $schemaFiles->addTestCase(new Doctrine_Import_Schema_TestCase()); 
    283 $schemaFiles->addTestCase(new Doctrine_Export_Schema_TestCase()); 
    284 $test->addTestCase($schemaFiles); 
    285  
    286283$data = new GroupTest('Data exporting/importing fixtures', 'data_fixtures'); 
    287284$data->addTestCase(new Doctrine_Data_Import_TestCase());