Skip to content

Commit 2f4c23e

Browse files
committed
Merge pull request #3 from Rican7/feature/getSize-improvements
New "getSize()" method for more compatibility with other file types
2 parents 8ee669c + 8256992 commit 2f4c23e

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/OneMightyRoar/PhpFileManager/FileObject.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ class FileObject extends SplFileObject
8585
'stream',
8686
);
8787

88+
/**
89+
* The protocol wrappers that support "stat" operations
90+
*
91+
* @static
92+
* @var array
93+
* @access protected
94+
*/
95+
protected static $stat_able_protocols = array(
96+
'file',
97+
'phar',
98+
'rar',
99+
);
100+
88101
/**
89102
* The actual "name" of the file object
90103
*
@@ -743,6 +756,30 @@ public function getObfuscatedName($with_extension = true)
743756
return $this->getHash();
744757
}
745758

759+
/**
760+
* Get the size of the file object
761+
*
762+
* This overrides the default "getSize()" method in SplFileInfo
763+
* that is inherited by this class, so that getting the file size
764+
* will work on non-"fstat"-able file types and stream wrappers
765+
*
766+
* @access public
767+
* @return int
768+
*/
769+
public function getSize()
770+
{
771+
if ($this->isWrapped() || $this->isTemp()) {
772+
$wrapper_info = $this->getWrapperInfo();
773+
774+
if (!in_array($wrapper_info['protocol'], static::$stat_able_protocols)) {
775+
return strlen($this->getRaw());
776+
}
777+
}
778+
779+
// Just call our parent, otherwise
780+
return parent::getSize();
781+
}
782+
746783

747784
/**
748785
* Sub-class converters

tests/OneMightyRoar/PhpFileManager/Tests/FileObjectTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,17 @@ public function testCreateFromDetectedType()
122122
$this->assertSame($file_object->getRaw(), $file_object_protocol_wrapped_buffer->getRaw());
123123
$this->assertSame($file_object->getRaw(), $file_object_protocol_wrapped_base64_buffer->getRaw());
124124

125-
return $file_object;
125+
return array(
126+
'name' => $test_name,
127+
'objects' => array(
128+
'file_object' => $file_object,
129+
'file_object_resource' => $file_object_resource,
130+
'file_object_buffer' => $file_object_buffer,
131+
'file_object_base64_buffer' => $file_object_base64_buffer,
132+
'file_object_protocol_wrapped_buffer' => $file_object_protocol_wrapped_buffer,
133+
'file_object_protocol_wrapped_base64_buffer' => $file_object_protocol_wrapped_base64_buffer,
134+
),
135+
);
126136
}
127137

128138
public function testIsBase64String()
@@ -419,6 +429,26 @@ public function testGetObfuscatedName()
419429
$this->assertContains('php', $raw_binary_php->getObfuscatedName(true));
420430
}
421431

432+
/**
433+
* @depends testCreateFromDetectedType
434+
*/
435+
public function testGetSize($data)
436+
{
437+
$last_size = null;
438+
439+
foreach ($data['objects'] as $data) {
440+
$this_size = $data->getSize();
441+
442+
$this->assertGreaterThan(0, $this_size);
443+
444+
if (null !== $last_size) {
445+
$this->assertEquals($last_size, $this_size);
446+
}
447+
448+
$last_size = $this_size;
449+
}
450+
}
451+
422452
/**
423453
* @expectedException UnexpectedValueException
424454
*/

0 commit comments

Comments
 (0)