|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Cycle Database package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Cycle\Database\Config; |
|
13
|
|
|
|
|
14
|
|
|
use Cycle\Database\Config\Postgres\ConnectionConfig; |
|
|
|
|
|
|
15
|
|
|
use Cycle\Database\Driver\Postgres\PostgresDriver; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @template-extends DriverConfig<ConnectionConfig> |
|
19
|
|
|
*/ |
|
20
|
|
|
class PostgresDriverConfig extends DriverConfig |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Default public schema name for all postgres connections. |
|
24
|
|
|
* |
|
25
|
|
|
* @var non-empty-string |
|
26
|
|
|
*/ |
|
27
|
|
|
public const DEFAULT_SCHEMA = 'public'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var non-empty-array<non-empty-string> |
|
|
|
|
|
|
31
|
|
|
* @psalm-readonly-allow-private-mutation |
|
32
|
|
|
*/ |
|
33
|
|
|
public array $schema; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param ConnectionConfig $connection |
|
37
|
|
|
* @param iterable<non-empty-string>|non-empty-string $schema List of available Postgres |
|
38
|
|
|
* schemas for "search path" (See also {@link https://www.postgresql.org/docs/9.6/ddl-schemas.html}). |
|
39
|
|
|
* The first parameter's item will be used as default schema. |
|
40
|
|
|
* |
|
41
|
|
|
* {@inheritDoc} |
|
42
|
|
|
*/ |
|
43
|
34 |
|
public function __construct( |
|
44
|
|
|
ConnectionConfig $connection, |
|
45
|
|
|
iterable|string $schema = self::DEFAULT_SCHEMA, |
|
46
|
|
|
string $driver = PostgresDriver::class, |
|
47
|
|
|
bool $reconnect = true, |
|
48
|
|
|
string $timezone = 'UTC', |
|
49
|
|
|
bool $queryCache = true, |
|
50
|
|
|
bool $readonlySchema = false, |
|
51
|
|
|
bool $readonly = false, |
|
52
|
|
|
) { |
|
53
|
|
|
/** @psalm-suppress ArgumentTypeCoercion */ |
|
54
|
34 |
|
parent::__construct( |
|
55
|
34 |
|
connection: $connection, |
|
56
|
|
|
driver: $driver, |
|
57
|
|
|
reconnect: $reconnect, |
|
58
|
|
|
timezone: $timezone, |
|
59
|
|
|
queryCache: $queryCache, |
|
60
|
|
|
readonlySchema: $readonlySchema, |
|
61
|
|
|
readonly: $readonly, |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
34 |
|
$this->schema = $this->bootSchema($schema); |
|
65
|
34 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param iterable<non-empty-string>|non-empty-string $schema |
|
69
|
|
|
* |
|
70
|
|
|
* @return array<non-empty-string> |
|
|
|
|
|
|
71
|
|
|
*/ |
|
72
|
34 |
|
private function bootSchema(iterable|string $schema): array |
|
73
|
|
|
{ |
|
74
|
|
|
// Cast any schema config variants to array |
|
75
|
34 |
|
$schema = match (true) { |
|
76
|
34 |
|
$schema instanceof \Traversable => \iterator_to_array($schema), |
|
|
|
|
|
|
77
|
34 |
|
\is_string($schema) => [$schema], |
|
78
|
28 |
|
default => $schema |
|
79
|
|
|
}; |
|
80
|
|
|
|
|
81
|
|
|
// Fill array by default in case that result array is empty |
|
82
|
34 |
|
if ($schema === []) { |
|
83
|
20 |
|
$schema = [self::DEFAULT_SCHEMA]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Remove schema duplications |
|
87
|
34 |
|
return \array_values(\array_unique($schema)); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public static function __set_state(array $an_array): object |
|
91
|
|
|
{ |
|
92
|
|
|
$config = parent::__set_state($an_array); |
|
93
|
|
|
$config->schema = $an_array['schema']; |
|
94
|
|
|
|
|
95
|
|
|
return $config; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: