Compare commits

..

2 Commits

Author SHA1 Message Date
Steve Boyd
5f5cf480d0
Merge 9cc00719c7 into ebbd6427b2 2024-10-16 06:15:26 +00:00
Steve Boyd
9cc00719c7 ENH Do not output core code deprecation messages by default 2024-10-16 19:15:20 +13:00
4 changed files with 51 additions and 69 deletions

View File

@ -36,9 +36,8 @@
"psr/container": "^1.1 || ^2.0",
"psr/http-message": "^1",
"sebastian/diff": "^4.0",
"silverstripe/config": "^2.2",
"silverstripe/config": "^2",
"silverstripe/assets": "^2.3",
"silverstripe/supported-modules": "^1",
"silverstripe/vendor-plugin": "^2",
"sminnee/callbacklist": "^0.1.1",
"symfony/cache": "^6.1",

View File

@ -19,6 +19,7 @@ use SilverStripe\View\TemplateGlobalProvider;
*/
class Controller extends RequestHandler implements TemplateGlobalProvider
{
/**
* An array of arguments extracted from the URL.
*

View File

@ -3,13 +3,11 @@
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.
@ -82,14 +80,7 @@ class Deprecation
/**
* @internal
*/
private static bool $showNoticesCalledFromSupportedCode = false;
/**
* Cache of supported module directories, read from silverstripe/supported-modules repositories.json
*
* @internal
*/
private static array $supportedModuleDirectories = [];
private static bool $showCalledFromSupportedCodeNotices = false;
/**
* Enable throwing deprecation warnings. By default, this excludes warnings for
@ -167,11 +158,11 @@ class Deprecation
private static function get_called_from_trace(array $backtrace, int $level): array
{
$newLevel = $level;
// handle closures inside withSuppressedNotice()
if (Deprecation::$insideNoticeSuppression
&& substr($backtrace[$newLevel]['function'], -strlen('{closure}')) === '{closure}'
) {
$newLevel = $newLevel + 2;
if (Deprecation::$insideNoticeSuppression) {
// handle closures inside withSuppressedNotice()
if (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) {
@ -196,46 +187,40 @@ class Deprecation
return $called;
}
private static function isCalledFromSupportedCode(array $backtrace): bool
private static function calledFromSupportedCode(array $backtrace): bool
{
$called = Deprecation::get_called_from_trace($backtrace, 1);
$file = $called['file'] ?? '';
if (!$file) {
return false;
if ($file) {
$isSupportedVendorFile = Deprecation::isSupportedVendorFile($file);
if (!$isSupportedVendorFile) {
return false;
}
}
return Deprecation::fileIsInSupportedModule($file);
return true;
}
/**
* Check whether a file (path to file) is in a supported module
* Whether the given file is supported code
*/
public static function fileIsInSupportedModule(string $file): bool
private static function isSupportedVendorFile(string $file): bool
{
// 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;
// This is a special case for silverstripe-framework when running in CI
if (str_contains($file, '/silverstripe-framework/')) {
return true;
}
foreach (Deprecation::$supportedModuleDirectories as $dir) {
if (str_contains($file, $dir)) {
return true;
}
}
return false;
// 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);
}
public static function isEnabled(): bool
@ -319,17 +304,14 @@ class Deprecation
/**
* If true, deprecation warnings will be shown for deprecated code which is called by core Silverstripe modules.
*/
public static function getShowNoticesCalledFromSupportedCode(): bool
public static function getShowCalledFromSupportedCodeNotices(): bool
{
return Deprecation::$showNoticesCalledFromSupportedCode;
return Deprecation::$showCalledFromSupportedCodeNotices;
}
/**
* Set whether deprecation warnings will be shown for deprecated code which is called by core Silverstripe modules.
*/
public static function setShowNoticesCalledFromSupportedCode(bool $value): void
public static function setShowCalledFromSupportedCodeNotices(bool $value): void
{
Deprecation::$showNoticesCalledFromSupportedCode = $value;
Deprecation::$showCalledFromSupportedCodeNotices = $value;
}
public static function outputNotices(): void
@ -345,11 +327,11 @@ class Deprecation
$arr = array_shift(Deprecation::$userErrorMessageBuffer);
$message = $arr['message'];
$calledWithNoticeSuppression = $arr['calledWithNoticeSuppression'];
$isCalledFromSupportedCode = $arr['isCalledFromSupportedCode'];
$calledFromSupportedCode = $arr['calledFromSupportedCode'];
if ($calledWithNoticeSuppression && !Deprecation::$showNoReplacementNotices) {
continue;
}
if ($isCalledFromSupportedCode && !Deprecation::$showNoticesCalledFromSupportedCode) {
if ($calledFromSupportedCode && !Deprecation::$showCalledFromSupportedCodeNotices) {
continue;
}
Deprecation::$isTriggeringError = true;
@ -385,10 +367,7 @@ class Deprecation
$data = [
'key' => sha1($string),
'message' => $string,
// 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,
'calledFromSupportedCode' => false,
'calledWithNoticeSuppression' => Deprecation::$insideNoticeSuppression
];
} else {
@ -423,7 +402,7 @@ class Deprecation
$data = [
'key' => sha1($string),
'message' => $string,
'isCalledFromSupportedCode' => Deprecation::isCalledFromSupportedCode($backtrace),
'calledFromSupportedCode' => Deprecation::calledFromSupportedCode($backtrace),
'calledWithNoticeSuppression' => Deprecation::$insideNoticeSuppression
];
}
@ -457,7 +436,8 @@ 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
* If $string is empty, then a standardised message will be used, which is:
* Will be removed without equivalent functionality to replace it.
*/
public static function noticeWithNoReplacment(
string $atVersion,

View File

@ -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::getShowNoticesCalledFromSupportedCode();
$this->showSupportedNoticesWasEnabled = Deprecation::getShowCalledFromSupportedCodeNotices();
$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,7 +49,9 @@ class DeprecationTest extends SapphireTest
// Fallback to default PHP error handler
return false;
});
// This is required to clear out the notice from instantiating DeprecationTestObject in TableBuilder::buildTables().
// 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."
Deprecation::outputNotices();
}
@ -60,7 +62,7 @@ class DeprecationTest extends SapphireTest
} else {
Deprecation::disable();
}
Deprecation::setShowNoticesCalledFromSupportedCode($this->showSupportedNoticesWasEnabled);
Deprecation::setShowCalledFromSupportedCodeNotices($this->showSupportedNoticesWasEnabled);
restore_error_handler();
$this->oldHandler = null;
parent::tearDown();
@ -81,7 +83,7 @@ class DeprecationTest extends SapphireTest
private function enableDeprecationNotices(bool $showNoReplacementNotices = false): void
{
Deprecation::enable($showNoReplacementNotices);
Deprecation::setShowNoticesCalledFromSupportedCode(true);
Deprecation::setShowCalledFromSupportedCodeNotices(true);
}
public function testNotice()
@ -258,11 +260,11 @@ class DeprecationTest extends SapphireTest
Deprecation::outputNotices();
}
public function testshowNoticesCalledFromSupportedCode()
public function testShowCalledFromSupportedCodeNotices()
{
$this->expectNotToPerformAssertions();
$this->enableDeprecationNotices(true);
// showNoticesCalledFromSupportedCode is set to true by default for these unit tests
// showCalledFromSupportedCodeNotices 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
@ -272,7 +274,7 @@ class DeprecationTest extends SapphireTest
// Deprecation::withSuppressedNotice(function () {
// Deprecation::notice('123', 'My message.');
// });
Deprecation::setShowNoticesCalledFromSupportedCode(false);
Deprecation::setShowCalledFromSupportedCodeNotices(false);
// notice()
$this->myDeprecatedMethod();
// noticeNoReplacement()