Skip to content

Commit

Permalink
Merge branch '4.2'
Browse files Browse the repository at this point in the history
* 4.2:
  Fix Cache error while using anonymous class
  [Cache] fix LockRegistry
  Update validators.cs.xlf
  Make translations consistent with other translations.
  Correct language code for ukrainian language in security translations.
  Fix return type of Request::getRequestFormat
  [Cache] Fix perf when using RedisCluster by reducing roundtrips to the servers
  • Loading branch information
nicolas-grekas committed Mar 15, 2019
2 parents 040dc7b + e981403 commit b7e798e
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 32 deletions.
19 changes: 15 additions & 4 deletions src/Symfony/Component/Cache/LockRegistry.php
Expand Up @@ -23,7 +23,7 @@
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class LockRegistry
final class LockRegistry
{
private static $openedFiles = [];
private static $lockedFiles = [];
Expand Down Expand Up @@ -75,7 +75,7 @@ public static function setFiles(array $files): array
return $previousFiles;
}

public static function compute(callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool)
public static function compute(callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata = null)
{
$key = self::$files ? crc32($item->getKey()) % \count(self::$files) : -1;

Expand All @@ -89,7 +89,18 @@ public static function compute(callable $callback, ItemInterface $item, bool &$s
if (flock($lock, LOCK_EX | LOCK_NB)) {
self::$lockedFiles[$key] = true;

return $callback($item, $save);
$value = $callback($item, $save);

if ($save) {
if ($setMetadata) {
$setMetadata($item);
}

$pool->save($item->set($value));
$save = false;
}

return $value;
}
// if we failed the race, retry locking in blocking mode to wait for the winner
flock($lock, LOCK_SH);
Expand Down Expand Up @@ -126,6 +137,6 @@ private static function open(int $key)
restore_error_handler();
}

self::$openedFiles[$key] = $h ?: @fopen(self::$files[$key], 'r');
return self::$openedFiles[$key] = $h ?: @fopen(self::$files[$key], 'r');
}
}
18 changes: 11 additions & 7 deletions src/Symfony/Component/Cache/Traits/ContractsTrait.php
Expand Up @@ -40,7 +40,7 @@ trait ContractsTrait
public function setCallbackWrapper(?callable $callbackWrapper): callable
{
$previousWrapper = $this->callbackWrapper;
$this->callbackWrapper = $callbackWrapper ?? function (callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool) {
$this->callbackWrapper = $callbackWrapper ?? function (callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata) {
return $callback($item, $save);
};

Expand All @@ -56,17 +56,19 @@ private function doGet(AdapterInterface $pool, string $key, callable $callback,
static $setMetadata;

$setMetadata = $setMetadata ?? \Closure::bind(
function (AdapterInterface $pool, ItemInterface $item, float $startTime) {
function (CacheItem $item, float $startTime, ?array &$metadata) {
if ($item->expiry > $endTime = microtime(true)) {
$item->newMetadata[ItemInterface::METADATA_EXPIRY] = $item->expiry;
$item->newMetadata[ItemInterface::METADATA_CTIME] = 1000 * (int) ($endTime - $startTime);
$item->newMetadata[CacheItem::METADATA_EXPIRY] = $metadata[CacheItem::METADATA_EXPIRY] = $item->expiry;
$item->newMetadata[CacheItem::METADATA_CTIME] = $metadata[CacheItem::METADATA_CTIME] = 1000 * (int) ($endTime - $startTime);
} else {
unset($metadata[CacheItem::METADATA_EXPIRY], $metadata[CacheItem::METADATA_CTIME]);
}
},
null,
CacheItem::class
);

return $this->contractsGet($pool, $key, function (CacheItem $item, bool &$save) use ($pool, $callback, $setMetadata) {
return $this->contractsGet($pool, $key, function (CacheItem $item, bool &$save) use ($pool, $callback, $setMetadata, &$metadata) {
// don't wrap nor save recursive calls
if (null === $callbackWrapper = $this->callbackWrapper) {
$value = $callback($item, $save);
Expand All @@ -78,8 +80,10 @@ function (AdapterInterface $pool, ItemInterface $item, float $startTime) {
$startTime = microtime(true);

try {
$value = $callbackWrapper($callback, $item, $save, $pool);
$setMetadata($pool, $item, $startTime);
$value = $callbackWrapper($callback, $item, $save, $pool, function (CacheItem $item) use ($setMetadata, $startTime, &$metadata) {
$setMetadata($item, $startTime, $metadata);
});
$setMetadata($item, $startTime, $metadata);

return $value;
} finally {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -1330,7 +1330,7 @@ public function setFormat($format, $mimeTypes)
*
* @param string|null $default The default format
*
* @return string The request format
* @return string|null The request format
*/
public function getRequestFormat($default = 'html')
{
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Validator/Mapping/Cache/Psr6Cache.php
Expand Up @@ -66,6 +66,11 @@ public function write(ClassMetadata $metadata)
*/
private function escapeClassName(string $class): string
{
if (false !== strpos($class, '@')) {
// anonymous class: replace all PSR6-reserved characters
return str_replace(["\0", '\\', '/', '@', ':', '{', '}', '(', ')'], '.', $class);
}

return str_replace('\\', '.', $class);
}
}
Expand Up @@ -36,23 +36,23 @@
</trans-unit>
<trans-unit id="9">
<source>This field was not expected.</source>
<target>Това поле не се е очаквало.</target>
<target>Полето не се е очаквало.</target>
</trans-unit>
<trans-unit id="10">
<source>This field is missing.</source>
<target>Това поле липсва.</target>
<target>Полето липсва.</target>
</trans-unit>
<trans-unit id="11">
<source>This value is not a valid date.</source>
<target>Стойността не е валидна дата (date).</target>
<target>Стойността не е валидна дата.</target>
</trans-unit>
<trans-unit id="12">
<source>This value is not a valid datetime.</source>
<target>Стойността не е валидна дата (datetime).</target>
<target>Стойността не е валидна дата и час.</target>
</trans-unit>
<trans-unit id="13">
<source>This value is not a valid email address.</source>
<target>Стойността не е валиден email адрес.</target>
<target>Стойността не е валиден имейл адрес.</target>
</trans-unit>
<trans-unit id="14">
<source>The file could not be found.</source>
Expand All @@ -68,7 +68,7 @@
</trans-unit>
<trans-unit id="17">
<source>The mime type of the file is invalid ({{ type }}). Allowed mime types are {{ types }}.</source>
<target>Майм типа на файла е невалиден ({{ type }}). Разрешени майм типове са {{ types }}.</target>
<target>Mime типа на файла е невалиден ({{ type }}). Разрешени mime типове са {{ types }}.</target>
</trans-unit>
<trans-unit id="18">
<source>This value should be {{ limit }} or less.</source>
Expand Down Expand Up @@ -104,7 +104,7 @@
</trans-unit>
<trans-unit id="26">
<source>This value is not a valid time.</source>
<target>Стойността не е валидно време (time).</target>
<target>Стойността не е валидно време.</target>
</trans-unit>
<trans-unit id="27">
<source>This value is not a valid URL.</source>
Expand Down Expand Up @@ -216,35 +216,35 @@
</trans-unit>
<trans-unit id="57">
<source>Invalid card number.</source>
<target>Невалиден номер на картата.</target>
<target>Невалиден номер на карта.</target>
</trans-unit>
<trans-unit id="58">
<source>Unsupported card type or invalid card number.</source>
<target>Неподдържан тип карта или невалиден номер на картата.</target>
<target>Неподдържан тип карта или невалиден номер на карта.</target>
</trans-unit>
<trans-unit id="59">
<source>This is not a valid International Bank Account Number (IBAN).</source>
<target>Невалиден Международен номер на банкова сметка (IBAN).</target>
<target>Това не е валиден Международен номер на банкова сметка (IBAN).</target>
</trans-unit>
<trans-unit id="60">
<source>This value is not a valid ISBN-10.</source>
<target>Невалиден ISBN-10.</target>
<target>Стойността не е валиден ISBN-10.</target>
</trans-unit>
<trans-unit id="61">
<source>This value is not a valid ISBN-13.</source>
<target>Невалиден ISBN-13.</target>
<target>Стойността не е валиден ISBN-13.</target>
</trans-unit>
<trans-unit id="62">
<source>This value is neither a valid ISBN-10 nor a valid ISBN-13.</source>
<target>Невалидна стойност както за ISBN-10, така и за ISBN-13 .</target>
<target>Стойността не е нито валиден ISBN-10, нито валиден ISBN-13.</target>
</trans-unit>
<trans-unit id="63">
<source>This value is not a valid ISSN.</source>
<target>Невалиден Международен стандартен сериен номер (ISSN).</target>
<target>Стойността не е валиден ISSN.</target>
</trans-unit>
<trans-unit id="64">
<source>This value is not a valid currency.</source>
<target>Невалидна валута.</target>
<target>Стойността не е валидна валута.</target>
</trans-unit>
<trans-unit id="65">
<source>This value should be equal to {{ compared_value }}.</source>
Expand Down Expand Up @@ -308,11 +308,11 @@
</trans-unit>
<trans-unit id="80">
<source>This value does not match the expected {{ charset }} charset.</source>
<target>Стойността не съвпада с {{ charset }}.</target>
<target>Стойността не съвпада с очакваната {{ charset }} кодировка.</target>
</trans-unit>
<trans-unit id="81">
<source>This is not a valid Business Identifier Code (BIC).</source>
<target>Невалиден бизнес идентификационен код (BIC).</target>
<target>Това не е валиден Бизнес идентификационен код (BIC).</target>
</trans-unit>
<trans-unit id="82">
<source>Error</source>
Expand All @@ -324,15 +324,15 @@
</trans-unit>
<trans-unit id="84">
<source>This value should be a multiple of {{ compared_value }}.</source>
<target>Тази стойност трябва да бъде кратно число на {{ compared_value }}.</target>
<target>Стойността трябва да бъде кратно число на {{ compared_value }}.</target>
</trans-unit>
<trans-unit id="85">
<source>This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.</source>
<target>Този бизнес идентификационен код (BIC) не е свързана с IBAN {{ iban }}.</target>
<target>Бизнес идентификационния код (BIC) не е свързан с IBAN {{ iban }}.</target>
</trans-unit>
<trans-unit id="86">
<source>This value should be valid JSON.</source>
<target>Тази стойност трябва да е валидна JSON.</target>
<target>Стойността трябва да е валиден JSON.</target>
</trans-unit>
</body>
</file>
Expand Down
Expand Up @@ -318,6 +318,22 @@
<source>Error</source>
<target>Chyba</target>
</trans-unit>
<trans-unit id="83">
<source>This is not a valid UUID.</source>
<target>Tato hodnota není platné UUID.</target>
</trans-unit>
<trans-unit id="84">
<source>This value should be a multiple of {{ compared_value }}.</source>
<target>Tato hodnota musí být násobek hodnoty {{ compared_value }}.</target>
</trans-unit>
<trans-unit id="85">
<source>This Business Identifier Code (BIC) is not associated with IBAN {{ iban }}.</source>
<target>Bankovní identifikační kód (BIC) neodpovídá mezinárodnímu číslu účtu (IBAN) {{ iban }}.</target>
</trans-unit>
<trans-unit id="86">
<source>This value should be valid JSON.</source>
<target>Tato hodnota musí být validní JSON.</target>
</trans-unit>
</body>
</file>
</xliff>
Expand Up @@ -23,4 +23,12 @@ public function testNameCollision()
$this->cache->write($metadata);
$this->assertFalse($this->cache->has('Foo_Bar'));
}

public function testNameWithInvalidChars()
{
$metadata = new ClassMetadata('class@anonymous/path/file');

$this->cache->write($metadata);
$this->assertTrue($this->cache->has('class@anonymous/path/file'));
}
}

0 comments on commit b7e798e

Please sign in to comment.