2010-10-15 05:54:31 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
2012-03-24 04:38:57 +01:00
|
|
|
* This file is the Framework bootstrap. It will get your environment ready to call Director::direct().
|
2008-02-25 03:10:37 +01:00
|
|
|
*
|
2008-09-30 01:41:50 +02:00
|
|
|
* It takes care of:
|
2013-07-22 03:52:00 +02:00
|
|
|
* - Including Constants.php to include _ss_environment and initialise necessary constants
|
2008-09-30 01:41:50 +02:00
|
|
|
* - Checking of PHP memory limit
|
|
|
|
* - Including all the files needed to get the manifest built
|
|
|
|
* - Building and including the manifest
|
2013-07-22 03:52:00 +02:00
|
|
|
*
|
2008-09-30 01:41:50 +02:00
|
|
|
* @todo This file currently contains a lot of bits and pieces, and its various responsibilities should probably be
|
2012-09-26 23:34:00 +02:00
|
|
|
* moved into different subsystems.
|
2013-07-22 03:52:00 +02:00
|
|
|
* @todo A lot of this stuff is very order-dependent. This could be decoupled.
|
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage core
|
|
|
|
*/
|
|
|
|
|
2008-09-30 01:41:50 +02:00
|
|
|
/**
|
2013-07-22 03:52:00 +02:00
|
|
|
* All errors are reported, including E_STRICT by default *unless* the site is in
|
|
|
|
* live mode, where reporting is limited to fatal errors and warnings (see later in this file)
|
2008-09-30 01:41:50 +02:00
|
|
|
*/
|
2013-07-22 03:52:00 +02:00
|
|
|
error_reporting(E_ALL | E_STRICT);
|
2012-09-19 06:10:33 +02:00
|
|
|
|
2008-09-29 01:03:28 +02:00
|
|
|
/**
|
2013-07-22 03:52:00 +02:00
|
|
|
* Include Constants (if it hasn't already been included) to pull in BASE_PATH, etc
|
2008-09-29 01:03:28 +02:00
|
|
|
*/
|
2013-07-22 03:52:00 +02:00
|
|
|
require_once dirname(__FILE__).'/Constants.php';
|
2008-09-29 01:03:28 +02:00
|
|
|
|
2015-06-11 22:52:41 +02:00
|
|
|
global $_increase_time_limit_max;
|
|
|
|
$_increase_time_limit_max = -1;
|
|
|
|
|
2008-09-29 14:33:07 +02:00
|
|
|
/**
|
|
|
|
* Priorities definition. These constants are used in calls to _t() as an optional argument
|
|
|
|
*/
|
|
|
|
define('PR_HIGH',100);
|
|
|
|
define('PR_MEDIUM',50);
|
|
|
|
define('PR_LOW',10);
|
|
|
|
|
2008-09-30 01:41:50 +02:00
|
|
|
/**
|
|
|
|
* Ensure we have enough memory
|
|
|
|
*/
|
2009-06-17 13:36:49 +02:00
|
|
|
increase_memory_limit_to('64M');
|
2008-09-30 01:41:50 +02:00
|
|
|
|
2012-02-19 21:18:14 +01:00
|
|
|
/**
|
|
|
|
* Ensure we don't run into xdebug's fairly conservative infinite recursion protection limit
|
|
|
|
*/
|
|
|
|
increase_xdebug_nesting_level_to(200);
|
|
|
|
|
2011-12-04 13:21:15 +01:00
|
|
|
/**
|
|
|
|
* Set default encoding
|
|
|
|
*/
|
2012-05-09 10:23:20 +02:00
|
|
|
mb_http_output('UTF-8');
|
|
|
|
mb_internal_encoding('UTF-8');
|
|
|
|
mb_regex_encoding('UTF-8');
|
2011-12-04 13:21:15 +01:00
|
|
|
|
2012-06-11 12:38:59 +02:00
|
|
|
/**
|
|
|
|
* Enable better garbage collection
|
|
|
|
*/
|
|
|
|
gc_enable();
|
|
|
|
|
2011-03-18 07:28:11 +01:00
|
|
|
// Include the files needed the initial manifest building, as well as any files
|
|
|
|
// that are needed for the boostrap process on every request.
|
|
|
|
require_once 'cache/Cache.php';
|
|
|
|
require_once 'core/Object.php';
|
|
|
|
require_once 'core/ClassInfo.php';
|
2013-06-18 22:59:34 +02:00
|
|
|
require_once 'core/DAG.php';
|
2013-03-20 14:45:43 +01:00
|
|
|
require_once 'core/Config.php';
|
2012-02-11 03:08:39 +01:00
|
|
|
require_once 'view/TemplateGlobalProvider.php';
|
2011-03-30 22:56:21 +02:00
|
|
|
require_once 'control/Director.php';
|
2011-03-18 07:28:11 +01:00
|
|
|
require_once 'dev/Debug.php';
|
2012-04-13 03:23:26 +02:00
|
|
|
require_once 'dev/DebugView.php';
|
2015-07-24 00:53:41 +02:00
|
|
|
require_once 'dev/CliDebugView.php';
|
2012-04-13 03:23:26 +02:00
|
|
|
require_once 'dev/Backtrace.php';
|
2011-03-18 07:28:11 +01:00
|
|
|
require_once 'filesystem/FileFinder.php';
|
2013-03-13 22:33:29 +01:00
|
|
|
require_once 'core/manifest/ManifestCache.php';
|
2011-03-30 22:56:21 +02:00
|
|
|
require_once 'core/manifest/ClassLoader.php';
|
2013-03-14 11:38:02 +01:00
|
|
|
require_once 'core/manifest/ConfigManifest.php';
|
|
|
|
require_once 'core/manifest/ConfigStaticManifest.php';
|
2011-03-30 22:56:21 +02:00
|
|
|
require_once 'core/manifest/ClassManifest.php';
|
|
|
|
require_once 'core/manifest/ManifestFileFinder.php';
|
|
|
|
require_once 'core/manifest/TemplateLoader.php';
|
|
|
|
require_once 'core/manifest/TemplateManifest.php';
|
|
|
|
require_once 'core/manifest/TokenisedRegularExpression.php';
|
2012-06-14 12:27:22 +02:00
|
|
|
require_once 'control/injector/Injector.php';
|
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
// Initialise the dependency injector as soon as possible, as it is
|
2012-06-14 12:27:22 +02:00
|
|
|
// subsequently used by some of the following code
|
2013-12-01 12:40:42 +01:00
|
|
|
$injector = new Injector(array('locator' => 'SilverStripeServiceConfigurationLocator'));
|
|
|
|
Injector::set_inst($injector);
|
2010-04-13 01:14:36 +02:00
|
|
|
|
2008-09-30 01:41:50 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// MANIFEST
|
|
|
|
|
2011-03-18 07:28:11 +01:00
|
|
|
// Regenerate the manifest if ?flush is set, or if the database is being built.
|
|
|
|
// The coupling is a hack, but it removes an annoying bug where new classes
|
|
|
|
// referenced in _config.php files can be referenced during the build process.
|
2014-11-25 00:57:42 +01:00
|
|
|
$requestURL = isset($_REQUEST['url']) ? trim($_REQUEST['url'], '/') : false;
|
2015-01-22 11:33:38 +01:00
|
|
|
$flush = (isset($_GET['flush']) || $requestURL === trim(BASE_URL . '/dev/build', '/'));
|
2014-11-25 00:57:42 +01:00
|
|
|
|
2014-02-04 00:26:28 +01:00
|
|
|
global $manifest;
|
2011-03-18 07:28:11 +01:00
|
|
|
$manifest = new SS_ClassManifest(BASE_PATH, false, $flush);
|
|
|
|
|
2012-11-08 17:46:33 +01:00
|
|
|
// Register SilverStripe's class map autoload
|
2011-03-18 07:28:11 +01:00
|
|
|
$loader = SS_ClassLoader::instance();
|
|
|
|
$loader->registerAutoloader();
|
|
|
|
$loader->pushManifest($manifest);
|
2008-09-30 01:41:50 +02:00
|
|
|
|
2012-11-08 17:46:33 +01:00
|
|
|
// Fall back to Composer's autoloader (e.g. for PHPUnit), if composer is used
|
|
|
|
if(file_exists(BASE_PATH . '/vendor/autoload.php')) {
|
|
|
|
require_once BASE_PATH . '/vendor/autoload.php';
|
|
|
|
}
|
|
|
|
|
2014-11-25 00:57:42 +01:00
|
|
|
// Now that the class manifest is up, load the static configuration
|
2013-02-26 04:44:48 +01:00
|
|
|
$configManifest = new SS_ConfigStaticManifest(BASE_PATH, false, $flush);
|
|
|
|
Config::inst()->pushConfigStaticManifest($configManifest);
|
|
|
|
|
2014-11-25 00:57:42 +01:00
|
|
|
// And then the yaml configuration
|
2012-03-09 05:20:20 +01:00
|
|
|
$configManifest = new SS_ConfigManifest(BASE_PATH, false, $flush);
|
2013-02-26 04:44:48 +01:00
|
|
|
Config::inst()->pushConfigYamlManifest($configManifest);
|
2012-03-09 05:20:20 +01:00
|
|
|
|
2014-11-25 00:57:42 +01:00
|
|
|
// Load template manifest
|
2011-03-24 11:30:57 +01:00
|
|
|
SS_TemplateLoader::instance()->pushManifest(new SS_TemplateManifest(
|
2014-11-25 00:57:42 +01:00
|
|
|
BASE_PATH, project(), false, $flush
|
2011-03-24 11:30:57 +01:00
|
|
|
));
|
|
|
|
|
2012-04-12 02:01:15 +02:00
|
|
|
// If in live mode, ensure deprecation, strict and notices are not reported
|
|
|
|
if(Director::isLive()) {
|
|
|
|
error_reporting(E_ALL & ~(E_DEPRECATED | E_STRICT | E_NOTICE));
|
2008-10-08 05:35:28 +02:00
|
|
|
}
|
2012-04-12 02:01:15 +02:00
|
|
|
|
2008-09-30 01:41:50 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// POST-MANIFEST COMMANDS
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load error handlers
|
|
|
|
*/
|
|
|
|
|
2015-07-24 00:53:41 +02:00
|
|
|
$errorHandler = Injector::inst()->get('ErrorHandler');
|
|
|
|
$errorHandler->start();
|
2012-05-09 14:26:29 +02:00
|
|
|
|
2008-09-30 01:41:50 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// HELPER FUNCTIONS
|
|
|
|
|
2009-03-31 21:32:19 +02:00
|
|
|
/**
|
|
|
|
* Creates a class instance by the "singleton" design pattern.
|
|
|
|
* It will always return the same instance for this class,
|
|
|
|
* which can be used for performance reasons and as a simple
|
|
|
|
* way to access instance methods which don't rely on instance
|
|
|
|
* data (e.g. the custom SilverStripe static handling).
|
|
|
|
*
|
|
|
|
* @param string $className
|
|
|
|
* @return Object
|
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
function singleton($className) {
|
2012-06-11 12:40:47 +02:00
|
|
|
if($className == "Config") user_error("Don't pass Config to singleton()", E_USER_ERROR);
|
2007-07-19 12:40:28 +02:00
|
|
|
if(!isset($className)) user_error("singleton() Called without a class", E_USER_ERROR);
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!is_string($className)) user_error("singleton() passed bad class_name: " . var_export($className,true),
|
|
|
|
E_USER_ERROR);
|
2012-06-11 12:40:47 +02:00
|
|
|
return Injector::inst()->get($className);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function project() {
|
|
|
|
global $project;
|
|
|
|
return $project;
|
|
|
|
}
|
|
|
|
|
2007-09-15 02:12:28 +02:00
|
|
|
/**
|
2008-10-17 17:40:59 +02:00
|
|
|
* @see i18n::_t()
|
2007-09-15 02:12:28 +02:00
|
|
|
*/
|
2012-05-01 22:13:50 +02:00
|
|
|
function _t($entity, $string = "", $context = "", $injection = "") {
|
|
|
|
return i18n::_t($entity, $string, $context, $injection);
|
2007-09-15 02:12:28 +02:00
|
|
|
}
|
2007-09-15 02:19:29 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
/**
|
|
|
|
* Increase the memory limit to the given level if it's currently too low.
|
2011-08-30 12:53:06 +02:00
|
|
|
* Only increases up to the maximum defined in {@link set_increase_memory_limit_max()},
|
|
|
|
* and defaults to the 'memory_limit' setting in the PHP configuration.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-06-28 04:36:46 +02:00
|
|
|
* @param A memory limit string, such as "64M". If omitted, unlimited memory will be set.
|
2011-08-30 12:53:06 +02:00
|
|
|
* @return Boolean TRUE indicates a successful change, FALSE a denied change.
|
2009-06-17 13:36:49 +02:00
|
|
|
*/
|
2009-06-28 04:36:46 +02:00
|
|
|
function increase_memory_limit_to($memoryLimit = -1) {
|
|
|
|
$curLimit = ini_get('memory_limit');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-28 04:36:46 +02:00
|
|
|
// Can't go higher than infinite
|
2011-08-30 12:53:06 +02:00
|
|
|
if($curLimit == -1 ) return true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-08-30 12:53:06 +02:00
|
|
|
// Check hard maximums
|
|
|
|
$max = get_increase_memory_limit_max();
|
2012-06-26 02:39:50 +02:00
|
|
|
|
|
|
|
if($max && $max != -1 && trANSLATE_MEMSTRING($memoryLimit) > translate_memstring($max)) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
// Increase the memory limit if it's too low
|
2009-06-28 04:36:46 +02:00
|
|
|
if($memoryLimit == -1 || translate_memstring($memoryLimit) > translate_memstring($curLimit)) {
|
2009-06-17 13:36:49 +02:00
|
|
|
ini_set('memory_limit', $memoryLimit);
|
2012-04-16 02:17:30 +02:00
|
|
|
}
|
2011-08-30 12:53:06 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$_increase_memory_limit_max = ini_get('memory_limit');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the maximum allowed value for {@link increase_memory_limit_to()}.
|
|
|
|
* The same result can also be achieved through 'suhosin.memory_limit'
|
|
|
|
* if PHP is running with the Suhosin system.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-30 12:53:06 +02:00
|
|
|
* @param Memory limit string
|
|
|
|
*/
|
|
|
|
function set_increase_memory_limit_max($memoryLimit) {
|
|
|
|
global $_increase_memory_limit_max;
|
|
|
|
$_increase_memory_limit_max = $memoryLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Memory limit string
|
|
|
|
*/
|
|
|
|
function get_increase_memory_limit_max() {
|
|
|
|
global $_increase_memory_limit_max;
|
|
|
|
return $_increase_memory_limit_max;
|
2009-06-17 13:36:49 +02:00
|
|
|
}
|
|
|
|
|
2012-02-19 21:18:14 +01:00
|
|
|
/**
|
|
|
|
* Increases the XDebug parameter max_nesting_level, which limits how deep recursion can go.
|
|
|
|
* Only does anything if (a) xdebug is installed and (b) the new limit is higher than the existing limit
|
|
|
|
*
|
|
|
|
* @param int $limit - The new limit to increase to
|
|
|
|
*/
|
|
|
|
function increase_xdebug_nesting_level_to($limit) {
|
|
|
|
if (function_exists('xdebug_enable')) {
|
|
|
|
$current = ini_get('xdebug.max_nesting_level');
|
|
|
|
if ((int)$current < $limit) ini_set('xdebug.max_nesting_level', $limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-17 13:36:49 +02:00
|
|
|
/**
|
|
|
|
* Turn a memory string, such as 512M into an actual number of bytes.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-06-17 13:36:49 +02:00
|
|
|
* @param A memory limit string, such as "64M"
|
|
|
|
*/
|
|
|
|
function translate_memstring($memString) {
|
|
|
|
switch(strtolower(substr($memString, -1))) {
|
|
|
|
case "k": return round(substr($memString, 0, -1)*1024);
|
|
|
|
case "m": return round(substr($memString, 0, -1)*1024*1024);
|
|
|
|
case "g": return round(substr($memString, 0, -1)*1024*1024*1024);
|
|
|
|
default: return round($memString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-28 04:36:46 +02:00
|
|
|
/**
|
2011-08-30 12:53:06 +02:00
|
|
|
* Increase the time limit of this script. By default, the time will be unlimited.
|
|
|
|
* Only works if 'safe_mode' is off in the PHP configuration.
|
|
|
|
* Only values up to {@link get_increase_time_limit_max()} are allowed.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-06-28 04:36:46 +02:00
|
|
|
* @param $timeLimit The time limit in seconds. If omitted, no time limit will be set.
|
2011-08-30 12:53:06 +02:00
|
|
|
* @return Boolean TRUE indicates a successful change, FALSE a denied change.
|
2009-06-28 04:36:46 +02:00
|
|
|
*/
|
|
|
|
function increase_time_limit_to($timeLimit = null) {
|
2011-08-30 12:53:06 +02:00
|
|
|
$max = get_increase_time_limit_max();
|
2015-06-11 22:52:41 +02:00
|
|
|
if($max != -1 && $max != null && $timeLimit > $max) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-06-28 04:36:46 +02:00
|
|
|
if(!ini_get('safe_mode')) {
|
|
|
|
if(!$timeLimit) {
|
|
|
|
set_time_limit(0);
|
2011-08-30 12:53:06 +02:00
|
|
|
return true;
|
2009-06-28 04:36:46 +02:00
|
|
|
} else {
|
|
|
|
$currTimeLimit = ini_get('max_execution_time');
|
2011-08-30 12:53:06 +02:00
|
|
|
// Only increase if its smaller
|
2009-06-28 04:36:46 +02:00
|
|
|
if($currTimeLimit && $currTimeLimit < $timeLimit) {
|
|
|
|
set_time_limit($timeLimit);
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2011-08-30 12:53:06 +02:00
|
|
|
return true;
|
2009-06-28 04:36:46 +02:00
|
|
|
}
|
2011-08-30 12:53:06 +02:00
|
|
|
} else {
|
|
|
|
return false;
|
2009-06-28 04:36:46 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-30 12:53:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the maximum allowed value for {@link increase_timeLimit_to()};
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-08-30 12:53:06 +02:00
|
|
|
* @param Int Limit in seconds
|
|
|
|
*/
|
|
|
|
function set_increase_time_limit_max($timeLimit) {
|
|
|
|
global $_increase_time_limit_max;
|
|
|
|
$_increase_time_limit_max = $timeLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Int Limit in seconds
|
|
|
|
*/
|
|
|
|
function get_increase_time_limit_max() {
|
|
|
|
global $_increase_time_limit_max;
|
|
|
|
return $_increase_time_limit_max;
|
|
|
|
}
|