backports murph-skeleton
This commit is contained in:
parent
ed6a13d773
commit
f15f93f932
|
@ -3,6 +3,7 @@
|
|||
namespace App\Core\Router;
|
||||
|
||||
use App\Core\Entity\Redirect;
|
||||
use App\Core\String\UrlBuilder;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
|
@ -13,6 +14,13 @@ use Symfony\Component\HttpFoundation\Request;
|
|||
*/
|
||||
class RedirectBuilder
|
||||
{
|
||||
protected UrlBuilder $urlBuilder;
|
||||
|
||||
public function __construct(UrlBuilder $urlBuilder)
|
||||
{
|
||||
$this->urlBuilder = $urlBuilder;
|
||||
}
|
||||
|
||||
public function buildResponse(Redirect $redirect, Request $request): RedirectResponse
|
||||
{
|
||||
return new RedirectResponse(
|
||||
|
@ -31,6 +39,8 @@ class RedirectBuilder
|
|||
$location = preg_replace('`'.$redirect->getRule().'`sU', $redirect->getLocation(), $data['path']);
|
||||
}
|
||||
|
||||
$location = $this->urlBuilder->replaceTags($location);
|
||||
|
||||
if ($redirect->getReuseQueryString() && count($request->query)) {
|
||||
$location .= sprintf('?%s', http_build_query($request->query->all()));
|
||||
}
|
||||
|
|
61
core/String/FileInformationBuilder.php
Normal file
61
core/String/FileInformationBuilder.php
Normal file
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
namespace App\Core\String;
|
||||
|
||||
use App\Core\FileManager\FsFileManager;
|
||||
use App\Core\Repository\FileInformationRepositoryQuery;
|
||||
|
||||
/**
|
||||
* class FileInformationBuilder.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class FileInformationBuilder
|
||||
{
|
||||
protected FsFileManager $fsManager;
|
||||
protected FileInformationRepositoryQuery $query;
|
||||
|
||||
public function __construct(FsFileManager $fsManager, FileInformationRepositoryQuery $query)
|
||||
{
|
||||
$this->fsManager = $fsManager;
|
||||
$this->query = $query;
|
||||
}
|
||||
|
||||
public function replaceTags(string $value)
|
||||
{
|
||||
preg_match_all(
|
||||
'#\{\{\s*fattr://(?P<hash>[a-z0-9]+)\/(?P<label>.+)\s*\}\}#isU',
|
||||
$value,
|
||||
$match,
|
||||
PREG_SET_ORDER
|
||||
);
|
||||
|
||||
$fileInfos = [];
|
||||
|
||||
foreach ($match as $block) {
|
||||
$hash = $block['hash'];
|
||||
$label = $block['label'];
|
||||
$tagValue = null;
|
||||
|
||||
if (!isset($fileInfos[$hash])) {
|
||||
$fileInfos[$hash] = $this->query->create()
|
||||
->where('.id LIKE :hash')
|
||||
->setParameter(':hash', $hash.'%')
|
||||
->findOne()
|
||||
;
|
||||
}
|
||||
|
||||
if ($fileInfos[$hash]) {
|
||||
foreach ($fileInfos[$hash]->getAttributes() as $attribute) {
|
||||
if ($attribute['label'] === $label) {
|
||||
$tagValue = htmlspecialchars($attribute['value'], ENT_HTML5 | ENT_QUOTES);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$value = str_replace($block[0], $tagValue, $value);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
65
core/String/UrlBuilder.php
Normal file
65
core/String/UrlBuilder.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
namespace App\Core\String;
|
||||
|
||||
use App\Core\Site\SiteRequest;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
|
||||
/**
|
||||
* class UrlBuilder.
|
||||
*
|
||||
* @author Simon Vieille <simon@deblan.fr>
|
||||
*/
|
||||
class UrlBuilder
|
||||
{
|
||||
protected UrlGeneratorInterface $urlGenerator;
|
||||
protected SiteRequest $siteRequest;
|
||||
|
||||
public function __construct(UrlGeneratorInterface $urlGenerator, SiteRequest $siteRequest)
|
||||
{
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->siteRequest = $siteRequest;
|
||||
}
|
||||
|
||||
public function replaceTags(string $value): string
|
||||
{
|
||||
preg_match_all(
|
||||
'#\{\{\s*url://(?P<route>[a-z0-9_]+)(\?(?P<params>.*))?\s*\}\}#isU',
|
||||
$value,
|
||||
$match,
|
||||
PREG_SET_ORDER
|
||||
);
|
||||
|
||||
$domain = $this->siteRequest->getDomain();
|
||||
|
||||
foreach ($match as $block) {
|
||||
$url = null;
|
||||
|
||||
try {
|
||||
$block['params'] = $block['params'] ?? '';
|
||||
$block['params'] = str_replace(['&', ' '], ['&', '%20'], $block['params']);
|
||||
$route = $block['route'];
|
||||
parse_str($block['params'], $params);
|
||||
|
||||
if (!isset($params['_domain'])) {
|
||||
$params['_domain'] = $domain;
|
||||
}
|
||||
|
||||
$url = $this->urlGenerator->generate($route, $params, UrlGeneratorInterface::ABSOLUTE_URL);
|
||||
|
||||
parse_str(parse_url($url)['query'] ?? '', $infos);
|
||||
|
||||
if (isset($infos['_domain'])) {
|
||||
unset($params['_domain']);
|
||||
|
||||
$url = $this->urlGenerator->generate($route, $params, UrlGeneratorInterface::ABSOLUTE_URL);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
}
|
||||
|
||||
$value = str_replace($block[0], $url, $value);
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
|
@ -7,14 +7,19 @@ use App\Core\Repository\FileInformationRepositoryQuery;
|
|||
use function Symfony\Component\String\u;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use App\Core\String\FileInformationBuilder;
|
||||
|
||||
class FileInformationExtension extends AbstractExtension
|
||||
{
|
||||
protected FsFileManager $fsManage²r;
|
||||
protected FileInformationRepositoryQuery $query;
|
||||
protected FileInformationBuilder $fileInfoBuilder;
|
||||
|
||||
public function __construct(FsFileManager $fsManager, FileInformationRepositoryQuery $query)
|
||||
public function __construct(
|
||||
FileInformationBuilder $fileInfoBuilder,
|
||||
FsFileManager $fsManager,
|
||||
FileInformationRepositoryQuery $query
|
||||
)
|
||||
{
|
||||
$this->fileInfoBuilder = $fileInfoBuilder;
|
||||
$this->fsManager = $fsManager;
|
||||
$this->query = $query;
|
||||
}
|
||||
|
@ -54,32 +59,8 @@ class FileInformationExtension extends AbstractExtension
|
|||
return null;
|
||||
}
|
||||
|
||||
public function fileAttributes(?string $content): ?string
|
||||
public function fileAttributes(?string $content): string
|
||||
{
|
||||
preg_match_all('#\{\{\s*fattr://(?P<hash>[a-z0-9]+)\/(?P<label>.+)\s*\}\}#isU', $content, $match, PREG_SET_ORDER);
|
||||
|
||||
foreach ($match as $block) {
|
||||
$hash = $block['hash'];
|
||||
$label = $block['label'];
|
||||
$value = null;
|
||||
|
||||
$fileInfo = $this->query->create()
|
||||
->where('.id LIKE :hash')
|
||||
->setParameter(':hash', $hash.'%')
|
||||
->findOne()
|
||||
;
|
||||
|
||||
if ($fileInfo) {
|
||||
foreach ($fileInfo->getAttributes() as $attribute) {
|
||||
if ($attribute['label'] === $label) {
|
||||
$value = htmlspecialchars($attribute['value'], ENT_HTML5 | ENT_QUOTES);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$content = str_replace($block[0], $value, $content);
|
||||
}
|
||||
|
||||
return $content;
|
||||
return $this->fileInfoBuilder->replaceTags((string) $content);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,16 +6,15 @@ use App\Core\Site\SiteRequest;
|
|||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use App\Core\String\UrlBuilder;
|
||||
|
||||
class UrlExtension extends AbstractExtension
|
||||
{
|
||||
protected UrlGeneratorInterface $urlGenerator;
|
||||
protected SiteRequest $siteRequest;
|
||||
protected UrlBuilder $urlBuilder;
|
||||
|
||||
public function __construct(UrlGeneratorInterface $urlGenerator, SiteRequest $siteRequest)
|
||||
public function __construct(UrlBuilder $urlBuilder)
|
||||
{
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->siteRequest = $siteRequest;
|
||||
$this->urlBuilder = $urlBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -30,36 +29,6 @@ class UrlExtension extends AbstractExtension
|
|||
|
||||
public function replaceUrl(?string $content)
|
||||
{
|
||||
preg_match_all('#\{\{\s*url://(?P<route>[a-z0-9_]+)(\?(?P<params>.*))?\s*\}\}#isU', $content, $match, PREG_SET_ORDER);
|
||||
|
||||
foreach ($match as $block) {
|
||||
$url = null;
|
||||
|
||||
try {
|
||||
$block['params'] = $block['params'] ?? '';
|
||||
$block['params'] = str_replace(['&', ' '], ['&', '%20'], $block['params']);
|
||||
$route = $block['route'];
|
||||
parse_str($block['params'], $params);
|
||||
|
||||
if (!isset($params['_domain'])) {
|
||||
$params['_domain'] = $this->siteRequest->getDomain();
|
||||
}
|
||||
|
||||
$url = $this->urlGenerator->generate($route, $params, UrlGeneratorInterface::ABSOLUTE_URL);
|
||||
|
||||
parse_str(parse_url($url)['query'] ?? '', $infos);
|
||||
|
||||
if (isset($infos['_domain'])) {
|
||||
unset($params['_domain']);
|
||||
|
||||
$url = $this->urlGenerator->generate($route, $params, UrlGeneratorInterface::ABSOLUTE_URL);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
}
|
||||
|
||||
$content = str_replace($block[0], $url, $content);
|
||||
}
|
||||
|
||||
return $content;
|
||||
return $this->urlBuilder->replaceTags((string) $content);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue