Created
March 13, 2015 16:45
-
-
Save eristoddle/94f72b5738d443a4ab1b to your computer and use it in GitHub Desktop.
Phalcon PHP Framework environment switching
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 | |
//... | |
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); | |
//... | |
} |
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 | |
/** | |
* 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