Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DoctrineSchemaBuilderFactory
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 3
90
0.00% covered (danger)
0.00%
0 / 1
 getSchemaBuilder
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSchemaChangeBuilder
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getPlatform
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2
3namespace Wikimedia\Rdbms;
4
5use Doctrine\DBAL\Platforms\AbstractPlatform;
6use Doctrine\DBAL\Platforms\SqlitePlatform;
7use Doctrine\DBAL\Types\Type;
8use InvalidArgumentException;
9
10/**
11 * @experimental
12 * @unstable
13 */
14class 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}