Skip to content

Commit

Permalink
[MediaBundle] Add web_root configuration to support both web and publ…
Browse files Browse the repository at this point in the history
…ic as the root dir for uploads (#2058)
  • Loading branch information
acrobat authored and sandergo90 committed Jul 26, 2018
1 parent 8675764 commit 44cf56c
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion src/Kunstmaan/MediaBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
25 changes: 25 additions & 0 deletions src/Kunstmaan/MediaBundle/Tests/Utils/SymfonyVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Kunstmaan\MediaBundle\Tests\Utils;

use Kunstmaan\MediaBundle\Utils\SymfonyVersion;
use Symfony\Component\HttpKernel\Kernel;

/**
* @covers \Kunstmaan\MediaBundle\Utils\SymfonyVersion
*/
class SymfonyVersionTest extends \PHPUnit_Framework_TestCase
{
public function testGetRootWebPath()
{
$path = SymfonyVersion::getRootWebPath();
$this->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));
}
}
35 changes: 35 additions & 0 deletions src/Kunstmaan/MediaBundle/Utils/SymfonyVersion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Kunstmaan\MediaBundle\Utils;

use Symfony\Component\HttpKernel\Kernel;

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

/**
* @return bool
*/
public static function isKernelLessThan($major, $minor = null, $patch = null)
{
return static::kernelVersionCompare('<', $major, $minor, $patch);
}

/**
* @return bool
*/
private static function kernelVersionCompare($operator, $major, $minor = null, $patch = null)
{
return version_compare(Kernel::VERSION_ID, sprintf("%d%'.02d%'.02d", $major, $minor ?: 0, $patch ?: 0), $operator);
}
}

0 comments on commit 44cf56c

Please sign in to comment.