Skip to content

Commit

Permalink
Use "contao.assets.files_context" for file URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Jun 24, 2022
1 parent ee7e745 commit f76053c
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 42 deletions.
2 changes: 1 addition & 1 deletion calendar-bundle/src/Resources/contao/classes/Calendar.php
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion calendar-bundle/src/Resources/contao/classes/Events.php
Expand Up @@ -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);
Expand Down
Expand Up @@ -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;

/**
Expand All @@ -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,
) {
}

Expand All @@ -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())
Expand Down
Expand Up @@ -23,7 +23,6 @@
class MakeServicesPublicPass implements CompilerPassInterface
{
private const IDS = [
'assets.context',
'assets.packages',
'database_connection',
'debug.stopwatch',
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/File/ModelMetadataTrait.php
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Resources/config/controller.yaml
Expand Up @@ -66,7 +66,7 @@ services:
arguments:
- '@contao.image.studio'
- '@contao.insert_tag.parser'
- '@assets.context'
- '@request_stack'

Contao\CoreBundle\Controller\ContentElement\ImagesController:
arguments:
Expand Down
4 changes: 3 additions & 1 deletion core-bundle/src/Resources/contao/drivers/DC_Folder.php
Expand Up @@ -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++)
{
Expand Down Expand Up @@ -2763,7 +2765,7 @@ protected function generateTree($path, $intMargin, $mount=false, $blnProtected=t
}
else
{
$return .= '<a href="' . Environment::get('path') . '/' . $currentEncoded . '" title="' . StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['view']) . '" target="_blank">' . Image::getHtml($objFile->icon, $objFile->mime) . '</a> ' . $strFileNameEncoded . $thumbnail . '</div> <div class="tl_right">';
$return .= '<a href="' . $staticUrl . $currentEncoded . '" title="' . StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['view']) . '" target="_blank">' . Image::getHtml($objFile->icon, $objFile->mime) . '</a> ' . $strFileNameEncoded . $thumbnail . '</div> <div class="tl_right">';
}

// Buttons
Expand Down
4 changes: 2 additions & 2 deletions core-bundle/src/Resources/contao/elements/ContentPlayer.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
);
}
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Resources/contao/forms/FormSubmit.php
Expand Up @@ -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;
}
}

Expand Down
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Resources/contao/pages/PageRedirect.php
Expand Up @@ -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));
Expand Down
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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}}',
Expand Down
Expand Up @@ -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));
Expand All @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions news-bundle/src/Resources/contao/classes/News.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f76053c

Please sign in to comment.