/** * Rotates the files. */ protected function rotate() { // update filename $this->url = $this->getTimedFilename(); $this->nextRotation = new \DateTime('tomorrow'); // skip GC of old logs if files are unlimited if (0 === $this->maxFiles) { return; } $fileInfo = pathinfo($this->filename); $glob = $fileInfo['dirname'] . '/' . $fileInfo['filename'] . '-*'; if (!empty($fileInfo['extension'])) { $glob .= '.' . $fileInfo['extension']; } $iterator = new \GlobIterator($glob); $count = $iterator->count(); if ($this->maxFiles >= $count) { // no files to remove return; } // Sorting the files by name to remove the older ones $array = iterator_to_array($iterator); usort($array, function ($a, $b) { return strcmp($b->getFilename(), $a->getFilename()); }); foreach (array_slice($array, $this->maxFiles) as $file) { if ($file->isWritable()) { unlink($file->getRealPath()); } } }
/** * * @param unknown $path * @throws \Exception * @return \GlobIterator */ protected function getFileSystemIterator($path) { $fs = new Filesystem(); if (!$fs->exists($path)) { throw new \Exception("No repository found at [{$path}]"); } $directory = 'pack'; if (!$fs->exists("{$path}/{$directory}")) { throw new \Exception("No '{$directory}' directory found at [{$path}]"); } $iterator = new \GlobIterator("{$path}/{$directory}/*.json"); if (!$iterator->count()) { throw new \Exception("No json file found at [{$path}/set]"); } return $iterator; }
<?php $iterator = new GlobIterator('*.php'); printf("Matched %d item(s)\r\n", $iterator->count());
<?php $iter = new GlobIterator(__DIR__ . "/../../sample_dir/*"); var_dump($iter->count()); var_dump($iter->getPath()); var_dump($iter->getPathname()); var_dump($iter->getFilename()); foreach ($iter as $file) { echo "{$file}\n"; }