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

Cleanup FileSystemLoader (Followup to #775) #785

Merged
merged 1 commit into from
Sep 6, 2016
Merged
Show file tree
Hide file tree
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
8 changes: 2 additions & 6 deletions Binary/Loader/FileSystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(
$this->mimeTypeGuesser = $mimeTypeGuesser;
$this->extensionGuesser = $extensionGuesser;

if (!($realRootPath = realpath($rootPath))) {
if (empty($rootPath) || !($realRootPath = realpath($rootPath))) {
throw new InvalidArgumentException(sprintf('Root image path not resolvable "%s"', $rootPath));
}

Expand All @@ -50,18 +50,14 @@ public function __construct(
*/
public function find($path)
{
if (!($absolutePath = realpath($this->rootPath.DIRECTORY_SEPARATOR.ltrim($path, DIRECTORY_SEPARATOR)))) {
if (!($absolutePath = realpath($this->rootPath.DIRECTORY_SEPARATOR.$path))) {
throw new NotLoadableException(sprintf('Source image not resolvable "%s"', $path));
}

if (0 !== strpos($absolutePath, $this->rootPath)) {
throw new NotLoadableException(sprintf('Source image invalid "%s" as it is outside of the defined root path', $absolutePath));
}

if (false == file_exists($absolutePath)) {
throw new NotLoadableException(sprintf('Source image not found in "%s"', $absolutePath));
}

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

return new FileBinary(
Expand Down
14 changes: 14 additions & 0 deletions Tests/Binary/Loader/FileSystemLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ public function testCouldBeConstructedWithExpectedArguments()
);
}

public function testThrowExceptionIfRootPathIsEmpty()
{
$this->setExpectedException(
'Liip\ImagineBundle\Exception\InvalidArgumentException',
'Root image path not resolvable'
);

new FileSystemLoader(
MimeTypeGuesser::getInstance(),
ExtensionGuesser::getInstance(),
''
);
}

public function testThrowExceptionIfRootPathDoesNotExist()
{
$this->setExpectedException(
Expand Down