first commit

This commit is contained in:
2025-08-02 16:30:27 +02:00
commit 23646bfcee
14851 changed files with 1750626 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
<?php
namespace KupShop\LocalePrefixBundle\EventListener;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Event\DynamicRouteEvent;
class DynamicRouteListener extends \KupShop\ContentBundle\EventListener\DynamicRouteListener
{
/** @var LanguageContext */
private $languageContext;
/**
* @required
*/
public function setLanguageContext(LanguageContext $languageContext)
{
$this->languageContext = $languageContext;
}
protected function getFallbackMenuLink(DynamicRouteEvent $event)
{
$link = parent::getFallbackMenuLink($event);
if ($link && $this->languageContext->getDefaultId() !== $this->languageContext->getActiveId()) {
$languagePrefix = '/'.$this->languageContext->getActiveId();
$link = $languagePrefix.'/'.ltrim($link, '/');
}
return $link;
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace KupShop\LocalePrefixBundle\EventListener;
use KupShop\LocalePrefixBundle\Util\LocalePrefixUtil;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class LanguageSetterSubscriber implements EventSubscriberInterface
{
public function __construct(private LocalePrefixUtil $localePrefixUtil)
{
}
public static function getSubscribedEvents()
{
return [
KernelEvents::REQUEST => [
['setLanguage', 9999],
],
];
}
public function setLanguage(KernelEvent $event)
{
$request = $event->getRequest();
$this->localePrefixUtil->setRequestAttribute($request);
$this->localePrefixUtil->setLanguageFromLocalePrefix($request->getPathInfo());
}
}

View File

@@ -0,0 +1,9 @@
<?php
namespace KupShop\LocalePrefixBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class LocalePrefixBundle extends Bundle
{
}

View File

@@ -0,0 +1,38 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false
KupShop\LocalePrefixBundle\:
resource: ../../{Util}
KupShop\LocalePrefixBundle\EventListener\LanguageSetterSubscriber:
class: KupShop\LocalePrefixBundle\EventListener\LanguageSetterSubscriber
KupShop\ContentBundle\EventListener\DynamicRouteListener:
class: KupShop\LocalePrefixBundle\EventListener\DynamicRouteListener
autowire: true
tags:
- {name: kernel.event_subscriber}
KupShop\KupShopBundle\Util\LegacyUrlGenerator:
class: KupShop\LocalePrefixBundle\Util\LegacyUrlGenerator
autoconfigure: false
public: true
KupShop\LocalePrefixBundle\Routing\LocalePrefixAwareRouter:
parent: router.default
autowire: true
autoconfigure: false
public: false
router:
class: KupShop\LocalePrefixBundle\Routing\LocalePrefixAwareRouter
parent: router.default
public: false
autoconfigure: false
autowire: true
Symfony\Component\Routing\RouterInterface:
alias: router

View File

@@ -0,0 +1,97 @@
<?php
namespace KupShop\LocalePrefixBundle\Routing;
use KupShop\KupShopBundle\Context\ContextManager;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use KupShop\KupShopBundle\Util\HTTPUtil;
use KupShop\LocalePrefixBundle\Util\LocalePrefixUtil;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
use Symfony\Contracts\Service\Attribute\Required;
class LocalePrefixAwareRouter extends \KupShop\I18nBundle\Routing\LanguageAwareRouter
{
#[Required]
public LocalePrefixUtil $localePrefixUtil;
public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH): string
{
if ($this->getLanguageContext()->getDefaultId() === $this->languageContext->getActiveId()) {
$localePrefix = '';
} else {
$localePrefix = '/'.$this->getLanguageContext()->getActiveId();
}
if ($referenceType == self::ABSOLUTE_URL) {
$url = parse_url($this->getGenerator()->generate($name, $parameters, $referenceType));
$url['path'] = $localePrefix.$url['path'];
return HTTPUtil::http_build_url($url);
}
return $localePrefix.$this->getGenerator()->generate($name, $parameters, $referenceType);
}
public function match($pathinfo): array
{
$this->localePrefixUtil->setLanguageContext($this->getLanguageContext());
return $this->getMatcher()->match($this->localePrefixUtil->getPathWithoutPrefix($pathinfo));
}
public function matchRequest(Request $request): array
{
$this->localePrefixUtil->setLanguageContext($this->getLanguageContext());
$matcher = $this->getMatcher();
if (!$matcher instanceof RequestMatcherInterface) {
// fallback to the default UrlMatcherInterface
return $matcher->match($this->localePrefixUtil->getPathWithoutPrefix($request->getPathInfo()));
}
if ($this->localePrefixUtil->getLanguageFromPath($request->getPathInfo())) {
return $matcher->matchRequest($request->duplicate(null, null, null, null, null, [
'REQUEST_URI' => mb_substr($request->getPathInfo(), 3),
]));
} else {
return $matcher->matchRequest($request);
}
}
public function generateInLanguage($language, $routeName, $params = [])
{
switch ($routeName) {
case 'kupshop_catalog_catalog_section_1':
case 'kupshop_catalog_catalog_section':
case 'kupshop_catalog_catalog_campaign':
$result = parent::generateInLanguage($language, $routeName, $params);
if ($this->getLanguageContext()->getDefaultId() != $language) {
$result = '/'.$language.$result;
}
break;
case 'products':
/** @var ContextManager $contextManager */
$contextManager = ServiceContainer::getService(ContextManager::class);
$result = $contextManager->activateContexts(
[LanguageContext::class => $language],
function () use ($language, $routeName, $params) {
return parent::generateInLanguage($language, $routeName, $params);
});
break;
case 'kupshop_indexedfilter_indexedfiltercatalog_indexedsection':
case 'page_not_found':
if ($url = $this->resolveCustomUrl($language, $params)) {
return '/'.$language.$url;
}
// no break
default:
$result = parent::generateInLanguage($language, $routeName, $params);
}
return $result;
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace KupShop\LocalePrefixBundle\Util;
use KupShop\KupShopBundle\Config;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\KupShopBundle\Util\StringUtil;
class LegacyUrlGenerator extends \KupShop\KupShopBundle\Util\LegacyUrlGenerator
{
public function generate($params): string
{
return static::getUrlWithLocalePrefix(
parent::generate($params),
(array) $params
);
}
public static function getUrlWithLocalePrefix(string $url, array $params): string
{
$languageContext = Contexts::get(LanguageContext::class);
if ($languageContext->translationActive()) {
$prefix = '';
if (!empty($params['absolute'])) {
$prefix = rtrim(Config::get()['Addr']['full'], '/');
$url = str_replace($prefix, '', $url);
}
$languagePrefix = '/'.$languageContext->getActiveId();
// Pokud uz vygenerovana URL zacina language prefixem, tak ho nechci pridat znovu
// URL muze zacinat language prefixem, pokud je uz v LegacyUrlGenerator generovana pres path()
if (StringUtil::startsWith($url, $languagePrefix.'/')) {
$languagePrefix = '';
}
// Odkazy na data jsou bez language prefixu
if (StringUtil::startsWith($url, '/data/')) {
$languagePrefix = '';
}
$url = $prefix.$languagePrefix.$url;
}
return $url;
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace KupShop\LocalePrefixBundle\Util;
use KupShop\KupShopBundle\Context\LanguageContext;
use KupShop\KupShopBundle\Util\Contexts;
use Symfony\Component\HttpFoundation\Request;
class LocalePrefixUtil
{
private ?LanguageContext $languageContext;
public function getLanguageFromPath(string $path): ?string
{
$langRaw = substr($path, 1, 3);
$lang = trim($langRaw, '/');
$languages = $this->getLanguageContext()->getSupported();
return (str_ends_with($langRaw, '/') && isset($languages[$lang])) ? $lang : null;
}
public function getPathWithoutPrefix(string $path): string
{
return $this->getLanguageFromPath($path) ? mb_substr($path, 3) : $path;
}
public function setRequestAttribute(Request $request)
{
if ($this->getLanguageFromPath($request->getPathInfo())) {
$request->attributes->set('uri-without-language-prefix', mb_substr($request->getRequestUri(), 3));
}
}
public function setLanguageFromLocalePrefix(string $path)
{
$languageContext = $this->getLanguageContext();
$languageContext->activate($this->getLanguageFromPath($path) ?? $languageContext->getDefaultId());
}
public function setLanguageContext(LanguageContext $languageContext): self
{
$this->languageContext = $languageContext;
return $this;
}
private function getLanguageContext(): LanguageContext
{
return $this->languageContext ?? Contexts::get(LanguageContext::class);
}
}