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

Fixed public directory when configured in composer.json #29533

Merged
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
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
Expand Down Expand Up @@ -65,7 +66,7 @@ protected function configure()
{
$this
->setDefinition(array(
new InputArgument('target', InputArgument::OPTIONAL, 'The target directory', 'public'),
new InputArgument('target', InputArgument::OPTIONAL, 'The target directory', null),
))
->addOption('symlink', null, InputOption::VALUE_NONE, 'Symlinks the assets instead of copying it')
->addOption('relative', null, InputOption::VALUE_NONE, 'Make relative symlinks')
Expand Down Expand Up @@ -107,6 +108,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$kernel = $this->getApplication()->getKernel();
$targetArg = rtrim($input->getArgument('target'), '/');

if (!$targetArg) {
$targetArg = $this->getPublicDirectory($this->getContainer());
}

if (!is_dir($targetArg)) {
$targetArg = (isset($baseDir) ? $baseDir : $kernel->getContainer()->getParameter('kernel.project_dir')).'/'.$targetArg;

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

return self::METHOD_COPY;
}

private function getPublicDirectory(ContainerInterface $container)
{
$defaultPublicDir = 'public';

if (!$container->hasParameter('kernel.project_dir')) {
return $defaultPublicDir;
}

$composerFilePath = $container->getParameter('kernel.project_dir').'/composer.json';

if (!file_exists($composerFilePath)) {
return $defaultPublicDir;
}

$composerConfig = json_decode(file_get_contents($composerFilePath), true);

if (isset($composerConfig['extra']['public-dir'])) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we also account for symfony-web-dir which is still used for application based on the Symfony Standard Edition?

Copy link
Member

Choose a reason for hiding this comment

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

we discussed about it in #29533 (comment)
wdyt?

Copy link
Member

Choose a reason for hiding this comment

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

I missed the discussion, but SensioDistributionBundle only handles the case when assets are installed using Composer scripts. IMO being able to run the command without having to specify the argument would be nice. On the other hand, anyone using 3.4 probably is already used to doing this. So maybe it's not worth it.

Copy link
Member

Choose a reason for hiding this comment

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

anyone using 3.4 probably is already used to doing this. So maybe it's not worth it.

same opinion here :)

return $composerConfig['extra']['public-dir'];
}

return $defaultPublicDir;
}
}
Expand Up @@ -27,8 +27,31 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('webserver.xml');

$publicDirectory = $this->getPublicDirectory($container);
$container->getDefinition('web_server.command.server_run')->replaceArgument(0, $publicDirectory);
$container->getDefinition('web_server.command.server_start')->replaceArgument(0, $publicDirectory);

if (!class_exists(ConsoleFormatter::class)) {
$container->removeDefinition('web_server.command.server_log');
}
}

private function getPublicDirectory(ContainerBuilder $container)
{
$kernelProjectDir = $container->getParameter('kernel.project_dir');
$publicDir = 'public';
$composerFilePath = $kernelProjectDir.'/composer.json';

if (!file_exists($composerFilePath)) {
return $kernelProjectDir.'/'.$publicDir;
}

$composerConfig = json_decode(file_get_contents($composerFilePath), true);

if (isset($composerConfig['extra']['public-dir'])) {
$publicDir = $composerConfig['extra']['public-dir'];
Copy link
Member

Choose a reason for hiding this comment

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

Should we do a ltrim($publicDir, '/') here to be completely sure that we won't have double slashes later?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is ok for me what do you think @nicolas-grekas?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

why should we care?

}

return $kernelProjectDir.'/'.$publicDir;
}
}
Expand Up @@ -21,8 +21,17 @@ class WebServerExtensionTest extends TestCase
public function testLoad()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.project_dir', __DIR__);
(new WebServerExtension())->load(array(), $container);

$this->assertSame(
__DIR__.'/test',
$container->getDefinition('web_server.command.server_run')->getArgument(0)
);
$this->assertSame(
__DIR__.'/test',
$container->getDefinition('web_server.command.server_start')->getArgument(0)
);
$this->assertTrue($container->hasDefinition('web_server.command.server_run'));
$this->assertTrue($container->hasDefinition('web_server.command.server_start'));
$this->assertTrue($container->hasDefinition('web_server.command.server_stop'));
Expand Down
@@ -0,0 +1,6 @@
{
"name": "test-composer.json",
"extra": {
"public-dir": "test"
}
}