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

add GaufretteFilesystemLoader #63

Merged
merged 6 commits into from
Apr 22, 2012
Merged
Show file tree
Hide file tree
Changes from 2 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
68 changes: 68 additions & 0 deletions Imagine/Data/Loader/StreamLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Liip\ImagineBundle\Imagine\Data\Loader;

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

use Imagine\Image\ImagineInterface;

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

/**
* The wrapper prefix to append to the path to be loaded.
*
* @var string
*/
protected $wrapperPrefix;

/**
* A stream context resource to use.
*
* @var \resource|null
Copy link
Contributor

Choose a reason for hiding this comment

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

resource is not a class but a PHP type so you should remove the \

*/
protected $context;

/**
* Constructor.
*
* @param \Imagine\Image\ImagineInterface $imagine
* @param string $wrapperPrefix
* @param \resource|null $context
*/
public function __construct(ImagineInterface $imagine, $wrapperPrefix, \resource $context = null)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you really typehint a resource ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, no you can't, fixed.

{
$this->imagine = $imagine;
$this->wrapperPrefix = $wrapperPrefix;
$this->context = $context;
}

/**
* @param string $path
*
* @return \Imagine\Image\ImageInterface
*/
public function find($path)
{
$name = $this->wrapperPrefix.$path;

/*
* This looks strange, but at least in PHP 5.3.8 it will raise an E_WARNING if the 4th parameter is null.
* fopen() will be called only once with the correct arguments.
*
* The error suppressing is solely to determ whether the file exists.
Copy link
Contributor

Choose a reason for hiding this comment

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

typo here

* file_exists() is not used as not all wrappers support stat() to actually check for existing resources.
*/
if (($this->context and @!fopen($name, 'r', null, $this->context)) || @!fopen($name, 'r')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

please use && instead of and

Copy link
Contributor

Choose a reason for hiding this comment

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

btw, you should close the stream you just opened to avoid keeping an eventual lock

throw new NotFoundHttpException('Source image not found.');
}

$image = $this->imagine->load(file_get_contents($name, null, $this->context));

return $image;
Copy link
Contributor

Choose a reason for hiding this comment

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

no need to create $image

}
}
5 changes: 3 additions & 2 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<!-- Data loaders' classes -->

<parameter key="liip_imagine.data.loader.filesystem.class">Liip\ImagineBundle\Imagine\Data\Loader\FileSystemLoader</parameter>
<parameter key="liip_imagine.data.loader.stream.class">Liip\ImagineBundle\Imagine\Data\Loader\StreamLoader</parameter>

<!-- Cache resolvers' classes -->

Expand All @@ -49,7 +50,7 @@
</parameters>

<services>

<!-- Utility services -->

<service id="liip_imagine.filter.manager" class="%liip_imagine.filter.manager.class%">
Expand Down Expand Up @@ -90,7 +91,7 @@
</service>

<!-- ImagineInterface instances -->

<service id="liip_imagine" alias="liip_imagine.gd" />

<service id="liip_imagine.gd" class="%liip_imagine.gd.class%" />
Expand Down