dev-master
9999999-devCakePHP Environments Library as a plugin
The Requires
by electblake
by OctoBear
CakePHP Environments Library as a plugin
CakePHP Environments Library as a plugin, (*1)
[Manual], (*2)
app/Plugin
Environments
[GIT Submodule], (*3)
In your app directory type:, (*4)
git submodule add git://github.com/OctoBear/cakephp-environments.git Plugin/Environments git submodule init git submodule update
[GIT Clone], (*5)
In your plugin directory type, (*6)
git clone git://github.com/OctoBear/cakephp-environments.git Environments
Before using, you MUST enable the plugin:, (*7)
CakePlugin::load('Environments');
If you are already using CakePlugin::loadAll();
before usage, then this is not necessary., (*8)
Setup a directory structure as follows:, (*9)
app/Config/bootstrap/environments.php app/Config/bootstrap/environments/production.php app/Config/bootstrap/environments/staging.php app/Config/bootstrap/environments/development.php
We specify multiple environment files to separate the various configurations and make it easy to inspect a given environment. This is not necessary, but useful if you are configuring a large amount of information., (*10)
Your environments.php
should contain the following:, (*11)
<?php CakePlugin::load('Environments'); App::uses('Environment', 'Environments.Lib'); include dirname(__FILE__) . DS . 'environments' . DS . 'production.php'; include dirname(__FILE__) . DS . 'environments' . DS . 'staging.php'; include dirname(__FILE__) . DS . 'environments' . DS . 'development.php'; Environment::start();
This will:, (*12)
Each environment may be configured as follows:, (*13)
Environment::configure($name, $params, $config = null, $callable = null);
Example usage of the Environment::configure()
call is available in the Config/bootstrap/environments
folder of this plugin., (*14)
$name
: Usually something like production
or development
. Good practice is to name it the same as the file it was in, so that there isn't any confusion as to what a given env file contains$params
: An array or boolean of environment variables used to check whether this environment applies. If a boolean and set to true, this environment is automatically initiated. A value of true
should only be used for development:, (*15)
# If a previous environment is not enabled, this one will be set Environment::configure('development', true, array('debug' => true));
If an array, the keys may be function names that are called, with the values being passed to it:, (*16)
Environment::configure('development', true, array( 'some_function' => 'aValueForThisCheck', 'another_function' => 'aDifferentValueForThisCheck', ));
In all other cases, we simply check the environment:, (*17)
Environment::configure('octo_development', true, array( 'SERVER_NAME' => 'octo-example.dev', )); Environment::configure('bear_development', true, array( 'SERVER_NAME' => 'bear-example.dev', )); Environment::configure('development', true, array( 'CAKE_ENV' => 'development', ));
If we are running in CLI, the only check enforced is CAKE_ENV
, and the value MUST be the name of the environment:, (*18)
CAKE_ENV=production Console/cake bake all
If the CAKE_ENV
environment variable is set at all, it takes precedence over everything, including a boolean value for $params
., (*19)
$config
: If set, an array of keys
=> values
that are set via Configure::write()
. Useful for batch-setting api keys, database connection information, etc.$callable
: A PHP callable, such as array('ClassName', 'functionName')
. In PHP53+, it is also possible to use an anonymous function. This is the preferred method of setting information that might depend upon the value of some function call.You may wish to set database connection information based upon the current environment. The following is the preferred method:, (*20)
<?php class DATABASE_CONFIG { public $default; public function __construct() { $this->default = array( 'datasource' => 'Database/Mysql', 'persistent' => false, 'host' => $this->read('MYSQL_DB_HOST'), 'login' => $this->read('MYSQL_USERNAME'), 'password' => $this->read('MYSQL_PASSWORD'), 'database' => $this->read('MYSQL_DB_NAME'), 'prefix' => $this->read('MYSQL_PREFIX'), 'encoding' => 'utf8', ); } public function read($key, $default = null) { $value = env($key); if ($value !== null) { return $value; } $value = Configure::read($key); if ($value !== null) { return $value; } return $default; } }
This way, the only place necessary to view connection information is in your configured environment. It is also possible to set the connection information via a CLI
wrapper, nginx
or apache
configuration, or even in your cloud provider's env settings, such as Heroku or PHPFog., (*21)
Copyright (c) 2010-2012 Jose Diaz-Gonzalez, (*22)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:, (*23)
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software., (*24)
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE., (*25)
CakePHP Environments Library as a plugin