Skip to content

Commit

Permalink
merged 1.0 branch into 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
robfrawley committed Apr 6, 2017
2 parents be47331 + 07d7ded commit f3e24cb
Show file tree
Hide file tree
Showing 108 changed files with 3,701 additions and 1,911 deletions.
24 changes: 13 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
---

sudo: false

language: php

php:
Expand All @@ -11,14 +13,14 @@ php:
- 7.1
- hhvm

sudo: false

cache:
directories:
- $HOME/.composer/cache/files

env:
- SYMFONY_VERSION=2.8.x
global:
- SYMFONY_VERSION=2.8.x
- SYMFONY_DEPRECATIONS_HELPER=weak

matrix:
include:
Expand All @@ -45,20 +47,20 @@ matrix:
env: SYMFONY_VERSION=dev-master

before_install:
- composer self-update
- if [ "${TRAVIS_PHP_VERSION}" == "5.3" ]; then composer remove --no-update --dev satooshi/php-coveralls; fi;
- if [ "${TRAVIS_PHP_VERSION}" != "hhvm" ]; then echo "memory_limit = -1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi;
- if [ "${SYMFONY_VERSION:0:3}" == "2.3" ]; then composer remove --dev friendsofphp/php-cs-fixer --no-update; fi;
- if [ "${TRAVIS_PHP_VERSION}" != "hhvm" ] && [ "${TRAVIS_PHP_VERSION}" != "5.3" ]; then composer require --dev league/flysystem:~1.0 --no-update; fi;
- if [ "${TRAVIS_PHP_VERSION}" != "hhvm" ] && [ "${TRAVIS_PHP_VERSION:0:1}" != "7" ]; then composer require --dev doctrine/mongodb-odm:~1.0 --no-update; yes "" | pecl -q install -f mongo; fi;
- if [ "${SYMFONY_VERSION:-x}" != "x" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --no-update; fi;
- cp ./.composer/config.json ~/.composer; composer global require "hirak/prestissimo:^0.3"
- if [ "${TRAVIS_PHP_VERSION}" == "5.3" ]; then composer remove --no-update --dev satooshi/php-coveralls; fi;
- if [ "${SYMFONY_VERSION:0:3}" == "2.3" ]; then composer remove --no-update --dev friendsofphp/php-cs-fixer; fi;
- if [ "${SYMFONY_VERSION:-x}" != "x" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --no-update; fi;
- if [ "${TRAVIS_PHP_VERSION}" != "hhvm" ] && [ "${TRAVIS_PHP_VERSION:0:3}" != "5.3" ]; then composer require --no-update --dev league/flysystem:~1.0; fi;
- if [ "${TRAVIS_PHP_VERSION}" != "hhvm" ] && [ "${TRAVIS_PHP_VERSION:0:1}" != "7" ]; then yes "" | pecl -q install -f mongo; composer require --no-update --dev doctrine/mongodb-odm:~1.0; fi;
- if [ "${TRAVIS_PHP_VERSION}" != "hhvm" ] && [ "${TRAVIS_PHP_VERSION:0:1}" == "7" ]; then yes "" | pecl -q install -f mongodb; travis_retry composer require --dev alcaeus/mongo-php-adapter:~1.0; composer require --no-update --dev doctrine/mongodb-odm:~1.0; fi

install:
- composer update $COMPOSER_FLAGS
- travis_retry composer update $COMPOSER_FLAGS

script:
- ./bin/phpunit -vvv
- bin/simple-phpunit -vvv || bin/phpunit -vvv

after_script:
- if [ "${TRAVIS_PHP_VERSION}" != "5.3" ]; then bin/coveralls -vvv; fi;
Expand Down
70 changes: 20 additions & 50 deletions Binary/Loader/FileSystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace Liip\ImagineBundle\Binary\Loader;

use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
use Liip\ImagineBundle\Binary\Locator\FileSystemLocator;
use Liip\ImagineBundle\Binary\Locator\LocatorInterface;
use Liip\ImagineBundle\Exception\InvalidArgumentException;
use Liip\ImagineBundle\Model\FileBinary;
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface;
Expand All @@ -30,78 +31,47 @@ class FileSystemLoader implements LoaderInterface
protected $extensionGuesser;

/**
* @var string
* @var LocatorInterface
*/
protected $dataRoots;
protected $locator;

/**
* @param MimeTypeGuesserInterface $mimeTypeGuesser
* @param MimeTypeGuesserInterface $mimeGuesser
* @param ExtensionGuesserInterface $extensionGuesser
* @param string[] $dataRoots
* @param LocatorInterface $locator
*/
public function __construct(
MimeTypeGuesserInterface $mimeTypeGuesser,
MimeTypeGuesserInterface $mimeGuesser,
ExtensionGuesserInterface $extensionGuesser,
$dataRoots
/* LocatorInterface $locator */
) {
$this->mimeTypeGuesser = $mimeTypeGuesser;
$this->mimeTypeGuesser = $mimeGuesser;
$this->extensionGuesser = $extensionGuesser;

$this->dataRoots = array_map(function ($root) {
if (!empty($root) && false !== $realRoot = realpath($root)) {
return $realRoot;
}

throw new InvalidArgumentException(sprintf('Root image path not resolvable "%s"', $root));
}, (array) $dataRoots);

if (count($this->dataRoots) === 0) {
if (count($dataRoots) === 0) {
throw new InvalidArgumentException('One or more data root paths must be specified.');
}

if (func_num_args() >= 4 && false === ($this->locator = func_get_arg(3)) instanceof LocatorInterface) {
throw new \InvalidArgumentException(sprintf('Method %s() expects a LocatorInterface for the forth argument.', __METHOD__));
} elseif (func_num_args() < 4) {
@trigger_error(sprintf('Method %s() will have a forth `LocatorInterface $locator` argument in version 2.0. Not defining it is deprecated since version 1.7.2', __METHOD__), E_USER_DEPRECATED);
$this->locator = new FileSystemLocator();
}

$this->locator->setOptions(array('roots' => (array) $dataRoots));
}

/**
* {@inheritdoc}
*/
public function find($path)
{
$path = $this->absolutePathRestrict($this->absolutePathLocate($path));
$path = $this->locator->locate($path);
$mime = $this->mimeTypeGuesser->guess($path);

return new FileBinary($path, $mime, $this->extensionGuesser->guess($mime));
}

/**
* @param string $path
*
* @return string
*/
private function absolutePathLocate($path)
{
foreach ($this->dataRoots as $root) {
if (false !== $realPath = realpath($root.DIRECTORY_SEPARATOR.$path)) {
return $realPath;
}
}

throw new NotLoadableException(sprintf('Source image not resolvable "%s" in root path(s) "%s"',
$path, implode(':', $this->dataRoots)));
}

/**
* @param string $path
*
* @return mixed
*/
private function absolutePathRestrict($path)
{
foreach ($this->dataRoots as $root) {
if (0 === strpos($path, $root)) {
return $path;
}
}

throw new NotLoadableException(sprintf('Source image invalid "%s" as it is outside of the defined root path(s) "%s"',
$path, implode(':', $this->dataRoots)));
}
}
32 changes: 32 additions & 0 deletions Binary/Locator/FileSystemInsecureLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Liip\ImagineBundle\Binary\Locator;

class FileSystemInsecureLocator extends FileSystemLocator
{
/**
* @param string $root
* @param string $path
*
* @return string|false
*/
protected function generateAbsolutePath($root, $path)
{
if (false !== strpos($path, '..'.DIRECTORY_SEPARATOR) ||
false !== strpos($path, DIRECTORY_SEPARATOR.'..') ||
false === file_exists($absolute = $root.DIRECTORY_SEPARATOR.$path)) {
return false;
}

return $absolute;
}
}
144 changes: 144 additions & 0 deletions Binary/Locator/FileSystemLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Liip\ImagineBundle\Binary\Locator;

use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
use Liip\ImagineBundle\Exception\InvalidArgumentException;
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class FileSystemLocator implements LocatorInterface
{
/**
* @var string[]
*/
private $roots = array();

/**
* @param array[] $options
*/
public function setOptions(array $options = array())
{
$resolver = new OptionsResolver();
$resolver->setDefaults(array('roots' => array()));

try {
$options = $resolver->resolve($options);
} catch (ExceptionInterface $e) {
throw new InvalidArgumentException(sprintf('Invalid options provided to %s()', __METHOD__), null, $e);
}

$this->roots = array_map(array($this, 'sanitizeRootPath'), (array) $options['roots']);
}

/**
* @param string $path
*
* @throws NotLoadableException
*
* @return string
*/
public function locate($path)
{
if (false !== $absolute = $this->locateUsingRootPlaceholder($path)) {
return $this->sanitizeAbsolutePath($absolute);
}

if (false !== $absolute = $this->locateUsingRootPathsSearch($path)) {
return $this->sanitizeAbsolutePath($absolute);
}

throw new NotLoadableException(sprintf('Source image not resolvable "%s" in root path(s) "%s"',
$path, implode(':', $this->roots)));
}

/**
* @param string $path
*
* @return bool|string
*/
private function locateUsingRootPathsSearch($path)
{
foreach ($this->roots as $root) {
if (false !== $absolute = $this->generateAbsolutePath($root, $path)) {
return $absolute;
}
}

return false;
}

/**
* @param string $path
*
* @return bool|string
*/
private function locateUsingRootPlaceholder($path)
{
if (0 !== strpos($path, '@') || 1 !== preg_match('{@(?<name>[^:]+):(?<path>.+)}', $path, $matches)) {
return false;
}

if (isset($this->roots[$matches['name']])) {
return $this->generateAbsolutePath($this->roots[$matches['name']], $matches['path']);
}

throw new NotLoadableException(sprintf('Invalid root placeholder "%s" for path "%s"',
$matches['name'], $matches['path']));
}

/**
* @param string $root
* @param string $path
*
* @return string|false
*/
protected function generateAbsolutePath($root, $path)
{
return realpath($root.DIRECTORY_SEPARATOR.$path);
}

/**
* @param string $root
*
* @throws InvalidArgumentException
*
* @return string
*/
private function sanitizeRootPath($root)
{
if (!empty($root) && false !== $realRoot = realpath($root)) {
return $realRoot;
}

throw new InvalidArgumentException(sprintf('Root image path not resolvable "%s"', $root));
}

/**
* @param string $path
*
* @throws NotLoadableException
*
* @return string
*/
private function sanitizeAbsolutePath($path)
{
foreach ($this->roots as $root) {
if (0 === strpos($path, $root)) {
return $path;
}
}

throw new NotLoadableException(sprintf('Source image invalid "%s" as it is outside of the defined root path(s) "%s"',
$path, implode(':', $this->roots)));
}
}
27 changes: 27 additions & 0 deletions Binary/Locator/LocatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Liip\ImagineBundle\Binary\Locator;

interface LocatorInterface
{
/**
* @param array $options[]
*/
public function setOptions(array $options = array());

/**
* @param string $path
*
* @return string
*/
public function locate($path);
}

0 comments on commit f3e24cb

Please sign in to comment.