8000 [VarExporter] throw component-specific exceptions by nicolas-grekas · Pull Request #28422 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[VarExporter] throw component-specific exceptions #28422

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

Merged
merged 1 commit into from
Sep 10, 2018
Merged
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
8000
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[VarExporter] throw component-specific exceptions
  • Loading branch information
nicolas-grekas committed Sep 10, 2018
commit 2c444927bc46454d48bb9e7a19f46b3419afadbd
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarExporter\Exception;

class ClassNotFoundException extends \Exception implements ExceptionInterface
{
public function __construct(string $class, \Throwable $previous = null)
{
parent::__construct(sprintf('Class "%s" not found.', $class), 0, $previous);
}
}
16 changes: 16 additions & 0 deletions src/Symfony/Component/VarExporter/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarExporter\Exception;

interface ExceptionInterface extends \Throwable
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\VarExporter\Exception;

class NotInstantiableTypeException extends \Exception implements ExceptionInterface
{
public function __construct(string $type)
{
parent::__construct(sprintf('Type "%s" is not instantiable.', $type));
}
}
13 changes: 9 additions & 4 deletions src/Symfony/Component/VarExporter/Internal/Exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\VarExporter\Internal;

use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException;

/**
* @author Nicolas Grekas <p@tchwork.com>
*
Expand All @@ -31,14 +33,14 @@ class Exporter
*
* @return int
*
* @throws \Exception When a value cannot be serialized
* @throws NotInstantiableTypeException When a value cannot be serialized
*/
public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount, &$valuesAreStatic)
{
$refs = $values;
foreach ($values as $k => $value) {
if (\is_resource($value)) {
throw new \Exception(sprintf("Serialization of '%s' resource is not allowed", \get_resource_type($value)));
throw new NotInstantiableTypeException(\get_resource_type($value).' resource');
}
$refs[$k] = $objectsPool;

Expand Down Expand Up @@ -77,9 +79,12 @@ public static function prepare($values, $objectsPool, &$refsPool, &$objectsCount
$arrayValue = (array) $value;

if (!isset(Registry::$reflectors[$class])) {
// Might throw Exception("Serialization of '...' is not allowed")
Registry::getClassReflector($class);
serialize(Registry::$prototypes[$class]);
try {
serialize(Registry::$prototypes[$class]);
} catch (\Exception $e) {
throw new NotInstantiableTypeException($class, $e);
}
if (\method_exists($class, '__sleep')) {
Registry::getClassReflector($class, Registry::$instantiableWithoutConstructor[$class], Registry::$cloneable[$class]);
}
Expand Down
14 changes: 9 additions & 5 deletions src/Symfony/Component/VarExporter/Internal/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Symfony\Component\VarExporter\Internal;

use Symfony\Component\VarExporter\Exception\ClassNotFoundException;
use Symfony\Component\VarExporter\Exception\NotInstantiableTypeException;

/**
* @author Nicolas Grekas <p@tchwork.com>
*
Expand All @@ -37,9 +40,7 @@ public static function unserialize($objects, $serializables)

try {
foreach ($serializables as $k => $v) {
if (false === $objects[$k] = unserialize($v)) {
throw new \Exception(error_get_last()['message'] ?? 'unserialize(): unknown error');
}
$objects[$k] = unserialize($v);
}
} finally {
ini_set('unserialize_callback_func', $unserializeCallback);
Expand All @@ -64,6 +65,9 @@ public static function f($class)

public static function getClassReflector($class, $instantiableWithoutConstructor = false, $cloneable = null)
{
if (!\class_exists($class)) {
throw new ClassNotFoundException($class);
}
$reflector = new \ReflectionClass($class);

if (self::$instantiableWithoutConstructor[$class] = $instantiableWithoutConstructor || !$reflector->isFinal()) {
Expand All @@ -77,14 +81,14 @@ public static function getClassReflector($class, $instantiableWithoutConstructor
if ('C:' === $proto && !$reflector->getMethod('unserialize')->isInternal()) {
$proto = null;
} elseif (false === $proto = @unserialize($proto.\strlen($class).':"'.$class.'":0:{}')) {
throw new \Exception(sprintf("Serialization of '%s' is not allowed", $class));
throw new NotInstantiableTypeException($class);
}
}
}

if (null === self::$cloneable[$class] = $cloneable) {
if (($proto instanceof \Reflector || $proto instanceof \ReflectionGenerator || $proto instanceof \ReflectionType || $proto instanceof \IteratorIterator || $proto instanceof \RecursiveIteratorIterator) && (!$proto instanceof \Serializable && !\method_exists($proto, '__wakeup'))) {
throw new \Exception(sprintf("Serialization of '%s' is not allowed", $class));
throw new NotInstantiableTypeException($class);
}

self::$cloneable[$class] = $reflector->isCloneable() && !$reflector->hasMethod('__clone');
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarExporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ It also provides a few improvements over `var_export()`/`serialize()`:

* the output is PSR-2 compatible;
* the output can be re-indented without messing up with `\r` or `\n` in the data
* missing classes throw a `ReflectionException` instead of being unserialized to
* missing classes throw a `ClassNotFoundException` instead of being unserialized to
`PHP_Incomplete_Class` objects;
* references involving `SplObjectStorage`, `ArrayObject` or `ArrayIterator`
instances are preserved;
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/VarExporter/Tests/VarExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class VarExporterTest extends TestCase
use VarDumperTestTrait;

/**
* @expectedException \ReflectionException
* @expectedExceptionMessage Class SomeNotExistingClass does not exist
* @expectedException \Symfony\Component\VarExporter\Exception\ClassNotFoundException
* @expectedExceptionMessage Class "SomeNotExistingClass" not found.
*/
public function testPhpIncompleteClassesAreForbidden()
{
Expand All @@ -36,8 +36,8 @@ public function testPhpIncompleteClassesAreForbidden()

/**
* @dataProvider provideFailingSerialization
* @expectedException \Exception
* @expectedExceptionMessageRegexp Serialization of '.*' is not allowed
* @expectedException \Symfony\Component\VarExporter\Exception\NotInstantiableTypeException
* @expectedExceptionMessageRegexp Type ".*" is not instantiable.
*/
public function testFailingSerialization($value)
{
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/VarExporter/VarExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\VarExporter;

use Symfony\Component\VarExporter\Exception\ExceptionInterface;
use Symfony\Component\VarExporter\Internal\Exporter;
use Symfony\Component\VarExporter\Internal\Hydrator;
use Symfony\Component\VarExporter\Internal\Registry;
Expand All @@ -36,7 +37,7 @@ final class VarExporter
*
* @return string The value exported as PHP code
*
* @throws \Exception When the provided value cannot be serialized
* @throws ExceptionInterface When the provided value cannot be serialized
*/
public static function export($value, bool &$isStaticValue = null): string
{
Expand Down
0