Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to Symfony's version of the Path helper #89

Merged
merged 5 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [7.1, 7.2, 7.3, 7.4, 8.0]
php: [7.2, 7.3, 7.4, 8.0]
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down Expand Up @@ -123,7 +123,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [7.1, 7.2, 7.4, 8.0]
php: [7.2, 7.4, 8.0]
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down Expand Up @@ -151,7 +151,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [7.1, 7.2, 7.3, 7.4, 8.0]
php: [7.2, 7.3, 7.4, 8.0]
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
}
],
"require": {
"php": "^7.1 || ^8.0",
"php": "^7.2 || ^8.0",
"ext-json": "*",
"imagine/imagine": "^0.7.1 || ^1.0",
"symfony/filesystem": "^2.8 || ^3.0 || ^4.0 || ^5.0",
"symfony/polyfill-php73": "^1.11",
"webmozart/path-util": "^2.0"
"symfony/filesystem": "^5.4 || ^6.0",
"symfony/polyfill-php73": "^1.11"
},
"conflict": {
"contao/imagine-svg": "<0.1.4 || >=2.0"
Expand Down
8 changes: 4 additions & 4 deletions src/DeferredImageStorageFilesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
use Contao\Image\Exception\RuntimeException;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Webmozart\PathUtil\Path;
use Symfony\Component\Filesystem\Path;

class DeferredImageStorageFilesystem implements DeferredImageStorageInterface
{
private const PATH_PREFIX = '/deferred';
private const PATH_PREFIX = 'deferred';
private const PATH_SUFFIX = '.json';

/**
Expand All @@ -46,7 +46,7 @@ public function __construct(string $cacheDir, Filesystem $filesystem = null)
$filesystem = new Filesystem();
}

$this->cacheDir = $cacheDir.self::PATH_PREFIX;
$this->cacheDir = Path::join($cacheDir, self::PATH_PREFIX);
$this->filesystem = $filesystem;
}

Expand Down Expand Up @@ -196,7 +196,7 @@ private function getConfigPath(string $path): string
throw new InvalidArgumentException(sprintf('Invalid storage key "%s"', $path));
}

return $this->cacheDir.'/'.$path.self::PATH_SUFFIX;
return Path::join($this->cacheDir, $path.self::PATH_SUFFIX);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/DeferredResizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Imagine\Image\ImagineInterface;
use Imagine\Image\Point;
use Symfony\Component\Filesystem\Filesystem;
use Webmozart\PathUtil\Path;
use Symfony\Component\Filesystem\Path;

class DeferredResizer extends Resizer implements DeferredResizerInterface
{
Expand Down Expand Up @@ -63,7 +63,7 @@ public function getDeferredImage(string $targetPath, ImagineInterface $imagine):
$config = $this->storage->get($targetPath);

return new DeferredImage(
$this->cacheDir.'/'.$targetPath,
Path::join($this->cacheDir, $targetPath),
$imagine,
new ImageDimensions(
new Box(
Expand Down Expand Up @@ -182,12 +182,12 @@ private function executeDeferredResize(string $targetPath, array $config, Imagin
$options = new ResizeOptions();
$options->setImagineOptions($config['options']['imagine_options']);

$path = Path::canonicalize($this->cacheDir.'/'.$config['path']);
$path = Path::join($this->cacheDir, $config['path']);

return parent::executeResize(
new Image($path, $imagine, $this->filesystem),
$coordinates,
$this->cacheDir.'/'.$targetPath,
Path::join($this->cacheDir, $targetPath),
$options
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use Imagine\Image\ImagineInterface;
use Imagine\Image\Metadata\MetadataBag;
use Symfony\Component\Filesystem\Filesystem;
use Webmozart\PathUtil\Path;
use Symfony\Component\Filesystem\Path;

class Image implements ImageInterface
{
Expand Down
6 changes: 3 additions & 3 deletions src/Resizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Imagine\Filter\Basic\Autorotate;
use Imagine\Image\Palette\RGB;
use Symfony\Component\Filesystem\Filesystem;
use Webmozart\PathUtil\Path;
use Symfony\Component\Filesystem\Path;

class Resizer implements ResizerInterface
{
Expand Down Expand Up @@ -157,7 +157,7 @@ protected function processResize(ImageInterface $image, ResizeConfiguration $con
return $this->createImage($image, $image->getPath());
}

$cachePath = $this->cacheDir.'/'.$this->createCachePath($image->getPath(), $coordinates, $options);
$cachePath = Path::join($this->cacheDir, $this->createCachePath($image->getPath(), $coordinates, $options));

if ($this->filesystem->exists($cachePath) && !$options->getBypassCache()) {
return $this->createImage($image, $cachePath);
Expand Down Expand Up @@ -213,6 +213,6 @@ static function ($value) {
$pathinfo = pathinfo($path);
$extension = $options->getImagineOptions()['format'] ?? strtolower($pathinfo['extension']);

return $hash[0].'/'.$pathinfo['filename'].'-'.substr($hash, 1).'.'.$extension;
return Path::join($hash[0], $pathinfo['filename'].'-'.substr($hash, 1).'.'.$extension);
}
}
17 changes: 9 additions & 8 deletions tests/DeferredImageStorageFilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Contao\Image\Exception\RuntimeException;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;

class DeferredImageStorageFilesystemTest extends TestCase
{
Expand All @@ -33,7 +34,7 @@ protected function setUp(): void
{
parent::setUp();

$this->rootDir = __DIR__.'/tmp';
$this->rootDir = Path::canonicalize(__DIR__.'/tmp');
}

/**
Expand Down Expand Up @@ -79,7 +80,7 @@ public function testGetLocked(string $key, array $value): void

$this->assertSame($value, $storage->getLocked($key));

$dataPath = $this->rootDir.'/deferred/'.$key.'.json';
$dataPath = Path::join($this->rootDir, 'deferred', $key.'.json');
$handle = fopen($dataPath, 'r+');

$this->assertFalse(flock($handle, LOCK_EX | LOCK_NB), 'Data file should be locked');
Expand Down Expand Up @@ -129,10 +130,10 @@ public function testInvalidJsonThrows(): void
{
$storage = new DeferredImageStorageFilesystem($this->rootDir);

if (!is_dir($this->rootDir.'/deferred')) {
mkdir($this->rootDir.'/deferred', 0777, true);
if (!is_dir(Path::join($this->rootDir, 'deferred'))) {
mkdir(Path::join($this->rootDir, 'deferred'), 0777, true);
}
file_put_contents($this->rootDir.'/deferred/test.json', 'invalid JSON');
file_put_contents(Path::join($this->rootDir, 'deferred/test.json'), 'invalid JSON');

$this->expectException(JsonException::class);

Expand All @@ -143,10 +144,10 @@ public function testInvalidJsonDataThrows(): void
{
$storage = new DeferredImageStorageFilesystem($this->rootDir);

if (!is_dir($this->rootDir.'/deferred')) {
mkdir($this->rootDir.'/deferred', 0777, true);
if (!is_dir(Path::join($this->rootDir, 'deferred'))) {
mkdir(Path::join($this->rootDir, 'deferred'), 0777, true);
}
file_put_contents($this->rootDir.'/deferred/test.json', '"JSON string instead of an array"');
file_put_contents(Path::join($this->rootDir, 'deferred/test.json'), '"JSON string instead of an array"');

$this->expectException(InvalidArgumentException::class);

Expand Down
37 changes: 19 additions & 18 deletions tests/DeferredResizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Imagine\Image\Point;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;

class DeferredResizerTest extends TestCase
{
Expand All @@ -47,7 +48,7 @@ protected function setUp(): void
{
parent::setUp();

$this->rootDir = __DIR__.'/tmp';
$this->rootDir = Path::canonicalize(__DIR__.'/tmp');
}

/**
Expand Down Expand Up @@ -86,7 +87,7 @@ static function (ResizeConfiguration $config, ImageDimensions $dimensions, Impor

(new GdImagine())
->create(new Box(100, 100))
->save($this->rootDir.'/dummy.jpg')
->save(Path::join($this->rootDir, 'dummy.jpg'))
;

$image = $this->createMock(Image::class);
Expand All @@ -97,7 +98,7 @@ static function (ResizeConfiguration $config, ImageDimensions $dimensions, Impor

$image
->method('getPath')
->willReturn($this->rootDir.'/dummy.jpg')
->willReturn(Path::join($this->rootDir, 'dummy.jpg'))
;

$image
Expand All @@ -123,7 +124,7 @@ static function (ResizeConfiguration $config, ImageDimensions $dimensions, Impor
$this->assertMatchesRegularExpression('(/[0-9a-f]/dummy-[0-9a-f]{8}.jpg$)', $deferredImage->getPath());
$this->assertFileDoesNotExist($deferredImage->getPath());
$this->assertFileExists(
$this->rootDir.'/deferred/'.substr($deferredImage->getPath(), \strlen($this->rootDir)).'.json'
Path::join($this->rootDir, 'deferred', substr($deferredImage->getPath(), \strlen($this->rootDir)).'.json')
);

/** @var DeferredImageInterface $deferredImage2 */
Expand All @@ -148,7 +149,7 @@ static function (ResizeConfiguration $config, ImageDimensions $dimensions, Impor
$this->assertEquals(new ImageDimensions(new Box(50, 50)), $resizedImage->getDimensions());
$this->assertFileExists($resizedImage->getPath());
$this->assertFileDoesNotExist(
$this->rootDir.'/deferred/'.substr($deferredImage->getPath(), \strlen($this->rootDir)).'.json'
Path::join($this->rootDir, 'deferred', substr($deferredImage->getPath(), \strlen($this->rootDir)).'.json')
);

// Calling resizeDeferredImage() a second time should return the already
Expand All @@ -165,12 +166,12 @@ static function (ResizeConfiguration $config, ImageDimensions $dimensions, Impor
$resizedImage = $resizer->resize(
$image,
(new ResizeConfiguration())->setWidth(100)->setHeight(100),
(new ResizeOptions())->setTargetPath($this->rootDir.'/target-path.jpg')
(new ResizeOptions())->setTargetPath(Path::join($this->rootDir, 'target-path.jpg'))
);

$this->assertNotInstanceOf(DeferredImageInterface::class, $resizedImage);
$this->assertEquals(new ImageDimensions(new Box(100, 100)), $resizedImage->getDimensions());
$this->assertSame($this->rootDir.'/target-path.jpg', $resizedImage->getPath());
$this->assertSame(Path::join($this->rootDir, 'target-path.jpg'), $resizedImage->getPath());
$this->assertFileExists($resizedImage->getPath());
}

Expand All @@ -196,7 +197,7 @@ public function testGetDeferredImage(): void

$imagine = $this->createMock(ImagineInterface::class);
$resizer = $this->createResizer(null, null, null, $storage);
$imagePath = $this->rootDir.'/a/foo-5fc1c9f9.jpg';
$imagePath = Path::join($this->rootDir, 'a/foo-5fc1c9f9.jpg');
$deferredImage = $resizer->getDeferredImage($imagePath, $imagine);

$this->assertInstanceOf(DeferredImageInterface::class, $deferredImage);
Expand All @@ -218,7 +219,7 @@ public function testGetMissingDeferredImage(): void

$imagine = $this->createMock(ImagineInterface::class);
$resizer = $this->createResizer(null, null, null, $storage);
$imagePath = $this->rootDir.'/a/foo-5fc1c9f9.jpg';
$imagePath = Path::join($this->rootDir, 'a/foo-5fc1c9f9.jpg');

$this->assertNull($resizer->getDeferredImage($imagePath, $imagine));
}
Expand All @@ -230,7 +231,7 @@ public function testResizeDeferredImageThrowsForOutsidePath(): void
$deferredImage = $this->createMock(DeferredImageInterface::class);
$deferredImage
->method('getPath')
->willReturn($this->rootDir.'/../foo.jpg')
->willReturn(Path::join($this->rootDir, '../foo.jpg'))
;

$this->expectException(InvalidArgumentException::class);
Expand All @@ -244,7 +245,7 @@ public function testResizeDeferredImageThrowsForMissingJson(): void
$deferredImage = $this->createMock(DeferredImageInterface::class);
$deferredImage
->method('getPath')
->willReturn($this->rootDir.'/foo.jpg')
->willReturn(Path::join($this->rootDir, '/foo.jpg'))
;

$this->expectException(FileNotExistsException::class);
Expand All @@ -259,7 +260,7 @@ public function testResizeDeferredImageThrowsForMissingImage(): void
->method('getLocked')
->with('foo.jpg', true)
->willReturn([
'path' => $this->rootDir.'/foo.jpg',
'path' => Path::join($this->rootDir, 'foo.jpg'),
'coordinates' => [
'size' => [
'width' => 100,
Expand Down Expand Up @@ -289,7 +290,7 @@ public function testResizeDeferredImageThrowsForMissingImage(): void
$deferredImage = $this->createMock(DeferredImageInterface::class);
$deferredImage
->method('getPath')
->willReturn($this->rootDir.'/foo.jpg')
->willReturn(Path::join($this->rootDir, 'foo.jpg'))
;

$this->expectException(FileNotExistsException::class);
Expand All @@ -312,7 +313,7 @@ public function testResizeDeferredImageDoesNotCatchStorageException(): void
$deferredImage = $this->createMock(DeferredImageInterface::class);
$deferredImage
->method('getPath')
->willReturn($this->rootDir.'/foo.jpg')
->willReturn(Path::join($this->rootDir, 'foo.jpg'))
;

$this->expectExceptionObject($storageException);
Expand All @@ -333,7 +334,7 @@ public function testResizeDeferredImageReturnsNullForLockedNonBlockingResize():
$deferredImage = $this->createMock(DeferredImageInterface::class);
$deferredImage
->method('getPath')
->willReturn($this->rootDir.'/foo.jpg')
->willReturn(Path::join($this->rootDir, 'foo.jpg'))
;

$this->assertNull($resizer->resizeDeferredImage($deferredImage, false));
Expand All @@ -352,7 +353,7 @@ public function testResizeDeferredImageThrowsForLockedBlockingResize(): void
$deferredImage = $this->createMock(DeferredImageInterface::class);
$deferredImage
->method('getPath')
->willReturn($this->rootDir.'/foo.jpg')
->willReturn(Path::join($this->rootDir, 'foo.jpg'))
;

$this->expectException(RuntimeException::class);
Expand All @@ -367,7 +368,7 @@ public function testResizeDeferredImageReleasesLockForFailedResize(): void
->method('getLocked')
->with('foo.jpg', true)
->willReturn([
'path' => $this->rootDir.'/foo.jpg',
'path' => Path::join($this->rootDir, 'foo.jpg'),
'coordinates' => [
'size' => [
'width' => 100,
Expand Down Expand Up @@ -397,7 +398,7 @@ public function testResizeDeferredImageReleasesLockForFailedResize(): void
$deferredImage = $this->createMock(DeferredImageInterface::class);
$deferredImage
->method('getPath')
->willReturn($this->rootDir.'/foo.jpg')
->willReturn(Path::join($this->rootDir, 'foo.jpg'))
;

$this->expectException(InvalidArgumentException::class);
Expand Down
Loading