Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2c44492

Browse files
[VarExporter] throw component-specific exceptions
1 parent3b931fe commit2c44492

File tree

8 files changed

+81
-15
lines changed

8 files changed

+81
-15
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\VarExporter\Exception;
13+
14+
class ClassNotFoundExceptionextends \Exceptionimplements ExceptionInterface
15+
{
16+
publicfunction__construct(string$class,\Throwable$previous =null)
17+
{
18+
parent::__construct(sprintf('Class "%s" not found.',$class),0,$previous);
19+
}
20+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\VarExporter\Exception;
13+
14+
interface ExceptionInterfaceextends \Throwable
15+
{
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Component\VarExporter\Exception;
13+
14+
class NotInstantiableTypeExceptionextends \Exceptionimplements ExceptionInterface
15+
{
16+
publicfunction__construct(string$type)
17+
{
18+
parent::__construct(sprintf('Type "%s" is not instantiable.',$type));
19+
}
20+
}

‎src/Symfony/Component/VarExporter/Internal/Exporter.php‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespaceSymfony\Component\VarExporter\Internal;
1313

14+
useSymfony\Component\VarExporter\Exception\NotInstantiableTypeException;
15+
1416
/**
1517
* @author Nicolas Grekas <p@tchwork.com>
1618
*
@@ -31,14 +33,14 @@ class Exporter
3133
*
3234
* @return int
3335
*
34-
* @throws\Exception When a value cannot be serialized
36+
* @throwsNotInstantiableTypeException When a value cannot be serialized
3537
*/
3638
publicstaticfunctionprepare($values,$objectsPool, &$refsPool, &$objectsCount, &$valuesAreStatic)
3739
{
3840
$refs =$values;
3941
foreach ($valuesas$k =>$value) {
4042
if (\is_resource($value)) {
41-
thrownew\Exception(sprintf("Serialization of '%s' resource is not allowed",\get_resource_type($value)));
43+
thrownewNotInstantiableTypeException(\get_resource_type($value).' resource');
4244
}
4345
$refs[$k] =$objectsPool;
4446

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

7981
if (!isset(Registry::$reflectors[$class])) {
80-
// Might throw Exception("Serialization of '...' is not allowed")
8182
Registry::getClassReflector($class);
82-
serialize(Registry::$prototypes[$class]);
83+
try {
84+
serialize(Registry::$prototypes[$class]);
85+
}catch (\Exception$e) {
86+
thrownewNotInstantiableTypeException($class,$e);
87+
}
8388
if (\method_exists($class,'__sleep')) {
8489
Registry::getClassReflector($class, Registry::$instantiableWithoutConstructor[$class], Registry::$cloneable[$class]);
8590
}

‎src/Symfony/Component/VarExporter/Internal/Registry.php‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespaceSymfony\Component\VarExporter\Internal;
1313

14+
useSymfony\Component\VarExporter\Exception\ClassNotFoundException;
15+
useSymfony\Component\VarExporter\Exception\NotInstantiableTypeException;
16+
1417
/**
1518
* @author Nicolas Grekas <p@tchwork.com>
1619
*
@@ -37,9 +40,7 @@ public static function unserialize($objects, $serializables)
3740

3841
try {
3942
foreach ($serializablesas$k =>$v) {
40-
if (false ===$objects[$k] =unserialize($v)) {
41-
thrownew \Exception(error_get_last()['message'] ??'unserialize(): unknown error');
42-
}
43+
$objects[$k] =unserialize($v);
4344
}
4445
}finally {
4546
ini_set('unserialize_callback_func',$unserializeCallback);
@@ -64,6 +65,9 @@ public static function f($class)
6465

6566
publicstaticfunctiongetClassReflector($class,$instantiableWithoutConstructor =false,$cloneable =null)
6667
{
68+
if (!\class_exists($class)) {
69+
thrownewClassNotFoundException($class);
70+
}
6771
$reflector =new \ReflectionClass($class);
6872

6973
if (self::$instantiableWithoutConstructor[$class] =$instantiableWithoutConstructor || !$reflector->isFinal()) {
@@ -77,14 +81,14 @@ public static function getClassReflector($class, $instantiableWithoutConstructor
7781
if ('C:' ===$proto && !$reflector->getMethod('unserialize')->isInternal()) {
7882
$proto =null;
7983
}elseif (false ===$proto = @unserialize($proto.\strlen($class).':"'.$class.'":0:{}')) {
80-
thrownew\Exception(sprintf("Serialization of '%s' is not allowed",$class));
84+
thrownewNotInstantiableTypeException($class);
8185
}
8286
}
8387
}
8488

8589
if (null ===self::$cloneable[$class] =$cloneable) {
8690
if (($protoinstanceof \Reflector ||$protoinstanceof \ReflectionGenerator ||$protoinstanceof \ReflectionType ||$protoinstanceof \IteratorIterator ||$protoinstanceof \RecursiveIteratorIterator) && (!$protoinstanceof \Serializable && !\method_exists($proto,'__wakeup'))) {
87-
thrownew\Exception(sprintf("Serialization of '%s' is not allowed",$class));
91+
thrownewNotInstantiableTypeException($class);
8892
}
8993

9094
self::$cloneable[$class] =$reflector->isCloneable() && !$reflector->hasMethod('__clone');

‎src/Symfony/Component/VarExporter/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ It also provides a few improvements over `var_export()`/`serialize()`:
1616

1717
* the output is PSR-2 compatible;
1818
* the output can be re-indented without messing up with`\r` or`\n` in the data
19-
* missing classes throw a`ReflectionException` instead of being unserialized to
19+
* missing classes throw a`ClassNotFoundException` instead of being unserialized to
2020
`PHP_Incomplete_Class` objects;
2121
* references involving`SplObjectStorage`,`ArrayObject` or`ArrayIterator`
2222
instances are preserved;

‎src/Symfony/Component/VarExporter/Tests/VarExporterTest.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class VarExporterTest extends TestCase
2121
use VarDumperTestTrait;
2222

2323
/**
24-
* @expectedException \ReflectionException
25-
* @expectedExceptionMessage Class SomeNotExistingClass doesnotexist
24+
* @expectedException \Symfony\Component\VarExporter\Exception\ClassNotFoundException
25+
* @expectedExceptionMessage Class"SomeNotExistingClass"notfound.
2626
*/
2727
publicfunctiontestPhpIncompleteClassesAreForbidden()
2828
{
@@ -36,8 +36,8 @@ public function testPhpIncompleteClassesAreForbidden()
3636

3737
/**
3838
* @dataProvider provideFailingSerialization
39-
* @expectedException \Exception
40-
* @expectedExceptionMessageRegexpSerialization of '.*' is notallowed
39+
* @expectedException \Symfony\Component\VarExporter\Exception\NotInstantiableTypeException
40+
* @expectedExceptionMessageRegexpType ".*" is notinstantiable.
4141
*/
4242
publicfunctiontestFailingSerialization($value)
4343
{

‎src/Symfony/Component/VarExporter/VarExporter.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespaceSymfony\Component\VarExporter;
1313

14+
useSymfony\Component\VarExporter\Exception\ExceptionInterface;
1415
useSymfony\Component\VarExporter\Internal\Exporter;
1516
useSymfony\Component\VarExporter\Internal\Hydrator;
1617
useSymfony\Component\VarExporter\Internal\Registry;
@@ -36,7 +37,7 @@ final class VarExporter
3637
*
3738
* @return string The value exported as PHP code
3839
*
39-
* @throws\Exception When the provided value cannot be serialized
40+
* @throwsExceptionInterface When the provided value cannot be serialized
4041
*/
4142
publicstaticfunctionexport($value,bool &$isStaticValue =null):string
4243
{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp