From 65ba709271494662f3edbea421c42fb0af8977f7 Mon Sep 17 00:00:00 2001 From: Daniel Hensby Date: Mon, 23 May 2016 21:40:05 +0100 Subject: [PATCH] Giving composer more responsibility over autoloading (#5572) --- cli-script.php | 9 +++++++++ composer.json | 4 ++++ core/Core.php | 11 ----------- dev/install/install.php | 2 +- dev/install/install.php5 | 13 ++++++++++++- main.php | 18 +++++++++++++----- thirdparty/tinymce/tiny_mce_gzip.php | 14 ++++++++++++-- 7 files changed, 51 insertions(+), 20 deletions(-) diff --git a/cli-script.php b/cli-script.php index d72c8645a..acd13cb1d 100755 --- a/cli-script.php +++ b/cli-script.php @@ -58,6 +58,15 @@ if(isset($_SERVER['argv'][1])) { $_GET['url'] = $_SERVER['argv'][1]; } +// require composers autoloader +if (file_exists($autoloadPath = dirname(__DIR__) . '/vendor/autoload.php')) { + require_once $autoloadPath; +} +else { + echo "Failed to include composer's autoloader, unable to continue\n"; + exit(1); +} + /** * Include SilverStripe's core code */ diff --git a/composer.json b/composer.json index 1e431ed61..c3315a7c7 100644 --- a/composer.json +++ b/composer.json @@ -31,6 +31,10 @@ } }, "autoload": { + "psr-4": { + "SilverStripe\\": "" + }, + "files": ["core/Constants.php"], "classmap": ["tests/behat/features/bootstrap"] } } diff --git a/core/Core.php b/core/Core.php index db03bed74..66801e7dd 100644 --- a/core/Core.php +++ b/core/Core.php @@ -3,7 +3,6 @@ * This file is the Framework bootstrap. It will get your environment ready to call Director::direct(). * * It takes care of: - * - Including Constants.php to include _ss_environment and initialise necessary constants * - Checking of PHP memory limit * - Including all the files needed to get the manifest built * - Building and including the manifest @@ -22,11 +21,6 @@ */ error_reporting(E_ALL | E_STRICT); -/** - * Include Constants (if it hasn't already been included) to pull in BASE_PATH, etc - */ -require_once dirname(__FILE__).'/Constants.php'; - global $_increase_time_limit_max; $_increase_time_limit_max = -1; @@ -110,11 +104,6 @@ $loader = SS_ClassLoader::instance(); $loader->registerAutoloader(); $loader->pushManifest($manifest); -// 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'; -} - // Now that the class manifest is up, load the static configuration $configManifest = new SS_ConfigStaticManifest(); Config::inst()->pushConfigStaticManifest($configManifest); diff --git a/dev/install/install.php b/dev/install/install.php index 62f7dd64a..edd96affa 100644 --- a/dev/install/install.php +++ b/dev/install/install.php @@ -15,7 +15,7 @@ define('FRAMEWORK_NAME', 'framework'); if (version_compare(phpversion(), '5.5.0', '<')) { - header("HTTP/1.1 500 Server Error"); + header($_SERVER['SERVER_PROTOCOL'] . " 500 Server Error"); echo str_replace( array('$PHPVersion', 'sapphire'), array(phpversion(), FRAMEWORK_NAME), diff --git a/dev/install/install.php5 b/dev/install/install.php5 index f771c127e..69d1bbef8 100755 --- a/dev/install/install.php5 +++ b/dev/install/install.php5 @@ -31,7 +31,18 @@ if(function_exists('session_start') && !session_id()) { session_start(); } -require_once FRAMEWORK_NAME . '/core/Constants.php'; // this also includes TempPath.php; +// require composers autoloader +if (file_exists($autoloadPath = dirname(__DIR__) . '/../../vendor/autoload.php')) { + require_once $autoloadPath; +} +else { + if (!headers_sent()) { + header($_SERVER['SERVER_PROTOCOL'] . " 500 Server Error"); + header('Content-Type: text/plain'); + } + echo "Failed to include composer's autoloader, unable to continue\n"; + exit(1); +} $envFileExists = defined('SS_ENVIRONMENT_FILE'); $usingEnv = $envFileExists && !empty($_REQUEST['useEnv']); diff --git a/main.php b/main.php index 348344fb0..154ec2309 100644 --- a/main.php +++ b/main.php @@ -15,7 +15,7 @@ */ if (version_compare(phpversion(), '5.5.0', '<')) { - header("HTTP/1.1 500 Server Error"); + header($_SERVER['SERVER_PROTOCOL'] . " 500 Server Error"); echo str_replace('$PHPVersion', phpversion(), file_get_contents("dev/install/php5-required.html")); die(); } @@ -52,10 +52,18 @@ if (version_compare(phpversion(), '5.5.0', '<')) { * @see Director::direct() */ -/** - * Include the defines that set BASE_PATH, etc - */ -require_once('core/Constants.php'); +// require composers autoloader +if (file_exists($autoloadPath = dirname(__DIR__) . '/vendor/autoload.php')) { + require_once $autoloadPath; +} +else { + if (!headers_sent()) { + header($_SERVER['SERVER_PROTOCOL'] . " 500 Server Error"); + header('Content-Type: text/plain'); + } + echo "Failed to include composer's autoloader, unable to continue\n"; + exit(1); +} // IIS will sometimes generate this. if(!empty($_SERVER['HTTP_X_ORIGINAL_URL'])) { diff --git a/thirdparty/tinymce/tiny_mce_gzip.php b/thirdparty/tinymce/tiny_mce_gzip.php index 22bd49f1f..588a4e136 100755 --- a/thirdparty/tinymce/tiny_mce_gzip.php +++ b/thirdparty/tinymce/tiny_mce_gzip.php @@ -12,7 +12,18 @@ $frameworkPath = rtrim(dirname(dirname(dirname(__FILE__))), DIRECTORY_SEPARATOR); $basePath = rtrim(dirname($frameworkPath), DIRECTORY_SEPARATOR); -require_once $frameworkPath . '/core/Constants.php'; +// require composers autoloader +if(file_exists($basePath . '/vendor/autoload.php')) { + require_once $basePath . '/vendor/autoload.php'; +} +else { + if (!headers_sent()) { + header($_SERVER['SERVER_PROTOCOL'] . " 500 Server Error"); + header('Content-Type: text/plain'); + } + echo "Failed to include composer's autoloader, unable to continue\n"; + exit(1); +} // Handle incoming request if it's a script call if (TinyMCE_Compressor::getParam("js")) { @@ -374,4 +385,3 @@ class TinyMCE_Compressor { return $content; } } -?>