Last active
January 24, 2018 12:38
-
-
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
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 | |
namespace AppBundle\DependencyInjection\Compiler; | |
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
/** | |
* @author Oskar Stark <[email protected]> | |
*/ | |
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