|
| 1 | +# Standalone connection provider |
| 2 | + |
| 3 | +When dealing with edge case environement where a static database URL cannot |
| 4 | +work, the *connection provider* feature may help you. |
| 5 | + |
| 6 | +Basic idea is to delegate the database connection URL string to a custom function. |
| 7 | + |
| 8 | +:::warning |
| 9 | +This feature is experimental and API is subject to change in the future. |
| 10 | +::: |
| 11 | + |
| 12 | +## Basic example |
| 13 | + |
| 14 | +Use case: you are working in a multi-tenant environment, each client database |
| 15 | +has the same database schema, but has different database credentials. |
| 16 | + |
| 17 | +Let's consider you already have a central component you can access using PHP code |
| 18 | +which allows you to find a client database connection: |
| 19 | + |
| 20 | +```php |
| 21 | +namespace MyApp; |
| 22 | + |
| 23 | +class MyClientInstanceRegistry |
| 24 | +{ |
| 25 | + public static function getDatabaseCredentialsForClient(string $id): array; |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +We are making this example simple for educational purpose, but in real life |
| 30 | +use cases you probably will have a more complex component initialization process |
| 31 | +for this. |
| 32 | + |
| 33 | +And you have the following anonymization configuration: |
| 34 | + |
| 35 | +```yaml |
| 36 | +connections: |
| 37 | + any_client: pgsql://clientid:clientpassword@somehostname/client1 |
| 38 | + |
| 39 | +anonymization: |
| 40 | + any_client: |
| 41 | + users: |
| 42 | + lastname: fr-fr.lastname |
| 43 | +``` |
| 44 | +
|
| 45 | +Your main obstacle here is to give the correct database URL for the client you |
| 46 | +with to act upon. Let's consider that your solution is to use an environment |
| 47 | +variable for acting upon each database independently, such as: |
| 48 | +
|
| 49 | +```sh |
| 50 | +CLIENT_ID="client1" vendor/bin/db-tools anonymize |
| 51 | +``` |
| 52 | + |
| 53 | +Following chapter will guide you throught three different methods to achieve this. |
| 54 | + |
| 55 | +## Implementing the connection provider interface |
| 56 | + |
| 57 | +You can implement the `ConnectionProvider` interface as such: |
| 58 | + |
| 59 | +```php |
| 60 | +namespace MyApp\DbToolsBundle; |
| 61 | + |
| 62 | +use MakinaCorpus\DbToolsBundle\Bridge\Standalone\ConnectionProvider; |
| 63 | + |
| 64 | +class MyClientConnectionProvider implements ConnectionProvider |
| 65 | +{ |
| 66 | + #[\Override] |
| 67 | + public function createConnectionDsn(string $name): string |
| 68 | + { |
| 69 | + if (!$clientId = getenv('CLIENT_ID')) { |
| 70 | + throw new \RuntimeException("Did you forget to set the CLIENT_ID environment variable?"); |
| 71 | + } |
| 72 | + |
| 73 | + $credentials = MyClientInstanceRegistry::getDatabaseCredentialsForClient($clientId); |
| 74 | + |
| 75 | + return \sprintf( |
| 76 | + 'pgsql://%s:%s@%s:%d/%s?some_option=some_value', |
| 77 | + \rawurlencode($credentials['username']), |
| 78 | + \rawurlencode($credentials['password']), |
| 79 | + \rawurlencode($credentials['hostname']), |
| 80 | + $credentials['port'], |
| 81 | + \rawurlencode($credentials['database']), |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +Then adapt your YAML configuration: |
| 88 | + |
| 89 | +```yaml |
| 90 | +connections: |
| 91 | + any_client: MyApp\DbToolsBundle\MyClientConnectionProvider |
| 92 | +``` |
| 93 | +
|
| 94 | +## Using an invokable class |
| 95 | +
|
| 96 | +You otherwise simply can write any class implenting the `__invoke()` method: |
| 97 | + |
| 98 | +```php |
| 99 | +namespace MyApp\DbToolsBundle; |
| 100 | +
|
| 101 | +class MyInvokableClientConnectionProvider |
| 102 | +{ |
| 103 | + public function __invoke() |
| 104 | + { |
| 105 | + if (!$clientId = getenv('CLIENT_ID')) { |
| 106 | + throw new \RuntimeException("Did you forget to set the CLIENT_ID environment variable?"); |
| 107 | + } |
| 108 | +
|
| 109 | + $credentials = MyClientInstanceRegistry::getDatabaseCredentialsForClient($clientId); |
| 110 | +
|
| 111 | + return \sprintf( |
| 112 | + 'pgsql://%s:%s@%s:%d/%s?some_option=some_value', |
| 113 | + \rawurlencode($credentials['username']), |
| 114 | + \rawurlencode($credentials['password']), |
| 115 | + \rawurlencode($credentials['hostname']), |
| 116 | + $credentials['port'], |
| 117 | + \rawurlencode($credentials['database']), |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +Then adapt your YAML configuration: |
| 124 | + |
| 125 | +```yaml |
| 126 | +connections: |
| 127 | + any_client: MyApp\DbToolsBundle\MyInvokableClientConnectionProvider |
| 128 | +``` |
| 129 | + |
| 130 | +## Using a class static method |
| 131 | + |
| 132 | +You can use an object static method: |
| 133 | + |
| 134 | +```php |
| 135 | +namespace MyApp\SomeNamespace; |
| 136 | +
|
| 137 | +class SomeExistingClass |
| 138 | +{ |
| 139 | + // ... your other code (or not). |
| 140 | +
|
| 141 | + public static function myDbToolsConnectionProvider() |
| 142 | + { |
| 143 | + if (!$clientId = getenv('CLIENT_ID')) { |
| 144 | + throw new \RuntimeException("Did you forget to set the CLIENT_ID environment variable?"); |
| 145 | + } |
| 146 | +
|
| 147 | + $credentials = MyClientInstanceRegistry::getDatabaseCredentialsForClient($clientId); |
| 148 | +
|
| 149 | + return \sprintf( |
| 150 | + 'pgsql://%s:%s@%s:%d/%s?some_option=some_value', |
| 151 | + \rawurlencode($credentials['username']), |
| 152 | + \rawurlencode($credentials['password']), |
| 153 | + \rawurlencode($credentials['hostname']), |
| 154 | + $credentials['port'], |
| 155 | + \rawurlencode($credentials['database']), |
| 156 | + ); |
| 157 | + } |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +Then adapt your YAML configuration: |
| 162 | + |
| 163 | +```yaml |
| 164 | +connections: |
| 165 | + any_client: MyApp\SomeNamespace\SomeExistingClass::myDbToolsConnectionProvider |
| 166 | +``` |
| 167 | + |
| 168 | +## Using an arbitrary function |
| 169 | + |
| 170 | +Or simply use any PHP function which was loaded using the autoloader: |
| 171 | + |
| 172 | +```php |
| 173 | +function my_dbtools_client_connection_provider(): string |
| 174 | +{ |
| 175 | + if (!$clientId = getenv('CLIENT_ID')) { |
| 176 | + throw new \RuntimeException("Did you forget to set the CLIENT_ID environment variable?"); |
| 177 | + } |
| 178 | +
|
| 179 | + $credentials = MyClientInstanceRegistry::getDatabaseCredentialsForClient($clientId); |
| 180 | +
|
| 181 | + return \sprintf( |
| 182 | + 'pgsql://%s:%s@%s:%d/%s?some_option=some_value', |
| 183 | + \rawurlencode($credentials['username']), |
| 184 | + \rawurlencode($credentials['password']), |
| 185 | + \rawurlencode($credentials['hostname']), |
| 186 | + $credentials['port'], |
| 187 | + \rawurlencode($credentials['database']), |
| 188 | + ); |
| 189 | +} |
| 190 | +``` |
| 191 | + |
| 192 | +Then adapt your YAML configuration: |
| 193 | + |
| 194 | +```yaml |
| 195 | +connections: |
| 196 | + any_client: my_dbtools_client_connection_provider |
| 197 | +``` |
| 198 | + |
| 199 | +## Notes |
| 200 | + |
| 201 | +In all cases, the method signature function is always the same `connection_provider(string $name): string`. |
| 202 | + |
| 203 | +You may omit the `$name` parameter if you don't intend to use it. It contains |
| 204 | +the connection name as specified in the YAML configuration `connections` value. |
0 commit comments