Changeset 3204 for trunk/lib/Doctrine.php
- Timestamp:
- 11/22/07 13:40:22 (14 months ago)
- Files:
-
- 1 modified
-
trunk/lib/Doctrine.php (modified) (40 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/Doctrine.php
r3155 r3204 38 38 */ 39 39 const VERSION = '0.1.0'; 40 40 41 41 /** 42 42 * ERROR CONSTANTS … … 362 362 */ 363 363 const HYDRATE_ARRAY = 3; 364 364 365 365 /** 366 366 * HYDRATE_NONE … … 436 436 */ 437 437 private static $_debug = false; 438 438 439 439 /** 440 440 * _loadedModels 441 * 441 * 442 442 * Array of all the loaded models and the path to each one for autoloading 443 443 * … … 445 445 */ 446 446 private static $_loadedModels = array(); 447 447 448 448 /** 449 449 * _validators … … 468 468 * debug 469 469 * 470 * @param string $bool 470 * @param string $bool 471 471 * @return void 472 472 */ … … 476 476 self::$_debug = (bool) $bool; 477 477 } 478 478 479 479 return self::$_debug; 480 480 } … … 491 491 self::$_path = dirname(__FILE__); 492 492 } 493 493 494 494 return self::$_path; 495 495 } … … 499 499 * 500 500 * Recursively load all models from a directory or array of directories 501 * 502 * @param string $directory Path to directory of models or array of directory paths501 * 502 * @param string $directory Path to directory of models or array of directory paths 503 503 * @return array $loadedModels 504 504 */ … … 507 507 if ($directory !== null) { 508 508 $manager = Doctrine_Manager::getInstance(); 509 509 510 510 foreach ((array) $directory as $dir) { 511 511 $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), 512 512 RecursiveIteratorIterator::LEAVES_ONLY); 513 514 513 foreach ($it as $file) { 515 514 $e = explode('.', $file->getFileName()); … … 528 527 * 529 528 * Get all the loaded models, you can provide an array of classes or it will use get_declared_classes() 530 * 529 * 531 530 * Will filter through an array of classes and return the Doctrine_Records out of them. 532 531 * If you do not specify $classes it will return all of the currently loaded Doctrine_Records 533 532 * 534 * @param $classesArray of classes to filter through, otherwise uses get_declared_classes()535 * @return array $loadedModels533 * @param classes Array of classes to filter through, otherwise uses get_declared_classes() 534 * @return array $loadedModels 536 535 */ 537 536 public static function getLoadedModels($classes = null) … … 541 540 $classes = array_merge($classes, array_keys(self::$_loadedModels)); 542 541 } 543 544 $parent = new ReflectionClass('Doctrine_Record'); 545 542 546 543 $loadedModels = array(); 547 544 548 545 foreach ((array) $classes as $name) { 549 $class = new ReflectionClass($name); 550 546 547 try { 548 $class = new ReflectionClass($name); 549 if (self::isValidModelClass($class)) { 550 $loadedModels[] = $name; 551 } 552 } catch (Exception $e) { 553 // Determine class names by the actual inclusion of the model file 554 // The possibility exists that the class name(s) contained in the model 555 // file is not the same as the actual model file name itself 556 if (isset(self::$_loadedModels[$name])) { 557 $declaredBefore = get_declared_classes(); 558 try { 559 require_once self::$_loadedModels[$name]; 560 $declaredAfter = get_declared_classes(); 561 // Using array_slice since array_diff is broken is some versions 562 $foundClasses = array_slice($declaredAfter, count($declaredBefore)-1); 563 if ($foundClasses) { 564 foreach ($foundClasses as $name) { 565 $class = new ReflectionClass($name); 566 if (self::isValidModelClass($class)) { 567 $loadedModels[] = $name; 568 } 569 } 570 } 571 } catch (Exception $e) { 572 continue; 573 } 574 } 575 } 576 577 } 578 579 return $loadedModels; 580 } 581 582 583 /** 584 * isValidModelClass 585 * 586 * Checks whether a reflection class is a valid Doctrine model class 587 * 588 * @param class A reflection class to validate 589 * @return boolean 590 */ 591 public static function isValidModelClass($class) 592 { 593 if ($class instanceof ReflectionClass) { 551 594 // Skip the following classes 552 595 // - abstract classes 553 // - not a subclass of Doctrine_Record 596 // - not a subclass of Doctrine_Record 554 597 // - don't have a setTableDefinition method 555 if ( $class->isAbstract() ||556 !$class->isSubClassOf($parent) ||557 !$class->hasMethod('setTableDefinition')) {558 continue;598 if (!$class->isAbstract() && 599 $class->isSubClassOf('Doctrine_Record') && 600 $class->hasMethod('setTableDefinition')) { 601 return true; 559 602 } 560 561 $loadedModels[] = $name; 562 } 563 564 return $loadedModels; 565 } 603 } 604 return false; 605 } 606 566 607 567 608 /** … … 569 610 * 570 611 * Get the connection object for a table by the actual table name 571 * 572 * @param string $tableName 612 * 613 * @param string $tableName 573 614 * @return object Doctrine_Connection 574 615 */ … … 576 617 { 577 618 $loadedModels = self::getLoadedModels(); 578 619 579 620 foreach ($loadedModels as $name) { 580 621 $model = new $name(); 581 622 $table = $model->getTable(); 582 623 583 624 if ($table->getTableName() == $tableName) { 584 return $table->getConnection(); 625 return $table->getConnection(); 585 626 } 586 627 } 587 628 588 629 return Doctrine_Manager::connection(); 589 630 } … … 620 661 621 662 $export = new Doctrine_Export_Schema(); 622 663 623 664 $result = $export->exportSchema($yamlPath, 'yml', $directory); 624 665 625 666 exec('rm -rf ' . $directory); 626 667 627 668 return $result; 628 669 } … … 642 683 $import = new Doctrine_Import_Schema(); 643 684 $import->setOptions($options); 644 685 645 686 return $import->importSchema($yamlPath, 'yml', $directory); 646 687 } … … 675 716 * generateSqlFromModels 676 717 * 677 * @param string $directory 718 * @param string $directory 678 719 * @return string $build String of sql queries. One query per line 679 720 */ … … 681 722 { 682 723 $sql = Doctrine_Manager::connection()->export->exportSql($directory); 683 724 684 725 $build = ''; 685 726 foreach ($sql as $query) { 686 727 $build .= $query.";\n"; 687 728 } 688 729 689 730 return $build; 690 731 } … … 702 743 { 703 744 $export = new Doctrine_Export_Schema(); 704 745 705 746 return $export->exportSchema($yamlPath, 'yml', $directory); 706 747 } … … 719 760 $specifiedConnections = (array) $specifiedConnections; 720 761 } 721 762 722 763 $manager = Doctrine_Manager::getInstance(); 723 764 $connections = $manager->getConnections(); 724 765 725 766 $results = array(); 726 767 727 768 foreach ($connections as $name => $connection) { 728 769 if ( ! empty($specifiedConnections) && !in_array($name, $specifiedConnections)) { 729 770 continue; 730 771 } 731 772 732 773 $info = $manager->parsePdoDsn($connection->getOption('dsn')); 733 774 $username = $connection->getOption('username'); 734 775 $password = $connection->getOption('password'); 735 776 736 777 // Make connection without database specified so we can create it 737 778 $connect = $manager->openConnection(new PDO($info['scheme'] . ':host=' . $info['host'], $username, $password), 'tmp_connection', false); 738 779 739 780 try { 740 781 // Create database 741 782 $connect->export->createDatabase($name); 742 783 743 784 // Close the tmp connection with no database 744 785 $manager->closeConnection($connect); 745 786 746 787 // Close original connection 747 788 $manager->closeConnection($connection); 748 789 749 790 // Reopen original connection with newly created database 750 791 $manager->openConnection(new PDO($info['dsn'], $username, $password), $name, true); 751 792 752 793 $results[$name] = true; 753 794 } catch (Exception $e) { … … 755 796 } 756 797 } 757 798 758 799 return $results; 759 800 } … … 772 813 $specifiedConnections = (array) $specifiedConnections; 773 814 } 774 815 775 816 $manager = Doctrine_Manager::getInstance(); 776 817 777 818 $connections = $manager->getConnections(); 778 819 779 820 $results = array(); 780 821 781 822 foreach ($connections as $name => $connection) { 782 823 if ( ! empty($specifiedConnections) && !in_array($name, $specifiedConnections)) { 783 824 continue; 784 825 } 785 826 786 827 try { 787 828 $connection->export->dropDatabase($name); 788 829 789 830 $results[$name] = true; 790 831 } catch (Exception $e) { … … 792 833 } 793 834 } 794 835 795 836 return $results; 796 837 } … … 808 849 { 809 850 $data = new Doctrine_Data(); 810 851 811 852 return $data->exportData($yamlPath, 'yml', array(), $individualFiles); 812 853 } … … 825 866 { 826 867 $data = new Doctrine_Data(); 827 868 828 869 if ( ! $append) { 829 870 $data->purge(); 830 871 } 831 872 832 873 return $data->importData($yamlPath, 'yml'); 833 874 } … … 835 876 /** 836 877 * migrate 837 * 878 * 838 879 * Migrate database to specified $to version. Migrates from current to latest if you do not specify. 839 880 * … … 846 887 { 847 888 $migration = new Doctrine_Migration($migrationsPath); 848 889 849 890 return $migration->migrate($to); 850 891 } … … 861 902 { 862 903 $builder = new Doctrine_Migration_Builder($migrationsPath); 863 904 864 905 return $builder->generateMigrationClass($className); 865 906 } … … 868 909 * generateMigrationsFromDb 869 910 * 870 * @param string $migrationsPath 911 * @param string $migrationsPath 871 912 * @return void 872 913 * @throws new Doctrine_Migration_Exception … … 875 916 { 876 917 $builder = new Doctrine_Migration_Builder($migrationsPath); 877 918 878 919 return $builder->generateMigrationsFromDb(); 879 920 } … … 882 923 * generateMigrationsFromModels 883 924 * 884 * @param string $migrationsPath 885 * @param string $modelsPath 925 * @param string $migrationsPath 926 * @param string $modelsPath 886 927 * @return void 887 928 */ … … 889 930 { 890 931 $builder = new Doctrine_Migration_Builder($migrationsPath); 891 932 892 933 return $builder->generateMigrationsFromModels($modelsPath); 893 934 } … … 896 937 * getTable 897 938 * 898 * @param string $tableName 939 * @param string $tableName 899 940 * @return void 900 941 */ … … 907 948 * fileFinder 908 949 * 909 * @param string $type 950 * @param string $type 910 951 * @return void 911 952 */ … … 946 987 return false; 947 988 } 948 989 949 990 if ( ! self::$_path) { 950 991 self::$_path = dirname(__FILE__); 951 992 } 952 993 953 994 $class = self::$_path . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; 954 995 955 996 if (file_exists($class)) { 956 997 require_once($class); 957 998 958 999 return true; 959 1000 } 960 1001 961 1002 $loadedModels = self::$_loadedModels; 962 1003 963 1004 if (isset($loadedModels[$className]) && file_exists($loadedModels[$className])) { 964 1005 require_once($loadedModels[$className]); 965 1006 966 1007 return true; 967 1008 } … … 988 1029 $indent .= " "; 989 1030 foreach ($var as $k => $v) { 990 1031 991 1032 $ret[] = $indent . $k . ' : ' . self::dump($v, false, $indent); 992 1033 } … … 1035 1076 * classifyCallback 1036 1077 * 1037 * Callback function to classify a classname properly. 1078 * Callback function to classify a classname properly. 1038 1079 * 1039 1080 * @param array $matches An array of matches from a pcre_replace call 1040 * @return string A string with matches 1 and mathces 3 in upper case. 1081 * @return string A string with matches 1 and mathces 3 in upper case. 1041 1082 */ 1042 1083 public static function classifyCallback($matches) … … 1061 1102 return true; 1062 1103 } 1063 1104 1064 1105 /** 1065 1106 * makeDirectories 1066 * 1107 * 1067 1108 * Makes the directories for a path recursively 1068 1109 * 1069 * @param string $path 1110 * @param string $path 1070 1111 * @return void 1071 1112 */ … … 1075 1116 return false; 1076 1117 } 1077 1118 1078 1119 if (is_dir($path) || is_file($path)) { 1079 1120 return true; 1080 1121 } 1081 1122 1082 return mkdir($path, $mode, true); 1083 } 1084 1123 return mkdir($path, $mode, true); 1124 } 1125 1085 1126 /** 1086 1127 * removeDirectories 1087 1128 * 1088 * @param string $folderPath 1129 * @param string $folderPath 1089 1130 * @return void 1090 1131 */ … … 1112 1153 } 1113 1154 } 1114 1155 1115 1156 /** 1116 1157 * getValidators … … 1124 1165 if (empty(self::$_validators)) { 1125 1166 $dir = Doctrine::getPath() . DIRECTORY_SEPARATOR . 'Doctrine' . DIRECTORY_SEPARATOR . 'Validator'; 1126 1167 1127 1168 $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::LEAVES_ONLY); 1128 1169 foreach ($files as $file) { 1129 1170 $e = explode('.', $file->getFileName()); 1130 1171 1131 1172 if (end($e) == 'php') { 1132 1173 $name = strtolower($e[0]); 1133 1174 1134 1175 self::$_validators[$name] = $name; 1135 1176 } 1136 1177 } 1137 1178 } 1138 1179 1139 1180 return self::$_validators; 1140 1181 }