Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Symfony 7 #2888

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"psr/clock": "^1.0",
"symfony/polyfill-mbstring": "^1.0",
"symfony/polyfill-php80": "^1.16",
"symfony/translation": "^3.4 || ^4.0 || ^5.0 || ^6.0"
"symfony/translation": "^3.4 || ^4.0 || ^5.0 || ^6.0 || ^7.0"
},
"require-dev": {
"doctrine/dbal": "^2.0 || ^3.1.4 || ^4.0",
Expand Down
38 changes: 38 additions & 0 deletions lazy/Carbon/TranslatorImmutableStrongType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Carbon;

use Symfony\Component\Config\ConfigCacheFactoryInterface;
use Symfony\Component\Translation\Exception\InvalidArgumentException;

if (!class_exists(LazyTranslatorImmutable::class, false)) {
class LazyTranslatorImmutable extends AbstractTranslatorImmutable
{
/**
* @throws InvalidArgumentException
*/
public function setLocale(string $locale): void
{
$this->setLocaleIdentifier($locale);
}

public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory): void
{
$this->setConfigCacheFactoryObject($configCacheFactory);
}

public function setFallbackLocales(array $locales): void
{
$this->setFallbackLocalesArray($locales);
}
}
}
34 changes: 34 additions & 0 deletions lazy/Carbon/TranslatorImmutableWeakType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Carbon;

use Symfony\Component\Config\ConfigCacheFactoryInterface;

if (!class_exists(LazyTranslatorImmutable::class, false)) {
class LazyTranslatorImmutable extends AbstractTranslatorImmutable
{
public function setLocale($locale)
{
$this->setLocaleIdentifier($locale);
}

public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
{
$this->setConfigCacheFactoryObject($configCacheFactory);
}

public function setFallbackLocales(array $locales)
{
$this->setFallbackLocalesArray($locales);
}
}
}
9 changes: 9 additions & 0 deletions lazy/Carbon/TranslatorStrongType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Carbon;

use Symfony\Component\Translation\Exception\InvalidArgumentException;
use Symfony\Component\Translation\MessageCatalogueInterface;

if (!class_exists(LazyTranslator::class, false)) {
Expand Down Expand Up @@ -48,5 +49,13 @@ private function getPrivateProperty($instance, string $field)
return $this->$field;
})->call($instance, $field);
}

/**
* @throws InvalidArgumentException
*/
public function setLocale(string $locale): void
{
parent::setLocaleIdentifier($locale);
}
}
}
13 changes: 13 additions & 0 deletions lazy/Carbon/TranslatorWeakType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Carbon;

use Symfony\Component\Translation\Exception\InvalidArgumentException;

if (!class_exists(LazyTranslator::class, false)) {
class LazyTranslator extends AbstractTranslator
{
Expand All @@ -28,5 +30,16 @@ public function trans($id, array $parameters = [], $domain = null, $locale = nul
{
return $this->translate($id, $parameters, $domain, $locale);
}

public function setLocale($locale)
{
try {
parent::setLocaleIdentifier($locale);
} catch (InvalidArgumentException $e) {
return false;
}

return true;
}
}
}
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@

<php>
<ini name="max_execution_time" value="0"/>
<ini name="memory_limit" value="256M"/>
</php>
</phpunit>
3 changes: 2 additions & 1 deletion src/Carbon/AbstractTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,14 @@ public function getMessages($locale = null)
}

/**
* @internal
* Set the current translator locale and indicate if the source locale file exists
*
* @param string $locale locale ex. en
*
* @return bool
*/
public function setLocale($locale)
protected function setLocaleIdentifier($locale)
{
$locale = preg_replace_callback('/[-_]([a-z]{2,}|\d{2,})/', function ($matches) {
// _2-letters or YUE is a region, _3+-letters is a variant
Expand Down
104 changes: 104 additions & 0 deletions src/Carbon/AbstractTranslatorImmutable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

/**
* This file is part of the Carbon package.
*
* (c) Brian Nesbitt <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Carbon;

use Carbon\Exceptions\ImmutableException;
use Symfony\Component\Config\ConfigCacheFactoryInterface;
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;

class AbstractTranslatorImmutable extends Translator
{
/** @var bool */
private $constructed = false;

public function __construct($locale, MessageFormatterInterface $formatter = null, $cacheDir = null, $debug = false)
{
parent::__construct($locale, $formatter, $cacheDir, $debug);
$this->constructed = true;
}

/**
* @codeCoverageIgnore
*/
public function setDirectories(array $directories)
{
$this->disallowMutation(__METHOD__);

return parent::setDirectories($directories);
}

/**
* @internal
*/
protected function setLocaleIdentifier($locale)
{
$this->disallowMutation(__METHOD__);

parent::setLocale($locale);

Check failure on line 46 in src/Carbon/AbstractTranslatorImmutable.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Method Carbon\AbstractTranslatorImmutable::setLocaleIdentifier() should return bool but return statement is missing.
}

/**
* @codeCoverageIgnore
*/
public function setMessages($locale, $messages)
{
$this->disallowMutation(__METHOD__);

return parent::setMessages($locale, $messages);
}

/**
* @codeCoverageIgnore
*/
public function setTranslations($messages)
{
$this->disallowMutation(__METHOD__);

return parent::setTranslations($messages);
}

/**
* @internal
* @codeCoverageIgnore
*/
protected function setConfigCacheFactoryObject(ConfigCacheFactoryInterface $configCacheFactory)
{
$this->disallowMutation(__METHOD__);

parent::setConfigCacheFactory($configCacheFactory);
}

public function resetMessages($locale = null)
{
$this->disallowMutation(__METHOD__);

return parent::resetMessages($locale);
}

/**
* @internal
* @codeCoverageIgnore
*/
protected function setFallbackLocalesArray(array $locales)
{
$this->disallowMutation(__METHOD__);

parent::setFallbackLocales($locales);
}

private function disallowMutation($method)
{
if ($this->constructed) {
throw new ImmutableException($method.' not allowed on '.static::class);
}
}
}
101 changes: 17 additions & 84 deletions src/Carbon/TranslatorImmutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,89 +11,22 @@

namespace Carbon;

use Carbon\Exceptions\ImmutableException;
use Symfony\Component\Config\ConfigCacheFactoryInterface;
use Symfony\Component\Translation\Formatter\MessageFormatterInterface;

class TranslatorImmutable extends Translator
use ReflectionMethod;
use Symfony\Component\Translation;
use Symfony\Contracts\Translation\TranslatorInterface;

$transMethod = new ReflectionMethod(
class_exists(TranslatorInterface::class)
? TranslatorInterface::class
: Translation\Translator::class,
'trans'
);

require $transMethod->hasReturnType()
? __DIR__.'/../../lazy/Carbon/TranslatorImmutableStrongType.php'
: __DIR__.'/../../lazy/Carbon/TranslatorImmutableWeakType.php';

class TranslatorImmutable extends LazyTranslatorImmutable

Check failure on line 29 in src/Carbon/TranslatorImmutable.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1)

Class Carbon\TranslatorImmutable extends unknown class Carbon\LazyTranslatorImmutable.
{
/** @var bool */
private $constructed = false;

public function __construct($locale, MessageFormatterInterface $formatter = null, $cacheDir = null, $debug = false)
{
parent::__construct($locale, $formatter, $cacheDir, $debug);
$this->constructed = true;
}

/**
* @codeCoverageIgnore
*/
public function setDirectories(array $directories)
{
$this->disallowMutation(__METHOD__);

return parent::setDirectories($directories);
}

public function setLocale($locale)
{
$this->disallowMutation(__METHOD__);

return parent::setLocale($locale);
}

/**
* @codeCoverageIgnore
*/
public function setMessages($locale, $messages)
{
$this->disallowMutation(__METHOD__);

return parent::setMessages($locale, $messages);
}

/**
* @codeCoverageIgnore
*/
public function setTranslations($messages)
{
$this->disallowMutation(__METHOD__);

return parent::setTranslations($messages);
}

/**
* @codeCoverageIgnore
*/
public function setConfigCacheFactory(ConfigCacheFactoryInterface $configCacheFactory)
{
$this->disallowMutation(__METHOD__);

parent::setConfigCacheFactory($configCacheFactory);
}

public function resetMessages($locale = null)
{
$this->disallowMutation(__METHOD__);

return parent::resetMessages($locale);
}

/**
* @codeCoverageIgnore
*/
public function setFallbackLocales(array $locales)
{
$this->disallowMutation(__METHOD__);

parent::setFallbackLocales($locales);
}

private function disallowMutation($method)
{
if ($this->constructed) {
throw new ImmutableException($method.' not allowed on '.static::class);
}
}
// Proxy dynamically loaded LazyTranslator in a static way
}
18 changes: 18 additions & 0 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
use Closure;
use DateTime;
use PHPUnit\Framework\TestCase;
use ReflectionMethod;
use ReflectionProperty;
use Symfony\Component\Translation;
use Symfony\Contracts\Translation\TranslatorInterface;
use Tests\PHPUnit\AssertObjectHasPropertyTrait;
use Throwable;

Expand Down Expand Up @@ -344,4 +347,19 @@ protected function firstValidTimezoneAmong(array $timezones): CarbonTimeZone

throw $firstError;
}

/**
* @return bool
*/
protected function translatorHasReturnType()
{
$transMethod = new ReflectionMethod(
class_exists(TranslatorInterface::class)
? TranslatorInterface::class
: Translation\Translator::class,
'trans'
);

return $transMethod->hasReturnType();
}
}
Loading
Loading