Skip to content

Commit

Permalink
Update to symfony 6.0 and drop support for symfony 4 and php 7.Update…
Browse files Browse the repository at this point in the history
… to symfony 6.0 and drop support for symfony 4
  • Loading branch information
mfadul24 committed Jul 31, 2023
1 parent f2ba556 commit 1851522
Show file tree
Hide file tree
Showing 30 changed files with 168 additions and 158 deletions.
2 changes: 1 addition & 1 deletion DependencyInjection/Compiler/SetRouterPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*/
class SetRouterPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
$container->setAlias('router', 'jms_i18n_routing.router')->setPublic(true);

Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

final class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): \Symfony\Component\Config\Definition\Builder\TreeBuilder
{
$tb = new TreeBuilder('jms_i18n_routing');

Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/JMSI18nRoutingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
class JMSI18nRoutingExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$config = $this->processConfiguration(new Configuration, $configs);

Expand Down Expand Up @@ -84,7 +84,7 @@ public function load(array $configs, ContainerBuilder $container)
}
}

public function getAlias()
public function getAlias(): string
{
return 'jms_i18n_routing';
}
Expand Down
6 changes: 3 additions & 3 deletions EventListener/CookieSettingListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,18 @@ public function __construct($cookieName, $cookieLifetime, $cookiePath, $cookieDo
$this->cookieHttponly = $cookieHttponly;
}

public function onKernelResponse(ResponseEvent $event)
public function onKernelResponse(ResponseEvent $event): void
{
//Check if the current response contains an error. If it does, do not set the cookie as the Locale may not be properly set
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType() || !($event->getResponse()->isSuccessful() || $event->getResponse()->isRedirection())) {
if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType() || !($event->getResponse()->isSuccessful() || $event->getResponse()->isRedirection())) {
return;
}

$request = $event->getRequest();

if (!$request->cookies->has($this->cookieName)
|| $request->cookies->get($this->cookieName) !== $request->getLocale()) {
$event->getResponse()->headers->setCookie(new Cookie($this->cookieName, $request->getLocale(), time() + $this->cookieLifetime, $this->cookiePath, $this->cookieDomain, $this->cookieSecure, $this->cookieHttponly));
$event->getResponse()->headers->setCookie(\Symfony\Component\HttpFoundation\Cookie::create($this->cookieName, $request->getLocale(), time() + $this->cookieLifetime, $this->cookiePath, $this->cookieDomain, $this->cookieSecure, $this->cookieHttponly));
}
}
}
4 changes: 2 additions & 2 deletions EventListener/LocaleChoosingListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function __construct($defaultLocale, array $locales, LocaleResolverInterf
$this->localeResolver = $localeResolver;
}

public function onKernelException(ExceptionEvent $event)
public function onKernelException(ExceptionEvent $event): void
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
if (HttpKernelInterface::MAIN_REQUEST !== $event->getRequestType()) {
return;
}

Expand Down
38 changes: 19 additions & 19 deletions Exception/NotAcceptableLanguageException.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php

/*
* Copyright 2012 Johannes M. Schmitt <schmittjoh@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*
* Copyright 2012 Johannes M. Schmitt <schmittjoh@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\I18nRoutingBundle\Exception;

class NotAcceptableLanguageException extends NotAcceptableException
Expand All @@ -36,8 +36,8 @@ public function getRequestedLanguage()
return $this->requestedLanguage;
}

public function getAvailableLanguages()
public function getAvailableLanguages(): array
{
return $this->availableLanguages;
}
}
}
7 changes: 4 additions & 3 deletions JMSI18nRoutingBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\DependencyInjection\Compiler\ResolveDefinitionTemplatesPass;
use JMS\I18nRoutingBundle\DependencyInjection\Compiler\SetRouterPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
Expand All @@ -31,13 +32,13 @@
*/
class JMSI18nRoutingBundle extends Bundle
{
public function build(ContainerBuilder $container)
public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new SetRouterPass());
}

