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
|
|
|
* |
32
|
|
|
* @psalm-readonly-allow-private-mutation |
33
|
|
|
*/ |
34
|
|
|
public array $schema; |
35
|
|
|
|
36
|
|
|
/** |
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
|
|
|
array $options = [], |
53
|
|
|
) { |
54
|
34 |
|
/** @psalm-suppress ArgumentTypeCoercion */ |
55
|
34 |
|
parent::__construct( |
56
|
|
|
connection: $connection, |
57
|
|
|
driver: $driver, |
58
|
|
|
reconnect: $reconnect, |
59
|
|
|
timezone: $timezone, |
60
|
|
|
queryCache: $queryCache, |
61
|
|
|
readonlySchema: $readonlySchema, |
62
|
|
|
readonly: $readonly, |
63
|
|
|
options: $options, |
64
|
34 |
|
); |
65
|
34 |
|
|
66
|
|
|
$this->schema = $this->bootSchema($schema); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param iterable<non-empty-string>|non-empty-string $schema |
71
|
|
|
* |
72
|
34 |
|
* @return array<non-empty-string> |
|
|
|
|
73
|
|
|
*/ |
74
|
|
|
private function bootSchema(iterable|string $schema): array |
75
|
34 |
|
{ |
76
|
34 |
|
// Cast any schema config variants to array |
77
|
34 |
|
$schema = match (true) { |
78
|
28 |
|
$schema instanceof \Traversable => \iterator_to_array($schema), |
|
|
|
|
79
|
|
|
\is_string($schema) => [$schema], |
80
|
|
|
default => $schema, |
81
|
|
|
}; |
82
|
34 |
|
|
83
|
20 |
|
// Fill array by default in case that result array is empty |
84
|
|
|
if ($schema === []) { |
85
|
|
|
$schema = [self::DEFAULT_SCHEMA]; |
86
|
|
|
} |
87
|
34 |
|
|
88
|
|
|
// Remove schema duplications |
89
|
|
|
return \array_values(\array_unique($schema)); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: