Created
December 29, 2015 01:47
-
-
Save pcahard/f4602f39a69b5c9a9bc1 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/php | |
<?php | |
/** | |
* create a bin directory in your symfony application | |
* save this script into this bin directory "config_parameters | |
* create a token file like prod.ini | |
* add your token like this: __MYSQL_DB__="foobar" | |
* rename your parameters.yml to parameters.yml.dist | |
* replace your values with tokens like this | |
* database_name= __MYSQL_DB__ | |
* save your token files into app/config/ini/ directory | |
* chmod +x ./bin/config_parameters | |
* run the command: | |
* ./bin/configure_parameters prod.ini | |
* @author gazari | |
*/ | |
class ConfigureApplication | |
{ | |
const | |
APP_INI_DIR = "app/config/ini", | |
APP_CONFIG_DIR = "app/config", | |
DIST_EXT = ".dist"; | |
protected | |
$args = array(), | |
$tokens = NULL; | |
public function __construct($arguments, $argumentsCount) | |
{ | |
$this->args = $arguments; | |
if (1 == $argumentsCount) { | |
$this->help(); | |
exit -1; | |
} | |
$this->doReplace(); | |
} | |
public function help() { | |
printf("%s your/configuration/file.ini\n", $this->args[0]); | |
} | |
public function doReplace() | |
{ | |
$tokens = $this->getTokens(); | |
foreach ($this->getDistfiles() as $file) { | |
$content = file_get_contents($file); | |
foreach ($tokens as $key => $value) { | |
$content = str_replace($key, $value, $content); | |
} | |
$configFile = str_replace(self::DIST_EXT, '', $file); | |
printf("writing : %s\n", $configFile); | |
file_put_contents($configFile, $content); | |
} | |
} | |
protected function getDistfiles() | |
{ | |
$distFiles = array(); | |
$expr = sprintf("/%s$/", self::DIST_EXT); | |
$iterator = new RecursiveDirectoryIterator( | |
realpath(__DIR__."/../".self::APP_CONFIG_DIR), | |
RecursiveDirectoryIterator::SKIP_DOTS | |
); | |
foreach ($iterator as $file) { | |
if (preg_match($expr, $file->getPathName()) && is_file($file->getPathName())) { | |
$distFiles[] = $file->getPathName(); | |
} | |
} | |
return $distFiles; | |
} | |
protected function getTokens() | |
{ | |
if (!$this->tokens) { | |
$this->tokens = $this->prepareTokens(parse_ini_file($this->getInitFile())); | |
} | |
return $this->tokens; | |
} | |
protected function getInitFile() | |
{ | |
$iniArgs = $this->args[1]; | |
if (is_file($iniArgs)) { | |
return $iniArgs; | |
} else { | |
if (!preg_match('/.ini$/', $iniArgs)) { | |
$iniArgs = sprintf('%s.ini', $iniArgs); | |
} | |
$expr = sprintf('/\/%s$/i', $iniArgs); | |
$iterator = new RecursiveIteratorIterator( | |
new RecursiveDirectoryIterator(realpath(__DIR__."/../".self::APP_INI_DIR)), | |
RecursiveIteratorIterator::SELF_FIRST & RecursiveDirectoryIterator::SKIP_DOTS | |
); | |
foreach ($iterator as $file) | |
{ | |
if (preg_match($expr, $file->getPathName()) && is_file($file->getPathName())) { | |
return $file->getPathName(); | |
} | |
} | |
} | |
throw new \Exception(sprintf("can't find ini file %s in %s", $iniArgs, self::APP_INI_DIR)); | |
} | |
protected function prepareTokens($tokens) | |
{ | |
$response = array(); | |
foreach ($tokens as $key => $value) { | |
if (preg_match('/^PHP:/', $value)) { | |
$value = str_replace('PHP:', '', $value); | |
eval(sprintf('$value = %s;', $value)); | |
} | |
$response[$key] = $value; | |
} | |
return $response; | |
} | |
} | |
$config = new ConfigureApplication($argv, $argc); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment