Skip to content

Commit e75c233

Browse files
committed
[VarDumper] Added support for SplFileObject
1 parent eb50b13 commit e75c233

File tree

3 files changed

+111
-6
lines changed

3 files changed

+111
-6
lines changed

src/Symfony/Component/VarDumper/Caster/SplCaster.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
*/
2121
class SplCaster
2222
{
23+
private static $splFileObjectFlags = array(
24+
\SplFileObject::DROP_NEW_LINE => 'DROP_NEW_LINE',
25+
\SplFileObject::READ_AHEAD => 'READ_AHEAD',
26+
\SplFileObject::SKIP_EMPTY => 'SKIP_EMPTY',
27+
\SplFileObject::READ_CSV => 'READ_CSV',
28+
);
29+
2330
public static function castArrayObject(\ArrayObject $c, array $a, Stub $stub, $isNested)
2431
{
2532
$prefix = Caster::PREFIX_VIRTUAL;
@@ -122,6 +129,54 @@ public static function castFileInfo(\SplFileInfo $c, array $a, Stub $stub, $isNe
122129
return $a;
123130
}
124131

132+
public static function castFileObject(\SplFileObject $c, array $a, Stub $stub, $isNested)
133+
{
134+
static $map = array(
135+
'csvControl' => 'getCsvControl',
136+
'flags' => 'getFlags',
137+
'maxLineLen' => 'getMaxLineLen',
138+
'fstat' => 'fstat',
139+
'eof' => 'eof',
140+
'key' => 'key',
141+
);
142+
143+
$prefix = Caster::PREFIX_VIRTUAL;
144+
145+
foreach ($map as $key => $accessor) {
146+
try {
147+
$a[$prefix.$key] = $c->$accessor();
148+
} catch (\Exception $e) {
149+
}
150+
}
151+
152+
if (isset($a[$prefix.'flags'])) {
153+
$flagsArray = array();
154+
foreach (self::$splFileObjectFlags as $value => $name) {
155+
if ($a[$prefix.'flags'] & $value) {
156+
$flagsArray[] = $name;
157+
}
158+
}
159+
$a[$prefix.'flags'] = new ConstStub(implode('|', $flagsArray), $a[$prefix.'flags']);
160+
}
161+
162+
if (isset($a[$prefix.'fstat'])) {
163+
$fstat = $a[$prefix.'fstat'];
164+
$fstat = array(
165+
'dev' => $fstat['dev'],
166+
'ino' => $fstat['ino'],
167+
'nlink' => $fstat['nlink'],
168+
'rdev' => $fstat['rdev'],
169+
'blksize' => $fstat['blksize'],
170+
'blocks' => $fstat['blocks'],
171+
'' => ''.(count($fstat) - 6),
172+
);
173+
174+
$a[$prefix.'fstat'] = $fstat;
175+
}
176+
177+
return $a;
178+
}
179+
125180
public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $isNested)
126181
{
127182
$a += array(

src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ abstract class AbstractCloner implements ClonerInterface
7979
'ArrayObject' => 'Symfony\Component\VarDumper\Caster\SplCaster::castArrayObject',
8080
'SplDoublyLinkedList' => 'Symfony\Component\VarDumper\Caster\SplCaster::castDoublyLinkedList',
8181
'SplFileInfo' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFileInfo',
82+
'SplFileObject' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFileObject',
8283
'SplFixedArray' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFixedArray',
8384
'SplHeap' => 'Symfony\Component\VarDumper\Caster\SplCaster::castHeap',
8485
'SplObjectStorage' => 'Symfony\Component\VarDumper\Caster\SplCaster::castObjectStorage',

src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ public function getCastFileInfoTests()
2323
return array(
2424
array(__FILE__, <<<'EOTXT'
2525
SplFileInfo {
26-
path: "%s/src/Symfony/Component/VarDumper/Tests/Caster"
26+
path: "%s/Tests/Caster"
2727
filename: "SplCasterTest.php"
2828
basename: "SplCasterTest.php"
29-
pathname: "%s/src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php"
29+
pathname: "%s/Tests/Caster/SplCasterTest.php"
3030
extension: "php"
31-
realPath: "%s/src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php"
31+
realPath: "%s/Tests/Caster/SplCasterTest.php"
3232
aTime: %s-%s-%d %d:%d:%d
3333
mTime: %s-%s-%d %d:%d:%d
3434
cTime: %s-%s-%d %d:%d:%d
3535
inode: %d
3636
size: %d
37-
perms: 0100644
38-
owner: 1000
39-
group: 1000
37+
perms: 0%d
38+
owner: %d
39+
group: %d
4040
type: "file"
4141
writable: true
4242
readable: true
@@ -72,4 +72,53 @@ public function testCastFileInfo($file, $dump)
7272
{
7373
$this->assertDumpMatchesFormat($dump, new \SplFileInfo($file));
7474
}
75+
76+
public function testCastFileObject()
77+
{
78+
$var = new \SplFileObject(__FILE__);
79+
$var->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::SKIP_EMPTY);
80+
$dump = <<<'EOTXT'
81+
SplFileObject {
82+
path: "%s/Tests/Caster"
83+
filename: "SplCasterTest.php"
84+
basename: "SplCasterTest.php"
85+
pathname: "%s/Tests/Caster/SplCasterTest.php"
86+
extension: "php"
87+
realPath: "%s/Tests/Caster/SplCasterTest.php"
88+
aTime: %s-%s-%d %d:%d:%d
89+
mTime: %s-%s-%d %d:%d:%d
90+
cTime: %s-%s-%d %d:%d:%d
91+
inode: %d
92+
size: %d
93+
perms: 0%d
94+
owner: %d
95+
group: %d
96+
type: "file"
97+
writable: true
98+
readable: true
99+
executable: false
100+
file: true
101+
dir: false
102+
link: false
103+
csvControl: array:2 [
104+
0 => ","
105+
1 => """
106+
]
107+
flags: DROP_NEW_LINE|SKIP_EMPTY
108+
maxLineLen: 0
109+
fstat: array:7 [
110+
"dev" => %d
111+
"ino" => %d
112+
"nlink" => %d
113+
"rdev" => 0
114+
"blksize" => %d
115+
"blocks" => %d
116+
"…" => "…20"
117+
]
118+
eof: false
119+
key: 0
120+
}
121+
EOTXT;
122+
$this->assertDumpMatchesFormat($dump, $var);
123+
}
75124
}

0 commit comments

Comments
 (0)
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