Skip to content
Snippets Groups Projects
Commit 3e7bf067 authored by Lisa Ridley's avatar Lisa Ridley
Browse files

Issue #3498983 by lhridley: Fixing CollectionOptimizer logic and tests, and...

Issue #3498983 by lhridley: Fixing CollectionOptimizer logic and tests, and adjusting the FlysystemServiceProvider class for deprecations.
parent ededf639
No related branches found
No related tags found
No related merge requests found
Pipeline #393968 failed
<?php
namespace Drupal\flysystem\Asset;
use Drupal\Core\Asset\CssCollectionOptimizerLazy as DrupalCssCollectionOptimizerLazy;
use Drupal\Core\File\Exception\FileException;
/**
* Optimizes CSS assets.
*/
class CssCollectionOptimizerLazy extends DrupalCssCollectionOptimizerLazy {
use SchemeExtensionTrait;
/**
* {@inheritdoc}
*/
public function deleteAll() {
try {
$this->fileSystem->deleteRecursive($this->getSchemeForExtension('css') . '://css');
}
catch (FileException $fileException) {
\Drupal::logger('flysystem')->error($fileException->getMessage());// @phpstan-ignore-line @codingStandardsIgnoreLine
}
}
}
......@@ -3,7 +3,6 @@
namespace Drupal\flysystem\Asset;
use Drupal\Core\Asset\JsCollectionOptimizer as DrupalJsCollectionOptimizer;
use Drupal\Core\Logger\LoggerChannelTrait;
/**
* Optimizes JavaScript assets.
......@@ -11,7 +10,6 @@ use Drupal\Core\Logger\LoggerChannelTrait;
class JsCollectionOptimizer extends DrupalJsCollectionOptimizer {// @phpstan-ignore-line @codingStandardsIgnoreLine
use SchemeExtensionTrait;
use LoggerChannelTrait;
/**
* {@inheritdoc}
......
<?php
namespace Drupal\flysystem\Asset;
use Drupal\Core\Asset\JsCollectionOptimizerLazy as DrupalJsCollectionOptimizerLazy;
use Drupal\Core\File\Exception\FileException;
/**
* Optimizes JavaScript assets.
*/
class JsCollectionOptimizerLazy extends DrupalJsCollectionOptimizerLazy {// @phpstan-ignore-line @codingStandardsIgnoreLine
use SchemeExtensionTrait;
/**
* {@inheritdoc}
*/
public function deleteAll() {
try {
$this->fileSystem->deleteRecursive($this->getSchemeForExtension('js') . '://js');
}
catch (FileException $fileException) {
\Drupal::logger('flysystem')->error($fileException->getMessage());// @phpstan-ignore-line @codingStandardsIgnoreLine
}
}
}
......@@ -21,12 +21,12 @@ trait SchemeExtensionTrait {
*/
public function getSchemeForExtension($extension) {
$has_assets_scheme = class_exists(AssetsStream::class);
$extension_scheme = 'public';
$extension_scheme = 'assets';
foreach (Settings::get('flysystem', []) as $scheme => $configuration) {
if (!empty($configuration['serve_' . $extension]) && !empty($configuration['driver'])) {
if ($has_assets_scheme) {
@trigger_error(sprintf('The serve_%s Flysystem option is deprecated in flysystem:2.1.0 and is removed from flysystem:3.0.0. Use the assets:// stream wrapper instead. See https://www.drupal.org/node/3328126', $extension), E_USER_DEPRECATED);
@trigger_error(sprintf('The serve_%s Flysystem option is deprecated in flysystem:2.1.0 and is removed from flysystem:2.3.0. Use the assets:// stream wrapper instead. See https://www.drupal.org/node/3328126', $extension), E_USER_DEPRECATED);
}
// Don't break, the last configured one will win.
$extension_scheme = $scheme;
......
......@@ -65,22 +65,20 @@ class FlysystemServiceProvider implements ServiceProviderInterface {
return;
}
$container
->getDefinition('asset.' . $extension . '.dumper')
->setClass('Drupal\flysystem\Asset\AssetDumper');
$optimizer = $container->getDefinition('asset.' . $extension . '.collection_optimizer');
if ($optimizer->getClass() === 'Drupal\Core\Asset\\' . ucfirst($extension) . 'CollectionOptimizer') {
@trigger_error(sprintf('The serve_%s Flysystem option is deprecated in flysystem:2.1.0 and is removed from flysystem:2.3.0. Use the assets:// stream wrapper instead. See https://www.drupal.org/node/3328126', $extension), E_USER_DEPRECATED);
$optimizer->setClass('Drupal\flysystem\Asset\\' . ucfirst($extension) . 'CollectionOptimizer');
}
if ($optimizer->getClass() === 'Drupal\Core\Asset\\' . ucfirst($extension) . 'CollectionOptimizerLazy') {
$optimizer->setClass('Drupal\flysystem\Asset\\' . ucfirst($extension) . 'CollectionOptimizerLazy');
}
$container
->getDefinition('asset.' . $extension . '.dumper')
->setClass('Drupal\flysystem\Asset\AssetDumper');
if ($extension === 'css') {
$container
->getDefinition('asset.css.optimizer')
->setClass('Drupal\flysystem\Asset\CssOptimizer');
->getDefinition('asset.' . $extension . '.optimizer')
->setClass('Drupal\flysystem\Asset\\' . ucfirst($extension) . 'Optimizer');
}
}
......
......@@ -4,7 +4,6 @@ namespace Drupal\Tests\flysystem\Kernel;
use Drupal\Core\Asset\AssetCollectionGrouperInterface;
use Drupal\Core\Asset\AssetOptimizerInterface;
use Drupal\Core\Asset\CssOptimizer;
use Drupal\Core\State\StateInterface;
use Drupal\flysystem\Asset\AssetDumper;
use Drupal\flysystem\Asset\CssCollectionOptimizer;
......@@ -122,8 +121,9 @@ class CollectionOptimizerTest extends KernelTestBase {
$grouper = $this->prophesize(AssetCollectionGrouperInterface::class);
$dumper = $this->prophesize(AssetDumper::class);
$state = $this->prophesize(StateInterface::class);
$cs_optimizer = $this->prophesize(AssetOptimizerInterface::class);
$optimizer = new CssCollectionOptimizer($grouper->reveal(), new CssOptimizer($this->fileUrlGenerator), $dumper->reveal(), $state->reveal(), $this->container->get('file_system'));
$optimizer = new CssCollectionOptimizer($grouper->reveal(), $cs_optimizer->reveal(), $dumper->reveal(), $state->reveal(), $this->container->get('file_system'));
$optimizer->deleteAll();
foreach ($this->cssFilesUnderTest() as $css_file => $expired) {
......
......@@ -26,8 +26,8 @@ class SchemeExtensionTraitTest extends UnitTestCase {
$trait = $this->getMockForTrait(SchemeExtensionTrait::class);
$this->assertSame('local', $trait->getSchemeForExtension('js'));
$this->assertSame('public', $trait->getSchemeForExtension('css'));
$this->assertSame('public', $trait->getSchemeForExtension('jpg'));
$this->assertSame('assets', $trait->getSchemeForExtension('css'));
$this->assertSame('assets', $trait->getSchemeForExtension('jpg'));
}
}
......@@ -91,18 +91,11 @@ class FlysystemServiceProviderTest extends UnitTestCase {
/**
* @covers \Drupal\flysystem\FlysystemServiceProvider
*/
public function test() {
public function testSwappingJsServices() {
// Test swapping the asset dumper.
$this->container->register('asset.js.dumper', AssetDumper::class);
(new FlysystemServiceProvider())->register($this->container);
$this->assertSame(AssetDumper::class, $this->container->getDefinition('asset.js.dumper')->getClass());
$this->container->register('asset.js.collection_optimizer', JsCollectionOptimizer::class);// @phpstan-ignore-line @codingStandardsIgnoreLine
(new FlysystemServiceProvider())->register($this->container);
$this->assertSame(AssetDumper::class, $this->container->getDefinition('asset.js.dumper')->getClass());
$this->assertSame(JsCollectionOptimizer::class, $this->container->getDefinition('asset.js.collection_optimizer')->getClass());// @phpstan-ignore-line @codingStandardsIgnoreLine
// A successful swap.
new Settings([
'flysystem' => [
'testscheme' => [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment