Skip to content

Commit 78906c0

Browse files
committed
recursive file quee folder changed from "exec(rm -fr /folder)" to
RecursiveDirectoryIterator
1 parent d4e4349 commit 78906c0

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

examples/file_example.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,29 @@
1212

1313
$driver = new FileDriver('file_queue');
1414

15-
/* TRUNCATE */
16-
exec('rm -fr file_queue/*');
15+
function delete($path)
16+
{
17+
$it = new RecursiveIteratorIterator(
18+
new RecursiveDirectoryIterator($path),
19+
RecursiveIteratorIterator::CHILD_FIRST
20+
);
21+
22+
/**
23+
* @var \SplFileInfo[] $it
24+
*/
25+
foreach ($it as $file) {
26+
if (in_array($file->getBasename(), array('.', '..'))) {
27+
continue;
28+
} elseif ($file->isDir()) {
29+
rmdir($file->getPathname());
30+
} elseif ($file->isFile() || $file->isLink()) {
31+
unlink($file->getPathname());
32+
}
33+
}
34+
}
35+
36+
37+
delete(__DIR__ . '/file_queue');
1738

1839
$queue = new Queue($driver);
1940

tests/PhpQueueFileTest.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,30 @@ class PhpQueueFileTest extends \PhpQueueTestDriver
77
{
88
private static $connection = 'file_queue';
99

10+
private static function delete($path)
11+
{
12+
$it = new \RecursiveIteratorIterator(
13+
new \RecursiveDirectoryIterator($path),
14+
\RecursiveIteratorIterator::CHILD_FIRST
15+
);
16+
/**
17+
* @var \SplFileInfo[] $it
18+
*/
19+
foreach ($it as $file) {
20+
21+
if (in_array($file->getBasename(), array('.', '..'))) {
22+
continue;
23+
} elseif ($file->isDir()) {
24+
rmdir($file->getPathname());
25+
} elseif ($file->isFile() || $file->isLink()) {
26+
unlink($file->getPathname());
27+
}
28+
}
29+
}
30+
1031
public static function prepareTestExecution()
1132
{
12-
exec('rm -fr ' . self::$connection . '/*');
33+
self::delete(self::$connection);
1334
}
1435

1536
/**
@@ -22,6 +43,6 @@ public static function getQueueDriver()
2243

2344
public static function tearDownAfterClass()
2445
{
25-
exec('rm -fr ' . self::$connection . '/*');
46+
self::delete(self::$connection);
2647
}
2748
}

0 commit comments

Comments
 (0)