Skip to content

Instantly share code, notes, and snippets.

@selvinortiz
Last active December 18, 2015 18:50
Show Gist options
  • Save selvinortiz/5828875 to your computer and use it in GitHub Desktop.
Save selvinortiz/5828875 to your computer and use it in GitHub Desktop.
CRAFT:Env
<?php
/**
* @=ENV
*
* @author Selvin Ortiz (http://twitter.com/selvinortiz)
*
* 1. Define the craft path relative to this file
* 2. Define the domain name this site is running under
* 3. Define the ip address that requested this resource
* 4. Define the environment we are working under
*
* @example
* The environment will be determined based on the following conditions
*
* (local)
* The ip address may be 127.0.0.1
* The ip address may be 127.0.0.2
* The domain name may contain the "local" trigger word as in "site.local" or "site.local.dev"
*
* (dev)
* The domain name may contain the "dev" trigger word as in "dev.site.com"
*
* (stage)
* The domain name may contain the "stage" trigger word as in "stage.site.com"
*
* (live)
* When all other conditions fail
*/
define('__CRAFT', realpath(__DIR__.'/../craft') );
define('__DOMAIN', $_SERVER['SERVER_NAME']); // Using instead of HTTP_HOST which is defined by the client header!
define('__IPADDRESS', $_SERVER['REMOTE_ADDR']);
// Sets up the local environment
// The local development environment is special and I treat it as such
switch ( __IPADDRESS )
{
case '127.0.0.1': // localhost
case '127.0.0.2': // vhost
define('__ENV', 'local');
break;
default:
if ( stripos(__DOMAIN, 'local') !== false )
{
define('__ENV', 'local');
}
break;
}
// Checks whether the local environment has been set
if ( !defined('__ENV') )
{
if ( stripos(__DOMAIN, 'dev') !== false )
{
define('__ENV', 'dev');
}
elseif ( stripos(__DOMAIN, 'stage') !== false )
{
define('__ENV', 'stage');
}
else
{
define('__ENV', 'live');
}
}
//--------------------------------------------------------------------------------
// @=CRAFT
//--------------------------------------------------------------------------------
$path = __CRAFT.'/app/index.php';
if ( ! is_file($path) )
{
exit('Your craft path is not set up correctly <var>'.__FILE__.'</var>');
}
require_once $path;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment