Skip to content

Commit

Permalink
Use SimplyMimeTypeGuesser to guess MIME type of filtered content
Browse files Browse the repository at this point in the history
  • Loading branch information
teohhanhui committed Aug 20, 2014
1 parent ce72038 commit cf98b7d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 42 deletions.
23 changes: 9 additions & 14 deletions Imagine/Filter/FilterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Liip\ImagineBundle\Imagine\Filter;

use Dflydev\ApacheMimeTypes\RepositoryInterface;
use Imagine\Image\ImagineInterface;
use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Binary\MimeTypeGuesserInterface;
use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface;
use Liip\ImagineBundle\Model\Binary;

Expand All @@ -21,28 +21,28 @@ class FilterManager
protected $imagine;

/**
* @var RepositoryInterface
* @var MimeTypeGuesserInterface
*/
protected $mimeTypeRepository;
protected $mimeTypeGuesser;

/**
* @var LoaderInterface[]
*/
protected $loaders = array();

/**
* @param FilterConfiguration $filterConfig
* @param ImagineInterface $imagine
* @param RepositoryInterface $mimeTypeRepository
* @param FilterConfiguration $filterConfig
* @param ImagineInterface $imagine
* @param MimeTypeGuesserInterface $mimeTypeGuesser
*/
public function __construct(
FilterConfiguration $filterConfig,
ImagineInterface $imagine,
RepositoryInterface $mimeTypeRepository
MimeTypeGuesserInterface $mimeTypeGuesser
) {
$this->filterConfig = $filterConfig;
$this->imagine = $imagine;
$this->mimeTypeRepository = $mimeTypeRepository;
$this->mimeTypeGuesser = $mimeTypeGuesser;
}

/**
Expand Down Expand Up @@ -106,13 +106,8 @@ public function apply(BinaryInterface $binary, array $config)
}

$filteredFormat = isset($config['format']) ? $config['format'] : $binary->getFormat();
$filteredMimeType = $this->mimeTypeRepository->findType($filteredFormat);
if (!isset($filteredMimeType)) {
throw new \RuntimeException(sprintf(
'Could not determine MIME type for "%s" format', $filteredFormat
));
}
$filteredContent = $image->get($filteredFormat, $options);
$filteredMimeType = $filteredFormat === $binary->getFormat() ? $binary->getMimeType() : $this->mimeTypeGuesser->guess($filteredContent);

return new Binary($filteredContent, $filteredMimeType, $filteredFormat);
}
Expand Down
7 changes: 1 addition & 6 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<service id="liip_imagine.filter.manager" class="%liip_imagine.filter.manager.class%">
<argument type="service" id="liip_imagine.filter.configuration" />
<argument type="service" id="liip_imagine" />
<argument type="service" id="liip_imagine.mime_type_repository" />
<argument type="service" id="liip_imagine.binary.mime_type_guesser" />
</service>

<service id="liip_imagine.data.manager" class="%liip_imagine.data.manager.class%">
Expand Down Expand Up @@ -241,11 +241,6 @@
<argument type="service" id="liip_imagine.mime_type_guesser" />
</service>

<service
id="liip_imagine.mime_type_repository"
class="Dflydev\ApacheMimeTypes\PhpRepository"
/>

<service id="liip_imagine.cache.signer" class="%liip_imagine.cache.signer.class%">
<argument>%kernel.secret%</argument>
</service>
Expand Down
77 changes: 57 additions & 20 deletions Tests/Imagine/Filter/FilterManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Liip\ImagineBundle\Tests\Filter;

use Dflydev\ApacheMimeTypes;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface;
use Liip\ImagineBundle\Model\Binary;
Expand Down Expand Up @@ -35,7 +34,7 @@ public function testThrowsIfNoLoadersAddedForFilterOnApplyFilter()
$filterManager = new FilterManager(
$config,
$this->createImagineMock(),
new ApacheMimeTypes\PhpRepository()
$this->getMockMimeTypeGuesser()
);

$this->setExpectedException('InvalidArgumentException', 'Could not find filter loader for "thumbnail" filter type');
Expand Down Expand Up @@ -92,7 +91,7 @@ public function testReturnFilteredBinaryWithExpectedContentOnApplyFilter()
$filterManager = new FilterManager(
$config,
$imagine,
new ApacheMimeTypes\PhpRepository()
$this->getMockMimeTypeGuesser()
);
$filterManager->addLoader('thumbnail', $loader);

Expand Down Expand Up @@ -151,7 +150,7 @@ public function testReturnFilteredBinaryWithFormatOfOriginalBinaryOnApplyFilter(
$filterManager = new FilterManager(
$config,
$imagine,
new ApacheMimeTypes\PhpRepository()
$this->getMockMimeTypeGuesser()
);
$filterManager->addLoader('thumbnail', $loader);

Expand Down Expand Up @@ -212,7 +211,7 @@ public function testReturnFilteredBinaryWithCustomFormatIfSetOnApplyFilter()
$filterManager = new FilterManager(
$config,
$imagine,
new ApacheMimeTypes\PhpRepository()
$this->getMockMimeTypeGuesser()
);
$filterManager->addLoader('thumbnail', $loader);

Expand Down Expand Up @@ -260,6 +259,12 @@ public function testReturnFilteredBinaryWithMimeTypeOfOriginalBinaryOnApplyFilte
->will($this->returnValue($image))
;

$mimeTypeGuesser = $this->getMockMimeTypeGuesser();
$mimeTypeGuesser
->expects($this->never())
->method('guess')
;

$loader = $this->getMockLoader();
$loader
->expects($this->once())
Expand All @@ -271,7 +276,7 @@ public function testReturnFilteredBinaryWithMimeTypeOfOriginalBinaryOnApplyFilte
$filterManager = new FilterManager(
$config,
$imagine,
new ApacheMimeTypes\PhpRepository()
$mimeTypeGuesser
);
$filterManager->addLoader('thumbnail', $loader);

Expand All @@ -285,6 +290,7 @@ public function testReturnFilteredBinaryWithMimeTypeOfCustomFormatIfSetOnApplyFi
{
$originalContent = 'aOriginalContent';
$originalMimeType = 'image/png';
$expectedContent = 'aFilteredContent';
$expectedMimeType = 'image/jpeg';

$binary = new Binary($originalContent, $originalMimeType, 'png');
Expand All @@ -311,7 +317,7 @@ public function testReturnFilteredBinaryWithMimeTypeOfCustomFormatIfSetOnApplyFi
$image
->expects($this->once())
->method('get')
->will($this->returnValue('aFilteredContent'))
->will($this->returnValue($expectedContent))
;

$imagine = $this->createImagineMock();
Expand All @@ -321,6 +327,14 @@ public function testReturnFilteredBinaryWithMimeTypeOfCustomFormatIfSetOnApplyFi
->will($this->returnValue($image))
;

$mimeTypeGuesser = $this->getMockMimeTypeGuesser();
$mimeTypeGuesser
->expects($this->once())
->method('guess')
->with($expectedContent)
->will($this->returnValue($expectedMimeType))
;

$loader = $this->getMockLoader();
$loader
->expects($this->once())
Expand All @@ -332,7 +346,7 @@ public function testReturnFilteredBinaryWithMimeTypeOfCustomFormatIfSetOnApplyFi
$filterManager = new FilterManager(
$config,
$imagine,
new ApacheMimeTypes\PhpRepository()
$mimeTypeGuesser
);
$filterManager->addLoader('thumbnail', $loader);

Expand Down Expand Up @@ -393,7 +407,7 @@ public function testAltersQualityOnApplyFilter()
$filterManager = new FilterManager(
$config,
$imagine,
new ApacheMimeTypes\PhpRepository()
$this->getMockMimeTypeGuesser()
);
$filterManager->addLoader('thumbnail', $loader);

Expand Down Expand Up @@ -450,7 +464,7 @@ public function testAlters100QualityIfNotSetOnApplyFilter()
$filterManager = new FilterManager(
$config,
$imagine,
new ApacheMimeTypes\PhpRepository()
$this->getMockMimeTypeGuesser()
);
$filterManager->addLoader('thumbnail', $loader);

Expand Down Expand Up @@ -516,7 +530,7 @@ public function testMergeRuntimeConfigWithOneFromFilterConfigurationOnApplyFilte
$filterManager = new FilterManager(
$config,
$imagine,
new ApacheMimeTypes\PhpRepository()
$this->getMockMimeTypeGuesser()
);
$filterManager->addLoader('thumbnail', $loader);

Expand All @@ -533,7 +547,7 @@ public function testThrowsIfNoLoadersAddedForFilterOnApply()
$filterManager = new FilterManager(
$this->createFilterConfigurationMock(),
$this->createImagineMock(),
new ApacheMimeTypes\PhpRepository()
$this->getMockMimeTypeGuesser()
);

$this->setExpectedException('InvalidArgumentException', 'Could not find filter loader for "thumbnail" filter type');
Expand Down Expand Up @@ -585,7 +599,7 @@ public function testReturnFilteredBinaryWithExpectedContentOnApply()
$filterManager = new FilterManager(
$this->createFilterConfigurationMock(),
$imagineMock,
new ApacheMimeTypes\PhpRepository()
$this->getMockMimeTypeGuesser()
);
$filterManager->addLoader('thumbnail', $loader);

Expand Down Expand Up @@ -636,7 +650,7 @@ public function testReturnFilteredBinaryWithFormatOfOriginalBinaryOnApply()
$filterManager = new FilterManager(
$this->createFilterConfigurationMock(),
$imagineMock,
new ApacheMimeTypes\PhpRepository()
$this->getMockMimeTypeGuesser()
);
$filterManager->addLoader('thumbnail', $loader);

Expand Down Expand Up @@ -688,7 +702,7 @@ public function testReturnFilteredBinaryWithCustomFormatIfSetOnApply()
$filterManager = new FilterManager(
$this->createFilterConfigurationMock(),
$imagineMock,
new ApacheMimeTypes\PhpRepository()
$this->getMockMimeTypeGuesser()
);
$filterManager->addLoader('thumbnail', $loader);

Expand Down Expand Up @@ -729,6 +743,12 @@ public function testReturnFilteredBinaryWithMimeTypeOfOriginalBinaryOnApply()
->will($this->returnValue($image))
;

$mimeTypeGuesser = $this->getMockMimeTypeGuesser();
$mimeTypeGuesser
->expects($this->never())
->method('guess')
;

$loader = $this->getMockLoader();
$loader
->expects($this->once())
Expand All @@ -740,7 +760,7 @@ public function testReturnFilteredBinaryWithMimeTypeOfOriginalBinaryOnApply()
$filterManager = new FilterManager(
$this->createFilterConfigurationMock(),
$imagineMock,
new ApacheMimeTypes\PhpRepository()
$mimeTypeGuesser
);
$filterManager->addLoader('thumbnail', $loader);

Expand All @@ -758,6 +778,7 @@ public function testReturnFilteredBinaryWithMimeTypeOfCustomFormatIfSetOnApply()
{
$originalContent = 'aOriginalContent';
$originalMimeType = 'image/png';
$expectedContent = 'aFilteredContent';
$expectedMimeType = 'image/jpeg';

$binary = new Binary($originalContent, $originalMimeType, 'png');
Expand All @@ -771,7 +792,7 @@ public function testReturnFilteredBinaryWithMimeTypeOfCustomFormatIfSetOnApply()
$image
->expects($this->once())
->method('get')
->will($this->returnValue('aFilteredContent'))
->will($this->returnValue($expectedContent))
;

$imagineMock = $this->createImagineMock();
Expand All @@ -781,6 +802,14 @@ public function testReturnFilteredBinaryWithMimeTypeOfCustomFormatIfSetOnApply()
->will($this->returnValue($image))
;

$mimeTypeGuesser = $this->getMockMimeTypeGuesser();
$mimeTypeGuesser
->expects($this->once())
->method('guess')
->with($expectedContent)
->will($this->returnValue($expectedMimeType))
;

$loader = $this->getMockLoader();
$loader
->expects($this->once())
Expand All @@ -792,7 +821,7 @@ public function testReturnFilteredBinaryWithMimeTypeOfCustomFormatIfSetOnApply()
$filterManager = new FilterManager(
$this->createFilterConfigurationMock(),
$imagineMock,
new ApacheMimeTypes\PhpRepository()
$mimeTypeGuesser
);
$filterManager->addLoader('thumbnail', $loader);

Expand Down Expand Up @@ -845,7 +874,7 @@ public function testAltersQualityOnApply()
$filterManager = new FilterManager(
$this->createFilterConfigurationMock(),
$imagineMock,
new ApacheMimeTypes\PhpRepository()
$this->getMockMimeTypeGuesser()
);
$filterManager->addLoader('thumbnail', $loader);

Expand Down Expand Up @@ -897,7 +926,7 @@ public function testAlters100QualityIfNotSetOnApply()
$filterManager = new FilterManager(
$this->createFilterConfigurationMock(),
$imagineMock,
new ApacheMimeTypes\PhpRepository()
$this->getMockMimeTypeGuesser()
);
$filterManager->addLoader('thumbnail', $loader);

Expand All @@ -917,4 +946,12 @@ protected function getMockLoader()
{
return $this->getMock('Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface');
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject|\Liip\ImagineBundle\Binary\MimeTypeGuesserInterface
*/
protected function getMockMimeTypeGuesser()
{
return $this->getMock('Liip\ImagineBundle\Binary\MimeTypeGuesserInterface');
}
}
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
"symfony/finder": "~2.3",
"symfony/filesystem": "~2.3",
"symfony/options-resolver": "~2.3",
"symfony/framework-bundle": "~2.3",
"dflydev/apache-mime-types": "~1.0"
"symfony/framework-bundle": "~2.3"
},

"require-dev": {
Expand Down

0 comments on commit cf98b7d

Please sign in to comment.