From 2627168d7a1fa14408b2b28f71632631fa371264 Mon Sep 17 00:00:00 2001 From: Rob Frawley 2nd Date: Tue, 30 Jan 2018 01:15:46 -0500 Subject: [PATCH] detect proper web root path based on symfony kernel version --- .gitignore | 1 + .../Loader/FileSystemLoaderFactory.php | 3 +- .../Resolver/WebPathResolverFactory.php | 3 +- Resources/doc/cache-resolver/web_path.rst | 10 +++--- Resources/doc/configuration.rst | 4 +-- .../Loader/FileSystemLoaderFactoryTest.php | 5 +-- .../Resolver/WebPathResolverFactoryTest.php | 3 +- Tests/Functional/AbstractSetupWebTestCase.php | 2 +- Tests/Functional/app/config/config.yml | 4 +-- Tests/Functional/app/{web => public}/.gitkeep | 0 .../app/{web => public}/images/cats.jpeg | Bin .../app/{web => public}/images/cats2.jpeg | Bin .../Framework/SymfonyFrameworkTest.php | 10 ++++++ Utility/Framework/SymfonyFramework.php | 31 ++++++++---------- 14 files changed, 44 insertions(+), 32 deletions(-) rename Tests/Functional/app/{web => public}/.gitkeep (100%) rename Tests/Functional/app/{web => public}/images/cats.jpeg (100%) rename Tests/Functional/app/{web => public}/images/cats2.jpeg (100%) diff --git a/.gitignore b/.gitignore index 3cf9f44af..a5674fceb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ /composer.phar /vendor/ /Tests/Functional/app/web/media/cache +/Tests/Functional/app/public/media/cache /.idea/ /var/ diff --git a/DependencyInjection/Factory/Loader/FileSystemLoaderFactory.php b/DependencyInjection/Factory/Loader/FileSystemLoaderFactory.php index 4462f86bd..698459178 100644 --- a/DependencyInjection/Factory/Loader/FileSystemLoaderFactory.php +++ b/DependencyInjection/Factory/Loader/FileSystemLoaderFactory.php @@ -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; @@ -59,7 +60,7 @@ public function addConfiguration(ArrayNodeDefinition $builder) ->end() ->treatNullLike([]) ->treatFalseLike([]) - ->defaultValue(['%kernel.root_dir%/../web']) + ->defaultValue([SymfonyFramework::getContainerResolvableRootWebPath()]) ->prototype('scalar') ->cannotBeEmpty() ->end() diff --git a/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php b/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php index 458f854d2..2a08e6f09 100644 --- a/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php +++ b/DependencyInjection/Factory/Resolver/WebPathResolverFactory.php @@ -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; @@ -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') diff --git a/Resources/doc/cache-resolver/web_path.rst b/Resources/doc/cache-resolver/web_path.rst index e3137c89d..8bf170c2a 100644 --- a/Resources/doc/cache-resolver/web_path.rst +++ b/Resources/doc/cache-resolver/web_path.rst @@ -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. diff --git a/Resources/doc/configuration.rst b/Resources/doc/configuration.rst index 817c939d9..1b1300e55 100644 --- a/Resources/doc/configuration.rst +++ b/Resources/doc/configuration.rst @@ -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 diff --git a/Tests/DependencyInjection/Factory/Loader/FileSystemLoaderFactoryTest.php b/Tests/DependencyInjection/Factory/Loader/FileSystemLoaderFactoryTest.php index 234c1d211..a06f90ff9 100644 --- a/Tests/DependencyInjection/Factory/Loader/FileSystemLoaderFactoryTest.php +++ b/Tests/DependencyInjection/Factory/Loader/FileSystemLoaderFactoryTest.php @@ -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; @@ -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'); @@ -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'); diff --git a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php index 15931169e..5312d0690 100644 --- a/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php +++ b/Tests/DependencyInjection/Factory/Resolver/WebPathResolverFactoryTest.php @@ -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; @@ -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']); diff --git a/Tests/Functional/AbstractSetupWebTestCase.php b/Tests/Functional/AbstractSetupWebTestCase.php index 9c4e6a2da..7fe0b6967 100644 --- a/Tests/Functional/AbstractSetupWebTestCase.php +++ b/Tests/Functional/AbstractSetupWebTestCase.php @@ -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); diff --git a/Tests/Functional/app/config/config.yml b/Tests/Functional/app/config/config.yml index b287af0d0..65b4ae069 100644 --- a/Tests/Functional/app/config/config.yml +++ b/Tests/Functional/app/config/config.yml @@ -29,7 +29,7 @@ liip_imagine: default: filesystem: - data_root: "%kernel.root_dir%/web" + data_root: "%kernel.root_dir%/public" foo: filesystem: @@ -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: diff --git a/Tests/Functional/app/web/.gitkeep b/Tests/Functional/app/public/.gitkeep similarity index 100% rename from Tests/Functional/app/web/.gitkeep rename to Tests/Functional/app/public/.gitkeep diff --git a/Tests/Functional/app/web/images/cats.jpeg b/Tests/Functional/app/public/images/cats.jpeg similarity index 100% rename from Tests/Functional/app/web/images/cats.jpeg rename to Tests/Functional/app/public/images/cats.jpeg diff --git a/Tests/Functional/app/web/images/cats2.jpeg b/Tests/Functional/app/public/images/cats2.jpeg similarity index 100% rename from Tests/Functional/app/web/images/cats2.jpeg rename to Tests/Functional/app/public/images/cats2.jpeg diff --git a/Tests/Utility/Framework/SymfonyFrameworkTest.php b/Tests/Utility/Framework/SymfonyFrameworkTest.php index 402f67ed0..ad9a13883 100644 --- a/Tests/Utility/Framework/SymfonyFrameworkTest.php +++ b/Tests/Utility/Framework/SymfonyFrameworkTest.php @@ -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 @@ -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); + } } diff --git a/Utility/Framework/SymfonyFramework.php b/Utility/Framework/SymfonyFramework.php index b0f544e6d..0733d4b91 100644 --- a/Utility/Framework/SymfonyFramework.php +++ b/Utility/Framework/SymfonyFramework.php @@ -18,11 +18,19 @@ 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 @@ -30,7 +38,7 @@ public static function hasDirectContainerBuilderLogging() * * @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); } @@ -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); } @@ -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); } }