From 44cf56ca917624f054e8aa35beb6df4b6e61a7d7 Mon Sep 17 00:00:00 2001 From: Jeroen Thora Date: Thu, 26 Jul 2018 08:35:25 +0200 Subject: [PATCH] [MediaBundle] Add web_root configuration to support both web and public as the root dir for uploads (#2058) --- .../DependencyInjection/Configuration.php | 5 +++ .../KunstmaanMediaExtension.php | 1 + .../MediaBundle/Resources/config/services.yml | 2 +- .../DependencyInjection/ConfigurationTest.php | 8 ++++- .../Tests/Utils/SymfonyVersionTest.php | 25 +++++++++++++ .../MediaBundle/Utils/SymfonyVersion.php | 35 +++++++++++++++++++ 6 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 src/Kunstmaan/MediaBundle/Tests/Utils/SymfonyVersionTest.php create mode 100644 src/Kunstmaan/MediaBundle/Utils/SymfonyVersion.php diff --git a/src/Kunstmaan/MediaBundle/DependencyInjection/Configuration.php b/src/Kunstmaan/MediaBundle/DependencyInjection/Configuration.php index 2d0426eda3..48f9a588da 100644 --- a/src/Kunstmaan/MediaBundle/DependencyInjection/Configuration.php +++ b/src/Kunstmaan/MediaBundle/DependencyInjection/Configuration.php @@ -2,6 +2,7 @@ namespace Kunstmaan\MediaBundle\DependencyInjection; +use Kunstmaan\MediaBundle\Utils\SymfonyVersion; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; @@ -38,6 +39,10 @@ public function getConfigTreeBuilder() ->defaultValue(array('php', 'htaccess')) ->prototype('scalar')->end() ->end() + ->scalarNode('web_root') + ->defaultValue(SymfonyVersion::getRootWebPath()) + ->cannotBeEmpty() + ->end() ->end(); return $treeBuilder; diff --git a/src/Kunstmaan/MediaBundle/DependencyInjection/KunstmaanMediaExtension.php b/src/Kunstmaan/MediaBundle/DependencyInjection/KunstmaanMediaExtension.php index dca4b4794c..c854fe9852 100644 --- a/src/Kunstmaan/MediaBundle/DependencyInjection/KunstmaanMediaExtension.php +++ b/src/Kunstmaan/MediaBundle/DependencyInjection/KunstmaanMediaExtension.php @@ -40,6 +40,7 @@ public function load(array $configs, ContainerBuilder $container) $container->setParameter('kunstmaan_media.remote_video', $config['remote_video']); $container->setParameter('kunstmaan_media.enable_pdf_preview', $config['enable_pdf_preview']); $container->setParameter('kunstmaan_media.blacklisted_extensions', $config['blacklisted_extensions']); + $container->setParameter('kunstmaan_media.full_media_path', $config['web_root'] . '%kunstmaan_media.media_path%'); $loader->load('services.yml'); $loader->load('handlers.yml'); diff --git a/src/Kunstmaan/MediaBundle/Resources/config/services.yml b/src/Kunstmaan/MediaBundle/Resources/config/services.yml index 9666d37296..81a72e33cc 100644 --- a/src/Kunstmaan/MediaBundle/Resources/config/services.yml +++ b/src/Kunstmaan/MediaBundle/Resources/config/services.yml @@ -110,7 +110,7 @@ services: kunstmaan_media.filesystem_adapter: class: Gaufrette\Adapter\Local arguments: - - '%kernel.root_dir%/../web%kunstmaan_media.media_path%' + - '%kunstmaan_media.full_media_path%' - true kunstmaan_media.filesystem: diff --git a/src/Kunstmaan/MediaBundle/Tests/DependencyInjection/ConfigurationTest.php b/src/Kunstmaan/MediaBundle/Tests/DependencyInjection/ConfigurationTest.php index 003498e994..f55e9ccb0c 100644 --- a/src/Kunstmaan/MediaBundle/Tests/DependencyInjection/ConfigurationTest.php +++ b/src/Kunstmaan/MediaBundle/Tests/DependencyInjection/ConfigurationTest.php @@ -5,6 +5,7 @@ use Kunstmaan\MediaBundle\DependencyInjection\Configuration; use Matthias\SymfonyConfigTest\PhpUnit\ConfigurationTestCaseTrait; use PHPUnit_Framework_TestCase; +use Symfony\Component\HttpKernel\Kernel; /** * Class ConfigurationTest @@ -31,9 +32,14 @@ public function testConfigGeneratesAsExpected() 'dailymotion' => false, ], 'enable_pdf_preview' => true, - 'blacklisted_extensions' => [] + 'blacklisted_extensions' => [], + 'web_root' => '%kernel.project_dir%/web' ]; + if (Kernel::VERSION_ID >= 40000) { + $array['web_root'] = '%kernel.project_dir%/public'; + } + $this->assertProcessedConfigurationEquals([$array], $array); } } diff --git a/src/Kunstmaan/MediaBundle/Tests/Utils/SymfonyVersionTest.php b/src/Kunstmaan/MediaBundle/Tests/Utils/SymfonyVersionTest.php new file mode 100644 index 0000000000..ac5e06a965 --- /dev/null +++ b/src/Kunstmaan/MediaBundle/Tests/Utils/SymfonyVersionTest.php @@ -0,0 +1,25 @@ +assertStringStartsWith('%kernel.project_dir%/', $path); + $this->assertStringEndsWith(Kernel::VERSION_ID < 40000 ? 'web' : 'public', $path); + } + + public function testIsKernelLessThan() + { + $this->assertTrue(SymfonyVersion::isKernelLessThan(100, 100, 100)); + $this->assertFalse(SymfonyVersion::isKernelLessThan(1, 1, 1)); + } +} diff --git a/src/Kunstmaan/MediaBundle/Utils/SymfonyVersion.php b/src/Kunstmaan/MediaBundle/Utils/SymfonyVersion.php new file mode 100644 index 0000000000..b673b4c563 --- /dev/null +++ b/src/Kunstmaan/MediaBundle/Utils/SymfonyVersion.php @@ -0,0 +1,35 @@ +