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

Rename the web folder to public #3084

Merged
merged 5 commits into from
Jun 18, 2021
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
10 changes: 9 additions & 1 deletion core-bundle/src/Command/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __construct(string $projectDir, string $uploadPath, string $imag
protected function configure(): void
{
$this
->addArgument('target', InputArgument::OPTIONAL, 'The target directory', 'web')
->addArgument('target', InputArgument::OPTIONAL, 'The target directory')
->setDescription('Installs the required Contao directories')
;
}
Expand All @@ -81,6 +81,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->fs = new Filesystem();
$this->webDir = $input->getArgument('target');

if (null === $this->webDir) {
if ($this->fs->exists($this->projectDir.'/web')) {
$this->webDir = 'web'; // backwards compatibility
} else {
$this->webDir = 'public';
}
}

$this->addEmptyDirs();

if (!empty($this->rows)) {
Expand Down
12 changes: 10 additions & 2 deletions core-bundle/src/Command/SymlinksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,23 @@ public function __construct(string $projectDir, string $uploadPath, string $logs
protected function configure(): void
{
$this
->addArgument('target', InputArgument::OPTIONAL, 'The target directory', 'web')
->setDescription('Symlinks the public resources into the web directory.')
->addArgument('target', InputArgument::OPTIONAL, 'The target directory')
->setDescription('Symlinks the public resources into the public directory.')
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->webDir = $input->getArgument('target');

if (null === $this->webDir) {
if ((new Filesystem())->exists($this->projectDir.'/web')) {
$this->webDir = 'web'; // backwards compatibility
} else {
$this->webDir = 'public';
}
}
Comment on lines +103 to +109
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now testing this, I'm wondering why we do not just rely on the config value here? IMHO the config should be the single point of truth once configured.


$this->generateSymlinks();

if (!empty($this->rows)) {
Expand Down
18 changes: 15 additions & 3 deletions core-bundle/src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Webmozart\PathUtil\Path;
Expand Down Expand Up @@ -103,7 +104,7 @@ static function (string $value): string {
->validate()
->ifTrue(
static function (string $v): int {
return preg_match('@^(app|assets|bin|config|contao|plugins|share|system|templates|var|vendor|web)(/|$)@', $v);
return preg_match('@^(app|assets|bin|config|contao|plugins|public|share|system|templates|var|vendor|web)(/|$)@', $v);
}
)
->thenInvalid('%s')
Expand All @@ -117,9 +118,9 @@ static function (string $v): int {
->defaultValue('.html')
->end()
->scalarNode('web_dir')
->info('Absolute path to the web directory. Defaults to %kernel.project_dir%/web.')
->info('Absolute path to the web directory. Defaults to %kernel.project_dir%/public.')
->cannotBeEmpty()
->defaultValue(Path::join($this->projectDir, 'web'))
->defaultValue($this->getDefaultWebDir())
->validate()
->always(
static function (string $value): string {
Expand Down Expand Up @@ -522,6 +523,17 @@ private function getLocales(): array
return array_values(array_unique($languages));
}

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

if ((new Filesystem())->exists($webDir)) {
return $webDir;
}

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

/**
* @return array<string>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public function __invoke(RequestEvent $event): void
return;
}

throw new InsecureInstallationException('Your installation is not secure. Please set the document root to the /web subfolder.');
throw new InsecureInstallationException('Your installation is not secure. Please set the document root to the /public subfolder.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<source>Recreate the symlinks</source>
</trans-unit>
<trans-unit id="tl_maintenance_jobs.symlinks.1">
<source>Recreates the symlinks in the &lt;code&gt;web/&lt;/code&gt; folder (document root).</source>
<source>Recreates the symlinks in the public folder (document root).</source>
</trans-unit>
<trans-unit id="tl_maintenance.crawler">
<source>Crawler</source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public function generateXmlFiles()
}

/**
* Generate the symlinks in the web/ folder
* Generate the symlinks in the public folder
*/
public function generateSymlinks()
{
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 web/
// 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 web/ 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 web/ 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 web/ 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 web/ 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 web/
// 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 web/
// Handle public bundle resources in the contao.web_dir folder
if (file_exists($projectDir . '/' . $webDir . '/' . $src))
{
$mtime = filemtime($projectDir . '/' . $webDir . '/' . $src);
Expand Down
6 changes: 3 additions & 3 deletions core-bundle/tests/Command/InstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ protected function tearDown(): void
$fs->remove($this->getTempDir().'/assets/images_test');
$fs->remove($this->getTempDir().'/assets/js');
$fs->remove($this->getTempDir().'/files_test');
$fs->remove($this->getTempDir().'/public/share');
$fs->remove($this->getTempDir().'/public/system');
$fs->remove($this->getTempDir().'/system/cache');
$fs->remove($this->getTempDir().'/system/config');
$fs->remove($this->getTempDir().'/system/initialize.php');
$fs->remove($this->getTempDir().'/system/modules/.gitignore');
$fs->remove($this->getTempDir().'/system/themes');
$fs->remove($this->getTempDir().'/system/tmp');
$fs->remove($this->getTempDir().'/templates');
$fs->remove($this->getTempDir().'/web/share');
$fs->remove($this->getTempDir().'/web/system');
}

public function testCreatesTheContaoFolders(): void
Expand All @@ -51,11 +51,11 @@ public function testCreatesTheContaoFolders(): void
$this->assertStringContainsString(' * assets/css', $output);
$this->assertStringContainsString(' * assets/images', $output);
$this->assertStringContainsString(' * assets/js', $output);
$this->assertStringContainsString(' * public/system', $output);
$this->assertStringContainsString(' * system/cache', $output);
$this->assertStringContainsString(' * system/config', $output);
$this->assertStringContainsString(' * system/tmp', $output);
$this->assertStringContainsString(' * templates', $output);
$this->assertStringContainsString(' * web/system', $output);
}

public function testHandlesCustomFilesAndImagesPaths(): void
Expand Down
24 changes: 12 additions & 12 deletions core-bundle/tests/Command/SymlinksCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ protected function tearDown(): void
parent::tearDown();

(new Filesystem())->remove([
Path::join($this->getTempDir(), 'public'),
Path::join($this->getTempDir(), 'system/config'),
Path::join($this->getTempDir(), 'system/logs'),
Path::join($this->getTempDir(), 'system/themes'),
Path::join($this->getTempDir(), 'var'),
Path::join($this->getTempDir(), 'web'),
]);
}

Expand All @@ -58,21 +58,21 @@ public function testSymlinksTheContaoFolders(): void

$this->assertSame(0, $code);

$this->assertNotRegExp('# web/files +files #', $display);
$this->assertRegExp('# web/files/public +files/public #', $display);
$this->assertRegExp('# web/system/modules/foobar/html/foo/bar +Skipped because system/modules/foobar/html will be symlinked\. #', $display);
$this->assertRegExp('# web/system/modules/foobar/assets +system/modules/foobar/assets #', $display);
$this->assertRegExp('# web/system/modules/foobar/html +system/modules/foobar/html #', $display);
$this->assertNotRegExp('# public/files +files #', $display);
$this->assertRegExp('# public/files/public +files/public #', $display);
$this->assertRegExp('# public/system/modules/foobar/html/foo/bar +Skipped because system/modules/foobar/html will be symlinked\. #', $display);
$this->assertRegExp('# public/system/modules/foobar/assets +system/modules/foobar/assets #', $display);
$this->assertRegExp('# public/system/modules/foobar/html +system/modules/foobar/html #', $display);
$this->assertRegExp('# vendor/contao/test-bundle/Resources/contao/themes/default #', $display);
$this->assertRegExp('# system/themes/flexible +vendor/contao/test-bundle/Resources/contao/themes/flexible #', $display);
$this->assertRegExp('# web/assets +assets #', $display);
$this->assertRegExp('# web/system/themes +system/themes #', $display);
$this->assertRegExp('# public/assets +assets #', $display);
$this->assertRegExp('# public/system/themes +system/themes #', $display);
$this->assertRegExp('# system/logs +var/logs #', $display);

$this->assertFileExists(Path::join(self::getTempDir(), 'web/files/public'));
$this->assertDirectoryExists(Path::join(self::getTempDir(), 'web/system/modules/foobar'));
$this->assertDirectoryExists(Path::join(self::getTempDir(), 'web/system/themes/default'));
$this->assertDirectoryExists(Path::join(self::getTempDir(), 'web/assets'));
$this->assertFileExists(Path::join(self::getTempDir(), 'public/files/public'));
$this->assertDirectoryExists(Path::join(self::getTempDir(), 'public/system/modules/foobar'));
$this->assertDirectoryExists(Path::join(self::getTempDir(), 'public/system/themes/default'));
$this->assertDirectoryExists(Path::join(self::getTempDir(), 'public/assets'));
}

public function testConvertsAbsolutePathsToRelativePaths(): void
Expand Down
20 changes: 10 additions & 10 deletions core-bundle/tests/Contao/CombinerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public static function setUpBeforeClass(): void

$fs = new Filesystem();
$fs->mkdir(static::getTempDir().'/assets/css');
$fs->mkdir(static::getTempDir().'/public');
$fs->mkdir(static::getTempDir().'/system/tmp');
$fs->mkdir(static::getTempDir().'/web');
}

protected function setUp(): void
Expand All @@ -48,7 +48,7 @@ protected function setUp(): void
;

$container = $this->getContainerWithContaoConfiguration($this->getTempDir());
$container->setParameter('contao.web_dir', $this->getTempDir().'/web');
$container->setParameter('contao.web_dir', $this->getTempDir().'/public');
$container->set('contao.assets.assets_context', $context);

System::setContainer($container);
Expand All @@ -57,9 +57,9 @@ protected function setUp(): void
public function testCombinesCssFiles(): void
{
$this->filesystem->dumpFile($this->getTempDir().'/file1.css', 'file1 { background: url("foo.bar") }');
$this->filesystem->dumpFile($this->getTempDir().'/web/file2.css', 'web/file2');
$this->filesystem->dumpFile($this->getTempDir().'/public/file2.css', 'public/file2');
$this->filesystem->dumpFile($this->getTempDir().'/file3.css', 'file3');
$this->filesystem->dumpFile($this->getTempDir().'/web/file3.css', 'web/file3');
$this->filesystem->dumpFile($this->getTempDir().'/public/file3.css', 'public/file3');

$mtime = filemtime($this->getTempDir().'/file1.css');

Expand Down Expand Up @@ -95,7 +95,7 @@ public function testCombinesCssFiles(): void

$this->assertStringEqualsFile(
$this->getTempDir().'/'.$combinedFile,
"file1 { background: url(\"../../foo.bar\") }\n@media screen{\nweb/file2\n}\n@media screen{\nfile3\n}\n"
"file1 { background: url(\"../../foo.bar\") }\n@media screen{\npublic/file2\n}\n@media screen{\nfile3\n}\n"
);

System::getContainer()->setParameter('kernel.debug', true);
Expand Down Expand Up @@ -152,7 +152,7 @@ public function testHandlesSpecialCharactersWhileFixingTheFilePaths(): void

$this->assertSame(
$expected,
$method->invokeArgs($class->newInstance(), [$css, ['name' => 'web/"test"/file.css']])
$method->invokeArgs($class->newInstance(), [$css, ['name' => 'public/"test"/file.css']])
);

$expected = <<<'EOF'
Expand All @@ -163,7 +163,7 @@ public function testHandlesSpecialCharactersWhileFixingTheFilePaths(): void

$this->assertSame(
$expected,
$method->invokeArgs($class->newInstance(), [$css, ['name' => "web/'test'/file.css"]])
$method->invokeArgs($class->newInstance(), [$css, ['name' => "public/'test'/file.css"]])
);

$expected = <<<'EOF'
Expand All @@ -174,7 +174,7 @@ public function testHandlesSpecialCharactersWhileFixingTheFilePaths(): void

$this->assertSame(
$expected,
$method->invokeArgs($class->newInstance(), [$css, ['name' => 'web/(test)/file.css']])
$method->invokeArgs($class->newInstance(), [$css, ['name' => 'public/(test)/file.css']])
);
}

Expand Down Expand Up @@ -253,10 +253,10 @@ public function testCombinesScssFiles(): void
public function testCombinesJsFiles(): void
{
$this->filesystem->dumpFile($this->getTempDir().'/file1.js', 'file1();');
$this->filesystem->dumpFile($this->getTempDir().'/web/file2.js', 'file2();');
$this->filesystem->dumpFile($this->getTempDir().'/public/file2.js', 'file2();');

$mtime1 = filemtime($this->getTempDir().'/file1.js');
$mtime2 = filemtime($this->getTempDir().'/web/file2.js');
$mtime2 = filemtime($this->getTempDir().'/public/file2.js');

$combiner = new Combiner();
$combiner->add('file1.js');
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/tests/Contao/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ private function getContainerWithImageServices(): ContainerBuilder
{
$container = $this->getContainerWithContaoConfiguration($this->getTempDir());
$container->setParameter('contao.image.target_dir', Path::join($this->getTempDir(), 'assets/images'));
$container->setParameter('contao.web_dir', Path::join($this->getTempDir(), 'web'));
$container->setParameter('contao.web_dir', Path::join($this->getTempDir(), 'public'));

$framework = $this->mockContaoFramework([
FilesModel::class => $this->createMock(Adapter::class),
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/tests/Contao/PictureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ private function getContainerWithImageServices(): ContainerBuilder
$framework = $this->mockContaoFramework($adapters);

$container = $this->getContainerWithContaoConfiguration($this->getTempDir());
$container->setParameter('contao.web_dir', $this->getTempDir().'/web');
$container->setParameter('contao.web_dir', $this->getTempDir().'/public');
$container->setParameter('contao.image.target_dir', $this->getTempDir().'/assets/images');
$container->set('contao.assets.files_context', $context);

Expand Down
15 changes: 15 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 Expand Up @@ -113,6 +127,7 @@ public function getInvalidUploadPaths(): \Generator
yield ['config'];
yield ['contao'];
yield ['plugins'];
yield ['public'];
yield ['share'];
yield ['system'];
yield ['templates'];
Expand Down