From eb50b139d655dd8586562c1fa113f931f2bed040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Mon, 20 Apr 2015 23:40:49 +0200 Subject: [PATCH 1/2] [VarDumper] Added support for SplFileInfo --- .../Component/VarDumper/Caster/SplCaster.php | 50 +++++++++++++ .../VarDumper/Cloner/AbstractCloner.php | 1 + .../Tests/Caster/ReflectionCasterTest.php | 2 - .../VarDumper/Tests/Caster/SplCasterTest.php | 75 +++++++++++++++++++ 4 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php diff --git a/src/Symfony/Component/VarDumper/Caster/SplCaster.php b/src/Symfony/Component/VarDumper/Caster/SplCaster.php index 79e8bb80b9afc..edc73a7a714c2 100644 --- a/src/Symfony/Component/VarDumper/Caster/SplCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/SplCaster.php @@ -72,6 +72,56 @@ public static function castDoublyLinkedList(\SplDoublyLinkedList $c, array $a, S return $a; } + public static function castFileInfo(\SplFileInfo $c, array $a, Stub $stub, $isNested) + { + static $map = array( + 'path' => 'getPath', + 'filename' => 'getFilename', + 'basename' => 'getBasename', + 'pathname' => 'getPathname', + 'extension' => 'getExtension', + 'realPath' => 'getRealPath', + 'aTime' => 'getATime', + 'mTime' => 'getMTime', + 'cTime' => 'getCTime', + 'inode' => 'getInode', + 'size' => 'getSize', + 'perms' => 'getPerms', + 'owner' => 'getOwner', + 'group' => 'getGroup', + 'type' => 'getType', + 'writable' => 'isWritable', + 'readable' => 'isReadable', + 'executable' => 'isExecutable', + 'file' => 'isFile', + 'dir' => 'isDir', + 'link' => 'isLink', + 'linkTarget' => 'getLinkTarget', + ); + + $prefix = Caster::PREFIX_VIRTUAL; + + foreach ($map as $key => $accessor) { + try { + $a[$prefix.$key] = $c->$accessor(); + } catch (\Exception $e) { + } + } + + if (isset($a[$prefix.'perms'])) { + $a[$prefix.'perms'] = new ConstStub(sprintf('0%o', $a[$prefix.'perms']), $a[$prefix.'perms']); + } + + static $mapDate = array('aTime', 'mTime', 'cTime'); + foreach ($mapDate as $key) { + if (isset($a[$prefix.$key])) { + $a[$prefix.$key] = new ConstStub(date('Y-m-d H:i:s', $a[$prefix.$key]), $a[$prefix.$key]); + } + } + + return $a; + } + public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $isNested) { $a += array( diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index e4f7a7c0538b7..b1efe6dbe427f 100644 --- a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php @@ -78,6 +78,7 @@ abstract class AbstractCloner implements ClonerInterface 'ArrayObject' => 'Symfony\Component\VarDumper\Caster\SplCaster::castArrayObject', 'SplDoublyLinkedList' => 'Symfony\Component\VarDumper\Caster\SplCaster::castDoublyLinkedList', + 'SplFileInfo' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFileInfo', 'SplFixedArray' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFixedArray', 'SplHeap' => 'Symfony\Component\VarDumper\Caster\SplCaster::castHeap', 'SplObjectStorage' => 'Symfony\Component\VarDumper\Caster\SplCaster::castObjectStorage', diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php index be4947278d5aa..fe3c65042fd93 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/ReflectionCasterTest.php @@ -11,8 +11,6 @@ namespace Symfony\Component\VarDumper\Tests\Caster; -use Symfony\Component\VarDumper\Cloner\VarCloner; -use Symfony\Component\VarDumper\Dumper\CliDumper; use Symfony\Component\VarDumper\Test\VarDumperTestCase; /** diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php new file mode 100644 index 0000000000000..6560acce40a56 --- /dev/null +++ b/src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php @@ -0,0 +1,75 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\VarDumper\Tests\Caster; + +use Symfony\Component\VarDumper\Test\VarDumperTestCase; + +/** + * @author Grégoire Pineau + */ +class SplCasterTest extends VarDumperTestCase +{ + public function getCastFileInfoTests() + { + return array( + array(__FILE__, <<<'EOTXT' +SplFileInfo { + path: "%s/src/Symfony/Component/VarDumper/Tests/Caster" + filename: "SplCasterTest.php" + basename: "SplCasterTest.php" + pathname: "%s/src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php" + extension: "php" + realPath: "%s/src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php" + aTime: %s-%s-%d %d:%d:%d + mTime: %s-%s-%d %d:%d:%d + cTime: %s-%s-%d %d:%d:%d + inode: %d + size: %d + perms: 0100644 + owner: 1000 + group: 1000 + type: "file" + writable: true + readable: true + executable: false + file: true + dir: false + link: false +} +EOTXT + ), + array('https://google.com/about', <<<'EOTXT' +SplFileInfo { + path: "https://google.com" + filename: "about" + basename: "about" + pathname: "https://google.com/about" + extension: "" + realPath: false + writable: false + readable: false + executable: false + file: false + dir: false + link: false +} +EOTXT + ), + ); + } + + /** @dataProvider getCastFileInfoTests */ + public function testCastFileInfo($file, $dump) + { + $this->assertDumpMatchesFormat($dump, new \SplFileInfo($file)); + } +} From e75c233b88fd1ef41c240201eac5d41390d54274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Tue, 21 Apr 2015 11:26:07 +0200 Subject: [PATCH 2/2] [VarDumper] Added support for SplFileObject --- .../Component/VarDumper/Caster/SplCaster.php | 55 +++++++++++++++++ .../VarDumper/Cloner/AbstractCloner.php | 1 + .../VarDumper/Tests/Caster/SplCasterTest.php | 61 +++++++++++++++++-- 3 files changed, 111 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/VarDumper/Caster/SplCaster.php b/src/Symfony/Component/VarDumper/Caster/SplCaster.php index edc73a7a714c2..a4d4760500d83 100644 --- a/src/Symfony/Component/VarDumper/Caster/SplCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/SplCaster.php @@ -20,6 +20,13 @@ */ class SplCaster { + private static $splFileObjectFlags = array( + \SplFileObject::DROP_NEW_LINE => 'DROP_NEW_LINE', + \SplFileObject::READ_AHEAD => 'READ_AHEAD', + \SplFileObject::SKIP_EMPTY => 'SKIP_EMPTY', + \SplFileObject::READ_CSV => 'READ_CSV', + ); + public static function castArrayObject(\ArrayObject $c, array $a, Stub $stub, $isNested) { $prefix = Caster::PREFIX_VIRTUAL; @@ -122,6 +129,54 @@ public static function castFileInfo(\SplFileInfo $c, array $a, Stub $stub, $isNe return $a; } + public static function castFileObject(\SplFileObject $c, array $a, Stub $stub, $isNested) + { + static $map = array( + 'csvControl' => 'getCsvControl', + 'flags' => 'getFlags', + 'maxLineLen' => 'getMaxLineLen', + 'fstat' => 'fstat', + 'eof' => 'eof', + 'key' => 'key', + ); + + $prefix = Caster::PREFIX_VIRTUAL; + + foreach ($map as $key => $accessor) { + try { + $a[$prefix.$key] = $c->$accessor(); + } catch (\Exception $e) { + } + } + + if (isset($a[$prefix.'flags'])) { + $flagsArray = array(); + foreach (self::$splFileObjectFlags as $value => $name) { + if ($a[$prefix.'flags'] & $value) { + $flagsArray[] = $name; + } + } + $a[$prefix.'flags'] = new ConstStub(implode('|', $flagsArray), $a[$prefix.'flags']); + } + + if (isset($a[$prefix.'fstat'])) { + $fstat = $a[$prefix.'fstat']; + $fstat = array( + 'dev' => $fstat['dev'], + 'ino' => $fstat['ino'], + 'nlink' => $fstat['nlink'], + 'rdev' => $fstat['rdev'], + 'blksize' => $fstat['blksize'], + 'blocks' => $fstat['blocks'], + '…' => '…'.(count($fstat) - 6), + ); + + $a[$prefix.'fstat'] = $fstat; + } + + return $a; + } + public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $isNested) { $a += array( diff --git a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php index b1efe6dbe427f..83cb921319dda 100644 --- a/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php +++ b/src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php @@ -79,6 +79,7 @@ abstract class AbstractCloner implements ClonerInterface 'ArrayObject' => 'Symfony\Component\VarDumper\Caster\SplCaster::castArrayObject', 'SplDoublyLinkedList' => 'Symfony\Component\VarDumper\Caster\SplCaster::castDoublyLinkedList', 'SplFileInfo' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFileInfo', + 'SplFileObject' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFileObject', 'SplFixedArray' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFixedArray', 'SplHeap' => 'Symfony\Component\VarDumper\Caster\SplCaster::castHeap', 'SplObjectStorage' => 'Symfony\Component\VarDumper\Caster\SplCaster::castObjectStorage', diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php index 6560acce40a56..299a3f93f56a4 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php @@ -23,20 +23,20 @@ public function getCastFileInfoTests() return array( array(__FILE__, <<<'EOTXT' SplFileInfo { - path: "%s/src/Symfony/Component/VarDumper/Tests/Caster" + path: "%s/Tests/Caster" filename: "SplCasterTest.php" basename: "SplCasterTest.php" - pathname: "%s/src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php" + pathname: "%s/Tests/Caster/SplCasterTest.php" extension: "php" - realPath: "%s/src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php" + realPath: "%s/Tests/Caster/SplCasterTest.php" aTime: %s-%s-%d %d:%d:%d mTime: %s-%s-%d %d:%d:%d cTime: %s-%s-%d %d:%d:%d inode: %d size: %d - perms: 0100644 - owner: 1000 - group: 1000 + perms: 0%d + owner: %d + group: %d type: "file" writable: true readable: true @@ -72,4 +72,53 @@ public function testCastFileInfo($file, $dump) { $this->assertDumpMatchesFormat($dump, new \SplFileInfo($file)); } + + public function testCastFileObject() + { + $var = new \SplFileObject(__FILE__); + $var->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::SKIP_EMPTY); + $dump = <<<'EOTXT' +SplFileObject { + path: "%s/Tests/Caster" + filename: "SplCasterTest.php" + basename: "SplCasterTest.php" + pathname: "%s/Tests/Caster/SplCasterTest.php" + extension: "php" + realPath: "%s/Tests/Caster/SplCasterTest.php" + aTime: %s-%s-%d %d:%d:%d + mTime: %s-%s-%d %d:%d:%d + cTime: %s-%s-%d %d:%d:%d + inode: %d + size: %d + perms: 0%d + owner: %d + group: %d + type: "file" + writable: true + readable: true + executable: false + file: true + dir: false + link: false + csvControl: array:2 [ + 0 => "," + 1 => """ + ] + flags: DROP_NEW_LINE|SKIP_EMPTY + maxLineLen: 0 + fstat: array:7 [ + "dev" => %d + "ino" => %d + "nlink" => %d + "rdev" => 0 + "blksize" => %d + "blocks" => %d + "…" => "…20" + ] + eof: false + key: 0 +} +EOTXT; + $this->assertDumpMatchesFormat($dump, $var); + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy