mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Compare commits
2 Commits
5f5cf480d0
...
edf8a32884
Author | SHA1 | Date | |
---|---|---|---|
|
edf8a32884 | ||
|
a1f73378da |
@ -36,8 +36,9 @@
|
||||
"psr/container": "^1.1 || ^2.0",
|
||||
"psr/http-message": "^1",
|
||||
"sebastian/diff": "^4.0",
|
||||
"silverstripe/config": "^2",
|
||||
"silverstripe/config": "^2.2",
|
||||
"silverstripe/assets": "^2.3",
|
||||
"silverstripe/supported-modules": "^1",
|
||||
"silverstripe/vendor-plugin": "^2",
|
||||
"sminnee/callbacklist": "^0.1.1",
|
||||
"symfony/cache": "^6.1",
|
||||
|
@ -19,7 +19,6 @@ use SilverStripe\View\TemplateGlobalProvider;
|
||||
*/
|
||||
class Controller extends RequestHandler implements TemplateGlobalProvider
|
||||
{
|
||||
|
||||
/**
|
||||
* An array of arguments extracted from the URL.
|
||||
*
|
||||
|
@ -3,11 +3,13 @@
|
||||
namespace SilverStripe\Dev;
|
||||
|
||||
use BadMethodCallException;
|
||||
use RuntimeException;
|
||||
use SilverStripe\Control\Director;
|
||||
use SilverStripe\Core\Environment;
|
||||
use SilverStripe\Core\Injector\InjectionCreator;
|
||||
use SilverStripe\Core\Injector\InjectorLoader;
|
||||
use SilverStripe\Core\Manifest\Module;
|
||||
use SilverStripe\Core\Path;
|
||||
|
||||
/**
|
||||
* Handles raising an notice when accessing a deprecated method, class, configuration, or behaviour.
|
||||
@ -80,7 +82,14 @@ class Deprecation
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
private static bool $showCalledFromSupportedCodeNotices = false;
|
||||
private static bool $showNoticesCalledFromSupportedCode = false;
|
||||
|
||||
/**
|
||||
* Cache of supported module directories, read from silverstripe/supported-modules repositories.json
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
private static array $supportedModuleDirectories = [];
|
||||
|
||||
/**
|
||||
* Enable throwing deprecation warnings. By default, this excludes warnings for
|
||||
@ -158,11 +167,11 @@ class Deprecation
|
||||
private static function get_called_from_trace(array $backtrace, int $level): array
|
||||
{
|
||||
$newLevel = $level;
|
||||
if (Deprecation::$insideNoticeSuppression) {
|
||||
// handle closures inside withSuppressedNotice()
|
||||
if (substr($backtrace[$newLevel]['function'], -strlen('{closure}')) === '{closure}') {
|
||||
$newLevel = $newLevel + 2;
|
||||
}
|
||||
// handle closures inside withSuppressedNotice()
|
||||
if (Deprecation::$insideNoticeSuppression
|
||||
&& substr($backtrace[$newLevel]['function'], -strlen('{closure}')) === '{closure}'
|
||||
) {
|
||||
$newLevel = $newLevel + 2;
|
||||
}
|
||||
// handle call_user_func
|
||||
if ($level === 4 && strpos($backtrace[2]['function'] ?? '', 'call_user_func') !== false) {
|
||||
@ -187,40 +196,46 @@ class Deprecation
|
||||
return $called;
|
||||
}
|
||||
|
||||
private static function calledFromSupportedCode(array $backtrace): bool
|
||||
private static function isCalledFromSupportedCode(array $backtrace): bool
|
||||
{
|
||||
$called = Deprecation::get_called_from_trace($backtrace, 1);
|
||||
$file = $called['file'] ?? '';
|
||||
if ($file) {
|
||||
$isSupportedVendorFile = Deprecation::isSupportedVendorFile($file);
|
||||
if (!$isSupportedVendorFile) {
|
||||
return false;
|
||||
}
|
||||
if (!$file) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return Deprecation::fileIsInSupportedModule($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the given file is supported code
|
||||
* Check whether a file (path to file) is in a supported module
|
||||
*/
|
||||
private static function isSupportedVendorFile(string $file): bool
|
||||
public static function fileIsInSupportedModule(string $file): bool
|
||||
{
|
||||
// This is a special case for silverstripe-framework when running in CI
|
||||
if (str_contains($file, '/silverstripe-framework/')) {
|
||||
return true;
|
||||
// Cache the supported modules list
|
||||
if (count(Deprecation::$supportedModuleDirectories) === 0) {
|
||||
// Manually load the supported modules list rather than use MetaData::getAllRepositoryMetaData()
|
||||
// because we do not want to make a network request which could slow down a website
|
||||
// While there is a small risk of the list being out of date, there is minimal downside to this
|
||||
$path = Path::join(BASE_PATH, 'vendor/silverstripe/supported-modules/repositories.json');
|
||||
if (!file_exists($path)) {
|
||||
throw new RuntimeException('Could not find supported modules list');
|
||||
}
|
||||
$json = json_decode(file_get_contents($path), true);
|
||||
if (is_null($json)) {
|
||||
throw new RuntimeException('Could not parse supported modules list');
|
||||
}
|
||||
$dirs = array_map(fn($module) => "/vendor/{$module['packagist']}/", $json['supportedModules']);
|
||||
// This is a special case for silverstripe-framework when running in CI
|
||||
// Needed because module is run in the root folder rather than in the vendor folder
|
||||
$dirs[] = '/silverstripe-framework/';
|
||||
Deprecation::$supportedModuleDirectories = $dirs;
|
||||
}
|
||||
// Doing a fairly simple check to see if a file is in a supported vendor folder, rather than whether
|
||||
// the module itself is actually supported
|
||||
$vendors = implode('|', [
|
||||
'bringyourownideas',
|
||||
'colymba',
|
||||
'cwp',
|
||||
'dnadesign',
|
||||
'silverstripe',
|
||||
'symbiote',
|
||||
'tractorcow',
|
||||
]);
|
||||
return (bool) preg_match("#/vendor/($vendors)/#", $file);
|
||||
foreach (Deprecation::$supportedModuleDirectories as $dir) {
|
||||
if (str_contains($file, $dir)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function isEnabled(): bool
|
||||
@ -304,14 +319,17 @@ class Deprecation
|
||||
/**
|
||||
* If true, deprecation warnings will be shown for deprecated code which is called by core Silverstripe modules.
|
||||
*/
|
||||
public static function getShowCalledFromSupportedCodeNotices(): bool
|
||||
public static function getShowNoticesCalledFromSupportedCode(): bool
|
||||
{
|
||||
return Deprecation::$showCalledFromSupportedCodeNotices;
|
||||
return Deprecation::$showNoticesCalledFromSupportedCode;
|
||||
}
|
||||
|
||||
public static function setShowCalledFromSupportedCodeNotices(bool $value): void
|
||||
/**
|
||||
* Set whether deprecation warnings will be shown for deprecated code which is called by core Silverstripe modules.
|
||||
*/
|
||||
public static function setShowNoticesCalledFromSupportedCode(bool $value): void
|
||||
{
|
||||
Deprecation::$showCalledFromSupportedCodeNotices = $value;
|
||||
Deprecation::$showNoticesCalledFromSupportedCode = $value;
|
||||
}
|
||||
|
||||
public static function outputNotices(): void
|
||||
@ -327,11 +345,11 @@ class Deprecation
|
||||
$arr = array_shift(Deprecation::$userErrorMessageBuffer);
|
||||
$message = $arr['message'];
|
||||
$calledWithNoticeSuppression = $arr['calledWithNoticeSuppression'];
|
||||
$calledFromSupportedCode = $arr['calledFromSupportedCode'];
|
||||
$isCalledFromSupportedCode = $arr['isCalledFromSupportedCode'];
|
||||
if ($calledWithNoticeSuppression && !Deprecation::$showNoReplacementNotices) {
|
||||
continue;
|
||||
}
|
||||
if ($calledFromSupportedCode && !Deprecation::$showCalledFromSupportedCodeNotices) {
|
||||
if ($isCalledFromSupportedCode && !Deprecation::$showNoticesCalledFromSupportedCode) {
|
||||
continue;
|
||||
}
|
||||
Deprecation::$isTriggeringError = true;
|
||||
@ -367,7 +385,10 @@ class Deprecation
|
||||
$data = [
|
||||
'key' => sha1($string),
|
||||
'message' => $string,
|
||||
'calledFromSupportedCode' => false,
|
||||
// Setting to `false` as here as any SCOPE_CONFIG notices from supported modules have
|
||||
// already been filtered out if needed if they came from a supported module in
|
||||
// SilverStripe\Config\Transformer\YamlTransformer::checkForDeprecatedConfig()
|
||||
'isCalledFromSupportedCode' => false,
|
||||
'calledWithNoticeSuppression' => Deprecation::$insideNoticeSuppression
|
||||
];
|
||||
} else {
|
||||
@ -402,7 +423,7 @@ class Deprecation
|
||||
$data = [
|
||||
'key' => sha1($string),
|
||||
'message' => $string,
|
||||
'calledFromSupportedCode' => Deprecation::calledFromSupportedCode($backtrace),
|
||||
'isCalledFromSupportedCode' => Deprecation::isCalledFromSupportedCode($backtrace),
|
||||
'calledWithNoticeSuppression' => Deprecation::$insideNoticeSuppression
|
||||
];
|
||||
}
|
||||
@ -436,8 +457,7 @@ class Deprecation
|
||||
|
||||
/**
|
||||
* Shorthand method to create a suppressed notice for something with no immediate replacement.
|
||||
* If $string is empty, then a standardised message will be used, which is:
|
||||
* Will be removed without equivalent functionality to replace it.
|
||||
* If $string is empty, then a standardised message will be used
|
||||
*/
|
||||
public static function noticeWithNoReplacment(
|
||||
string $atVersion,
|
||||
|
@ -33,7 +33,7 @@ class DeprecationTest extends SapphireTest
|
||||
// https://github.com/laminas/laminas-di/pull/30#issuecomment-927585210
|
||||
parent::setup();
|
||||
$this->noticesWereEnabled = Deprecation::isEnabled();
|
||||
$this->showSupportedNoticesWasEnabled = Deprecation::getShowCalledFromSupportedCodeNotices();
|
||||
$this->showSupportedNoticesWasEnabled = Deprecation::getShowNoticesCalledFromSupportedCode();
|
||||
$this->oldHandler = set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline) {
|
||||
if ($errno === E_USER_DEPRECATED) {
|
||||
if (str_contains($errstr, 'SilverStripe\\Dev\\Tests\\DeprecationTest')) {
|
||||
@ -49,9 +49,7 @@ class DeprecationTest extends SapphireTest
|
||||
// Fallback to default PHP error handler
|
||||
return false;
|
||||
});
|
||||
// This is required to clear out the message
|
||||
// 'SilverStripe\Dev\Tests\DeprecationTest\DeprecationTestObject is deprecated. Some class message. Called from
|
||||
// SilverStripe\ORM\Connect\TableBuilder->buildTables."
|
||||
// This is required to clear out the notice from instantiating DeprecationTestObject in TableBuilder::buildTables().
|
||||
Deprecation::outputNotices();
|
||||
}
|
||||
|
||||
@ -62,7 +60,7 @@ class DeprecationTest extends SapphireTest
|
||||
} else {
|
||||
Deprecation::disable();
|
||||
}
|
||||
Deprecation::setShowCalledFromSupportedCodeNotices($this->showSupportedNoticesWasEnabled);
|
||||
Deprecation::setShowNoticesCalledFromSupportedCode($this->showSupportedNoticesWasEnabled);
|
||||
restore_error_handler();
|
||||
$this->oldHandler = null;
|
||||
parent::tearDown();
|
||||
@ -83,7 +81,7 @@ class DeprecationTest extends SapphireTest
|
||||
private function enableDeprecationNotices(bool $showNoReplacementNotices = false): void
|
||||
{
|
||||
Deprecation::enable($showNoReplacementNotices);
|
||||
Deprecation::setShowCalledFromSupportedCodeNotices(true);
|
||||
Deprecation::setShowNoticesCalledFromSupportedCode(true);
|
||||
}
|
||||
|
||||
public function testNotice()
|
||||
@ -260,11 +258,11 @@ class DeprecationTest extends SapphireTest
|
||||
Deprecation::outputNotices();
|
||||
}
|
||||
|
||||
public function testShowCalledFromSupportedCodeNotices()
|
||||
public function testshowNoticesCalledFromSupportedCode()
|
||||
{
|
||||
$this->expectNotToPerformAssertions();
|
||||
$this->enableDeprecationNotices(true);
|
||||
// showCalledFromSupportedCodeNotices is set to true by default for these unit tests
|
||||
// showNoticesCalledFromSupportedCode is set to true by default for these unit tests
|
||||
// as it is testing code within vendor/silverstripe
|
||||
// This test is to ensure that the method works as expected when we disable this
|
||||
// and we should expect no exceptions to be thrown
|
||||
@ -274,7 +272,7 @@ class DeprecationTest extends SapphireTest
|
||||
// Deprecation::withSuppressedNotice(function () {
|
||||
// Deprecation::notice('123', 'My message.');
|
||||
// });
|
||||
Deprecation::setShowCalledFromSupportedCodeNotices(false);
|
||||
Deprecation::setShowNoticesCalledFromSupportedCode(false);
|
||||
// notice()
|
||||
$this->myDeprecatedMethod();
|
||||
// noticeNoReplacement()
|
||||
|
Loading…
Reference in New Issue
Block a user