Skip to content

Commit

Permalink
Merge pull request #169 from liip/phpcr_loader_tweaks
Browse files Browse the repository at this point in the history
made the phpcr loader search for the requested path with or without a file extension
  • Loading branch information
lsmith77 committed Apr 4, 2013
2 parents 0e88d21 + a711eff commit 6153c82
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
63 changes: 50 additions & 13 deletions Imagine/Data/Loader/DoctrinePHPCRLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,75 @@

namespace Liip\ImagineBundle\Imagine\Data\Loader;

use Doctrine\ODM\PHPCR\DocumentManager;
use Imagine\Image\ImagineInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class DoctrinePHPCRLoader extends AbstractDoctrineLoader
class DoctrinePHPCRLoader extends FileSystemLoader
{
/**
* @var DocumentManager
*/
protected $manager;

/**
* @var string
*/
protected $rootPath;
protected $class;

/**
* Constructor.
*
* @param ImagineInterface $imagine
* @param ObjectManager $manager
* @param string $class
* @param string $rootPath
* @param ImagineInterface $imagine
* @param array $formats
* @param string $rootPath
* @param DocumentManager $manager
* @param string $class
*/
public function __construct(ImagineInterface $imagine, ObjectManager $manager, $class = null, $rootPath = '')
public function __construct(ImagineInterface $imagine, array $formats, $rootPath, DocumentManager $manager, $class = null)
{
parent::__construct($imagine, $manager, $class);
parent::__construct($imagine, $formats, $rootPath);

$this->manager = $manager;
$this->class = $class;
$this->rootPath = $rootPath;
}

protected function mapPathToId($path)
protected function getStreamFromImage($image)
{
return $this->rootPath.'/'.ltrim($path, '/');
return $image->getContent();
}

protected function getStreamFromImage($image)
/**
* @param string $path
*
* @return \Imagine\Image\ImageInterface
*/
public function find($path)
{
return $image->getContent();
$file = $this->rootPath.'/'.ltrim($path, '/');
$info = $this->getFileInfo($file);
$name = $info['dirname'].'/'.$info['filename'];

// consider full path as provided (with or without an extension)
$paths = array($file);
foreach ($this->formats as $format) {
// consider all possible alternative extensions
if (empty($info['extension']) || $info['extension'] !== $format) {
$paths[] = $name.'.'.$format;
}
}

// if the full path contained an extension, also consider the full path without an extension
if ($file !== $name) {
$paths[] = $name;
}

$images = $this->manager->findMany($this->class, $paths);
if (!$images->count()) {
throw new NotFoundHttpException(sprintf('Source image not found with id "%s"', $path));
}

return $this->imagine->load(stream_get_contents($this->getStreamFromImage($images->first())));
}
}
7 changes: 4 additions & 3 deletions Resources/doc/data-loader/doctrine-phpcr.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ service definition:

``` xml
<service id="liip_imagine.data.loader.phpcr" class="Liip\ImagineBundle\Imagine\Data\Loader\DoctrinePHPCRLoader">
<tag name="liip_imagine.data.loader" loader="phpcr" />
<tag name="liip_imagine.data.loader" loader="doctrine_phpcr" />
<argument type="service" id="liip_imagine" />
<argument>%liip_imagine.formats%</argument>
<argument>%symfony_cmf_create.static_basepath%</argument>
<argument type="service" id="doctrine_phpcr.odm.document_manager" />
<argument>%symfony_cmf_create.image.model_class%</argument>
<argument>%symfony_cmf_create.static_basepath%</argument>
</service>
```

Note this loader extends from AbstractDoctrineLoader. It is quite easy to extend this abstract class
Note there is an AbstractDoctrineLoader. It is quite easy to extend this abstract class
to create a new Doctrine loader for the ORM or another ODM.

- [Back to data loaders](../data-loaders.md)
Expand Down

0 comments on commit 6153c82

Please sign in to comment.