Skip to content

Commit

Permalink
Merge pull request #291 from formapro-forks/mime-type-guesser
Browse files Browse the repository at this point in the history
[1.0] Rework data loaders. Introduce mime type guesser.
  • Loading branch information
havvg committed Jan 9, 2014
2 parents 0b5200a + 76864e4 commit 99c407f
Show file tree
Hide file tree
Showing 28 changed files with 811 additions and 175 deletions.
20 changes: 20 additions & 0 deletions Binary/BinaryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace Liip\ImagineBundle\Binary;

interface BinaryInterface
{
/**
* @return string
*/
public function getContent();

/**
* @return string
*/
public function getMimeType();

/**
* @return string
*/
public function getFormat();
}
12 changes: 12 additions & 0 deletions Binary/MimeTypeGuesserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
namespace Liip\ImagineBundle\Binary;

interface MimeTypeGuesserInterface
{
/**
* @param string $binary The image binary
*
* @return string|null mime type or null if it could be not be guessed.
*/
function guess($binary);
}
42 changes: 42 additions & 0 deletions Binary/SimpleMimeTypeGuesser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
namespace Liip\ImagineBundle\Binary;

use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface as SymfonyMimeTypeGuesserInterface;

class SimpleMimeTypeGuesser implements MimeTypeGuesserInterface
{
/**
* @var \Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface
*/
protected $mimeTypeGuesser;

/**
* @param SymfonyMimeTypeGuesserInterface $mimeTypeGuesser
*/
public function __construct(SymfonyMimeTypeGuesserInterface $mimeTypeGuesser)
{
$this->mimeTypeGuesser = $mimeTypeGuesser;
}

/**
* {@inheritDoc}
*/
public function guess($binary)
{
$tmpFile = tempnam(sys_get_temp_dir(), 'liip-imagine-bundle');

try {
file_put_contents($tmpFile, $binary);

$mimeType = $this->mimeTypeGuesser->guess($tmpFile);

unlink($tmpFile);

return $mimeType;
} catch (\Exception $e) {
unlink($tmpFile);

throw $e;
}
}
}
15 changes: 11 additions & 4 deletions Controller/ImagineController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Liip\ImagineBundle\Controller;

use Imagine\Image\ImagineInterface;
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
use Liip\ImagineBundle\Imagine\Data\DataManager;
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
Expand All @@ -28,17 +29,22 @@ class ImagineController
protected $cacheManager;

/**
* Constructor.
*
* @var ImagineInterface
*/
protected $imagine;

/**
* @param DataManager $dataManager
* @param FilterManager $filterManager
* @param CacheManager $cacheManager
* @param ImagineInterface $imagine
*/
public function __construct(DataManager $dataManager, FilterManager $filterManager, CacheManager $cacheManager)
public function __construct(DataManager $dataManager, FilterManager $filterManager, CacheManager $cacheManager, ImagineInterface $imagine)
{
$this->dataManager = $dataManager;
$this->filterManager = $filterManager;
$this->cacheManager = $cacheManager;
$this->imagine = $imagine;
}

/**
Expand All @@ -56,7 +62,8 @@ public function filterAction(Request $request, $path, $filter)
return new RedirectResponse($this->cacheManager->resolve($path, $filter), 301);
}

$image = $this->dataManager->find($filter, $path);
$binary = $this->dataManager->find($filter, $path);
$image = $this->imagine->load($binary->getContent());

$response = $this->filterManager->get($request, $filter, $image, $path);

Expand Down
57 changes: 49 additions & 8 deletions Imagine/Data/DataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,23 @@

use Liip\ImagineBundle\Imagine\Data\Loader\LoaderInterface;
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
use Liip\ImagineBundle\Binary\MimeTypeGuesserInterface;
use Liip\ImagineBundle\Model\Binary;
use Liip\ImagineBundle\Binary\BinaryInterface;
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface;

class DataManager
{
/**
* @var MimeTypeGuesserInterface
*/
protected $mimeTypeGuesser;

/**
* @var ExtensionGuesserInterface
*/
protected $extensionGuesser;

/**
* @var FilterConfiguration
*/
Expand All @@ -23,15 +37,21 @@ class DataManager
protected $loaders = array();

/**
* Constructor.
*
* @param FilterConfiguration $filterConfig
* @param string $defaultLoader
* @param MimeTypeGuesserInterface $mimeTypeGuesser
* @param ExtensionGuesserInterface $extensionGuesser
* @param FilterConfiguration $filterConfig
* @param string $defaultLoader
*/
public function __construct(FilterConfiguration $filterConfig, $defaultLoader = null)
{
public function __construct(
MimeTypeGuesserInterface $mimeTypeGuesser,
ExtensionGuesserInterface $extensionGuesser,
FilterConfiguration $filterConfig,
$defaultLoader = null
) {
$this->mimeTypeGuesser = $mimeTypeGuesser;
$this->filterConfig = $filterConfig;
$this->defaultLoader = $defaultLoader;
$this->extensionGuesser = $extensionGuesser;
}

/**
Expand Down Expand Up @@ -78,12 +98,33 @@ public function getLoader($filter)
* @param string $filter
* @param string $path
*
* @return \Imagine\Image\ImageInterface
* @throws \LogicException
*
* @return \Liip\ImagineBundle\Binary\BinaryInterface
*/
public function find($filter, $path)
{
$loader = $this->getLoader($filter);

return $loader->find($path);
$binary = $loader->find($path);
if (!$binary instanceof BinaryInterface) {
$mimeType = $this->mimeTypeGuesser->guess($binary);

$binary = new Binary(
$binary,
$mimeType,
$this->extensionGuesser->guess($mimeType)
);
}

if (null === $binary->getMimeType()) {
throw new \LogicException(sprintf('The mime type of image %s was not guessed.', $path));
}

if (0 !== strpos($binary->getMimeType(), 'image/')) {
throw new \LogicException(sprintf('The mime type of image %s must be image/xxx got %s.', $path, $binary->getMimeType()));
}

return $binary;
}
}
17 changes: 3 additions & 14 deletions Imagine/Data/Loader/AbstractDoctrineLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@

