function array_merge_recursive_replace($paArray1, $paArray2) { if (!is_array($paArray1) || !is_array($paArray2)) { return $paArray2; } foreach ($paArray2 as $sKey2 => $sValue2) { $paArray1[$sKey2] = array_merge_recursive_replace(@$paArray1[$sKey2], $sValue2); } return $paArray1; }
/** * logic used to auto load the correct config based on environment * @param array $arrays ... List of arrays to merge * @return array */ function array_merge_recursive_replace() { $arrays = func_get_args(); $base = array_shift($arrays); foreach ($arrays as $array) { reset($base); while (list($key, $value) = @each($array)) { if (is_array($value) && isset($base[$key]) && @is_array($base[$key])) { $base[$key] = array_merge_recursive_replace($base[$key], $value); } else { $base[$key] = $value; } } } return $base; }
<?php /** * load low level helper here so it also works when used in conjunction with phalcon devtools */ require_once API_PATH . 'bin/base.php'; // your main application config file $config = ['application' => ['appDir' => APPLICATION_PATH, "controllersDir" => APPLICATION_PATH . 'controllers/', "modelsDir" => APPLICATION_PATH . 'models/', "entitiesDir" => APPLICATION_PATH . 'entities/', "responsesDir" => APPLICATION_PATH . 'responses/', "librariesDir" => APPLICATION_PATH . 'libraries/', "exceptionsDir" => APPLICATION_PATH . 'exceptions/', 'baseUri' => '/', 'basePath' => '/', 'debugApp' => true, 'cacheDir' => '/tmp/', 'tempDir' => '/tmp/', 'loggingDir' => '/tmp/'], 'namespaces' => ['models' => 'PhalconRest\\Models\\', 'controllers' => 'PhalconRest\\Controllers\\', 'libraries' => 'PhalconRest\\Libraries\\', 'entities' => 'PhalconRest\\Entities\\'], 'security' => true, 'feature_flags' => ['fastBelongsTo' => false, 'fastHasMany' => false]]; // incorporate the correct environmental config file // TODO throw error if no file is found? $overridePath = APPLICATION_PATH . 'config/config.php'; if (file_exists($overridePath)) { $config = array_merge_recursive_replace($config, require $overridePath); } else { throw new HTTPException("Fatal Exception Caught.", 500, array('dev' => "Invalid Environmental Config! Could not load the specific config file. Your environment is: " . APPLICATION_ENV . " but not matching file was found in /app/config/", 'code' => '23897293759275')); } return $config;
<?php // define if it isn't already in palce // need for CLI scenarios defined('APPLICATION_PATH') || define('APPLICATION_PATH', __DIR__ . '/../'); /** * load low level helper here so it also works when used in conjunction with phalcon devtools */ require_once APPLICATION_PATH . 'helpers/base.php'; require_once APPLICATION_PATH . 'helpers/file.php'; // your main aplication config file // app/config/config.php $config = ['application' => ['appDir' => APPLICATION_PATH, "controllersDir" => APPLICATION_PATH . '/controllers/', "modelsDir" => APPLICATION_PATH . '/models/', "entitiesDir" => APPLICATION_PATH . '/entities/', "responsesDir" => APPLICATION_PATH . '/responses/', "exceptionsDir" => APPLICATION_PATH . '/exceptions/', "librariesDir" => APPLICATION_PATH . '/libraries/'], 'smtp' => ['fromName' => 'Smith & Carson', 'fromEmail' => '*****@*****.**', 'server' => 'ssl://smtp.googlemail.com', 'port' => 465, 'security' => 'tls', 'username' => '*****@*****.**', 'password' => '7d8ev8f75d)'], 'namespaces' => ['models' => "PhalconRest\\Models\\", 'controllers' => "PhalconRest\\Controllers\\", 'libraries' => "PhalconRest\\Libraries\\", 'entities' => "PhalconRest\\Entities\\"]]; // override production config by enviroment config $override_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . APPLICATION_ENV . '.php'; // log the correct combination of config values $config = file_exists($override_path) ? array_merge_recursive_replace($config, require APPLICATION_ENV . '.php') : $config; // load security rules if they have been defined $security_rules_path = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'security_rules.php'; $config = file_exists($security_rules_path) ? array_merge_recursive_replace($config, require 'security_rules.php') : $config; return new \Phalcon\Config($config);
/** * Recursively merges two arrays, where duplicate items in the first array * are replaced with items in the second array. * * @param array $array1 * @param array $array2 * @return array */ function array_merge_recursive_replace($array1, $array2) { $arrays = func_get_args(); $narrays = count($arrays); $ret = $arrays[0]; for ($i = 1; $i < $narrays; $i++) { foreach ($arrays[$i] as $key => $value) { if (is_array($value) && isset($ret[$key])) { $ret[$key] = array_merge_recursive_replace($ret[$key], $value); } else { $ret[$key] = $value; } } } return $ret; }