Last active
December 22, 2015 07:39
-
-
Save mcrumm/6439870 to your computer and use it in GitHub Desktop.
Symfony2 configuration for Engine Yard environment variables.
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
# app/config/config.yml | |
imports: | |
- { resource: parameters.yml } | |
- { resource: engineyard.php } | |
- { resource: security.yml } | |
... |
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 | |
// app/config/engineyard.php | |
if ('prod' !== $container->getParameter('kernel.environment')) { | |
return; | |
} | |
// Change this to the path to your project config directory. | |
$configPath = '/data/my_project/shared/config'; | |
$eyParams = array( | |
'DB_HOST' => 'database_host', | |
'DB_NAME' => 'database_name', | |
'DB_USER' => 'database_user', | |
'DB_PASS' => 'database_password', | |
); | |
if (!isset($_SERVER['DB_HOST'])) { | |
$dbConfig = $configPath . '/database.yml'; | |
engineyard_parse_db($dbConfig, $container); | |
} else { | |
engineyard_parse_server($eyParams, $container); | |
} | |
$envCustom = $configPath . '/env.custom'; | |
engineyard_parse_env($envCustom, $container); | |
// ---------------------------------------------------------- | |
function engineyard_parse_server($params, $container) | |
{ | |
foreach ($params as $key => $value) { | |
if (isset($_SERVER[$key])) { | |
$container->setParameter($value, $_SERVER[$key]); | |
} | |
} | |
} | |
function engineyard_parse_env($filename, $container) | |
{ | |
$params = parse_ini_file($filename); | |
if (false === $params) { | |
engineyard_bad_config($filename); | |
} | |
foreach ($params['env'] as $key => $value) { | |
$container->setParameter(strtolower($key), $value); | |
} | |
} | |
function engineyard_parse_db($filename, $container) | |
{ | |
$yaml = new Symfony\Component\Yaml\Yaml(); | |
$contents = file_get_contents($filename); | |
if (false === $contents) { | |
engineyard_bad_config($filename); | |
} | |
try { | |
$params = $yaml->parse($contents); | |
} catch (\Exception $badYaml) { | |
engineyard_bad_config($filename); | |
} | |
$env = $container->getParameter('kernel.environment'); | |
if (!array_key_exists($env, $params)) { | |
return; | |
} | |
$data = $params[$env]; | |
$names = array( | |
'database' => 'database_name', | |
'username' => 'database_user', | |
'password' => 'database_password', | |
'host' => 'database_host', | |
); | |
foreach ($names as $key => $param) { | |
if (array_key_exists($key, $data)) { | |
$container->setParameter($param, $data[$key]); | |
} | |
} | |
} | |
function engineyard_bad_config($filename) | |
{ | |
throw new \RuntimeException('Could not parse config ' . $filename); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment