Skip to content

Commit

Permalink
[VarDumper] Added support for SplFileInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx committed Apr 21, 2015
1 parent 862bdf1 commit eb50b13
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 2 deletions.
50 changes: 50 additions & 0 deletions src/Symfony/Component/VarDumper/Caster/SplCaster.php
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Expand Up @@ -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',
Expand Down
Expand Up @@ -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;

/**
Expand Down
75 changes: 75 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/Caster/SplCasterTest.php
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <lyrixx@lyrixx.info>
*/
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));
}
}

0 comments on commit eb50b13

Please sign in to comment.