diff --git a/CHANGELOG.md b/CHANGELOG.md index a50ddff..4c48e2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # Yii VarDumper -## 1.4.1 under development +## 2.0.0 under development -- no changes in this release. +- Enh #76: Add a variable type to output when it can't be exported more (@xepozz) ## 1.4.0 October 28, 2022 diff --git a/src/VarDumper.php b/src/VarDumper.php index 83ecbae..33095f0 100644 --- a/src/VarDumper.php +++ b/src/VarDumper.php @@ -39,9 +39,15 @@ */ final class VarDumper { + public const VAR_TYPE_PROPERTY = '$__type__$'; public const OBJECT_ID_PROPERTY = '$__id__$'; public const OBJECT_CLASS_PROPERTY = '$__class__$'; public const DEPTH_LIMIT_EXCEEDED_PROPERTY = '$__depth_limit_exceeded__$'; + + public const VAR_TYPE_ARRAY = 'array'; + public const VAR_TYPE_OBJECT = 'object'; + public const VAR_TYPE_RESOURCE = 'resource'; + /** * @var mixed Variable to dump. */ @@ -353,10 +359,20 @@ private function exportPrimitives(mixed $var, int $depth, int $level): mixed switch (gettype($var)) { case 'resource': case 'resource (closed)': - return '{resource}'; + /** @var resource $var */ + $id = get_resource_id($var); + $type = get_resource_type($var); + + return [ + self::VAR_TYPE_PROPERTY => self::VAR_TYPE_RESOURCE, + 'id' => $id, + 'type' => $type, + 'closed' => !is_resource($var), + ]; case 'array': if ($depth <= $level) { return [ + self::VAR_TYPE_PROPERTY => self::VAR_TYPE_ARRAY, self::DEPTH_LIMIT_EXCEEDED_PROPERTY => true, ]; } @@ -372,6 +388,7 @@ private function exportPrimitives(mixed $var, int $depth, int $level): mixed $objectId = $this->getObjectId($var); if ($depth <= $level) { return [ + self::VAR_TYPE_PROPERTY => self::VAR_TYPE_OBJECT, self::OBJECT_ID_PROPERTY => $objectId, self::OBJECT_CLASS_PROPERTY => $objectClass, self::DEPTH_LIMIT_EXCEEDED_PROPERTY => true, diff --git a/tests/VarDumperTest.php b/tests/VarDumperTest.php index d9fe9db..3fc34ac 100644 --- a/tests/VarDumperTest.php +++ b/tests/VarDumperTest.php @@ -819,6 +819,13 @@ public function asJsonDataProvider(): array $objectWithPrivatePropertiesId = spl_object_id($objectWithPrivateProperties); $objectWithPrivatePropertiesClass = str_replace('\\', '\\\\', PrivateProperties::class); + $openedResource = fopen('php://input', 'rb'); + $openedResourceId = get_resource_id($openedResource); + + $closedResource = fopen('php://input', 'rb'); + $closedResourceId = get_resource_id($closedResource); + fclose($closedResource); + return [ 'custom debug info' => [ $dummyDebugInfo, @@ -912,9 +919,27 @@ static function () { true, 'true', ], - 'resource' => [ - fopen('php://input', 'rb'), - '"{resource}"', + 'opened resource' => [ + $openedResource, + << [ + $closedResource, + << [ [], @@ -1023,6 +1048,7 @@ static function () { "\$__id__\$": "{$nestedObjectId}", "\$__class__\$": "stdClass", "nested": { + "\$__type__\$": "object", "\$__id__\$": "{$nestedObjectId}", "\$__class__\$": "stdClass", "\$__depth_limit_exceeded__\$": true @@ -1047,6 +1073,7 @@ static function () { [ [ { + "\$__type__\$": "array", "\$__depth_limit_exceeded__\$": true } ] @@ -1101,6 +1128,13 @@ public function asPrimitivesDataProvider(): array $objectWithPrivatePropertiesId = spl_object_id($objectWithPrivateProperties); $objectWithPrivatePropertiesClass = PrivateProperties::class; + $openedResource = fopen('php://input', 'rb'); + $openedResourceId = get_resource_id($openedResource); + + $closedResource = fopen('php://input', 'rb'); + $closedResourceId = get_resource_id($closedResource); + fclose($closedResource); + return [ 'custom debug info' => [ $dummyDebugInfo, @@ -1186,9 +1220,23 @@ static function () { true, true, ], - 'resource' => [ - fopen('php://input', 'rb'), - '{resource}', + 'opened resource' => [ + $openedResource, + [ + '$__type__$' => 'resource', + 'id' => $openedResourceId, + 'type' => 'stream', + 'closed' => false, + ], + ], + 'closed resource' => [ + $closedResource, + [ + '$__type__$' => 'resource', + 'id' => $closedResourceId, + 'type' => 'Unknown', + 'closed' => true, + ], ], 'empty array' => [ [], @@ -1285,6 +1333,7 @@ static function () { 'nested' => [ '$__id__$' => "$nestedObjectId", '$__class__$' => stdClass::class, + '$__type__$' => 'object', '$__depth_limit_exceeded__$' => true, ], ], @@ -1305,6 +1354,7 @@ static function () { [ [ [ + '$__type__$' => 'array', '$__depth_limit_exceeded__$' => true, ], ],