Skip to content

Commit

Permalink
API Stop using deprecated API
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Nov 24, 2022
1 parent 2058293 commit 7a4b4b5
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 23 deletions.
4 changes: 3 additions & 1 deletion src/Core/Convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public static function json2obj($val)
*/
public static function json2array($val)
{
Deprecation::notice('4.4.0', 'Use json_decode() instead');
Deprecation::notice('4.4.0', 'Use json_decode($val, true) instead');

return json_decode($val ?? '', true);
}
Expand Down Expand Up @@ -331,9 +331,11 @@ public static function xml2array($val, $disableDoctypes = false, $disableExterna
* @param SimpleXMLElement $xml
*
* @return mixed
* @deprecated 4.11.0 Will be removed without equivalent functionality
*/
protected static function recursiveXMLToArray($xml)
{
Deprecation::notice('4.11.0', 'Will be removed without equivalent functionality');
$x = null;
if ($xml instanceof SimpleXMLElement) {
$attributes = $xml->attributes();
Expand Down
7 changes: 7 additions & 0 deletions src/Dev/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,23 @@ public static function outputNotices(): void
if (!self::isEnabled()) {
return;
}
$outputMessages = [];
// using a while loop with array_shift() to ensure that self::$userErrorMessageBuffer will have
// have values removed from it before calling user_error()
while (count(self::$userErrorMessageBuffer)) {
$arr = array_shift(self::$userErrorMessageBuffer);
$message = $arr['message'];
// often the same deprecation message appears dozens of times, which isn't helpful
// only need to show a single instance of each message
if (in_array($message, $outputMessages)) {
continue;
}
$calledInsideWithNoReplacement = $arr['calledInsideWithNoReplacement'];
if ($calledInsideWithNoReplacement && !self::$showNoReplacementNotices) {
continue;
}
user_error($message, E_USER_DEPRECATED);
$outputMessages[] = $message;
}
}

Expand Down
44 changes: 28 additions & 16 deletions src/View/Requirements_Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class Requirements_Backend
* Use the injected minification service to minify any javascript file passed to {@link combine_files()}.
*
* @var bool
* @deprecated 4.0.1 Will be removed without equivalent functionality
*/
protected $minifyCombinedFiles = false;

Expand Down Expand Up @@ -212,6 +213,7 @@ class Requirements_Backend

/**
* @var Requirements_Minifier
* @deprecated 4.0.1 Will be removed without equivalent functionality
*/
protected $minifier = null;

Expand Down Expand Up @@ -251,9 +253,11 @@ public function getMinifier()
* Set a new minification service for this backend
*
* @param Requirements_Minifier $minifier
* @deprecated 4.0.1 Will be removed without equivalent functionality
*/
public function setMinifier(Requirements_Minifier $minifier = null)
{
Deprecation::notice('4.0.1', 'Will be removed without equivalent functionality');
$this->minifier = $minifier;
}

Expand Down Expand Up @@ -389,9 +393,11 @@ public function getForceJSToBottom()
* Check if minify files should be combined
*
* @return bool
* @deprecated 4.0.1 Will be removed without equivalent functionality
*/
public function getMinifyCombinedFiles()
{
Deprecation::notice('4.0.1', 'Will be removed without equivalent functionality');
return $this->minifyCombinedFiles;
}

Expand All @@ -400,9 +406,11 @@ public function getMinifyCombinedFiles()
*
* @param bool $minify
* @return $this
* @deprecated 4.0.1 Will be removed without equivalent functionality
*/
public function setMinifyCombinedFiles($minify)
{
Deprecation::notice('4.0.1', 'Will be removed without equivalent functionality');
$this->minifyCombinedFiles = $minify;
return $this;
}
Expand Down Expand Up @@ -1381,22 +1389,25 @@ protected function getCombinedFileURL($combinedFile, $fileList, $type)
$combinedFileID = File::join_paths($this->getCombinedFilesFolder(), $combinedFile);

// Send file combination request to the backend, with an optional callback to perform regeneration
$minify = $this->getMinifyCombinedFiles();
if ($minify && !$this->minifier) {
throw new Exception(
sprintf(
<<<MESSAGE
Cannot minify files without a minification service defined.
Set %s::minifyCombinedFiles to false, or inject a %s service on
%s.properties.minifier
MESSAGE
,
__CLASS__,
Requirements_Minifier::class,
__CLASS__
)
);
}
$minify = Deprecation::withNoReplacement(function () {
$minify = $this->getMinifyCombinedFiles();
if ($minify && !$this->minifier) {
throw new Exception(
sprintf(
<<<MESSAGE
Cannot minify files without a minification service defined.
Set %s::minifyCombinedFiles to false, or inject a %s service on
%s.properties.minifier
MESSAGE
,
__CLASS__,
Requirements_Minifier::class,
__CLASS__
)
);
}
return $minify;
});

$combinedURL = $this
->getAssetHandler()
Expand All @@ -1416,6 +1427,7 @@ function () use ($fileList, $minify, $type) {
$fileContent = $this->resolveCSSReferences($fileContent, $file);
}
// Use configured minifier
// @deprecated
if ($minify) {
$fileContent = $this->minifier->minify($fileContent, $type, $file);
}
Expand Down
4 changes: 3 additions & 1 deletion tests/php/Core/ConvertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,9 @@ public function testRaw2JsonWithContext()
*/
public function testXML2Array()
{

if (Deprecation::isEnabled()) {
$this->markTestSkipped('Test calls deprecated code');
}
$inputXML = <<<XML
<?xml version="1.0"?>
<!DOCTYPE results [
Expand Down
8 changes: 4 additions & 4 deletions tests/php/Core/ObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function testStaticGetterMethod()
$obj = singleton(MyObject::class);
$this->assertEquals(
'MyObject',
$obj->stat('mystaticProperty'),
$obj::config()->get('mystaticProperty'),
'Uninherited statics through stat() on a singleton behave the same as built-in PHP statics'
);
}
Expand All @@ -129,7 +129,7 @@ public function testStaticInheritanceGetters()
}
$subObj = singleton(MyObject::class);
$this->assertEquals(
$subObj->stat('mystaticProperty'),
$subObj::config()->get('mystaticProperty'),
'MyObject',
'Statics defined on a parent class are available through stat() on a subclass'
);
Expand All @@ -144,7 +144,7 @@ public function testStaticSettingOnSingletons()
$singleton2 = singleton(MyObject::class);
$singleton1->set_stat('mystaticProperty', 'changed');
$this->assertEquals(
$singleton2->stat('mystaticProperty'),
$singleton2::config()->get('mystaticProperty'),
'changed',
'Statics setting is populated throughout singletons without explicitly clearing cache'
);
Expand All @@ -159,7 +159,7 @@ public function testStaticSettingOnInstances()
$instance2 = new ObjectTest\MyObject();
$instance1->set_stat('mystaticProperty', 'changed');
$this->assertEquals(
$instance2->stat('mystaticProperty'),
$instance2::config()->get('mystaticProperty'),
'changed',
'Statics setting through set_stat() is populated throughout instances without explicitly clearing cache'
);
Expand Down
5 changes: 4 additions & 1 deletion tests/php/View/RequirementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use SilverStripe\Core\Manifest\ResourceURLGenerator;
use SilverStripe\Control\SimpleResourceURLGenerator;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\Deprecation;
use SilverStripe\View\SSViewer;
use SilverStripe\View\ThemeResourceLoader;

Expand Down Expand Up @@ -302,7 +303,9 @@ protected function setupRequirements($backend)
$backend->clear();
$backend->clearCombinedFiles();
$backend->setCombinedFilesFolder('_combinedfiles');
$backend->setMinifyCombinedFiles(false);
Deprecation::withNoReplacement(function () use ($backend) {
$backend->setMinifyCombinedFiles(false);
});
$backend->setCombinedFilesEnabled(true);
Requirements::flush();
}
Expand Down
3 changes: 3 additions & 0 deletions tests/php/View/SSViewerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ public function testRequirementsCombine()

public function testRequirementsMinification()
{
if (Deprecation::isEnabled()) {
$this->markTestSkipped('Test calls deprecated code');
}
/** @var Requirements_Backend $testBackend */
$testBackend = Injector::inst()->create(Requirements_Backend::class);
$testBackend->setSuffixRequirements(false);
Expand Down

0 comments on commit 7a4b4b5

Please sign in to comment.