Skip to content

Commit e90c94c

Browse files
bug #29533 Fixed public directory when configured in composer.json (alexander-schranz)
This PR was merged into the 3.4 branch. Discussion ---------- Fixed public directory when configured in composer.json | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT As documented you should be able to change the public-dir by composer.json so the server:run and assets:install should also use that configuration when available: https://symfony.com/doc/3.4/configuration/override_dir_structure.html#override-the-web-directory https://symfony.com/doc/current/configuration/override_dir_structure.html#override-the-public-directory #SymfonyConHackDay2018 Commits ------- c45062b71a fixed public directory of web server and assets install when configured in composer.json
2 parents 955f9f1 + 548839f commit e90c94c

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

Command/AssetsInstallCommand.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Input\InputOption;
1818
use Symfony\Component\Console\Output\OutputInterface;
1919
use Symfony\Component\Console\Style\SymfonyStyle;
20+
use Symfony\Component\DependencyInjection\ContainerInterface;
2021
use Symfony\Component\Filesystem\Exception\IOException;
2122
use Symfony\Component\Filesystem\Filesystem;
2223
use Symfony\Component\Finder\Finder;
@@ -65,7 +66,7 @@ protected function configure()
6566
{
6667
$this
6768
->setDefinition(array(
68-
new InputArgument('target', InputArgument::OPTIONAL, 'The target directory', 'public'),
69+
new InputArgument('target', InputArgument::OPTIONAL, 'The target directory', null),
6970
))
7071
->addOption('symlink', null, InputOption::VALUE_NONE, 'Symlinks the assets instead of copying it')
7172
->addOption('relative', null, InputOption::VALUE_NONE, 'Make relative symlinks')
@@ -107,6 +108,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
107108
$kernel = $this->getApplication()->getKernel();
108109
$targetArg = rtrim($input->getArgument('target'), '/');
109110

111+
if (!$targetArg) {
112+
$targetArg = $this->getPublicDirectory($this->getContainer());
113+
}
114+
110115
if (!is_dir($targetArg)) {
111116
$targetArg = (isset($baseDir) ? $baseDir : $kernel->getContainer()->getParameter('kernel.project_dir')).'/'.$targetArg;
112117

@@ -288,4 +293,27 @@ private function hardCopy($originDir, $targetDir)
288293

289294
return self::METHOD_COPY;
290295
}
296+
297+
private function getPublicDirectory(ContainerInterface $container)
298+
{
299+
$defaultPublicDir = 'public';
300+
301+
if (!$container->hasParameter('kernel.project_dir')) {
302+
return $defaultPublicDir;
303+
}
304+
305+
$composerFilePath = $container->getParameter('kernel.project_dir').'/composer.json';
306+
307+
if (!file_exists($composerFilePath)) {
308+
return $defaultPublicDir;
309+
}
310+
311+
$composerConfig = json_decode(file_get_contents($composerFilePath), true);
312+
313+
if (isset($composerConfig['extra']['public-dir'])) {
314+
return $composerConfig['extra']['public-dir'];
315+
}
316+
317+
return $defaultPublicDir;
318+
}
291319
}

0 commit comments

Comments
 (0)