Skip to content

Instantly share code, notes, and snippets.

@OskarStark
Last active January 24, 2018 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OskarStark/e3074594e33f0bbdb61bb8362801e105 to your computer and use it in GitHub Desktop.
Save OskarStark/e3074594e33f0bbdb61bb8362801e105 to your computer and use it in GitHub Desktop.
compiler pass to get content of PLATFORM_VARIABLES env var of Platform.sh
<?php
namespace AppBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* @author Oskar Stark <oskarstark@googlemail.com>
*/
class PlatformShCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (getenv('PLATFORM_RELATIONSHIPS')) {
$relationships = json_decode(base64_decode(getenv('PLATFORM_RELATIONSHIPS')), true);
foreach ($relationships['database'] as $endpoint) {
if (empty($endpoint['query']['is_master'])) {
continue;
}
$container->setParameter('database_driver', 'pdo_'.$endpoint['scheme']);
$container->setParameter('database_host', $endpoint['host']);
$container->setParameter('database_port', $endpoint['port']);
$container->setParameter('database_name', $endpoint['path']);
$container->setParameter('database_user', $endpoint['username']);
$container->setParameter('database_password', $endpoint['password']);
$container->setParameter('database_path', '');
}
if (isset($relationships['cache'][0])) {
$container->setParameter('env(MEMCACHED_HOST)', $relationships['cache'][0]['host']);
$container->setParameter('env(MEMCACHED_PORT)', $relationships['cache'][0]['port']);
}
}
if (getenv('PLATFORM_VARIABLES')) {
$variables = json_decode(base64_decode(getenv('PLATFORM_VARIABLES')), true);
foreach ($variables as $key => $value) {
// ignore php settings from .platform.app.yaml
if ('php:' == substr($key, 0, 4)) {
continue;
}
$container->setParameter(sprintf('env(%s)', $key), $value);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment