Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 449b18c

Browse files
feature #14424 [VarDumper] Added support for SplFileInfo (lyrixx)
This PR was merged into the 2.8 branch. Discussion ---------- [VarDumper] Added support for SplFileInfo | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | ~ Before: ``` SplFileInfo {#3} ``` After: ``` SplFileInfo {#3 path: "/home/greg/dev/github.com/lyrixx/gaga-photo/fixtures" filename: "grisou.jpg" basename: "grisou.jpg" pathname: "/home/greg/dev/github.com/lyrixx/gaga-photo/fixtures/grisou.jpg" extension: "jpg" realPath: "/home/greg/dev/github.com/lyrixx/gaga-photo/fixtures/grisou.jpg" aTime: 2015-04-20 23:21:15 mTime: 2014-08-17 15:37:36 cTime: 2015-04-20 23:09:51 inode: 12197643 size: 966553 perms: 0100750 owner: 1000 group: 1000 type: "file" writable: true readable: true executable: true file: true dir: false link: false } ``` For a link: ``` SplFileInfo {#3 path: "" filename: "link" basename: "link" pathname: "link" extension: "" realPath: "/home/greg/dev/github.com/symfony/var-dumper/composer.json" aTime: 2015-04-20 23:39:15 mTime: 2015-04-20 23:39:15 cTime: 2015-04-20 23:39:15 inode: 11272232 size: 967 perms: 0100644 owner: 1000 group: 1000 type: "link" writable: true readable: true executable: false file: true dir: false link: true linkTarget: "composer.json" } ``` For a remote file: ``` SplFileInfo {#3 path: "http://google.com" filename: "foobar.php" basename: "foobar.php" pathname: "http://google.com/foobar.php" extension: "php" realPath: false writable: false readable: false executable: false file: false dir: false link: false } ``` For a CSV: ``` SplFileObject {#3 path: "" filename: "test.csv" basename: "test.csv" pathname: "test.csv" extension: "csv" realPath: "/home/greg/dev/github.com/symfony/var-dumper/test.csv" aTime: 2015-04-21 12:55:07 mTime: 2015-04-21 12:55:07 cTime: 2015-04-21 12:55:07 inode: 11272237 size: 0 perms: 0100644 owner: 1000 group: 1000 type: "file" writable: true readable: true executable: false file: true dir: false link: false csvControl: array:2 [ 0 => "," 1 => """ ] flags: SKIP_EMPTY|READ_CSV maxLineLen: 0 fstat: array:7 [ "dev" => 64513 "ino" => 11272237 "nlink" => 1 "rdev" => 0 "blksize" => 4096 "blocks" => 0 "…" => "…20" ] eof: false key: 0 } Commits ------- e75c233 [VarDumper] Added support for SplFileObject eb50b13 [VarDumper] Added support for SplFileInfo
2 parents 862bdf1 + e75c233 commit 449b18c

File tree

4 files changed

+231
-2
lines changed

4 files changed

+231
-2
lines changed

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

Lines changed: 105 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;
@@ -72,6 +79,104 @@ public static function castDoublyLinkedList(\SplDoublyLinkedList $c, array $a, S
7279
return $a;
7380
}
7481

82+
public static function castFileInfo(\SplFileInfo $c, array $a, Stub $stub, $isNested)
83+
{
84+
static $map = array(
85+
'path' => 'getPath',
86+
'filename' => 'getFilename',
87+
'basename' => 'getBasename',
88+
'pathname' => 'getPathname',
89+
'extension' => 'getExtension',
90+
'realPath' => 'getRealPath',
91+
'aTime' => 'getATime',
92+
'mTime' => 'getMTime',
93+
'cTime' => 'getCTime',
94+
'inode' => 'getInode',
95+
'size' => 'getSize',
96+
'perms' => 'getPerms',
97+
'owner' => 'getOwner',
98+
'group' => 'getGroup',
99+
'type' => 'getType',
100+
'writable' => 'isWritable',
101+
'readable' => 'isReadable',
102+
'executable' => 'isExecutable',
103+
'file' => 'isFile',
104+
'dir' => 'isDir',
105+
'link' => 'isLink',
106+
'linkTarget' => 'getLinkTarget',
107+
);
108+
109+
$prefix = Caster::PREFIX_VIRTUAL;
110+
111+
foreach ($map as $key => $accessor) {
112+
try {
113+
$a[$prefix.$key] = $c->$accessor();
114+
} catch (\Exception $e) {
115+
}
116+
}
117+
118+
if (isset($a[$prefix.'perms'])) {
119+
$a[$prefix.'perms'] = new ConstStub(sprintf('0%o', $a[$prefix.'perms']), $a[$prefix.'perms']);
120+
}
121+
122+
static $mapDate = array('aTime', 'mTime', 'cTime');
123+
foreach ($mapDate as $key) {
124+
if (isset($a[$prefix.$key])) {
125+
$a[$prefix.$key] = new ConstStub(date('Y-m-d H:i:s', $a[$prefix.$key]), $a[$prefix.$key]);
126+
}
127+
}
128+
129+
return $a;
130+
}
131+
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+
75180
public static function castFixedArray(\SplFixedArray $c, array $a, Stub $stub, $isNested)
76181
{
77182
$a += array(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ abstract class AbstractCloner implements ClonerInterface
7878

7979
'ArrayObject' => 'Symfony\Component\VarDumper\Caster\SplCaster::castArrayObject',
8080
'SplDoublyLinkedList' => 'Symfony\Component\VarDumper\Caster\SplCaster::castDoublyLinkedList',
81+
'SplFileInfo' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFileInfo',
82+
'SplFileObject' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFileObject',
8183
'SplFixedArray' => 'Symfony\Component\VarDumper\Caster\SplCaster::castFixedArray',
8284
'SplHeap' => 'Symfony\Component\VarDumper\Caster\SplCaster::castHeap',
8385
'SplObjectStorage' => 'Symfony\Component\VarDumper\Caster\SplCaster::castObjectStorage',

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

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

1212
namespace Symfony\Component\VarDumper\Tests\Caster;
1313

14-
use Symfony\Component\VarDumper\Cloner\VarCloner;
15-
use Symfony\Component\VarDumper\Dumper\CliDumper;
1614
use Symfony\Component\VarDumper\Test\VarDumperTestCase;
1715

1816
/**
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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+
namespace Symfony\Component\VarDumper\Tests\Caster;
13+
14+
use Symfony\Component\VarDumper\Test\VarDumperTestCase;
15+
16+
/**
17+
* @author Grégoire Pineau <[email protected]>
18+
*/
19+
class SplCasterTest extends VarDumperTestCase
20+
{
21+
public function getCastFileInfoTests()
22+
{
23+
return array(
24+
array(__FILE__, <<<'EOTXT'
25+
SplFileInfo {
26+
path: "%s/Tests/Caster"
27+
filename: "SplCasterTest.php"
28+
basename: "SplCasterTest.php"
29+
pathname: "%s/Tests/Caster/SplCasterTest.php"
30+
extension: "php"
31+
realPath: "%s/Tests/Caster/SplCasterTest.php"
32+
aTime: %s-%s-%d %d:%d:%d
33+
mTime: %s-%s-%d %d:%d:%d
34+
cTime: %s-%s-%d %d:%d:%d
35+
inode: %d
36+
size: %d
37+
perms: 0%d
38+
owner: %d
39+
group: %d
40+
type: "file"
41+
writable: true
42+
readable: true
43+
executable: false
44+
file: true
45+
dir: false
46+
link: false
47+
}
48+
EOTXT
49+
),
50+
array('https://google.com/about', <<<'EOTXT'
51+
SplFileInfo {
52+
path: "https://google.com"
53+
filename: "about"
54+
basename: "about"
55+
pathname: "https://google.com/about"
56+
extension: ""
57+
realPath: false
58+
writable: false
59+
readable: false
60+
executable: false
61+
file: false
62+
dir: false
63+
link: false
64+
}
65+
EOTXT
66+
),
67+
);
68+
}
69+
70+
/** @dataProvider getCastFileInfoTests */
71+
public function testCastFileInfo($file, $dump)
72+
{
73+
$this->assertDumpMatchesFormat($dump, new \SplFileInfo($file));
74+
}
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+
}
124+
}

0 commit comments

Comments
 (0)