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

handle image extensions in doctrine loader #276

Merged
merged 1 commit into from
Dec 4, 2013
Merged
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
33 changes: 31 additions & 2 deletions Imagine/Data/Loader/AbstractDoctrineLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,29 @@ abstract class AbstractDoctrineLoader implements LoaderInterface
*/
protected $class;

/**
* @var array
*/
protected $formats;

/**
* Constructor.
*
* @param ImagineInterface $imagine
* @param ObjectManager $manager
* @param string $class
* @param array $formats possible image formats to look up file ids
*/
public function __construct(ImagineInterface $imagine, ObjectManager $manager, $class = null)
{
public function __construct(
ImagineInterface $imagine,
ObjectManager $manager,
$class = null,
array $formats = array()
) {
$this->imagine = $imagine;
$this->manager = $manager;
$this->class = $class;
$this->formats = $formats;
}

/**
Expand Down Expand Up @@ -64,6 +75,24 @@ public function find($path)
{
$image = $this->manager->find($this->class, $this->mapPathToId($path));

if (!$image) {
// try to find the image without extension
$info = pathinfo($path);
$name = $info['dirname'].'/'.$info['filename'];

Copy link
Contributor

Choose a reason for hiding this comment

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

extra new line

// attempt to determine available format
foreach ($this->formats as $format) {
if ($image = $this->manager->find($this->class, $this->mapPathToId($name.'.'.$format))) {
break;
}
}

// maybe the image has an id without extension
if (!$image) {
$image = $this->manager->find($this->class, $this->mapPathToId($name));
}
}

if (!$image) {
throw new NotFoundHttpException(sprintf('Source image not found with id "%s"', $path));
}
Expand Down