Coverage for Doctrine

Back to coverage report

1 <?php
2 /*
3  *  $Id: Doctrine.php 2989 2007-10-22 16:32:07Z romanb $
4  *
5  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16  *
17  * This software consists of voluntary contributions made by many individuals
18  * and is licensed under the LGPL. For more information, see
19  * <http://www.phpdoctrine.com>.
20  */
21
22 /**
23  * Doctrine
24  * the base class of Doctrine framework
25  *
26  * @package     Doctrine
27  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
28  * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
29  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
30  * @link        www.phpdoctrine.com
31  * @since       1.0
32  * @version     $Revision: 2989 $
33  */
34 final class Doctrine
35 {
36     /**
37      * VERSION
38      */
39     const VERSION                   = '0.1.0';
40     
41     /**
42      * ERROR CONSTANTS
43      */
44     const ERR                       = -1;
45     const ERR_SYNTAX                = -2;
46     const ERR_CONSTRAINT            = -3;
47     const ERR_NOT_FOUND             = -4;
48     const ERR_ALREADY_EXISTS        = -5;
49     const ERR_UNSUPPORTED           = -6;
50     const ERR_MISMATCH              = -7;
51     const ERR_INVALID               = -8;
52     const ERR_NOT_CAPABLE           = -9;
53     const ERR_TRUNCATED             = -10;
54     const ERR_INVALID_NUMBER        = -11;
55     const ERR_INVALID_DATE          = -12;
56     const ERR_DIVZERO               = -13;
57     const ERR_NODBSELECTED          = -14;
58     const ERR_CANNOT_CREATE         = -15;
59     const ERR_CANNOT_DELETE         = -16;
60     const ERR_CANNOT_DROP           = -17;
61     const ERR_NOSUCHTABLE           = -18;
62     const ERR_NOSUCHFIELD           = -19;
63     const ERR_NEED_MORE_DATA        = -20;
64     const ERR_NOT_LOCKED            = -21;
65     const ERR_VALUE_COUNT_ON_ROW    = -22;
66     const ERR_INVALID_DSN           = -23;
67     const ERR_CONNECT_FAILED        = -24;
68     const ERR_EXTENSION_NOT_FOUND   = -25;
69     const ERR_NOSUCHDB              = -26;
70     const ERR_ACCESS_VIOLATION      = -27;
71     const ERR_CANNOT_REPLACE        = -28;
72     const ERR_CONSTRAINT_NOT_NULL   = -29;
73     const ERR_DEADLOCK              = -30;
74     const ERR_CANNOT_ALTER          = -31;
75     const ERR_MANAGER               = -32;
76     const ERR_MANAGER_PARSE         = -33;
77     const ERR_LOADMODULE            = -34;
78     const ERR_INSUFFICIENT_DATA     = -35;
79     const ERR_CLASS_NAME            = -36;
80
81     /**
82      * PDO derived constants
83      */
84     const CASE_LOWER = 2;
85     const CASE_NATURAL = 0;
86     const CASE_UPPER = 1;
87     const CURSOR_FWDONLY = 0;
88     const CURSOR_SCROLL = 1;
89     const ERRMODE_EXCEPTION = 2;
90     const ERRMODE_SILENT = 0;
91     const ERRMODE_WARNING = 1;
92     const FETCH_ASSOC = 2;
93     const FETCH_BOTH = 4;
94     const FETCH_BOUND = 6;
95     const FETCH_CLASS = 8;
96     const FETCH_CLASSTYPE = 262144;
97     const FETCH_COLUMN = 7;
98     const FETCH_FUNC = 10;
99     const FETCH_GROUP = 65536;
100     const FETCH_INTO = 9;
101     const FETCH_LAZY = 1;
102     const FETCH_NAMED = 11;
103     const FETCH_NUM = 3;
104     const FETCH_OBJ = 5;
105     const FETCH_ORI_ABS = 4;
106     const FETCH_ORI_FIRST = 2;
107     const FETCH_ORI_LAST = 3;
108     const FETCH_ORI_NEXT = 0;
109     const FETCH_ORI_PRIOR = 1;
110     const FETCH_ORI_REL = 5;
111     const FETCH_SERIALIZE = 524288;
112     const FETCH_UNIQUE = 196608;
113     const NULL_EMPTY_STRING = 1;
114     const NULL_NATURAL = 0;
115     const NULL_TO_STRING         = NULL;
116     const PARAM_BOOL = 5;
117     const PARAM_INPUT_OUTPUT = -2147483648;
118     const PARAM_INT = 1;
119     const PARAM_LOB = 3;
120     const PARAM_NULL = 0;
121     const PARAM_STMT = 4;
122     const PARAM_STR = 2;
123
124     /**
125      * ATTRIBUTE CONSTANTS
126      */
127
128     /**
129      * PDO derived attributes
130      */
131     const ATTR_AUTOCOMMIT           = 0;
132     const ATTR_PREFETCH             = 1;
133     const ATTR_TIMEOUT              = 2;
134     const ATTR_ERRMODE              = 3;
135     const ATTR_SERVER_VERSION       = 4;
136     const ATTR_CLIENT_VERSION       = 5;
137     const ATTR_SERVER_INFO          = 6;
138     const ATTR_CONNECTION_STATUS    = 7;
139     const ATTR_CASE                 = 8;
140     const ATTR_CURSOR_NAME          = 9;
141     const ATTR_CURSOR               = 10;
142     const ATTR_ORACLE_NULLS         = 11;
143     const ATTR_PERSISTENT           = 12;
144     const ATTR_STATEMENT_CLASS      = 13;
145     const ATTR_FETCH_TABLE_NAMES    = 14;
146     const ATTR_FETCH_CATALOG_NAMES  = 15;
147     const ATTR_DRIVER_NAME          = 16;
148     const ATTR_STRINGIFY_FETCHES    = 17;
149     const ATTR_MAX_COLUMN_LEN       = 18;
150
151     /**
152      * Doctrine constants
153      */
154     const ATTR_LISTENER             = 100;
155     const ATTR_QUOTE_IDENTIFIER     = 101;
156     const ATTR_FIELD_CASE           = 102;
157     const ATTR_IDXNAME_FORMAT       = 103;
158     const ATTR_SEQNAME_FORMAT       = 104;
159     const ATTR_SEQCOL_NAME          = 105;
160     const ATTR_CMPNAME_FORMAT       = 118;
161     const ATTR_DBNAME_FORMAT        = 117;
162     const ATTR_TBLCLASS_FORMAT      = 119;
163     const ATTR_EXPORT               = 140;
164     const ATTR_DECIMAL_PLACES       = 141;
165
166     const ATTR_PORTABILITY          = 106;
167     const ATTR_VALIDATE             = 107;
168     const ATTR_COLL_KEY             = 108;
169     const ATTR_QUERY_LIMIT          = 109;
170     const ATTR_DEFAULT_TABLE_TYPE   = 112;
171     const ATTR_DEF_TEXT_LENGTH      = 113;
172     const ATTR_DEF_VARCHAR_LENGTH   = 114;
173     const ATTR_DEF_TABLESPACE       = 115;
174     const ATTR_EMULATE_DATABASE     = 116;
175     const ATTR_USE_NATIVE_ENUM      = 117;
176     const ATTR_DEFAULT_SEQUENCE     = 133;
177
178     const ATTR_FETCHMODE            = 118;
179     const ATTR_NAME_PREFIX          = 121;
180     const ATTR_CREATE_TABLES        = 122;
181     const ATTR_COLL_LIMIT           = 123;
182
183     const ATTR_CACHE                = 150;
184     const ATTR_CACHE_LIFESPAN       = 151;
185     const ATTR_LOAD_REFERENCES      = 153;
186     const ATTR_RECORD_LISTENER      = 154;
187     const ATTR_THROW_EXCEPTIONS     = 155;
188
189     /**
190      * LIMIT CONSTANTS
191      */
192
193     /**
194      * constant for row limiting
195      */
196     const LIMIT_ROWS       = 1;
197
198     /**
199      * constant for record limiting
200      */
201     const LIMIT_RECORDS    = 2;
202
203     /**
204      * FETCHMODE CONSTANTS
205      */
206
207     /**
208      * IMMEDIATE FETCHING
209      * mode for immediate fetching
210      */
211     const FETCH_IMMEDIATE       = 0;
212
213     /**
214      * BATCH FETCHING
215      * mode for batch fetching
216      */
217     const FETCH_BATCH           = 1;
218
219     /**
220      * LAZY FETCHING
221      * mode for offset fetching
222      */
223     const FETCH_OFFSET          = 3;
224
225     /**
226      * LAZY OFFSET FETCHING
227      * mode for lazy offset fetching
228      */
229     const FETCH_LAZY_OFFSET     = 4;
230
231     /**
232      * FETCH CONSTANTS
233      */
234
235
236     /**
237      * FETCH VALUEHOLDER
238      */
239     const FETCH_VHOLDER         = 1;
240
241     /**
242      * FETCH RECORD
243      *
244      * Specifies that the fetch method shall return Doctrine_Record
245      * objects as the elements of the result set.
246      *
247      * This is the default fetchmode.
248      */
249     const FETCH_RECORD          = 2;
250
251     /**
252      * FETCH ARRAY
253      */
254     const FETCH_ARRAY           = 3;
255
256     /**
257      * PORTABILITY CONSTANTS
258      */
259
260     /**
261      * Portability: turn off all portability features.
262      * @see self::ATTR_PORTABILITY
263      */
264     const PORTABILITY_NONE          = 0;
265
266     /**
267      * Portability: convert names of tables and fields to case defined in the
268      * "field_case" option when using the query*(), fetch*() methods.
269      * @see self::ATTR_PORTABILITY
270      */
271     const PORTABILITY_FIX_CASE      = 1;
272
273     /**
274      * Portability: right trim the data output by query*() and fetch*().
275      * @see self::ATTR_PORTABILITY
276      */
277     const PORTABILITY_RTRIM         = 2;
278
279     /**
280      * Portability: force reporting the number of rows deleted.
281      * @see self::ATTR_PORTABILITY
282      */
283     const PORTABILITY_DELETE_COUNT  = 4;
284
285     /**
286      * Portability: convert empty values to null strings in data output by
287      * query*() and fetch*().
288      * @see self::ATTR_PORTABILITY
289      */
290     const PORTABILITY_EMPTY_TO_NULL = 8;
291
292     /**
293      * Portability: removes database/table qualifiers from associative indexes
294      * @see self::ATTR_PORTABILITY
295      */
296     const PORTABILITY_FIX_ASSOC_FIELD_NAMES = 16;
297
298     /**
299      * Portability: makes Doctrine_Expression throw exception for unportable RDBMS expressions
300      * @see self::ATTR_PORTABILITY
301      */
302     const PORTABILITY_EXPR          = 32;
303
304     /**
305      * Portability: turn on all portability features.
306      * @see self::ATTR_PORTABILITY
307      */
308     const PORTABILITY_ALL           = 63;
309
310     /**
311      * LOCKMODE CONSTANTS
312      */
313
314     /**
315      * mode for optimistic locking
316      */
317     const LOCK_OPTIMISTIC       = 0;
318
319     /**
320      * mode for pessimistic locking
321      */
322     const LOCK_PESSIMISTIC      = 1;
323
324     /**
325      * EXPORT CONSTANTS
326      */
327
328     /**
329      * EXPORT_NONE
330      */
331     const EXPORT_NONE               = 0;
332
333     /**
334      * EXPORT_TABLES
335      */
336     const EXPORT_TABLES             = 1;
337
338     /**
339      * EXPORT_CONSTRAINTS
340      */
341     const EXPORT_CONSTRAINTS        = 2;
342
343     /**
344      * EXPORT_PLUGINS
345      */
346     const EXPORT_PLUGINS            = 4;
347
348     /**
349      * EXPORT_ALL
350      */
351     const EXPORT_ALL                = 7;
352
353     /**
354      * HYDRATION CONSTANTS
355      */
356     const HYDRATE_RECORD            = 2;
357
358     /**
359      * HYDRATE_ARRAY
360      */
361     const HYDRATE_ARRAY             = 3;
362     
363     /**
364      * HYDRATE_NONE
365      */
366     const HYDRATE_NONE              = 4;
367
368     /**
369      * VALIDATION CONSTANTS
370      */
371     const VALIDATE_NONE             = 0;
372
373     /**
374      * VALIDATE_LENGTHS
375      */
376     const VALIDATE_LENGTHS          = 1;
377
378     /**
379      * VALIDATE_TYPES
380      */
381     const VALIDATE_TYPES            = 2;
382
383     /**
384      * VALIDATE_CONSTRAINTS
385      */
386     const VALIDATE_CONSTRAINTS      = 4;
387
388     /**
389      * VALIDATE_ALL
390      */
391     const VALIDATE_ALL              = 7;
392
393     /**
394      * IDENTIFIER_AUTOINC
395      *
396      * constant for auto_increment identifier
397      */
398     const IDENTIFIER_AUTOINC        = 1;
399
400     /**
401      * IDENTIFIER_SEQUENCE
402      *
403      * constant for sequence identifier
404      */
405     const IDENTIFIER_SEQUENCE       = 2;
406
407     /**
408      * IDENTIFIER_NATURAL
409      *
410      * constant for normal identifier
411      */
412     const IDENTIFIER_NATURAL        = 3;
413
414     /**
415      * IDENTIFIER_COMPOSITE
416      *
417      * constant for composite identifier
418      */
419     const IDENTIFIER_COMPOSITE      = 4;
420
421     /**
422      * Path
423      *
424      * @var string $path            doctrine root directory
425      */
426     private static $_path;
427
428     /**
429      * Debug
430      *
431      * Bool true/false
432      *
433      * @var boolean $_debug
434      */
435     private static $_debug = false;
436
437     /**
438      * __construct
439      *
440      * @return void
441      * @throws Doctrine_Exception
442      */
443     public function __construct()
444     {
445         throw new Doctrine_Exception('Doctrine is static class. No instances can be created.');
446     }
447
448     /**
449      * debug
450      *
451      * @param string $bool 
452      * @return void
453      */
454     public static function debug($bool = null)
455     {
456         if ($bool !== null) {
457             self::$_debug = (bool) $bool;
458         }
459         
460         return self::$_debug;
461     }
462
463     /**
464      * getPath
465      * returns the doctrine root
466      *
467      * @return string
468      */
469     public static function getPath()
470     {
471         if ( ! self::$_path) {
472             self::$_path = dirname(__FILE__);
473         }
474         
475         return self::$_path;
476     }
477
478     /**
479      * loadAll
480      * loads all runtime classes
481      *
482      * @return void
483      */
484     public static function loadAll()
485     {
486         return self::loadAllRuntimeClasses();
487     }
488
489     /**
490      * importSchema
491      * method for importing existing schema to Doctrine_Record classes
492      *
493      * @param string $directory Directory to write your models to
494      * @param array $databases Array of databases to generate models for
495      * @return boolean
496      */
497     public static function importSchema($directory, array $databases = array())
498     {
499         return self::generateModelsFromDb($directory, $databases);
500     }
501
502     /**
503      * exportSchema
504      * method for exporting Doctrine_Record classes to a schema
505      *
506      * @param string $directory Directory containing your models
507      * @return void
508      */
509     public static function exportSchema($directory = null)
510     {
511         return self::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 self::generateSqlFromModels($directory);
523     }
524
525     /**
526      * loadAllRuntimeClasses
527      *
528      * loads all runtime classes
529      *
530      * @return void
531      */
532     public static function loadAllRuntimeClasses()
533     {
534         $classes = Doctrine_Compiler::getRuntimeClasses();
535
536         foreach ($classes as $class) {
537             self::autoload($class);
538         }
539     }
540
541     /**
542      * loadModels
543      *
544      * Recursively load all models from a directory or array of directories
545      * 
546      * @param string $directory Path to directory of models or array of directory paths
547      * @return array $loadedModels
548      */
549     public static function loadModels($directory)
550     {
551         $declared = get_declared_classes();
552         
553         if ($directory !== null) {
554             foreach ((array) $directory as $dir) {
555                 $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir),
556                                                         RecursiveIteratorIterator::LEAVES_ONLY);
557
558                 foreach ($it as $file) {
559                     $e = explode('.', $file->getFileName());
560                     if (end($e) === 'php' && strpos($file->getFileName(), '.inc') === false) {
561                         require_once $file->getPathName();
562                     }
563                 }
564             }
565             
566             $declared = array_diff(get_declared_classes(), $declared);
567         }
568         
569         return self::getLoadedModels($declared);
570     }
571
572     /**
573      * getLoadedModels
574      *
575      * Get all the loaded models, you can provide an array of classes or it will use get_declared_classes()
576      * 
577      * Will filter through an array of classes and return the Doctrine_Records out of them.
578      * If you do not specify $classes it will return all of the currently loaded Doctrine_Records
579      *
580      * @param $classes Array of classes to filter through, otherwise uses get_declared_classes()
581      * @return array $loadedModels
582      */
583     public static function getLoadedModels($classes = null)
584     {
585         if ($classes === null) {
586             $classes = get_declared_classes();
587         }
588         
589         $parent = new ReflectionClass('Doctrine_Record');
590         
591         $loadedModels = array();
592         
593         foreach ((array) $classes as $name) {
594             $class = new ReflectionClass($name);
595             
596             // Skip the following classes
597             // - abstract classes
598             // - not a subclass of Doctrine_Record 
599             // - don't have a setTableDefinition method
600             if ($class->isAbstract() || 
601                 !$class->isSubClassOf($parent) || 
602                 !$class->hasMethod('setTableDefinition')) {
603                 continue;
604             }
605             
606             $loadedModels[] = $name;
607         }
608         
609         return $loadedModels;
610     }
611
612     /**
613      * getConnectionByTableName
614      *
615      * Get the connection object for a table by the actual table name
616      * 
617      * @param string $tableName 
618      * @return object Doctrine_Connection
619      */
620     public static function getConnectionByTableName($tableName)
621     {
622         $loadedModels = self::getLoadedModels();
623         
624         foreach ($loadedModels as $name) {
625             $model = new $name();
626             $table = $model->getTable();
627             
628             if ($table->getTableName() == $tableName) {
629                return $table->getConnection(); 
630             }
631         }
632         
633         return Doctrine_Manager::connection();
634     }
635
636     /**
637      * generateModelsFromDb
638      *
639      * method for importing existing schema to Doctrine_Record classes
640      *
641      * @param string $directory Directory to write your models to
642      * @param array $databases Array of databases to generate models for
643      * @return boolean
644      * @throws Exception
645      */
646     public static function generateModelsFromDb($directory, array $databases = array())
647     {
648         return Doctrine_Manager::connection()->import->importSchema($directory, $databases);
649     }
650
651     /**
652      * generateYamlFromDb
653      *
654      * Generates models from database to temporary location then uses those models to generate a yaml schema file.
655      * This should probably be fixed. We should write something to generate a yaml schema file directly from the database.
656      *
657      * @param string $yamlPath Path to write oyur yaml schema file to
658      * @return void
659      */
660     public static function generateYamlFromDb($yamlPath)
661     {
662         $directory = '/tmp/tmp_doctrine_models';
663
664         Doctrine::generateModelsFromDb($directory);
665
666         $export = new Doctrine_Export_Schema();
667         
668         $result = $export->exportSchema($yamlPath, 'yml', $directory);
669         
670         exec('rm -rf ' . $directory);
671         
672         return $result;
673     }
674
675     /**
676      * generateModelsFromYaml
677      *
678      * Generate a yaml schema file from an existing directory of models
679      *
680      * @param string $yamlPath Path to your yaml schema files
681      * @param string $directory Directory to generate your models in
682      * @return void
683      */
684     public static function generateModelsFromYaml($yamlPath, $directory)
685     {
686         $import = new Doctrine_Import_Schema();
687         $import->generateBaseClasses(true);
688         
689         return $import->importSchema($yamlPath, 'yml', $directory);
690     }
691
692     /**
693      * createTablesFromModels
694      *
695      * Creates database tables for the models in the specified directory
696      *
697      * @param string $directory Directory containing your models
698      * @return void
699      */
700     public static function createTablesFromModels($directory = null)
701     {
702         return Doctrine_Manager::connection()->export->exportSchema($directory);
703     }
704
705     /**
706      * generateSqlFromModels
707      *
708      * @param string $directory 
709      * @return string $build  String of sql queries. One query per line
710      */
711     public static function generateSqlFromModels($directory = null)
712     {
713         $sql = Doctrine_Manager::connection()->export->exportSql($directory);
714         
715         $build = '';
716         foreach ($sql as $query) {
717             $build .= $query.";\n";
718         }
719         
720         return $build;
721     }
722
723     /**
724      * generateYamlFromModels
725      *
726      * Generate yaml schema file for the models in the specified directory
727      *
728      * @param string $yamlPath Path to your yaml schema files
729      * @param string $directory Directory to generate your models in
730      * @return void
731      */
732     public static function generateYamlFromModels($yamlPath, $directory)
733     {
734         $export = new Doctrine_Export_Schema();
735         
736         return $export->exportSchema($yamlPath, 'yml', $directory);
737     }
738
739     /**
740      * createDatabases
741      *
742      * Creates databases for connections
743      *
744      * @param string $specifiedConnections Array of connections you wish to create the database for
745      * @return void
746      */
747     public static function createDatabases($specifiedConnections = array())
748     {
749         if ( ! is_array($specifiedConnections)) {
750             $specifiedConnections = (array) $specifiedConnections;
751         }
752         
753         $manager = Doctrine_Manager::getInstance();
754         $connections = $manager->getConnections();
755         
756         foreach ($connections as $name => $connection) {
757             if ( ! empty($specifiedConnections) && !in_array($name, $specifiedConnections)) {
758                 continue;
759             }
760             
761             $info = $manager->parsePdoDsn($connection->getOption('dsn'));
762             $username = $connection->getOption('username');
763             $password = $connection->getOption('password');
764             
765             // Make connection without database specified so we can create it
766             $connect = $manager->openConnection(new PDO($info['scheme'] . ':host=' . $info['host'], $username, $password), 'tmp_connection', false);
767             
768             try {
769                 // Create database
770                 $connect->export->createDatabase($name);
771                 
772                 // Close the tmp connection with no database
773                 $manager->closeConnection($connect);
774                 
775                 // Close original connection
776                 $manager->closeConnection($connection);
777                 
778                 // Reopen original connection with newly created database
779                 $manager->openConnection(new PDO($info['dsn'], $username, $password), $name, true);
780             } catch (Exception $e) {
781                 
782             }
783         }
784     }
785
786     /**
787      * dropDatabases
788      *
789      * Drops databases for connections
790      *
791      * @param string $specifiedConnections Array of connections you wish to drop the database for
792      * @return void
793      */
794     public static function dropDatabases($specifiedConnections = array())
795     {
796         if ( ! is_array($specifiedConnections)) {
797             $specifiedConnections = (array) $specifiedConnections;
798         }
799         
800         $manager = Doctrine_Manager::getInstance();
801         
802         $connections = $manager->getConnections();
803         
804         foreach ($connections as $name => $connection) {
805             if ( ! empty($specifiedConnections) && !in_array($name, $specifiedConnections)) {
806                 continue;
807             }
808             
809             try {
810                 $connection->export->dropDatabase($name);
811             } catch (Exception $e) {
812                 
813             }
814         }
815     }
816
817     /**
818      * dumpData
819      *
820      * Dump data to a yaml fixtures file
821      *
822      * @param string $yamlPath Path to write the yaml data fixtures to
823      * @param string $individualFiles Whether or not to dump data to individual fixtures files
824      * @return void
825      */
826     public static function dumpData($yamlPath, $individualFiles = false)
827     {
828         $data = new Doctrine_Data();
829         
830         return $data->exportData($yamlPath, 'yml', array(), $individualFiles);
831     }
832
833     /**
834      * loadData
835      *
836      * Load data from a yaml fixtures file.
837      * The output of dumpData can be fed to loadData
838      *
839      * @param string $yamlPath Path to your yaml data fixtures
840      * @param string $append Whether or not to append the data
841      * @return void
842      */
843     public static function loadData($yamlPath, $append = false)
844     {
845         $data = new Doctrine_Data();
846         
847         if ( ! $append) {
848             $data->purge();
849         }
850         
851         return $data->importData($yamlPath, 'yml');
852     }
853
854     /**
855      * loadDummyData
856      *
857      * Populdate your models with dummy data
858      *
859      * @param string $append Whether or not to append the data
860      * @param string $num Number of records to populate
861      * @return void
862      */
863     public static function loadDummyData($append, $num = 5)
864     {
865         $data = new Doctrine_Data();
866
867         if ( ! $append) {
868           $data->purge();
869         }
870         
871         return $data->importDummyData($num);
872     }
873
874     /**
875      * migrate
876      * 
877      * Migrate database to specified $to version. Migrates from current to latest if you do not specify.
878      *
879      * @param string $migrationsPath Path to migrations directory which contains your migration classes
880      * @param string $to Version you wish to migrate to.
881      * @return bool true
882      * @throws new Doctrine_Migration_Exception
883      */
884     public static function migrate($migrationsPath, $to = null)
885     {
886         $migration = new Doctrine_Migration($migrationsPath);
887         
888         return $migration->migrate($to);
889     }
890
891     /**
892      * generateMigrationClass
893      *
894      * Generate new migration class skeleton
895      *
896      * @param string $className Name of the Migration class to generate
897      * @param string $migrationsPath Path to directory which contains your migration classes
898      */
899     public static function generateMigrationClass($className, $migrationsPath)
900     {
901         $builder = new Doctrine_Migration_Builder($migrationsPath);
902         
903         return $builder->generateMigrationClass($className);
904     }
905
906     /**
907      * generateMigrationsFromDb
908      *
909      * @param string $migrationsPath 
910      * @return void
911      * @throws new Doctrine_Migration_Exception
912      */
913     public static function generateMigrationsFromDb($migrationsPath)
914     {
915         $builder = new Doctrine_Migration_Builder($migrationsPath);
916         
917         return $builder->generateMigrationsFromDb();
918     }
919
920     /**
921      * generateMigrationsFromModels
922      *
923      * @param string $migrationsPath 
924      * @param string $modelsPath 
925      * @return void
926      */
927     public static function generateMigrationsFromModels($migrationsPath, $modelsPath = null)
928     {
929         $builder = new Doctrine_Migration_Builder($migrationsPath);
930         
931         return $builder->generateMigrationsFromModels($modelsPath);
932     }
933
934     /**
935      * getTable
936      *
937      * @param string $tableName 
938      * @return void
939      */
940     public static function getTable($tableName)
941     {
942         return Doctrine_Manager::table($tableName);
943     }
944
945     /**
946      * connection
947      *
948      * @param string $adapter 
949      * @param string $name 
950      * @return void
951      */
952     public static function connection($adapter, $name = null)
953     {
954         return Doctrine_Manager::connection($adapter, $name);
955     }
956
957     /**
958      * fileFinder
959      *
960      * @param string $type 
961      * @return void
962      */
963     public static function fileFinder($type)
964     {
965         return Doctrine_FileFinder::type($type);
966     }
967
968     /**
969      * compile
970      * method for making a single file of most used doctrine runtime components
971      * including the compiled file instead of multiple files (in worst
972      * cases dozens of files) can improve performance by an order of magnitude
973      *
974      * @param string $target
975      *
976      * @throws Doctrine_Exception
977      * @return void
978      */
979     public static function compile($target = null, $includedDrivers = array())
980     {
981         return Doctrine_Compiler::compile($target, $includedDrivers);
982     }
983
984     /**
985      * simple autoload function
986      * returns true if the class was loaded, otherwise false
987      *
988      * @param string $classname
989      * @return boolean
990      */
991     public static function autoload($classname)
992     {
993         if (class_exists($classname, false)) {
994             return false;
995         }
996         
997         if ( ! self::$_path) {
998             self::$_path = dirname(__FILE__);
999         }
1000         
1001         $class = self::$_path . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $classname) . '.php';
1002
1003         if ( ! file_exists($class)) {
1004             return false;
1005         }
1006
1007         require_once($class);
1008
1009         return true;
1010     }
1011
1012     /**
1013      * dump
1014      *
1015      * dumps a given variable
1016      *
1017      * @param mixed $var        a variable of any type
1018      * @param boolean $output   whether to output the content
1019      * @return void|string
1020      */
1021     public static function dump($var, $output = true)
1022     {
1023         $ret = array();
1024         switch (gettype($var)) {
1025             case 'array':
1026                 $ret[] = 'Array(';
1027                 foreach ($var as $k => $v) {
1028                     $ret[] = $k . ' : ' . self::dump($v, false);
1029                 }
1030                 $ret[] = ")";
1031                 break;
1032             case 'object':
1033                 $ret[] = 'Object(' . get_class($var) . ')';
1034                 break;
1035             default:
1036                 $ret[] = var_export($var, true);
1037         }
1038         if ($output) {
1039             print implode("\n", $ret);
1040         }
1041         return implode("\n", $ret);
1042     }
1043
1044     /**
1045      * returns table name from class name
1046      *
1047      * @param string $classname
1048      * @return string
1049      */
1050     public static function tableize($classname)
1051     {
1052          return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $classname));
1053     }
1054
1055     /**
1056      * returns class name from table name
1057      *
1058      * @param string $tablename
1059      * @return string
1060      */
1061     public static function classify($tablename)
1062     {
1063         return preg_replace_callback('~(_?)(_)([\w])~', array("Doctrine", "classifyCallback"), ucfirst($tablename));
1064     }
1065
1066     /**
1067      * Callback function to classify a classname propperly. 
1068      *
1069      * @param array $matches An array of matches from a pcre_replace call
1070      * @return string A string with matches 1 and mathces 3 in upper case. 
1071      */
1072     public static function classifyCallback($matches)
1073     {
1074         return $matches[1] . strtoupper($matches[3]);
1075     }
1076
1077     /**
1078      * checks for valid class name (uses camel case and underscores)
1079      *
1080      * @param string $classname
1081      * @return boolean
1082      */
1083     public static function isValidClassname($classname)
1084     {
1085         if (preg_match('~(^[a-z])|(_[a-z])|([\W])|(_{2})~', $classname)) {
1086             return false;
1087         }
1088
1089         return true;
1090     }
1091 }