Skip to content

Commit

Permalink
Dynamically set the default value
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Jun 18, 2021
1 parent a56bfaf commit 40c6b92
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
13 changes: 12 additions & 1 deletion core-bundle/src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static function (string $v): int {
->scalarNode('web_dir')
->info('Absolute path to the web directory. Defaults to %kernel.project_dir%/public.')
->cannotBeEmpty()
->defaultValue(Path::join($this->projectDir, 'public'))
->defaultValue($this->getDefaultWebDir())
->validate()
->always(
static function (string $value): string {
Expand Down Expand Up @@ -522,6 +522,17 @@ private function getLocales(): array
return array_values(array_unique($languages));
}

private function getDefaultWebDir(): string
{
$webDir = Path::join($this->projectDir, 'web');

if (file_exists($webDir)) {
return $webDir;
}

return Path::join($this->projectDir, 'public');
}

/**
* @return array<string>
*
Expand Down
6 changes: 3 additions & 3 deletions core-bundle/src/Resources/contao/library/Contao/Combiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function add($strFile, $strVersion=null, $strMedia='all')
// Check the source file
if (!file_exists($this->strRootDir . '/' . $strFile))
{
// Handle public bundle resources in the document root
// Handle public bundle resources in the contao.web_dir folder
if (file_exists($this->strRootDir . '/' . $this->strWebDir . '/' . $strFile))
{
$strFile = $this->strWebDir . '/' . $strFile;
Expand Down Expand Up @@ -229,7 +229,7 @@ public function getFileUrls($strUrl=null)
{
$name = $arrFile['name'];

// Strip the public directory prefix (see #328)
// Strip the contao.web_dir directory prefix (see #328)
if (strncmp($name, $this->strWebDir . '/', \strlen($this->strWebDir) + 1) === 0)
{
$name = substr($name, \strlen($this->strWebDir) + 1);
Expand Down Expand Up @@ -447,7 +447,7 @@ protected function fixPaths($content, $arrFile)
{
$strName = $arrFile['name'];

// Strip the public directory prefix
// Strip the contao.web_dir directory prefix
if (strpos($strName, $this->strWebDir . '/') === 0)
{
$strName = substr($strName, \strlen($this->strWebDir) + 1);
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/src/Resources/contao/library/Contao/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public function getResizedPath()
$path = $this->resizedPath;
$webDir = StringUtil::stripRootDir(System::getContainer()->getParameter('contao.web_dir'));

// Strip the public directory prefix (see #337)
// Strip the contao.web_dir directory prefix (see #337)
if (strncmp($path, $webDir . '/', \strlen($webDir) + 1) === 0)
{
$path = substr($path, \strlen($webDir) + 1);
Expand Down Expand Up @@ -708,7 +708,7 @@ public static function getHtml($src, $alt='', $attributes='')

$objFile = new File($src);

// Strip the public directory prefix (see #337)
// Strip the contao.web_dir directory prefix (see #337)
if (strncmp($src, $webDir . '/', \strlen($webDir) + 1) === 0)
{
$src = substr($src, \strlen($webDir) + 1);
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/src/Resources/contao/library/Contao/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ public static function generateStyleTag($href, $media=null, $mtime=false)
{
$webDir = StringUtil::stripRootDir($container->getParameter('contao.web_dir'));

// Handle public bundle resources in the document root
// Handle public bundle resources in the contao.web_dir folder
if (file_exists($projectDir . '/' . $webDir . '/' . $href))
{
$mtime = filemtime($projectDir . '/' . $webDir . '/' . $href);
Expand Down Expand Up @@ -646,7 +646,7 @@ public static function generateScriptTag($src, $async=false, $mtime=false, $hash
{
$webDir = StringUtil::stripRootDir($container->getParameter('contao.web_dir'));

// Handle public bundle resources in the document root
// Handle public bundle resources in the contao.web_dir folder
if (file_exists($projectDir . '/' . $webDir . '/' . $src))
{
$mtime = filemtime($projectDir . '/' . $webDir . '/' . $src);
Expand Down
14 changes: 14 additions & 0 deletions core-bundle/tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\Definition\PrototypedArrayNode;
use Symfony\Component\Filesystem\Filesystem;

class ConfigurationTest extends TestCase
{
Expand Down Expand Up @@ -55,6 +56,19 @@ public function testAddsTheImagineService(): void
$this->assertSame('my_super_service', $configuration['image']['imagine_service']);
}

public function testAdjustsTheDefaultWebDir(): void
{
$configuration = (new Processor())->processConfiguration($this->configuration, []);

$this->assertSame('public', basename($configuration['web_dir']));

(new Filesystem())->mkdir($this->getTempDir().'/web');

$configuration = (new Processor())->processConfiguration($this->configuration, []);

$this->assertSame('web', basename($configuration['web_dir']));
}

/**
* @dataProvider getPaths
*/
Expand Down

0 comments on commit 40c6b92

Please sign in to comment.