public function getContainerExtension()
public function getContainerExtension(): ?ExtensionInterface
{
return new JMSI18nRoutingExtension();
}
}
}
4 changes: 2 additions & 2 deletions Router/DefaultLocaleResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct($cookieName, array $hostMap = array())
/**
* {@inheritDoc}
*/
public function resolveLocale(Request $request, array $availableLocales)
public function resolveLocale(Request $request, array $availableLocales): ?string
{
if ($this->hostMap && isset($this->hostMap[$host = $request->getHost()])) {
return $this->hostMap[$host];
Expand Down Expand Up @@ -74,4 +74,4 @@ public function resolveLocale(Request $request, array $availableLocales)

return null;
}
}
}
12 changes: 6 additions & 6 deletions Router/DefaultPatternGenerationStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
*/
class DefaultPatternGenerationStrategy implements PatternGenerationStrategyInterface
{
const STRATEGY_PREFIX = 'prefix';
const STRATEGY_PREFIX_EXCEPT_DEFAULT = 'prefix_except_default';
const STRATEGY_CUSTOM = 'custom';
public const STRATEGY_PREFIX = 'prefix';
public const STRATEGY_PREFIX_EXCEPT_DEFAULT = 'prefix_except_default';
public const STRATEGY_CUSTOM = 'custom';

private $strategy;
private $translator;
Expand All @@ -40,7 +40,7 @@ public function __construct($strategy, TranslatorInterface $translator, array $l
/**
* {@inheritDoc}
*/
public function generateI18nPatterns($routeName, Route $route)
public function generateI18nPatterns($routeName, Route $route): array
{
$patterns = array();
foreach ($route->getOption('i18n_locales') ?: $this->locales as $locale) {
Expand Down Expand Up @@ -81,12 +81,12 @@ public function generateI18nPatterns($routeName, Route $route)
/**
* {@inheritDoc}
*/
public function addResources(RouteCollection $i18nCollection)
public function addResources(RouteCollection $i18nRouteCollection): void
{
foreach ($this->locales as $locale) {
if (file_exists($metadata = $this->cacheDir.'/translations/catalogue.'.$locale.'.php.meta')) {
foreach (unserialize(file_get_contents($metadata)) as $resource) {
$i18nCollection->addResource($resource);
$i18nRouteCollection->addResource($resource);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Router/DefaultRouteExclusionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
class DefaultRouteExclusionStrategy implements RouteExclusionStrategyInterface
{
public function shouldExcludeRoute($routeName, Route $route)
public function shouldExcludeRoute($routeName, Route $route): bool
{
if ('_' === $routeName[0]) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion Router/I18nLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(RouteExclusionStrategyInterface $routeExclusionStrat
$this->patternGenerationStrategy = $patternGenerationStrategy;
}

public function load(RouteCollection $collection)
public function load(RouteCollection $collection): RouteCollection
{
$i18nCollection = new RouteCollection();
foreach ($collection->getResources() as $resource) {
Expand Down
52 changes: 24 additions & 28 deletions Router/I18nRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
namespace JMS\I18nRoutingBundle\Router;

use JMS\I18nRoutingBundle\Exception\NotAcceptableLanguageException;

use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;
use Symfony\Component\Routing\RequestContext;

/**
* I18n Router implementation.
Expand All @@ -35,12 +35,12 @@
*/
class I18nRouter extends Router
{
private $hostMap = array();
private $i18nLoaderId;
private array $hostMap = array();
private ?string $i18nLoaderId;
private $container;
protected $defaultLocale;
private $redirectToHost = true;
private $localeResolver;
private bool $redirectToHost = true;
private ?LocaleResolverInterface $localeResolver;

/**
* Constructor.
Expand All @@ -58,7 +58,7 @@ public function __construct()
$this->container = func_get_arg(0);
}

public function setLocaleResolver(LocaleResolverInterface $resolver)
public function setLocaleResolver(LocaleResolverInterface $resolver): void
{
$this->localeResolver = $resolver;
}
Expand All @@ -69,35 +69,35 @@ public function setLocaleResolver(LocaleResolverInterface $resolver)
*
* @param Boolean $bool
*/
public function setRedirectToHost($bool)
public function setRedirectToHost(bool $bool): void
{
$this->redirectToHost = (Boolean) $bool;
$this->redirectToHost = $bool;
}

/**
* Sets the host map to use.
*
* @param array $hostMap a map of locales to hosts
*/
public function setHostMap(array $hostMap)
public function setHostMap(array $hostMap): void
{
$this->hostMap = $hostMap;
}

public function setI18nLoaderId($id)
public function setI18nLoaderId(string $id): void
{
$this->i18nLoaderId = $id;
}

public function setDefaultLocale($locale)
public function setDefaultLocale(string $locale): void
{
$this->defaultLocale = $locale;
}

/**
* {@inheritdoc}
*/
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH): string
{
// determine the most suitable locale to use for route generation
$currentLocale = $this->context->getParameter('_locale');
Expand Down Expand Up @@ -147,27 +147,27 @@ public function generate($name, $parameters = array(), $referenceType = self::AB
/**
* {@inheritdoc}
*/
public function match($url)
public function match(string $pathinfo): array
{
return $this->matchI18n(parent::match($url), $url);
return $this->matchI18n(parent::match($pathinfo), $pathinfo);
}

public function getRouteCollection()
public function getRouteCollection(): \Symfony\Component\Routing\RouteCollection
{
$collection = parent::getRouteCollection();

return $this->container->get($this->i18nLoaderId)->load($collection);
}

public function getOriginalRouteCollection()
public function getOriginalRouteCollection(): \Symfony\Component\Routing\RouteCollection
{
return parent::getRouteCollection();
}

/**
* To make compatible with Symfony <2.4
*/
public function matchRequest(Request $request)
public function matchRequest(Request $request): array
{
$matcher = $this->getMatcher();
$pathInfo = $request->getPathInfo();
Expand All @@ -179,12 +179,8 @@ public function matchRequest(Request $request)
return $this->matchI18n($matcher->matchRequest($request), $pathInfo);
}

private function matchI18n(array $params, $url)
private function matchI18n(array $params, string $url): array
{
if (false === $params) {
return false;
}

$request = $this->getRequest();

if (isset($params['_locales'])) {
Expand Down Expand Up @@ -279,7 +275,7 @@ private function matchI18n(array $params, $url)
/**
* @return Request|null
*/
private function getRequest()
private function getRequest(): ?Request
{
$request = null;
if ($this->container->has('request_stack')) {
Expand Down
4 changes: 2 additions & 2 deletions Router/LocaleResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ interface LocaleResolverInterface
* @return string|null may return null if no suitable locale is found, may also
* return a locale which is not available for the matched route
*/
function resolveLocale(Request $request, array $availableLocales);
}
function resolveLocale(Request $request, array $availableLocales): ?string;
}
Loading

0 comments on commit 1851522

Please sign in to comment.