Skip to content

Commit 3b5207a

Browse files
committed
added callback mechanism to show progress during ops
1 parent e3a27bd commit 3b5207a

File tree

5 files changed

+82
-7
lines changed

5 files changed

+82
-7
lines changed

src/Archive.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ abstract class Archive
1010
const COMPRESS_GZIP = 1;
1111
const COMPRESS_BZIP = 2;
1212

13+
/** @var callable */
14+
protected $callback;
15+
1316
/**
1417
* Set the compression level and type
1518
*
@@ -117,6 +120,18 @@ abstract public function getArchive();
117120
*/
118121
abstract public function save($file);
119122

123+
/**
124+
* Set a callback function to be called whenever a file is added or extracted.
125+
*
126+
* The callback is called with a FileInfo object as parameter. You can use this to show progress
127+
* info during an operation.
128+
*
129+
* @param callable $callback
130+
*/
131+
public function setCallback($callback)
132+
{
133+
$this->callback = $callback;
134+
}
120135
}
121136

122137
class ArchiveIOException extends \Exception

src/Tar.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ public function extract($outdir, $strip = '', $exclude = '', $include = '')
187187
$this->skipbytes(ceil($header['size'] / 512) * 512); // the size is usually 0 for directories
188188
}
189189

190+
if(is_callable($this->callback)) {
191+
call_user_func($this->callback, $fileinfo);
192+
}
190193
$extracted[] = $fileinfo;
191194
}
192195

@@ -276,6 +279,10 @@ public function addFile($file, $fileinfo = '')
276279
$this->close();
277280
throw new ArchiveCorruptedException("The size of $file changed while reading, archive corrupted. read $read expected ".$fileinfo->getSize());
278281
}
282+
283+
if(is_callable($this->callback)) {
284+
call_user_func($this->callback, $fileinfo);
285+
}
279286
}
280287

281288
/**
@@ -302,6 +309,10 @@ public function addData($fileinfo, $data)
302309
for ($s = 0; $s < $len; $s += 512) {
303310
$this->writebytes(pack("a512", substr($data, $s, 512)));
304311
}
312+
313+
if (is_callable($this->callback)) {
314+
call_user_func($this->callback, $fileinfo);
315+
}
305316
}
306317

307318
/**

src/Zip.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ public function extract($outdir, $strip = '', $exclude = '', $include = '')
155155

156156
// nothing more to do for directories
157157
if ($fileinfo->getIsdir()) {
158+
if(is_callable($this->callback)) {
159+
call_user_func($this->callback, $fileinfo);
160+
}
158161
continue;
159162
}
160163

@@ -231,6 +234,9 @@ public function extract($outdir, $strip = '', $exclude = '', $include = '')
231234

232235
touch($output, $fileinfo->getMtime());
233236
//FIXME what about permissions?
237+
if(is_callable($this->callback)) {
238+
call_user_func($this->callback, $fileinfo);
239+
}
234240
}
235241

236242
$this->close();
@@ -353,6 +359,10 @@ public function addData($fileinfo, $data)
353359
$name,
354360
(bool) $this->complevel
355361
);
362+
363+
if(is_callable($this->callback)) {
364+
call_user_func($this->callback, $fileinfo);
365+
}
356366
}
357367

358368
/**

tests/TarTestCase.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
1-
<?php
1+
<?php /** @noinspection PhpUnhandledExceptionInspection */
22

33
namespace splitbrain\PHPArchive;
44

5-
use splitbrain\PHPArchive\Tar;
6-
use PHPUnit\Framework\TestCase;
75
use org\bovigo\vfs\vfsStream;
6+
use PHPUnit\Framework\TestCase;
87

