API Remove Director.environment_type config and replace with static methods

This commit is contained in:
Damian Mooyman 2017-02-22 16:13:42 +13:00
parent a6e9a7111b
commit 3958788858

View File

@ -81,14 +81,14 @@ class Director implements TemplateGlobalProvider
* *
* @var array * @var array
*/ */
private static $dev_servers = array(); protected static $dev_servers = array();
/** /**
* @config * @config
* *
* @var array * @var array
*/ */
private static $test_servers = array(); protected static $test_servers = array();
/** /**
* Setting this explicitly specifies the protocol ("http" or "https") used, overriding the normal * Setting this explicitly specifies the protocol ("http" or "https") used, overriding the normal
@ -101,13 +101,6 @@ class Director implements TemplateGlobalProvider
*/ */
private static $alternate_protocol; private static $alternate_protocol;
/**
* @config
*
* @var string
*/
private static $alternate_host;
/** /**
* @config * @config
* *
@ -120,7 +113,7 @@ class Director implements TemplateGlobalProvider
* *
* @var string * @var string
*/ */
private static $environment_type; protected static $environment_type;
/** /**
* Process the given URL, creating the appropriate controller and executing it. * Process the given URL, creating the appropriate controller and executing it.
@ -429,7 +422,7 @@ class Director implements TemplateGlobalProvider
*/ */
protected static function handleRequest(HTTPRequest $request, Session $session, DataModel $model) protected static function handleRequest(HTTPRequest $request, Session $session, DataModel $model)
{ {
$rules = Director::config()->get('rules'); $rules = Director::config()->uninherited('rules');
if (isset($_REQUEST['debug'])) { if (isset($_REQUEST['debug'])) {
Debug::show($rules); Debug::show($rules);
@ -598,11 +591,11 @@ class Director implements TemplateGlobalProvider
} }
} }
if ($host = static::config()->get('alternate_host')) { if ($host = Director::config()->uninherited('alternate_host')) {
return $host; return $host;
} }
if ($baseURL = static::config()->get('alternate_base_url')) { if ($baseURL = Director::config()->uninherited('alternate_base_url')) {
if (preg_match('/^(http[^:]*:\/\/[^\/]+)(\/|$)/', $baseURL, $matches)) { if (preg_match('/^(http[^:]*:\/\/[^\/]+)(\/|$)/', $baseURL, $matches)) {
return parse_url($baseURL, PHP_URL_HOST); return parse_url($baseURL, PHP_URL_HOST);
} }
@ -711,20 +704,6 @@ class Director implements TemplateGlobalProvider
} }
} }
/**
* Sets the root URL for the website. If the site isn't accessible from the URL you provide,
* weird things will happen.
*
* @deprecated 4.0 Use the "Director.alternate_base_url" config setting instead.
*
* @param string $baseURL
*/
public static function setBaseURL($baseURL)
{
Deprecation::notice('4.0', 'Use the "Director.alternate_base_url" config setting instead');
Config::inst()->update('SilverStripe\\Control\\Director', 'alternate_base_url', $baseURL);
}
/** /**
* Returns the root filesystem folder for the site. It will be automatically calculated unless * Returns the root filesystem folder for the site. It will be automatically calculated unless
* it is overridden with {@link setBaseFolder()}. * it is overridden with {@link setBaseFolder()}.
@ -733,24 +712,10 @@ class Director implements TemplateGlobalProvider
*/ */
public static function baseFolder() public static function baseFolder()
{ {
$alternate = static::config()->get('alternate_base_folder'); $alternate = Director::config()->uninherited('alternate_base_folder');
return ($alternate) ? $alternate : BASE_PATH; return ($alternate) ? $alternate : BASE_PATH;
} }
/**
* Sets the root folder for the website. If the site isn't accessible from the folder you provide,
* weird things will happen.
*
* @deprecated 4.0 Use the "Director.alternate_base_folder" config setting instead.
*
* @param string $baseFolder
*/
public static function setBaseFolder($baseFolder)
{
Deprecation::notice('4.0', 'Use the "Director.alternate_base_folder" config setting instead');
Config::inst()->update('SilverStripe\\Control\\Director', 'alternate_base_folder', $baseFolder);
}
/** /**
* Turns an absolute URL or folder into one that's relative to the root of the site. This is useful * Turns an absolute URL or folder into one that's relative to the root of the site. This is useful
* when turning a URL into a filesystem reference, or vice versa. * when turning a URL into a filesystem reference, or vice versa.
@ -1172,40 +1137,49 @@ class Director implements TemplateGlobalProvider
* Once the environment type is set, it can be checked with {@link Director::isDev()}, * Once the environment type is set, it can be checked with {@link Director::isDev()},
* {@link Director::isTest()}, and {@link Director::isLive()}. * {@link Director::isTest()}, and {@link Director::isLive()}.
* *
* @deprecated 4.0 Use the "Director.environment_type" config setting instead * @param string $environment
*
* @param $et string
*/ */
public static function set_environment_type($et) public static function set_environment_type($environment)
{ {
if ($et != 'dev' && $et != 'test' && $et != 'live') { if (!in_array($environment, ['dev', 'test', 'live'])) {
user_error( throw new \InvalidArgumentException(
"Director::set_environment_type passed '$et'. It should be passed dev, test, or live", "Director::set_environment_type passed '$environment'. It should be passed dev, test, or live"
E_USER_WARNING
); );
} else {
Deprecation::notice('4.0', 'Use the "Director.environment_type" config setting instead');
Config::inst()->update('SilverStripe\\Control\\Director', 'environment_type', $et);
} }
self::$environment_type = $environment;
} }
/** /**
* Can also be checked with {@link Director::isDev()}, {@link Director::isTest()}, and * Can also be checked with {@link Director::isDev()}, {@link Director::isTest()}, and
* {@link Director::isLive()}. * {@link Director::isLive()}.
* *
* @return bool|string * @return bool
*/ */
public static function get_environment_type() public static function get_environment_type()
{ {
if (Director::isLive()) { // Check saved session
return 'live'; if ($env = self::session_environment()) {
} elseif (Director::isTest()) { return $env;
return 'test';
} elseif (Director::isDev()) {
return 'dev';
} else {
return false;
} }
// Check set
if (self::$environment_type) {
return self::$environment_type;
}
// Check getenv
if ($env = getenv('SS_ENVIRONMENT_TYPE')) {
return $env;
}
// Check if we are running on one of the test servers
if (in_array(static::host(), self::$dev_servers)) {
return 'dev';
}
if (in_array(static::host(), self::$test_servers)) {
return 'test';
}
return 'live';
} }
/** /**
@ -1216,7 +1190,7 @@ class Director implements TemplateGlobalProvider
*/ */
public static function isLive() public static function isLive()
{ {
return !(Director::isDev() || Director::isTest()); return self::get_environment_type() === 'live';
} }
/** /**
@ -1227,19 +1201,7 @@ class Director implements TemplateGlobalProvider
*/ */
public static function isDev() public static function isDev()
{ {
// Check session return self::get_environment_type() === 'dev';
if ($env = self::session_environment()) {
return $env === 'dev';
}
// Check config
if (Config::inst()->get('SilverStripe\\Control\\Director', 'environment_type') === 'dev') {
return true;
}
// Check if we are running on one of the test servers
$devServers = (array)Config::inst()->get('SilverStripe\\Control\\Director', 'dev_servers');
return in_array(static::host(), $devServers);
} }
/** /**
@ -1250,24 +1212,7 @@ class Director implements TemplateGlobalProvider
*/ */
public static function isTest() public static function isTest()
{ {
// In case of isDev and isTest both being set, dev has higher priority return self::get_environment_type() === 'test';
if (self::isDev()) {
return false;
}
// Check saved session
if ($env = self::session_environment()) {
return $env === 'test';
}
// Check config
if (Config::inst()->get('SilverStripe\\Control\\Director', 'environment_type') === 'test') {
return true;
}
// Check if we are running on one of the test servers
$testServers = (array)Config::inst()->get('SilverStripe\\Control\\Director', 'test_servers');
return in_array(static::host(), $testServers);
} }
/** /**
@ -1275,7 +1220,7 @@ class Director implements TemplateGlobalProvider
* *
* @return null|string * @return null|string
*/ */
protected static function session_environment() public static function session_environment()
{ {
// Set session from querystring // Set session from querystring
if (isset($_GET['isDev'])) { if (isset($_GET['isDev'])) {