From d37d011a8d10c69f8180ec01d2a4954d83850c57 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 23 Mar 2021 19:45:58 +0100 Subject: [PATCH] Fixed the handling of custom image prefix --- src/BuildConfig.php | 2 +- src/Listener/CopyImagesListener.php | 8 +++++-- tests/Command/BuildDocsCommandTest.php | 24 +++++++++++++++++++ .../expected/main/form/form_type.html | 6 ++--- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/BuildConfig.php b/src/BuildConfig.php index 1a86dab..07320b1 100644 --- a/src/BuildConfig.php +++ b/src/BuildConfig.php @@ -37,7 +37,7 @@ public function __construct() $this->theme = Configuration::THEME_DEFAULT; $this->symfonyVersion = '4.4'; $this->excludedPaths = []; - $this->imagesPublicPrefix = '/_images'; + $this->imagesPublicPrefix = ''; } public function createFileFinder(): Finder diff --git a/src/Listener/CopyImagesListener.php b/src/Listener/CopyImagesListener.php index acde373..2e16487 100644 --- a/src/Listener/CopyImagesListener.php +++ b/src/Listener/CopyImagesListener.php @@ -53,7 +53,11 @@ public function preNodeRender(PreNodeRenderEvent $event) $newAbsoluteFilePath = $this->buildConfig->getImagesDir().'/'.$fileInfo->getFilename(); $fs->copy($sourceImage, $newAbsoluteFilePath, true); - $newUrlPath = $this->buildConfig->getImagesPublicPrefix().'/'.$fileInfo->getFilename(); - $node->setValue($node->getEnvironment()->relativeUrl($newUrlPath)); + if ('' === $this->buildConfig->getImagesPublicPrefix()) { + $newUrlPath = $node->getEnvironment()->relativeUrl('_images/'.$fileInfo->getFilename()); + } else { + $newUrlPath = $this->buildConfig->getImagesPublicPrefix().'/'.$fileInfo->getFilename(); + } + $node->setValue($newUrlPath); } } diff --git a/tests/Command/BuildDocsCommandTest.php b/tests/Command/BuildDocsCommandTest.php index de611a0..d1cfaa3 100644 --- a/tests/Command/BuildDocsCommandTest.php +++ b/tests/Command/BuildDocsCommandTest.php @@ -90,6 +90,30 @@ public function testBuildDocsForPdf() $this->assertStringContainsString('[OK] Build complete', $output); } + public function testBuildDocsWithCustomImagePrefix() + { + $buildConfig = $this->createBuildConfig(); + $buildConfig->setImagesPublicPrefix('/some/custom/prefix-for-images'); + $outputDir = __DIR__.'/../_output'; + + $filesystem = new Filesystem(); + $filesystem->remove($outputDir); + $filesystem->mkdir($outputDir); + + $output = $this->executeCommand( + $buildConfig, + [ + 'source-dir' => __DIR__.'/../fixtures/source/main', + 'output-dir' => $outputDir, + ] + ); + + $this->assertStringContainsString('[OK] Build complete', $output); + + $generatedHtml = file_get_contents($outputDir.'/index.html'); + $this->assertStringContainsString('/some/custom/prefix-for-images/symfony-logo.png', $generatedHtml); + } + private function executeCommand(BuildConfig $buildConfig, array $input): string { $input['--no-theme'] = true; diff --git a/tests/fixtures/expected/main/form/form_type.html b/tests/fixtures/expected/main/form/form_type.html index d2e828e..aa3cc5d 100644 --- a/tests/fixtures/expected/main/form/form_type.html +++ b/tests/fixtures/expected/main/form/form_type.html @@ -3,15 +3,15 @@ - +

FormType Documentation

- +
- \ No newline at end of file +