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

[RFC] Allow to set a base dir for Resizer to remove from hash #32

Merged
merged 6 commits into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 15 additions & 3 deletions src/Resizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,20 @@ class Resizer implements ResizerInterface
*/
private $cacheDir;

/**
* @var string
*/
private $baseDir;

/**
* Constructor.
*
* @param string $cacheDir
* @param string $baseDir
* @param ResizeCalculatorInterface|null $calculator
* @param Filesystem|null $filesystem
*/
public function __construct($cacheDir, ResizeCalculatorInterface $calculator = null, Filesystem $filesystem = null)
public function __construct($cacheDir, $baseDir = null, ResizeCalculatorInterface $calculator = null, Filesystem $filesystem = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you not add it as last argument? Would save a lot of adjustments below.

{
if (null === $calculator) {
$calculator = new ResizeCalculator();
Expand All @@ -53,6 +59,7 @@ public function __construct($cacheDir, ResizeCalculatorInterface $calculator = n
}

$this->cacheDir = (string) $cacheDir;
$this->baseDir = $baseDir;
$this->calculator = $calculator;
$this->filesystem = $filesystem;
}
Expand Down Expand Up @@ -165,16 +172,21 @@ protected function createImage(ImageInterface $image, $path)
* @param ResizeCoordinatesInterface $coordinates
* @param ResizeOptionsInterface $options
*
* @return string The realtive target path
* @return string The relative target path
*/
private function createCachePath($path, ResizeCoordinatesInterface $coordinates, ResizeOptionsInterface $options)
{
$pathinfo = pathinfo($path);
$imagineOptions = $options->getImagineOptions();
ksort($imagineOptions);

$hashPath = $path;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be $relPath.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no $baseDir the path would not be relative, wouldn’t $relPath be misleading then?

if (null !== $this->baseDir) {
$hashPath = $this->filesystem->makePathRelative($path, $this->baseDir);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use Webmozart\PathUtil\Path here instead. As in src/Image.php:108

}

$hash = substr(md5(implode('|', array_merge(
[$path, filemtime($path), $coordinates->getHash()],
[$hashPath, filemtime($path), $coordinates->getHash()],
array_keys($imagineOptions),
array_values($imagineOptions)
))), 0, 9);
Expand Down
15 changes: 8 additions & 7 deletions tests/ResizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function testResize()
->willReturn(new ResizeCoordinates(new Box(100, 100), new Point(0, 0), new Box(100, 100)))
;

$resizer = $this->createResizer(null, $calculator);
$resizer = $this->createResizer(null, null, $calculator);

if (!is_dir($this->rootDir)) {
mkdir($this->rootDir, 0777, true);
Expand Down Expand Up @@ -172,7 +172,7 @@ public function testResizeSvg()
->willReturn(new ResizeCoordinates(new Box(100, 100), new Point(0, 0), new Box(100, 100)))
;

$resizer = $this->createResizer(null, $calculator);
$resizer = $this->createResizer(null, null, $calculator);

$image = $this
->getMockBuilder('Contao\Image\Image')
Expand Down Expand Up @@ -235,7 +235,7 @@ public function testResizeCache()
->willReturn(new ResizeCoordinates(new Box(100, 100), new Point(0, 0), new Box(100, 100)))
;

$resizer = $this->createResizer(null, $calculator);
$resizer = $this->createResizer(null, null, $calculator);

if (!is_dir($this->rootDir)) {
mkdir($this->rootDir, 0777, true);
Expand Down Expand Up @@ -411,7 +411,7 @@ public function testResizeSameDimensions()
->willReturn(new ResizeCoordinates(new Box(100, 100), new Point(0, 0), new Box(100, 100)))
;

$resizer = $this->createResizer(null, $calculator);
$resizer = $this->createResizer(null, null, $calculator);

if (!is_dir($this->rootDir)) {
mkdir($this->rootDir, 0777, true);
Expand Down Expand Up @@ -480,7 +480,7 @@ public function testResizeSameDimensionsRelative()
->willReturn(new ResizeCoordinates(new Box(100, 100), new Point(0, 0), new Box(100, 100)))
;

$resizer = $this->createResizer(null, $calculator);
$resizer = $this->createResizer(null, null, $calculator);

$image = $this
->getMockBuilder('Contao\Image\Image')
Expand Down Expand Up @@ -518,17 +518,18 @@ public function testResizeSameDimensionsRelative()
* Creates a resizer instance helper.
*
* @param string $cacheDir
* @param string $baseDir
* @param ResizeCalculatorInterface $calculator
* @param Filesystem $filesystem
*
* @return Resizer
*/
private function createResizer($cacheDir = null, $calculator = null, $filesystem = null)
private function createResizer($cacheDir = null, $baseDir = null, $calculator = null, $filesystem = null)
{
if (null === $cacheDir) {
$cacheDir = $this->rootDir;
}

return new Resizer($cacheDir, $calculator, $filesystem);
return new Resizer($cacheDir, $baseDir, $calculator, $filesystem);
}
}