Changeset 4357
- Timestamp:
- 05/10/08 04:09:24 (14 months ago)
- Files:
-
- 1 modified
-
branches/0.11/lib/Doctrine/Import/Builder.php (modified) (52 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/0.11/lib/Doctrine/Import/Builder.php
r4340 r4357 41 41 /** 42 42 * _path 43 * 43 * 44 44 * the path where imported files are being generated 45 45 * … … 47 47 */ 48 48 protected $_path = ''; 49 49 50 50 /** 51 51 * _packagesPrefix … … 71 71 /** 72 72 * _suffix 73 * 73 * 74 74 * File suffix to use when writing class definitions 75 75 * … … 80 80 /** 81 81 * _generateBaseClasses 82 * 82 * 83 83 * Bool true/false for whether or not to generate base classes 84 84 * … … 107 107 /** 108 108 * _baseClassesDirectory 109 * 109 * 110 110 * Directory to put the generate base classes in 111 111 * … … 113 113 */ 114 114 protected $_baseClassesDirectory = 'generated'; 115 115 116 116 /** 117 117 * _baseClassName … … 163 163 } 164 164 } 165 165 166 166 /** 167 167 * setPackagePath 168 168 * 169 * @param string $packagesPrefix 169 * @param string $packagesPrefix 170 170 * @return void 171 171 */ … … 178 178 * setPackagesPath 179 179 * 180 * @param string $packagesPath 180 * @param string $packagesPath 181 181 * @return void 182 182 */ … … 187 187 } 188 188 } 189 189 190 190 /** 191 191 * generateBaseClasses … … 225 225 * Generate physical accessors for columns and relationships 226 226 * 227 * @param boolean $bool 227 * @param boolean $bool 228 228 * @return boolean $generateAccessors 229 229 */ … … 240 240 * setBaseClassPrefix 241 241 * 242 * @param string $prefix 242 * @param string $prefix 243 243 * @return void 244 244 */ … … 247 247 $this->_baseClassPrefix = $prefix; 248 248 } 249 249 250 250 /** 251 251 * getBaseClassPrefix … … 257 257 return $this->_baseClassPrefix; 258 258 } 259 259 260 260 /** 261 261 * setBaseClassesDirectory … … 267 267 $this->_baseClassesDirectory = $baseClassesDirectory; 268 268 } 269 269 270 270 /** 271 271 * setBaseClassName … … 277 277 $this->_baseClassName = $className; 278 278 } 279 279 280 280 /** 281 281 * setSuffix 282 282 * 283 * @param string $suffix 283 * @param string $suffix 284 284 * @return void 285 285 */ … … 302 302 * setOptions 303 303 * 304 * @param string $options 304 * @param string $options 305 305 * @return void 306 306 */ … … 317 317 * setOption 318 318 * 319 * @param string $key 320 * @param string $value 319 * @param string $key 320 * @param string $value 321 321 * @return void 322 322 */ … … 324 324 { 325 325 $name = 'set' . Doctrine_Inflector::classify($key); 326 326 327 327 if (method_exists($this, $name)) { 328 328 $this->$name($value); … … 335 335 /** 336 336 * loadTemplate 337 * 337 * 338 338 * Loads the class template used for generating classes 339 339 * 340 340 * @return void 341 341 */ 342 public function loadTemplate() 342 public function loadTemplate() 343 343 { 344 344 if (isset(self::$_tpl)) { … … 368 368 return; 369 369 } 370 370 371 371 $ret = array(); 372 372 373 373 $i = 0; 374 374 375 375 if (isset($definition['inheritance']['type']) && $definition['inheritance']['type'] == 'concrete') { 376 376 $ret[$i] = " parent::setTableDefinition();"; 377 377 $i++; 378 378 } 379 379 380 380 if (isset($definition['tableName']) && !empty($definition['tableName'])) { 381 381 $ret[$i] = " ".'$this->setTableName(\''. $definition['tableName'].'\');'; 382 382 $i++; 383 383 } 384 384 385 385 if (isset($definition['columns']) && is_array($definition['columns']) && !empty($definition['columns'])) { 386 386 $ret[$i] = $this->buildColumns($definition['columns']); 387 387 $i++; 388 388 } 389 389 390 390 if (isset($definition['indexes']) && is_array($definition['indexes']) && !empty($definition['indexes'])) { 391 391 $ret[$i] = $this->buildIndexes($definition['indexes']); 392 392 $i++; 393 393 } 394 394 395 395 if (isset($definition['attributes']) && is_array($definition['attributes']) && !empty($definition['attributes'])) { 396 396 $ret[$i] = $this->buildAttributes($definition['attributes']); 397 397 $i++; 398 398 } 399 399 400 400 if (isset($definition['options']) && is_array($definition['options']) && !empty($definition['options'])) { 401 401 $ret[$i] = $this->buildOptions($definition['options']); 402 402 $i++; 403 403 } 404 404 405 405 if (isset($definition['inheritance']['subclasses']) && ! empty($definition['inheritance']['subclasses'])) { 406 406 $ret[$i] = " ".'$this->setSubClasses('. $this->varExport($definition['inheritance']['subclasses']).');'; 407 407 $i++; 408 408 } 409 409 410 410 $code = implode(PHP_EOL, $ret); 411 411 $code = trim($code); 412 412 413 413 return PHP_EOL . " public function setTableDefinition()" . PHP_EOL . ' {' . PHP_EOL . ' ' . $code . PHP_EOL . ' }'; 414 414 } … … 417 417 * buildSetUp 418 418 * 419 * @param array $options 420 * @param array $columns 421 * @param array $relations 419 * @param array $options 420 * @param array $columns 421 * @param array $relations 422 422 * @return string 423 423 */ … … 443 443 } 444 444 445 if ($relation['type'] === Doctrine_Relation::ONE || 445 if ($relation['type'] === Doctrine_Relation::ONE || 446 446 $relation['type'] === Doctrine_Relation::ONE_COMPOSITE) { 447 447 $ret[$i] = " ".'$this->hasOne(\'' . $class . $alias . '\''; … … 449 449 $ret[$i] = " ".'$this->hasMany(\'' . $class . $alias . '\''; 450 450 } 451 451 452 452 $a = array(); 453 453 … … 455 455 $a[] = '\'refClass\' => ' . $this->varExport($relation['refClass']); 456 456 } 457 457 458 458 if (isset($relation['deferred']) && $relation['deferred']) { 459 459 $a[] = '\'default\' => ' . $this->varExport($relation['deferred']); 460 460 } 461 461 462 462 if (isset($relation['local']) && $relation['local']) { 463 463 $a[] = '\'local\' => ' . $this->varExport($relation['local']); 464 464 } 465 465 466 466 if (isset($relation['foreign']) && $relation['foreign']) { 467 467 $a[] = '\'foreign\' => ' . $this->varExport($relation['foreign']); 468 468 } 469 469 470 470 if (isset($relation['onDelete']) && $relation['onDelete']) { 471 471 $a[] = '\'onDelete\' => ' . $this->varExport($relation['onDelete']); 472 472 } 473 473 474 474 if (isset($relation['onUpdate']) && $relation['onUpdate']) { 475 475 $a[] = '\'onUpdate\' => ' . $this->varExport($relation['onUpdate']); 476 476 } 477 478 if (isset($relation['equal']) && $relation['equal']) { 479 $a[] = '\'equal\' => ' . $this->varExport($relation['equal']); 480 } 481 477 478 if (isset($relation['equal']) && $relation['equal']) { 479 $a[] = '\'equal\' => ' . $this->varExport($relation['equal']); 480 } 481 482 482 if ( ! empty($a)) { 483 483 $ret[$i] .= ', ' . 'array('; … … 485 485 $ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')'; 486 486 } 487 487 488 488 $ret[$i] .= ');'.PHP_EOL; 489 489 $i++; … … 503 503 $code = implode(PHP_EOL, $ret); 504 504 $code = trim($code); 505 505 506 506 return PHP_EOL . ' public function setUp()' . PHP_EOL . ' {' . PHP_EOL . ' ' . $code . PHP_EOL . ' }'; 507 507 } … … 510 510 * buildColumns 511 511 * 512 * @param string $array 512 * @param string $array 513 513 * @return void 514 514 */ … … 519 519 $columnName = isset($column['name']) ? $column['name']:$name; 520 520 $build .= " ".'$this->hasColumn(\'' . $columnName . '\', \'' . $column['type'] . '\''; 521 521 522 522 if ($column['length']) { 523 523 $build .= ', ' . $column['length']; … … 541 541 $build .= ', ' . $this->varExport($options); 542 542 } 543 543 544 544 $build .= ');' . PHP_EOL; 545 545 } 546 546 547 547 return $build; 548 548 } … … 555 555 * So we do some string replacing to clean it up 556 556 * 557 * @param string $var 557 * @param string $var 558 558 * @return void 559 559 */ … … 581 581 $accessors[] = $name; 582 582 } 583 583 584 584 foreach ($definition['relations'] as $relation) { 585 585 $accessors[] = $relation['alias']; 586 586 } 587 587 588 588 $ret = ''; 589 589 foreach ($accessors as $name) { … … 596 596 // setters 597 597 $ret .= PHP_EOL . ' public function set' . Doctrine_Inflector::classify(Doctrine_Inflector::tableize($name)) . "(\${$name}, \$load = true)" . PHP_EOL; 598 $ret .= " {" . PHP_EOL;598 $ret .= " {" . PHP_EOL; 599 599 $ret .= " return \$this->set('{$name}', \${$name}, \$load);" . PHP_EOL; 600 600 $ret .= " }" . PHP_EOL; … … 607 607 * buildTemplates 608 608 * 609 * @param string $array 609 * @param string $array 610 610 * @return void 611 611 */ … … 614 614 $build = ''; 615 615 foreach ($templates as $name => $options) { 616 616 617 617 if (is_array($options) && !empty($options)) { 618 618 $optionsPhp = $this->varExport($options); 619 619 620 620 $build .= " \$this->loadTemplate('" . $name . "', " . $optionsPhp . ");" . PHP_EOL; 621 621 } else { … … 627 627 } 628 628 } 629 629 630 630 return $build; 631 631 } … … 634 634 * buildActAs 635 635 * 636 * @param string $array 636 * @param string $array 637 637 * @return void 638 638 */ … … 643 643 if (is_array($options) && !empty($options)) { 644 644 $optionsPhp = $this->varExport($options); 645 645 646 646 $build .= " \$this->actAs('" . $name . "', " . $optionsPhp . ");" . PHP_EOL; 647 647 } else { … … 653 653 } 654 654 } 655 655 656 656 return $build; 657 657 } … … 660 660 * buildAttributes 661 661 * 662 * @param string $array 662 * @param string $array 663 663 * @return void 664 664 */ … … 667 667 $build = PHP_EOL; 668 668 foreach ($attributes as $key => $value) { 669 669 670 670 if (is_bool($value)) 671 671 { … … 675 675 $value = array($value); 676 676 } 677 677 678 678 $values = ''; 679 679 foreach ($value as $attr) { 680 680 $values .= "Doctrine::" . strtoupper($key) . "_" . strtoupper($attr) . ' ^ '; 681 681 } 682 682 683 683 // Trim last ^ 684 684 $values = substr($values, 0, strlen($values) - 3); 685 685 } 686 686 687 687 $build .= " \$this->setAttribute(Doctrine::ATTR_" . strtoupper($key) . ", " . $values . ");" . PHP_EOL; 688 688 } 689 689 690 690 return $build; 691 691 } 692 692 693 693 /** 694 694 * buildTableOptions 695 695 * 696 * @param string $array 696 * @param string $array 697 697 * @return void 698 698 */ … … 703 703 $build .= " \$this->option('$name', " . $this->varExport($value) . ");" . PHP_EOL; 704 704 } 705 705 706 706 return $build; 707 707 } … … 710 710 * buildIndexes 711 711 * 712 * @param string $array 712 * @param string $array 713 713 * @return void 714 714 */ … … 755 755 756 756 $accessorsCode = (isset($definition['generate_accessors']) && $definition['generate_accessors'] === true) ? $this->buildAccessors($definition):null; 757 757 758 758 $content = sprintf(self::$_tpl, $abstract, 759 759 $className, … … 762 762 $setUpCode, 763 763 $accessorsCode); 764 764 765 765 return $content; 766 766 } … … 769 769 * buildRecord 770 770 * 771 * @param array $options 772 * @param array $columns 773 * @param array $relations 774 * @param array $indexes 775 * @param array $attributes 776 * @param array $templates 777 * @param array $actAs 771 * @param array $options 772 * @param array $columns 773 * @param array $relations 774 * @param array $indexes 775 * @param array $attributes 776 * @param array $templates 777 * @param array $actAs 778 778 * @return void= 779 779 */ … … 783 783 throw new Doctrine_Import_Builder_Exception('Missing class name.'); 784 784 } 785 785 786 786 $definition['topLevelClassName'] = $definition['className']; 787 787 788 788 if ($this->generateBaseClasses()) { 789 789 $definition['is_package'] = (isset($definition['package']) && $definition['package']) ? true:false; 790 790 791 791 if ($definition['is_package']) { 792 792 $e = explode('.', trim($definition['package'])); 793 793 $definition['package_name'] = $e[0]; 794 794 795 795 $definition['package_path'] = ! empty($e) ? implode(DIRECTORY_SEPARATOR, $e):$definition['package_name']; 796 796 } … … 798 798 $topLevel = $definition; 799 799 unset($topLevel['tableName']); 800 800 801 801 // If we have a package then we need to make this extend the package definition and not the base definition 802 802 // The package definition will then extends the base definition … … 809 809 // Package level definition that extends from the base definition 810 810 if (isset($definition['package'])) { 811 811 812 812 $packageLevel = $definition; 813 813 $packageLevel['className'] = $topLevel['inheritance']['extends']; … … 819 819 $packageLevel['is_package_class'] = true; 820 820 unset($packageLevel['connection']); 821 821 822 822 $packageLevel['tableClassName'] = $packageLevel['className'] . 'Table'; 823 823 $packageLevel['inheritance']['tableExtends'] = isset($definition['inheritance']['extends']) ? $definition['inheritance']['extends'] . 'Table':'Doctrine_Table'; 824 824 825 825 $topLevel['tableClassName'] = $topLevel['topLevelClassName'] . 'Table'; 826 826 $topLevel['inheritance']['tableExtends'] = $packageLevel['className'] . 'Table'; … … 837 837 838 838 $this->writeDefinition($baseClass); 839 839 840 840 if ( ! empty($packageLevel)) { 841 841 $this->writeDefinition($packageLevel); 842 842 } 843 843 844 844 $this->writeDefinition($topLevel); 845 845 } else { … … 878 878 * writeDefinition 879 879 * 880 * @param array $options 881 * @param array $columns 882 * @param array $relations 883 * @param array $indexes 884 * @param array $attributes 885 * @param array $templates 886 * @param array $actAs 880 * @param array $options 881 * @param array $columns 882 * @param array $relations 883 * @param array $indexes 884 * @param array $attributes 885 * @param array $templates 886 * @param array $actAs 887 887 * @return void 888 888 */