Last active
January 16, 2018 04:09
-
-
Save cburyta/44d29d7fdecf6c862df7acdbfcc8796c to your computer and use it in GitHub Desktop.
Pull database configs into a Symfony app using symfony/flex on Platform.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# file: config/packages/doctrine.yaml | |
parameters: | |
# Adds a fallback DATABASE_URL if the env var is not set. | |
# This allows you to run cache:warmup even if your | |
# environment variables are not available yet. | |
# You should not need to change this value. | |
env(DATABASE_URL): '' | |
doctrine: | |
dbal: | |
url: '%env(DATABASE_URL)%' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# file: config/parameters_platform.php | |
$relationships = getenv("PLATFORM_RELATIONSHIPS"); | |
if (!$relationships) { | |
return; | |
} | |
$relationships = json_decode(base64_decode($relationships), TRUE); | |
foreach ($relationships['database'] as $endpoint) { | |
if (empty($endpoint['query']['is_master'])) { | |
continue; | |
} | |
// setup database url for environment with platform.sh env var | |
$database_url = "{$endpoint['scheme']}://{$endpoint['username']}:{$endpoint['password']}@{$endpoint['host']}:{$endpoint['port']}/{$endpoint['path']}"; | |
// pass the parameter to the app | |
$container->setParameter('env(DATABASE_URL)', $database_url); | |
} | |
# Store session into /tmp. | |
ini_set('session.save_path', '/tmp/sessions'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# file: config/services.yaml | |
imports: | |
- { resource: 'parameters_platform.php', ignore_errors: true } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment