Skip to content

Commit

Permalink
Merge branch '4.2' into 4.3
Browse files Browse the repository at this point in the history
* 4.2:
  [HttpFoundation] Throw exception when the \"session\" extension is not loaded
  remove invalid test cases
  [Serializer] Fixed PHP of DenormalizableInterface::denormalize
  [Cache] work aroung PHP memory leak
  [Finder] docblock fixes
  pass error code as a string
  Catch JsonException and rethrow in JsonEncode
  • Loading branch information
nicolas-grekas committed Jun 28, 2019
2 parents e62e2dd + b8c4809 commit a218efe
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 32 deletions.
Expand Up @@ -225,6 +225,10 @@ public function load(array $configs, ContainerBuilder $container)
}

if ($this->isConfigEnabled($container, $config['session'])) {
if (!\extension_loaded('session')) {
throw new \LogicException('PHP extension "session" is required.');
}

$this->sessionConfigEnabled = true;
$this->registerSessionConfiguration($config['session'], $container, $loader);
if (!empty($config['test'])) {
Expand Down
57 changes: 48 additions & 9 deletions src/Symfony/Component/Cache/Traits/PhpFilesTrait.php
Expand Up @@ -50,12 +50,15 @@ public function prune()
{
$time = time();
$pruned = true;
$getExpiry = true;

set_error_handler($this->includeHandler);
try {
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
try {
list($expiresAt) = include $file;
if (\is_array($expiresAt = include $file)) {
$expiresAt = $expiresAt[0];
}
} catch (\ErrorException $e) {
$expiresAt = $time;
}
Expand Down Expand Up @@ -87,15 +90,21 @@ protected function doFetch(array $ids)
$values = [];

begin:
$getExpiry = false;

foreach ($ids as $id) {
if (null === $value = $this->values[$id] ?? null) {
$missingIds[] = $id;
} elseif ('N;' === $value) {
$values[$id] = null;
} elseif ($value instanceof \Closure) {
$values[$id] = $value();
} else {
} elseif (!\is_object($value)) {
$values[$id] = $value;
} elseif (!$value instanceof LazyValue) {
// calling a Closure is for @deprecated BC and should be removed in Symfony 5.0
$values[$id] = $value();
} elseif (false === $values[$id] = include $value->file) {
unset($values[$id], $this->values[$id]);
$missingIds[] = $id;
}
if (!$this->appendOnly) {
unset($this->values[$id]);
Expand All @@ -108,10 +117,18 @@ protected function doFetch(array $ids)

set_error_handler($this->includeHandler);
try {
$getExpiry = true;

foreach ($missingIds as $k => $id) {
try {
$file = $this->files[$id] ?? $this->files[$id] = $this->getFile($id);
list($expiresAt, $this->values[$id]) = include $file;

if (\is_array($expiresAt = include $file)) {
[$expiresAt, $this->values[$id]] = $expiresAt;
} elseif ($now < $expiresAt) {
$this->values[$id] = new LazyValue($file);
}

if ($now >= $expiresAt) {
unset($this->values[$id], $missingIds[$k]);
}
Expand Down Expand Up @@ -140,7 +157,13 @@ protected function doHave($id)
set_error_handler($this->includeHandler);
try {
$file = $this->files[$id] ?? $this->files[$id] = $this->getFile($id);
list($expiresAt, $value) = include $file;
$getExpiry = true;

if (\is_array($expiresAt = include $file)) {
[$expiresAt, $value] = $expiresAt;
} elseif ($this->appendOnly) {
$value = new LazyValue($file);
}
} catch (\ErrorException $e) {
return false;
} finally {
Expand Down Expand Up @@ -189,13 +212,16 @@ protected function doSave(array $values, $lifetime)
}

if (!$isStaticValue) {
$value = str_replace("\n", "\n ", $value);
$value = "static function () {\n\n return {$value};\n\n}";
// We cannot use a closure here because of https://bugs.php.net/76982
$value = str_replace('\Symfony\Component\VarExporter\Internal\\', '', $value);
$value = "<?php\n\nnamespace Symfony\Component\VarExporter\Internal;\n\nreturn \$getExpiry ? {$expiry} : {$value};\n";
} else {
$value = "<?php return [{$expiry}, {$value}];\n";
}

$file = $this->files[$key] = $this->getFile($key, true);
// Since OPcache only compiles files older than the script execution start, set the file's mtime in the past
$ok = $this->write($file, "<?php return [{$expiry}, {$value}];\n", self::$startTime - 10) && $ok;
$ok = $this->write($file, $value, self::$startTime - 10) && $ok;

if ($allowCompile) {
@opcache_invalidate($file, true);
Expand Down Expand Up @@ -241,3 +267,16 @@ protected function doUnlink($file)
return @unlink($file);
}
}

/**
* @internal
*/
class LazyValue
{
public $file;

public function __construct($file)
{
$this->file = $file;
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Finder/Finder.php
Expand Up @@ -582,7 +582,7 @@ public function ignoreUnreadableDirs($ignore = true)
/**
* Searches files and directories which match defined rules.
*
* @param string|array $dirs A directory path or an array of directories
* @param string|string[] $dirs A directory path or an array of directories
*
* @return $this
*
Expand Down
Expand Up @@ -25,7 +25,7 @@ class ExcludeDirectoryFilterIterator extends \FilterIterator implements \Recursi

/**
* @param \Iterator $iterator The Iterator to filter
* @param array $directories An array of directories to exclude
* @param string[] $directories An array of directories to exclude
*/
public function __construct(\Iterator $iterator, array $directories)
{
Expand Down
Expand Up @@ -103,6 +103,10 @@ class NativeSessionStorage implements SessionStorageInterface
*/
public function __construct(array $options = [], $handler = null, MetadataBag $metaBag = null)
{
if (!\extension_loaded('session')) {
throw new \LogicException('PHP extension "session" is required.');
}

$options += [
'cache_limiter' => '',
'cache_expire' => 0,
Expand Down
Expand Up @@ -24,6 +24,10 @@ class PhpBridgeSessionStorage extends NativeSessionStorage
*/
public function __construct($handler = null, MetadataBag $metaBag = null)
{
if (!\extension_loaded('session')) {
throw new \LogicException('PHP extension "session" is required.');
}

$this->setMetadataBag($metaBag);
$this->setSaveHandler($handler);
}
Expand Down
13 changes: 9 additions & 4 deletions src/Symfony/Component/Serializer/Encoder/JsonEncode.php
Expand Up @@ -47,14 +47,19 @@ public function __construct($defaultContext = [])
*/
public function encode($data, $format, array $context = [])
{
$jsonEncodeOptions = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];
$encodedJson = json_encode($data, $jsonEncodeOptions);
$options = $context[self::OPTIONS] ?? $this->defaultContext[self::OPTIONS];

if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $jsonEncodeOptions)) {
try {
$encodedJson = json_encode($data, $options);
} catch (\JsonException $e) {
throw new NotEncodableValueException($e->getMessage(), 0, $e);
}

if (\PHP_VERSION_ID >= 70300 && (JSON_THROW_ON_ERROR & $options)) {
return $encodedJson;
}

if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($jsonEncodeOptions & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
if (JSON_ERROR_NONE !== json_last_error() && (false === $encodedJson || !($options & JSON_PARTIAL_OUTPUT_ON_ERROR))) {
throw new NotEncodableValueException(json_last_error_msg());
}

Expand Down
Expand Up @@ -34,7 +34,7 @@ interface DenormalizableInterface
* differently based on different input formats
* @param array $context Options for denormalizing
*
* @return object
* @return object|object[]
*/
public function denormalize(DenormalizerInterface $denormalizer, $data, $format = null, array $context = []);
}
Expand Up @@ -148,20 +148,6 @@ public function testValidComparisonToPropertyPath($comparedValue)
$this->assertNoViolation();
}

/**
* @dataProvider provideValidComparisonsToPropertyPath
*/
public function testValidComparisonToPropertyPathOnArray($comparedValue)
{
$constraint = $this->createConstraint(['propertyPath' => '[root][value]']);

$this->setObject(['root' => ['value' => 5]]);

$this->validator->validate($comparedValue, $constraint);

$this->assertNoViolation();
}

public function testNoViolationOnNullObjectWithPropertyPath()
{
$constraint = $this->createConstraint(['propertyPath' => 'propertyPath']);
Expand Down
Expand Up @@ -511,7 +511,7 @@ public function testAddCustomizedViolation()
->setParameter('%param%', 'value')
->setInvalidValue('Invalid value')
->setPlural(2)
->setCode(42)
->setCode('42')
->addViolation();
};

Expand All @@ -528,7 +528,7 @@ public function testAddCustomizedViolation()
$this->assertSame($entity, $violations[0]->getRoot());
$this->assertSame('Invalid value', $violations[0]->getInvalidValue());
$this->assertSame(2, $violations[0]->getPlural());
$this->assertSame(42, $violations[0]->getCode());
$this->assertSame('42', $violations[0]->getCode());
}

public function testNoDuplicateValidationIfClassConstraintInMultipleGroups()
Expand Down

0 comments on commit a218efe

Please sign in to comment.