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

[Dependency Injection] Detect proper web root path based on Symfony kernel version #1044

Merged
merged 1 commit into from Jan 30, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,5 +4,6 @@
/composer.phar
/vendor/
/Tests/Functional/app/web/media/cache
/Tests/Functional/app/public/media/cache
/.idea/
/var/
Expand Up @@ -12,6 +12,7 @@
namespace Liip\ImagineBundle\DependencyInjection\Factory\Loader;

use Liip\ImagineBundle\Exception\InvalidArgumentException;
use Liip\ImagineBundle\Utility\Framework\SymfonyFramework;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
Expand Down Expand Up @@ -59,7 +60,7 @@ public function addConfiguration(ArrayNodeDefinition $builder)
->end()
->treatNullLike([])
->treatFalseLike([])
->defaultValue(['%kernel.root_dir%/../web'])
->defaultValue([SymfonyFramework::getContainerResolvableRootWebPath()])
->prototype('scalar')
->cannotBeEmpty()
->end()
Expand Down
Expand Up @@ -11,6 +11,7 @@

namespace Liip\ImagineBundle\DependencyInjection\Factory\Resolver;

use Liip\ImagineBundle\Utility\Framework\SymfonyFramework;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;

Expand Down Expand Up @@ -50,7 +51,7 @@ public function addConfiguration(ArrayNodeDefinition $builder)
$builder
->children()
->scalarNode('web_root')
->defaultValue('%kernel.root_dir%/../web')
->defaultValue(SymfonyFramework::getContainerResolvableRootWebPath())
->cannotBeEmpty()
->end()
->scalarNode('cache_prefix')
Expand Down
10 changes: 6 additions & 4 deletions Resources/doc/cache-resolver/web_path.rst
Expand Up @@ -35,16 +35,18 @@ Configuration
resolvers:
profile_photos:
web_path:
web_root: "%kernel.root_dir%/../web"
# use %kernel.project_dir%/web for Symfony prior to 4.0.0
web_root: "%kernel.project_dir%/public"
cache_prefix: "media/cache"

There are several configuration options available:

* ``web_root`` - must be the absolute path to you application's web root. This
is used to determine where to put generated image files, so that apache
will pick them up before handing the request to Symfony2 next time they
are requested.
Default value: ``%kernel.root_dir%/../web``
will pick them up before handing the request to Symfony next time they
are requested. The default value ends with ``web`` for Symfony prior to
version ``4.0.0``.
Default value: ``%kernel.project_dir%/(public|web)``
* ``cache_prefix`` - this is also used in the path for image generation, so
as to not clutter your web root with cached images. For example by default,
the images would be written to the ``web/media/cache/`` directory.
Expand Down
4 changes: 2 additions & 2 deletions Resources/doc/configuration.rst
Expand Up @@ -13,13 +13,13 @@ The default configuration for the bundle looks like this:
resolvers:
default:
web_path:
web_root: ~ # %kernel.root_dir%/../web
web_root: ~ # %kernel.project_dir%/public (%kernel.project_dir%/web for Symfony < 4.0.0)
cache_prefix: ~ # media/cache

loaders:
default:
filesystem:
data_root: ~ # %kernel.root_dir%/../web/
data_root: ~ # %kernel.project_dir%/public (%kernel.project_dir%/web for Symfony < 4.0.0)

driver: gd
cache: default
Expand Down
Expand Up @@ -16,6 +16,7 @@
use Liip\ImagineBundle\Tests\DependencyInjection\Factory\FactoryTestCase;
use Liip\ImagineBundle\Tests\Functional\Fixtures\BarBundle\LiipBarBundle;
use Liip\ImagineBundle\Tests\Functional\Fixtures\FooBundle\LiipFooBundle;
use Liip\ImagineBundle\Utility\Framework\SymfonyFramework;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -252,7 +253,7 @@ public function testProcessCorrectlyOptionsOnAddConfiguration()

public function testAddDefaultOptionsIfNotSetOnAddConfiguration()
{
$expectedDataRoot = array('%kernel.root_dir%/../web');
$expectedDataRoot = array(SymfonyFramework::getContainerResolvableRootWebPath());

$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('filesystem', 'array');
Expand All @@ -270,7 +271,7 @@ public function testAddDefaultOptionsIfNotSetOnAddConfiguration()

public function testAddAsScalarExpectingArrayNormalizationOfConfiguration()
{
$expectedDataRoot = array('%kernel.root_dir%/../web');
$expectedDataRoot = array(SymfonyFramework::getContainerResolvableRootWebPath());

$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('filesystem', 'array');
Expand Down
Expand Up @@ -13,6 +13,7 @@

use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\ResolverFactoryInterface;
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactory;
use Liip\ImagineBundle\Utility\Framework\SymfonyFramework;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -103,7 +104,7 @@ public function testAddDefaultOptionsIfNotSetOnAddConfiguration()
));

