Skip to content

Instantly share code, notes, and snippets.

@eristoddle
Created March 13, 2015 16:45
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 eristoddle/94f72b5738d443a4ab1b to your computer and use it in GitHub Desktop.
Save eristoddle/94f72b5738d443a4ab1b to your computer and use it in GitHub Desktop.
Phalcon PHP Framework environment switching
<?php
//...
protected function _setEnvironment($config)
{
$env = new \stdClass();
if (array_key_exists('ENV', $_SERVER) && isset($_SERVER['ENV'])) {
$env->name = $_SERVER['ENV'];
} else {
$env->host = $_SERVER['HTTP_HOST'];
foreach ($config['domains'] as $k => $v) {
if (property_exists($v, $env->host)) {
$env->name = $k;
}
}
}
if (!property_exists($env, 'name')) {
$env->name = 'local';
}
$this->di->set('env', $env);
}
protected function _registerServices()
{
//...
$config = include __DIR__ . '/../../../config/config.php';
$this->_setEnvironment($config);
$envConfig = include __DIR__ . '/../../../config/' . $this->di->get('env')->name . '/config.php';
$config->merge($envConfig);
$this->di->set('config', $config);
//...
}
<?php
/**
* Simple application configuration
*/
return new \Phalcon\Config([
'databases' => [
'default' => 'master',
'master' => [
'adapter' => 'Mysql',
'host' => 'localhost',
'username' => 'root',
'password' => 'root',
'dbname' => 'phalcon',
'persistent' => true,
'charset' => 'utf8'
],
'postgres' => [
'adapter' => 'Postgresql',
'host' => 'localhost',
'username' => 'root',
'password' => 'root',
'dbname' => 'phalcon',
'persistent' => true,
'charset' => 'utf8'
],
],
'domains' => [
'local' => [
'phalcon.local',
'phalcon.local:8000',
'localhost'
],
'dev' => [
'api.dev'
],
'qa' => [
'api.qa'
],
'uat' => [
'api.uat'
],
'prod' => [
'realsite.com'
]
],
'application' => [
'baseUri' => '/',
'annotations' => ['adapter' => 'Memory'],
'models' => [
'metadata' => ['adapter' => 'Memory']
]
]
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment