Debian Codegraph @

includes/libs/rdbms/dbal/DoctrineSchemaBuilderFactory.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Rdbms;
4
5 use Doctrine\DBAL\Platforms\AbstractPlatform;
6 use Doctrine\DBAL\Platforms\SqlitePlatform;
7 use Doctrine\DBAL\Types\Type;
8 use InvalidArgumentException;
9
10 /**
11 * @experimental
12 * @unstable
13 */
14 class DoctrineSchemaBuilderFactory {
15
16 /**
17 * @param string $platform one of strings 'mysql', 'postgres' or 'sqlite'
18 * @return DoctrineSchemaBuilder
19 */
20 public function getSchemaBuilder( string $platform ) {
21 return new DoctrineSchemaBuilder( $this->getPlatform( $platform ) );
22 }
23
24 /**
25 * @param string $platform one of strings 'mysql', 'postgres' or 'sqlite'
26 * @return DoctrineSchemaChangeBuilder
27 */
28 public function getSchemaChangeBuilder( $platform ) {
29 return new DoctrineSchemaChangeBuilder( $this->getPlatform( $platform ) );
30 }
31
32 /**
33 * @param string $platform
34 * @return AbstractPlatform
35 */
36 private function getPlatform( string $platform ) {
37 switch ( $platform ) {
38 case 'mysql':
39 $platformObject = new MWMySQLPlatform;
40 break;
41 case 'postgres':
42 $platformObject = new MWPostgreSqlPlatform;
43 break;
44 case 'sqlite':
45 $platformObject = new SqlitePlatform;
46 break;
47 default:
48 throw new InvalidArgumentException( 'Unknown platform: ' . $platform );
49 }
50
51 $customTypes = [
52 'Enum' => [ EnumType::class, EnumType::ENUM ],
53 'Tinyint' => [ TinyIntType::class, TinyIntType::TINYINT ],
54 'Timestamp' => [ TimestampType::class, TimestampType::TIMESTAMP ],
55 ];
56
57 foreach ( $customTypes as $type => [ $class, $name ] ) {
58 if ( !Type::hasType( $name ) ) {
59 Type::addType( $name, $class );
60 }
61 $platformObject->registerDoctrineTypeMapping( $type, $name );
62 }
63
64 return $platformObject;
65 }
66 }