abstract class AbstractDoctrineLoader implements LoaderInterface
{
/**
* @var ImagineInterface
*/
protected $imagine;

/**
* @var ObjectManager
*/
Expand All @@ -24,15 +19,11 @@ abstract class AbstractDoctrineLoader implements LoaderInterface
protected $class;

/**
* Constructor.
*
* @param ImagineInterface $imagine
* @param ObjectManager $manager
* @param string $class
*/
public function __construct(ImagineInterface $imagine, ObjectManager $manager, $class = null)
public function __construct(ObjectManager $manager, $class = null)
{
$this->imagine = $imagine;
$this->manager = $manager;
$this->class = $class;
}
Expand All @@ -56,9 +47,7 @@ abstract protected function mapPathToId($path);
abstract protected function getStreamFromImage($image);

/**
* @param string $path
*
* @return \Imagine\Image\ImageInterface
* {@inheritDoc}
*/
public function find($path)
{
Expand All @@ -68,6 +57,6 @@ public function find($path)
throw new NotFoundHttpException(sprintf('Source image not found with id "%s"', $path));
}

return $this->imagine->load(stream_get_contents($this->getStreamFromImage($image)));
return stream_get_contents($this->getStreamFromImage($image));
}
}
7 changes: 2 additions & 5 deletions Imagine/Data/Loader/ExtendedFileSystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@ class ExtendedFileSystemLoader extends FileSystemLoader
protected $transformers;

/**
* Constructor.
*
* @param ImagineInterface $imagine
* @param array $formats
* @param string $rootPath
* @param TransformerInterface[] $transformers
*/
public function __construct(ImagineInterface $imagine, $formats, $rootPath, array $transformers)
public function __construct($formats, $rootPath, array $transformers)
{
parent::__construct($imagine, $formats, $rootPath);
parent::__construct($formats, $rootPath);

$this->transformers = $transformers;
}
Expand Down
42 changes: 30 additions & 12 deletions Imagine/Data/Loader/FileSystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

namespace Liip\ImagineBundle\Imagine\Data\Loader;

use Imagine\Image\ImagineInterface;
use Liip\ImagineBundle\Model\Binary;
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class FileSystemLoader implements LoaderInterface
{
/**
* @var ImagineInterface
* @var MimeTypeGuesserInterface
*/
protected $imagine;
protected $mimeTypeGuesser;

/**
* @var ExtensionGuesserInterface
*/
protected $extensionGuesser;

/**
* @var array
Expand All @@ -23,17 +31,21 @@ class FileSystemLoader implements LoaderInterface
protected $rootPath;

/**
* Constructor.
*
* @param ImagineInterface $imagine
* @param array $formats
* @param string $rootPath
* @param MimeTypeGuesserInterface $mimeTypeGuesser
* @param ExtensionGuesserInterface $extensionGuesser
* @param array $formats
* @param string $rootPath
*/
public function __construct(ImagineInterface $imagine, array $formats, $rootPath)
{
$this->imagine = $imagine;
public function __construct(
MimeTypeGuesserInterface $mimeTypeGuesser,
ExtensionGuesserInterface $extensionGuesser,
array $formats,
$rootPath
){
$this->formats = $formats;
$this->rootPath = realpath($rootPath);
$this->mimeTypeGuesser = $mimeTypeGuesser;
$this->extensionGuesser = $extensionGuesser;
}

/**
Expand Down Expand Up @@ -93,6 +105,12 @@ public function find($path)
}
}

return $this->imagine->open($absolutePath);
$mimeType = $this->mimeTypeGuesser->guess($absolutePath);

return new Binary(
file_get_contents($absolutePath),
$mimeType,
$this->extensionGuesser->guess($mimeType)
);
}
}
14 changes: 2 additions & 12 deletions Imagine/Data/Loader/GridFSLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@

namespace Liip\ImagineBundle\Imagine\Data\Loader;

use Imagine\Image\ImagineInterface;
use Doctrine\ODM\MongoDB\DocumentManager;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class GridFSLoader implements LoaderInterface
{
/**
* @var ImagineInterface
*/
protected $imagine;

/**
* @var DocumentManager
*/
Expand All @@ -24,15 +18,11 @@ class GridFSLoader implements LoaderInterface
protected $class;

/**
* Constructs
*
* @param ImagineInterface $imagine
* @param DocumentManager $dm
* @param string $class
*/
public function __construct(ImagineInterface $imagine, DocumentManager $dm, $class)
public function __construct(DocumentManager $dm, $class)
{
$this->imagine = $imagine;
$this->dm = $dm;
$this->class = $class;
}
Expand All @@ -52,6 +42,6 @@ public function find($id)
throw new NotFoundHttpException(sprintf('Source image not found with id "%s"', $id));
}

return $this->imagine->load($image['file']->getBytes());
return $image['file']->getBytes();
}
}
2 changes: 1 addition & 1 deletion Imagine/Data/Loader/LoaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface LoaderInterface
*
* @param mixed $path
*
* @return \Imagine\Image\ImageInterface
* @return \Liip\ImagineBundle\Binary\BinaryInterface|string An image binary content
*/
function find($path);
}
Loading

0 comments on commit 99c407f

Please sign in to comment.