98
class TarTestCase extends TestCase
109
{
10+
/** @var int callback counter */
11+
protected $counter = 0;
12+
1113
/**
1214
* file extensions that several tests use
1315
*/
1416
protected $extensions = array('tar');
1517

18+
/** @inheritdoc */
1619
protected function setUp()
1720
{
1821
parent::setUp();
@@ -27,12 +30,22 @@ protected function setUp()
2730
vfsStream::setup('home_root_path');
2831
}
2932

33+
/** @inheritdoc */
3034
protected function tearDown()
3135
{
3236
parent::tearDown();
3337
$this->extensions[] = null;
3438
}
3539

40+
/**
41+
* Callback check function
42+
* @param FileInfo $fileinfo
43+
*/
44+
public function increaseCounter($fileinfo) {
45+
$this->assertInstanceOf('\\splitbrain\\PHPArchive\\FileInfo', $fileinfo);
46+
$this->counter++;
47+
}
48+
3649
/*
3750
* dependency for tests needing zlib extension to pass
3851
*/
@@ -170,23 +183,30 @@ public function testDogfood()
170183
$archive = sys_get_temp_dir() . '/dwtartest' . md5(time()) . '.' . $ext;
171184
$extract = sys_get_temp_dir() . '/dwtartest' . md5(time() + 1);
172185

186+
$this->counter = 0;
173187
$tar = new Tar();
188+
$tar->setCallback(array($this, 'increaseCounter'));
174189
$tar->create($archive);
175190
foreach ($input as $path) {
176191
$file = basename($path);
177192
$tar->addFile($path, $file);
178193
}
179194
$tar->close();
180195
$this->assertFileExists($archive);
196+
$this->assertEquals(count($input), $this->counter);
181197

198+
$this->counter = 0;
182199
$tar = new Tar();
200+
$tar->setCallback(array($this, 'increaseCounter'));
183201
$tar->open($archive);
184202
$tar->extract($extract, '', '/FileInfo\\.php/', '/.*\\.php/');
185203

186204
$this->assertFileExists("$extract/Tar.php");
187205
$this->assertFileExists("$extract/Zip.php");
188206
$this->assertFileNotExists("$extract/FileInfo.php");
189207

208+
$this->assertEquals(count($input) - 1, $this->counter);
209+
190210
$this->nativeCheck($archive, $ext);
191211

192212
self::RDelete($extract);

tests/ZipTestCase.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
<?php
1+
<?php /** @noinspection PhpUnhandledExceptionInspection */
22

33
namespace splitbrain\PHPArchive;
44

5-
use splitbrain\PHPArchive\Zip;
6-
use PHPUnit\Framework\TestCase;
75
use org\bovigo\vfs\vfsStream;
6+
use PHPUnit\Framework\TestCase;
87

98
class ZipTestCase extends TestCase
109
{
10+
/** @var int callback counter */
11+
protected $counter = 0;
12+
13+
/** @inheritdoc */
1114
protected function setUp()
1215
{
1316
vfsStream::setup('home_root_path');
1417
}
1518

19+
/**
20+
* Callback check function
21+
* @param FileInfo $fileinfo
22+
*/
23+
public function increaseCounter($fileinfo) {
24+
$this->assertInstanceOf('\\splitbrain\\PHPArchive\\FileInfo', $fileinfo);
25+
$this->counter++;
26+
}
27+
1628
/*
1729
* dependency for tests needing zip extension to pass
1830
*/
@@ -203,23 +215,30 @@ public function testDogFood()
203215
$archive = sys_get_temp_dir() . '/dwziptest' . md5(time()) . '.zip';
204216
$extract = sys_get_temp_dir() . '/dwziptest' . md5(time() + 1);
205217

218+
$this->counter = 0;
206219
$zip = new Zip();
220+
$zip->setCallback(array($this, 'increaseCounter'));
207221
$zip->create($archive);
208-
foreach($input as $path) {
222+
foreach ($input as $path) {
209223
$file = basename($path);
210224
$zip->addFile($path, $file);
211225
}
212226
$zip->close();
213227
$this->assertFileExists($archive);
228+
$this->assertEquals(count($input), $this->counter);
214229

230+
$this->counter = 0;
215231
$zip = new Zip();
232+
$zip->setCallback(array($this, 'increaseCounter'));
216233
$zip->open($archive);
217234
$zip->extract($extract, '', '/FileInfo\\.php/', '/.*\\.php/');
218235

219236
$this->assertFileExists("$extract/Tar.php");
220237
$this->assertFileExists("$extract/Zip.php");
221238
$this->assertFileNotExists("$extract/FileInfo.php");
222239

240+
$this->assertEquals(count($input) - 1, $this->counter);
241+
223242
$this->nativeCheck($archive);
224243
$this->native7ZipCheck($archive);
225244

0 commit comments

Comments
 (0)