Skip to content

Commit

Permalink
Merge pull request #444 from formapro-forks/stream-loader-throws-when…
Browse files Browse the repository at this point in the history
…-content-cannot-be-read

[stream] throws exception when content cannot be read.
  • Loading branch information
makasim committed Jun 6, 2014
2 parents 006a641 + b2e579a commit 0a39eb7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
14 changes: 12 additions & 2 deletions Binary/Loader/StreamLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,22 @@ public function find($path)
* file_exists() is not used as not all wrappers support stat() to actually check for existing resources.
*/
if (($this->context && !$resource = @fopen($name, 'r', null, $this->context)) || !$resource = @fopen($name, 'r')) {
throw new NotLoadableException('Source image not found.');
throw new NotLoadableException(sprintf('Source image %s not found.', $name));
}

// Closing the opened stream to avoid locking of the resource to find.
fclose($resource);

return file_get_contents($name, null, $this->context);
try {
$content = file_get_contents($name, null, $this->context);
} catch (\Exception $e) {
throw new NotLoadableException(sprintf('Source image %s could not be loaded.', $name, $e));
}

if (false === $content) {
throw new NotLoadableException(sprintf('Source image %s could not be loaded.', $name));
}

return $content;
}
}
8 changes: 6 additions & 2 deletions Tests/Binary/Loader/StreamLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ public function testThrowsIfInvalidPathGivenOnFind()
{
$loader = new StreamLoader('file://');

$path = $this->tempDir.'/invalid.jpeg';

$this->setExpectedException(
'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException'
'Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException',
'Source image file://'.$path.' not found.'
);
$loader->find($this->tempDir.'/invalid.jpeg');

$loader->find($path);
}

public function testReturnImageContentOnFind()
Expand Down

0 comments on commit 0a39eb7

Please sign in to comment.