$this->assertArrayHasKey('web_root', $config);
$this->assertEquals('%kernel.root_dir%/../web', $config['web_root']);
$this->assertEquals(SymfonyFramework::getContainerResolvableRootWebPath(), $config['web_root']);

$this->assertArrayHasKey('cache_prefix', $config);
$this->assertEquals('media/cache', $config['cache_prefix']);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/AbstractSetupWebTestCase.php
Expand Up @@ -41,7 +41,7 @@ public function setUp()
parent::setUp();

$this->client = $this->createClient();
$this->webRoot = self::$kernel->getContainer()->getParameter('kernel.root_dir').'/web';
$this->webRoot = sprintf('%s/public', self::$kernel->getContainer()->getParameter('kernel.root_dir'));
$this->cacheRoot = $this->webRoot.'/media/cache';
$this->filesystem = new Filesystem();
$this->filesystem->remove($this->cacheRoot);
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/app/config/config.yml
Expand Up @@ -29,7 +29,7 @@ liip_imagine:

default:
filesystem:
data_root: "%kernel.root_dir%/web"
data_root: "%kernel.root_dir%/public"

foo:
filesystem:
Expand Down Expand Up @@ -65,7 +65,7 @@ liip_imagine:

default:
web_path:
web_root: "%kernel.root_dir%/web"
web_root: "%kernel.root_dir%/public"
cache_prefix: media/cache

filter_sets:
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions Tests/Utility/Framework/SymfonyFrameworkTest.php
Expand Up @@ -11,7 +11,9 @@

namespace Liip\ImagineBundle\Tests\Utility\Framework;

use Liip\ImagineBundle\Tests\Filter\PasteFilterLoaderTest;
use Liip\ImagineBundle\Utility\Framework\SymfonyFramework;
use Symfony\Component\HttpKernel\Kernel;

/**
* @covers \Liip\ImagineBundle\Utility\Framework\SymfonyFramework
Expand Down Expand Up @@ -44,4 +46,12 @@ public function testHasDirectContainerBuilderLogging()
$this->assertFalse(SymfonyFramework::hasDirectContainerBuilderLogging());
}
}

public function testGetContainerResolvableRootWebPath()
{
$path = SymfonyFramework::getContainerResolvableRootWebPath();

$this->assertStringStartsWith('%kernel.project_dir%/', $path);
$this->assertStringEndsWith(Kernel::VERSION_ID < 40000 ? 'web' : 'public', $path);
}
}
31 changes: 13 additions & 18 deletions Utility/Framework/SymfonyFramework.php
Expand Up @@ -18,19 +18,27 @@ class SymfonyFramework
/**
* @return bool
*/
public static function hasDirectContainerBuilderLogging()
public static function hasDirectContainerBuilderLogging(): bool
{
return method_exists('\Symfony\Component\DependencyInjection\ContainerBuilder', 'log');
}

/**
* @return string
*/
public static function getContainerResolvableRootWebPath(): string
{
return sprintf('%%kernel.project_dir%%/%s', self::isKernelLessThan(4) ? 'web' : 'public');
}

/**
* @param int $major
* @param int|null $minor
* @param int|null $patch
*
* @return bool
*/
public static function isKernelGreaterThanOrEqualTo($major, $minor = null, $patch = null)
public static function isKernelGreaterThanOrEqualTo(int $major, int $minor = null, int $patch = null): bool
{
return static::kernelVersionCompare('>=', $major, $minor, $patch);
}
Expand All @@ -42,7 +50,7 @@ public static function isKernelGreaterThanOrEqualTo($major, $minor = null, $patc
*
* @return bool
*/
public static function isKernelLessThan($major, $minor = null, $patch = null)
public static function isKernelLessThan(int $major, int $minor = null, int $patch = null): bool
{
return static::kernelVersionCompare('<', $major, $minor, $patch);
}
Expand All @@ -55,21 +63,8 @@ public static function isKernelLessThan($major, $minor = null, $patch = null)
*
* @return bool
*/
private static function kernelVersionCompare($operator, $major, $minor = null, $patch = null)
private static function kernelVersionCompare(string $operator, int $major, int $minor = null, int $patch = null): bool
{
$vernum = $major;
$kernel = Kernel::MAJOR_VERSION;

if ($minor) {
$vernum .= '.'.$minor;
$kernel .= '.'.Kernel::MINOR_VERSION;

if ($patch) {
$vernum .= '.'.$patch;
$kernel .= '.'.Kernel::RELEASE_VERSION;
}
}

return version_compare($kernel, $vernum, $operator);
return version_compare(Kernel::VERSION_ID, sprintf("%d%'.02d%'.02d", $major, $minor ?: 0, $patch ?: 0), $operator);
}
}