mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-22 21:34:28 +02:00
lib: Add NonEmptyFileIterator
Add iterator for iterating over non-empty files.
This commit is contained in:
parent
b214ed5d3b
commit
00a09284ef
49
library/Icinga/File/NonEmptyFileIterator.php
Normal file
49
library/Icinga/File/NonEmptyFileIterator.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}}
|
||||||
|
// {{{ICINGA_LICENSE_HEADER}}
|
||||||
|
|
||||||
|
namespace Icinga\File;
|
||||||
|
|
||||||
|
use FilterIterator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Iterator over non-empty files
|
||||||
|
*
|
||||||
|
* Usage example:
|
||||||
|
* <code>
|
||||||
|
* <?php
|
||||||
|
*
|
||||||
|
* namespace Icinga\Example;
|
||||||
|
*
|
||||||
|
* use RecursiveDirectoryIterator;
|
||||||
|
* use RecursiveIteratorIterator;
|
||||||
|
* use Icinga\File\NonEmptyFilterIterator;
|
||||||
|
*
|
||||||
|
* $nonEmptyFiles = new NonEmptyFileIterator(
|
||||||
|
* new RecursiveIteratorIterator(
|
||||||
|
* new RecursiveDirectoryIterator(__DIR__),
|
||||||
|
* RecursiveIteratorIterator::SELF_FIRST
|
||||||
|
* )
|
||||||
|
* );
|
||||||
|
* </code>
|
||||||
|
*/
|
||||||
|
class NonEmptyFileIterator extends FilterIterator
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Accept non-empty files
|
||||||
|
*
|
||||||
|
* @return bool Whether the current element of the iterator is acceptable
|
||||||
|
* through this filter
|
||||||
|
*/
|
||||||
|
public function accept()
|
||||||
|
{
|
||||||
|
$current = $this->current();
|
||||||
|
/** @type $current \SplFileInfo */
|
||||||
|
if (! $current->isFile()
|
||||||
|
|| $current->getSize() === 0
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user