public function testGetDate()
 {
     $it = new DirectoryIterator('tests/test-files/logs');
     $it->setFilenameFormat(new FilenameFormat('payment.{Ymd}.log'));
     foreach ($it as $item) {
         if ($it->isFile()) {
             $this->assertEquals(\DateTime::createFromFormat('Ymd', '20160324'), $item->getFilenameDate());
             return;
         }
     }
 }
Beispiel #2
0
 /**
  * Delete files by a custom callback function
  *
  * For example, do a database lookup on the order ID stored within the filename to ensure the order has been completed, if so delete the file.
  *
  * Callback function must accept one parameter (DirectoryIterator $file) and must return true (delete file) or false (do not delete file)
  *
  * Example to delete all files over 5Mb in size:
  * $callback = function(DirectoryIterator $file) {
  *     if ($file->getSize() > 5 * 1024 * 1024) {
  *         return true;
  *     } else {
  *         return false;
  *     }
  * }
  *
  * @param callable $callback
  * @return array Array of deleted files
  * @throws FilenameFormatException
  * @throws RotateException
  */
 public function deleteByCallback(callable $callback)
 {
     if (!$this->hasFilenameFormat()) {
         throw new FilenameFormatException('You must set a filename format to match files against');
     }
     $deleted = [];
     $dir = new DirectoryIterator($this->getFilenameFormat()->getPath());
     $dir->setFilenameFormat($this->getFilenameFormat());
     foreach ($dir as $file) {
         if (!$file->isDot() && $file->isMatch()) {
             if ($callback($file)) {
                 if (!$this->isDryRun()) {
                     $results = $this->delete($file->getPathname());
                     $deleted = array_merge($deleted, $results);
                 } else {
                     $deleted[] = $file->getPathname();
                 }
             }
         }
     }
     return $deleted;
 }