From f76053c1b486407db1346d002a158506991eaea2 Mon Sep 17 00:00:00 2001 From: Leo Feyer Date: Fri, 24 Jun 2022 08:21:57 +0200 Subject: [PATCH] Use "contao.assets.files_context" for file URLs --- .../src/Resources/contao/classes/Calendar.php | 2 +- .../src/Resources/contao/classes/Events.php | 2 +- .../ContentElement/HyperlinkController.php | 6 ++-- .../Compiler/MakeServicesPublicPass.php | 1 - core-bundle/src/File/ModelMetadataTrait.php | 2 +- .../src/Resources/config/controller.yaml | 2 +- .../Resources/contao/drivers/DC_Folder.php | 4 ++- .../contao/elements/ContentPlayer.php | 4 +-- .../src/Resources/contao/forms/FormSubmit.php | 2 +- .../contao/library/Contao/InsertTags.php | 2 +- .../Resources/contao/pages/PageRedirect.php | 2 +- .../HyperlinkControllerTest.php | 28 +++---------------- .../Compiler/MakeServicesPublicPassTest.php | 2 -- .../src/Resources/contao/classes/News.php | 4 +-- 14 files changed, 21 insertions(+), 42 deletions(-) diff --git a/calendar-bundle/src/Resources/contao/classes/Calendar.php b/calendar-bundle/src/Resources/contao/classes/Calendar.php index 9d0c0ba97ed..53210dc8496 100644 --- a/calendar-bundle/src/Resources/contao/classes/Calendar.php +++ b/calendar-bundle/src/Resources/contao/classes/Calendar.php @@ -387,7 +387,7 @@ protected function addEvent($objEvent, $intStart, $intEnd, $strUrl, $strBase='', if (Validator::isRelativeUrl($url)) { - $url = System::getContainer()->get('assets.context')->getBasePath() . '/' . $url; + $url = Environment::get('path') . '/' . $url; } $link = $url; diff --git a/calendar-bundle/src/Resources/contao/classes/Events.php b/calendar-bundle/src/Resources/contao/classes/Events.php index 55ff72c2e69..8d3dd291980 100644 --- a/calendar-bundle/src/Resources/contao/classes/Events.php +++ b/calendar-bundle/src/Resources/contao/classes/Events.php @@ -427,7 +427,7 @@ public static function generateEventUrl($objEvent, $blnAbsolute=false) if (Validator::isRelativeUrl($url)) { - $url = System::getContainer()->get('assets.context')->getBasePath() . '/' . $url; + $url = Environment::get('path') . '/' . $url; } self::$arrUrlCache[$strCacheKey] = StringUtil::ampersand($url); diff --git a/core-bundle/src/Controller/ContentElement/HyperlinkController.php b/core-bundle/src/Controller/ContentElement/HyperlinkController.php index b2ef3a05d64..f04a71e526d 100644 --- a/core-bundle/src/Controller/ContentElement/HyperlinkController.php +++ b/core-bundle/src/Controller/ContentElement/HyperlinkController.php @@ -19,8 +19,8 @@ use Contao\CoreBundle\String\HtmlAttributes; use Contao\CoreBundle\Twig\FragmentTemplate; use Contao\Validator; -use Symfony\Component\Asset\Context\RequestStackContext; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; /** @@ -31,7 +31,7 @@ class HyperlinkController extends AbstractContentElementController public function __construct( private readonly Studio $studio, private readonly InsertTagParser $insertTagParser, - private readonly RequestStackContext $requestStackContext, + private readonly RequestStack $requestStack, ) { } @@ -41,7 +41,7 @@ protected function getResponse(FragmentTemplate $template, ContentModel $model, $href = $this->insertTagParser->replaceInline($model->url); if (Validator::isRelativeUrl($href)) { - $href = $this->requestStackContext->getBasePath().'/'.$href; + $href = $this->requestStack->getMainRequest()?->getBasePath().'/'.$href; } $linkAttributes = (new HtmlAttributes()) diff --git a/core-bundle/src/DependencyInjection/Compiler/MakeServicesPublicPass.php b/core-bundle/src/DependencyInjection/Compiler/MakeServicesPublicPass.php index eaaa5a1883c..891fd3a6bb1 100644 --- a/core-bundle/src/DependencyInjection/Compiler/MakeServicesPublicPass.php +++ b/core-bundle/src/DependencyInjection/Compiler/MakeServicesPublicPass.php @@ -23,7 +23,6 @@ class MakeServicesPublicPass implements CompilerPassInterface { private const IDS = [ - 'assets.context', 'assets.packages', 'database_connection', 'debug.stopwatch', diff --git a/core-bundle/src/File/ModelMetadataTrait.php b/core-bundle/src/File/ModelMetadataTrait.php index 05be1a1d254..771fae7ab5a 100644 --- a/core-bundle/src/File/ModelMetadataTrait.php +++ b/core-bundle/src/File/ModelMetadataTrait.php @@ -44,7 +44,7 @@ public function getOverwriteMetadata(): Metadata|null $url = $data['imageUrl']; if (Validator::isRelativeUrl($url)) { - $url = System::getContainer()->get('assets.context')->getBasePath().'/'.$url; + $url = System::getContainer()->get('contao.assets.files_context')->getStaticUrl().$url; } $data[Metadata::VALUE_URL] = $url; diff --git a/core-bundle/src/Resources/config/controller.yaml b/core-bundle/src/Resources/config/controller.yaml index 48cb3b5dd3e..93d00237755 100644 --- a/core-bundle/src/Resources/config/controller.yaml +++ b/core-bundle/src/Resources/config/controller.yaml @@ -66,7 +66,7 @@ services: arguments: - '@contao.image.studio' - '@contao.insert_tag.parser' - - '@assets.context' + - '@request_stack' Contao\CoreBundle\Controller\ContentElement\ImagesController: arguments: diff --git a/core-bundle/src/Resources/contao/drivers/DC_Folder.php b/core-bundle/src/Resources/contao/drivers/DC_Folder.php index c48d4c673cf..264727d8e16 100644 --- a/core-bundle/src/Resources/contao/drivers/DC_Folder.php +++ b/core-bundle/src/Resources/contao/drivers/DC_Folder.php @@ -2696,6 +2696,8 @@ protected function generateTree($path, $intMargin, $mount=false, $blnProtected=t return $return; } + $staticUrl = System::getContainer()->get('contao.assets.files_context')->getStaticUrl(); + // Process files for ($h=0, $c=\count($files); $h<$c; $h++) { @@ -2763,7 +2765,7 @@ protected function generateTree($path, $intMargin, $mount=false, $blnProtected=t } else { - $return .= '' . Image::getHtml($objFile->icon, $objFile->mime) . ' ' . $strFileNameEncoded . $thumbnail . '
'; + $return .= '' . Image::getHtml($objFile->icon, $objFile->mime) . ' ' . $strFileNameEncoded . $thumbnail . '
'; } // Buttons diff --git a/core-bundle/src/Resources/contao/elements/ContentPlayer.php b/core-bundle/src/Resources/contao/elements/ContentPlayer.php index 7a5167b3bfb..c3c789a0021 100644 --- a/core-bundle/src/Resources/contao/elements/ContentPlayer.php +++ b/core-bundle/src/Resources/contao/elements/ContentPlayer.php @@ -97,7 +97,7 @@ protected function compile() // Optional poster if ($this->posterSRC && ($objFile = FilesModel::findByUuid($this->posterSRC)) !== null) { - $this->Template->poster = Environment::get('path') . '/' . $objFile->path; + $this->Template->poster = System::getContainer()->get('contao.assets.files_context')->getStaticUrl() . $objFile->path; } $objFiles = $this->objFiles; @@ -146,7 +146,7 @@ protected function compile() $arrFiles[$objFile->extension] = array ( - 'path' => Environment::get('path') . '/' . $objFileModel->path, + 'path' => System::getContainer()->get('contao.assets.files_context')->getStaticUrl() . $objFileModel->path, 'mime' => $objFile->mime, ); } diff --git a/core-bundle/src/Resources/contao/forms/FormSubmit.php b/core-bundle/src/Resources/contao/forms/FormSubmit.php index a665be19003..fa4059fa549 100644 --- a/core-bundle/src/Resources/contao/forms/FormSubmit.php +++ b/core-bundle/src/Resources/contao/forms/FormSubmit.php @@ -100,7 +100,7 @@ public function parse($arrAttributes=null) if ($objModel !== null && is_file(System::getContainer()->getParameter('kernel.project_dir') . '/' . $objModel->path)) { - $this->src = Environment::get('path') . '/' . $objModel->path; + $this->src = System::getContainer()->get('contao.assets.files_context')->getStaticUrl() . $objModel->path; } } diff --git a/core-bundle/src/Resources/contao/library/Contao/InsertTags.php b/core-bundle/src/Resources/contao/library/Contao/InsertTags.php index a7029470e24..c11700bcb21 100644 --- a/core-bundle/src/Resources/contao/library/Contao/InsertTags.php +++ b/core-bundle/src/Resources/contao/library/Contao/InsertTags.php @@ -1017,7 +1017,7 @@ static function ($allowedTag) if ($objFile !== null) { - $arrCache[$strTag] = System::urlEncode(System::getContainer()->get('assets.context')->getBasePath() . '/' . $objFile->path); + $arrCache[$strTag] = System::urlEncode(System::getContainer()->get('contao.assets.files_context')->getStaticUrl() . $objFile->path); break; } } diff --git a/core-bundle/src/Resources/contao/pages/PageRedirect.php b/core-bundle/src/Resources/contao/pages/PageRedirect.php index 7a36c26e09c..7ae6fa93913 100644 --- a/core-bundle/src/Resources/contao/pages/PageRedirect.php +++ b/core-bundle/src/Resources/contao/pages/PageRedirect.php @@ -32,7 +32,7 @@ public function getResponse($objPage) if (Validator::isRelativeUrl($url)) { - $url = System::getContainer()->get('assets.context')->getBasePath() . '/' . $url; + $url = Environment::get('path') . '/' . $url; } return new RedirectResponse($url, $this->getRedirectStatusCode($objPage)); diff --git a/core-bundle/tests/Controller/ContentElement/HyperlinkControllerTest.php b/core-bundle/tests/Controller/ContentElement/HyperlinkControllerTest.php index 534466fb913..93e269451b4 100644 --- a/core-bundle/tests/Controller/ContentElement/HyperlinkControllerTest.php +++ b/core-bundle/tests/Controller/ContentElement/HyperlinkControllerTest.php @@ -14,21 +14,14 @@ use Contao\CoreBundle\Controller\ContentElement\HyperlinkController; use Contao\StringUtil; -use Symfony\Component\Asset\Context\RequestStackContext; +use Symfony\Component\HttpFoundation\RequestStack; class HyperlinkControllerTest extends ContentElementTestCase { public function testOutputsSimpleLink(): void { - $requestStackContext = $this->createMock(RequestStackContext::class); - $requestStackContext - ->expects($this->once()) - ->method('getBasePath') - ->willReturn('') - ; - $response = $this->renderWithModelData( - new HyperlinkController($this->getDefaultStudio(), $this->getDefaultInsertTagParser(), $requestStackContext), + new HyperlinkController($this->getDefaultStudio(), $this->getDefaultInsertTagParser(), new RequestStack()), [ 'type' => 'hyperlink', 'url' => 'my-link.html', @@ -53,14 +46,8 @@ public function testOutputsSimpleLink(): void public function testOutputsLinkWithBeforeAndAfterText(): void { - $requestStackContext = $this->createMock(RequestStackContext::class); - $requestStackContext - ->expects($this->never()) - ->method('getBasePath') - ; - $response = $this->renderWithModelData( - new HyperlinkController($this->getDefaultStudio(), $this->getDefaultInsertTagParser(), $requestStackContext), + new HyperlinkController($this->getDefaultStudio(), $this->getDefaultInsertTagParser(), new RequestStack()), [ 'type' => 'hyperlink', 'url' => 'https://www.php.net/manual/en/function.sprintf.php', @@ -86,15 +73,8 @@ public function testOutputsLinkWithBeforeAndAfterText(): void public function testOutputsImageLink(): void { - $requestStackContext = $this->createMock(RequestStackContext::class); - $requestStackContext - ->expects($this->once()) - ->method('getBasePath') - ->willReturn('') - ; - $response = $this->renderWithModelData( - new HyperlinkController($this->getDefaultStudio(), $this->getDefaultInsertTagParser(), $requestStackContext), + new HyperlinkController($this->getDefaultStudio(), $this->getDefaultInsertTagParser(), new RequestStack()), [ 'type' => 'hyperlink', 'url' => 'foo.html#{{demo}}', diff --git a/core-bundle/tests/DependencyInjection/Compiler/MakeServicesPublicPassTest.php b/core-bundle/tests/DependencyInjection/Compiler/MakeServicesPublicPassTest.php index 54607ebbea4..bfa2533ff24 100644 --- a/core-bundle/tests/DependencyInjection/Compiler/MakeServicesPublicPassTest.php +++ b/core-bundle/tests/DependencyInjection/Compiler/MakeServicesPublicPassTest.php @@ -24,7 +24,6 @@ public function testMakesTheServicesPublic(): void $container = new ContainerBuilder(); // Definitions - $container->setDefinition('assets.context', (new Definition())->setPublic(false)); $container->setDefinition('assets.packages', (new Definition())->setPublic(false)); $container->setDefinition('fragment.handler', (new Definition())->setPublic(false)); $container->setDefinition('monolog.logger.contao', (new Definition())->setPublic(false)); @@ -47,7 +46,6 @@ public function testMakesTheServicesPublic(): void $pass->process($container); // Definitions - $this->assertTrue($container->getDefinition('assets.context')->isPublic()); $this->assertTrue($container->getDefinition('assets.packages')->isPublic()); $this->assertTrue($container->getDefinition('fragment.handler')->isPublic()); $this->assertTrue($container->getDefinition('monolog.logger.contao')->isPublic()); diff --git a/news-bundle/src/Resources/contao/classes/News.php b/news-bundle/src/Resources/contao/classes/News.php index 820d81a1666..e46d7f8b448 100644 --- a/news-bundle/src/Resources/contao/classes/News.php +++ b/news-bundle/src/Resources/contao/classes/News.php @@ -317,7 +317,7 @@ public static function generateNewsUrl($objItem, $blnAddArchive=false, $blnAbsol if (Validator::isRelativeUrl($url)) { - $url = System::getContainer()->get('assets.context')->getBasePath() . '/' . $url; + $url = Environment::get('path') . '/' . $url; } self::$arrUrlCache[$strCacheKey] = StringUtil::ampersand($url); @@ -426,7 +426,7 @@ protected function getLink($objItem, $strUrl, $strBase='') if (Validator::isRelativeUrl($url)) { - $url = System::getContainer()->get('assets.context')->getBasePath() . '/' . $url; + $url = Environment::get('path') . '/' . $url; } return $url;