Last active
January 2, 2016 19:28
-
-
Save motin/8350122 to your computer and use it in GitHub Desktop.
Example environment bootstrap files for any php project
This file contains hidden or 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 | |
/** | |
* Include this file as follows: | |
* | |
* $envbootstrap = dirname(__FILE__) . '/../../../common/settings/envbootstrap.php'; | |
* if (!is_readable($envbootstrap)) { | |
* echo "Main envbootstrap file not available."; | |
* die(2); | |
* } | |
* require_once($envbootstrap); | |
*/ | |
namespace neam\envbootstrap; | |
// Useful for getting new environments configured (use temporarily while setting up new servers) | |
if (false) { | |
var_dump($_SERVER, $_ENV);die(); | |
} | |
if (false) { | |
ini_set("display_errors", true); | |
error_reporting(E_ALL); | |
} | |
// Abstract helper class | |
require('inc/neam/envbootstrap/Running.php'); | |
// Application-specific helper classes | |
require('running.php'); | |
require('identity.php'); | |
require('environment.php'); | |
// Initial environment bootstrap | |
Environment::bootstrap(); | |
// Add secret constants from non-versioned secrets-file | |
require('secrets.php'); | |
// Defines infrastructure = all external services, usernames, api:s, servers, ports etc depending on environment | |
require('infrastructure.php'); |
This file contains hidden or 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 | |
namespace neam\envbootstrap; | |
class Environment | |
{ | |
static public function bootstrap() | |
{ | |
// Debug | |
//var_dump(Running::inPath(), Running::usingDomainName());var_dump(Identity::brand(), Identity::dataset());die(); | |
if (Running::asCli()) { | |
define('PUBLIC_PATH', 'http://cli/'); | |
define('CDN_PATH', PUBLIC_PATH); | |
} else { | |
// Enabled pretty html errors when not in cli | |
ini_set('html_errors', true); | |
$ssl = !empty($_SERVER['HTTPS']) || isset($_SERVER['HTTP_X_SSL_REQUEST']); | |
define('PUBLIC_PATH', ($ssl ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/'); | |
} | |
// General error reporting level | |
error_reporting(E_ALL); | |
if (Running::onWebserver() == "a-webserver-reference") { | |
define('ENV', 'a-webserver-reference:' . Identity::dataset()->database); | |
define('DEV', false); // change this to false in production | |
define('DEBUG_REDIRECTS', false); | |
define('DEBUG_LOGS', false); | |
//define('PHP_ERROR_LOG_PATH', ini_get("error_log")); | |
// Service constants | |
define('FB_APP_ID', ''); | |
define('GA_TRACKING_ID', 'UA-XXXXXXXX-1'); // Ratataa internal google analytics tracking | |
define('CDN_PATH', PUBLIC_PATH); | |
} elseif (Running::onWebserver() == "another-webserver-reference") { | |
define('ENV', 'another-webserver-reference:' . Identity::dataset()->database); | |
define('DEV', false); // change this to false in production | |
define('DEBUG_REDIRECTS', false); | |
define('DEBUG_LOGS', false); | |
//define('PHP_ERROR_LOG_PATH', ini_get("error_log")); | |
// Service constants | |
define('FB_APP_ID', ''); | |
define('GA_TRACKING_ID', 'UA-XXXXXXXX-1'); // Ratataa internal google analytics tracking | |
define('CDN_PATH', PUBLIC_PATH); | |
} else { | |
throw new Exception('Currently unconfigured webserver: ' . Running::onWebserver()); | |
} | |
if (DEV) { | |
if (!defined('YII_DEBUG')) define('YII_DEBUG', true); | |
ini_set("display_errors", true); | |
} else { | |
if (!defined('YII_DEBUG')) define('YII_DEBUG', false); | |
ini_set("display_errors", false); | |
} | |
// todo refactor | |
define('CONTACT_EMAIL', \adoveo\envbootstrap\Identity::brand()->supportEmail); | |
$GLOBALS['env_config']['mail-sender-email'] = \adoveo\envbootstrap\Identity::brand()->mailSentByMail; | |
$GLOBALS['env_config']['mail-sender-name'] = \adoveo\envbootstrap\Identity::brand()->mailSentByName; | |
} | |
} |
This file contains hidden or 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 | |
namespace neam\envbootstrap; | |
class Identity | |
{ | |
static public function brand() | |
{ | |
if (strpos(Running::usingDomainName(), "hostname") !== false) { | |
$return = new \stdClass(); | |
$return->siteName = "Site name"; | |
$return->domain = "hostname.com"; | |
$return->theme = "thetheme"; | |
$return->language = "sv"; | |
$return->supportEmail = "info@" . $return->domain; | |
$return->mailSentByMail = "noreply@" . $return->domain; | |
$return->mailSentByName = $return->siteName; | |
} else if (strpos(Running::usingDomainName(), "anotherhostname") !== false) { | |
$return = new \stdClass(); | |
$return->siteName = "Another site name"; | |
$return->domain = "anotherhostname.com"; | |
$return->theme = "theothertheme"; | |
$return->language = "sv"; | |
$return->supportEmail = "info@" . $return->domain; | |
$return->mailSentByMail = "noreply@" . $return->domain; | |
$return->mailSentByName = $return->siteName; | |
} else { | |
throw new \Exception("Can't detect brand. Running::usingDomainName(): " . Running::usingDomainName()); | |
} | |
/* | |
if (Running::onWebserver() !== "rackspace-r1") { | |
$return->mailSentByName = "[" . Running::onWebserver() . "] " . $return->mailSentByName; | |
} | |
*/ | |
return $return; | |
} | |
static public function dataset() | |
{ | |
if (strpos(Running::usingDomainName(), "hostname") !== false) { | |
$return = new \stdClass(); | |
$return->database = "adbname"; | |
$return->uploadfolder = "foobar"; | |
return $return; | |
} else if (strpos(Running::usingDomainName(), "anotherhostname") !== false) { | |
$return = new \stdClass(); | |
$return->database = "anotherdbnamemaybe"; | |
$return->uploadfolder = "foobar"; | |
return $return; | |
} else { | |
throw new \Exception("Can't detect dataset. Running::usingDomainName(): " . Running::usingDomainName()); | |
} | |
} | |
} |
This file contains hidden or 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 | |
namespace neam\envbootstrap; | |
abstract class Running | |
{ | |
static public function asUser() | |
{ | |
$return = $_ENV["user"]; | |
return self::mustNotBeEmpty($return); | |
} | |
static public function onHostname() | |
{ | |
$return = php_uname("n"); | |
return self::mustNotBeEmpty($return); | |
} | |
static public function asCli() | |
{ | |
return php_sapi_name() == 'cli'; | |
} | |
static public function inPath() | |
{ | |
if (self::asCli()) { | |
$return = $_SERVER['PWD']; | |
} else { | |
$return = $_SERVER['DOCUMENT_ROOT']; | |
} | |
return self::mustNotBeEmpty($return); | |
} | |
static public function inRealPath() | |
{ | |
if (self::asCli()) { | |
$return = realpath($_SERVER['PWD']); | |
} else { | |
$return = realpath($_SERVER['DOCUMENT_ROOT']); | |
} | |
return self::mustNotBeEmpty($return); | |
} | |
static public function usingDomainName() | |
{ | |
if (self::asCli()) { | |
$http_host = getenv('HTTP_HOST'); | |
if (empty($http_host)) { | |
throw new \CException("Environment variable HTTP_HOST needs to be set"); | |
} | |
$return = $http_host; | |
} else { | |
$return = $_SERVER['HTTP_HOST']; | |
} | |
return self::mustNotBeEmpty($return); | |
} | |
static private function mustNotBeEmpty($val) | |
{ | |
if (empty($val)) { | |
throw new \Exception("Must have non-empty property"); | |
} | |
return $val; | |
} | |
static public function onWebserver() | |
{ | |
} | |
} |
This file contains hidden or 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 | |
/** | |
* Hardware/software/webservice/server resources | |
*/ | |
switch (ENV) { | |
case 'a-webserver-reference:adbname': | |
$GLOBALS['env_config']['components-mail'] = array( | |
'transportType' => 'smtp', | |
'transportOptions' => array( | |
'host' => 'smtp.example.com', | |
'username' => SMTP_AUTH_USERNAME, | |
'password' => SMTP_AUTH_PASSWORD, | |
'port' => 587, | |
'timeout' => 2, | |
'encryption' => 'tls' | |
), | |
'logging' => true, | |
'dryRun' => false | |
); | |
break; | |
case 'another-webserver-reference:anotherdbnamemaybe': | |
$GLOBALS['env_config']['components-mail'] = array( | |
'transportType' => 'php', | |
'logging' => true, | |
'dryRun' => true | |
); | |
break; | |
default: | |
throw new Exception("ENV environment variable not recognized: '" . ENV . "'"); | |
} |
This file contains hidden or 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 | |
namespace neam\envbootstrap; | |
class Running extends \neam\envbootstrap\Running | |
{ | |
static public function onWebserver() | |
{ | |
$production_level = null; | |
if ( | |
strpos(Running::inPath(), '/var/www') !== false && Running::onHostname() == 'ahostname' | |
) { | |
return "a-webserver-reference"; | |
} elseif ( | |
strpos(Running::inPath(), '/opt/web/some/docroot/www') !== false && Running::onHostname() == 'anotherhostname' | |
) { | |
return "another-webserver-reference"; | |
} else { | |
throw new \Exception("Can't detect production level. Revealing path: " . Running::inPath() . " Revealing real path: " . Running::inRealPath()); | |
} | |
} | |
} | |
This file contains hidden or 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 | |
define('SMTP_AUTH_USERNAME', '[email protected]'); | |
define('SMTP_AUTH_PASSWORD', '...'); | |
define('YII_GII_PASSWORD', '...'); | |
switch (ENV) { | |
case 'envrefhere': | |
define('YII_DB_NAME', 'foo'); | |
define('YII_DB_USER', 'bar'); | |
define('YII_DB_PASSWORD', 'zoo'); | |
define('YII_DB_HOST', '127.0.0.1'); | |
define('YII_DB_PORT', '3306'); | |
define('FB_APP_SECRET', '...'); | |
break; | |
default: | |
throw new Exception("[secrets] ENV environment variable not recognized: '" . ENV . "'"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment