/** * @param $path * @return mixed|string */ private function getFile($path) { $returnView = $this->viewFolder . $path . '.php'; if (!file_exists($returnView)) { $dir = $path; $dir = explode('/', $dir); unset($dir[count($dir) - 1]); $dir = implode('/', $dir); $it = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->viewFolder . $dir)); while ($it->valid()) { if (!$it->isDot()) { $cache['SelectView'] = strrev($path); $cache['SelectView'] = explode("/", $cache['SelectView'])[0]; $cache['SelectView'] = strrev($cache['SelectView']); $cache['ReturnView'] = explode(".", $it->key()); unset($cache['ReturnView'][count($cache['ReturnView']) - 1]); $cache['ReturnView'] = implode('.', $cache['ReturnView']); $cache['ReturnView'] = explode("\\", $cache['ReturnView']); $cache['ReturnView'] = $cache['ReturnView'][count($cache['ReturnView']) - 1]; if ($cache['ReturnView'] == $cache['SelectView']) { $returnView = $it->key(); } } $it->next(); } if (!isset($returnView)) { die('Can not find a file matching these arguments: getView("' . $this->getPath($path, TRUE) . '")'); } return $returnView; } }
/** * Function to start a scan in a directory. * @return Array * @throws DirectoryNotFoundException */ public static function scanDir($parentDirectory) { $occurences = array(array()); //if the directory/file does not exists, then throw and error. if (!file_exists($parentDirectory)) { throw new DirectoryORFileNotFoundException("ERROR: Directory not found!"); } //get the list of all the files inside this directory. $allFiles = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($parentDirectory)); $fileList = array(); //remove (.), (..) and directories from the list of all files so that only files are left. while ($allFiles->valid()) { if (!$allFiles->isDot()) { if (!is_dir($allFiles->key())) { array_push($fileList, $allFiles->key()); } } $allFiles->next(); } $i = 0; foreach ($fileList as $file) { if (pathinfo($file, PATHINFO_EXTENSION) != "php") { continue; } $occurences[$i]['file'] = realpath($file); $occurences[$i]['result'] = Scanner::scanFile($file); $i++; } return $occurences; }
/** * Do the job. * Throw exceptions on errors (the job will be retried). */ public function execute() { global $CFG; $tmpdir = $CFG->tempdir; // Default to last weeks time. $time = strtotime('-1 week'); $dir = new \RecursiveDirectoryIterator($tmpdir); // Show all child nodes prior to their parent. $iter = new \RecursiveIteratorIterator($dir, \RecursiveIteratorIterator::CHILD_FIRST); for ($iter->rewind(); $iter->valid(); $iter->next()) { $node = $iter->getRealPath(); if (!is_readable($node)) { continue; } // Check if file or directory is older than the given time. if ($iter->getMTime() < $time) { if ($iter->isDir() && !$iter->isDot()) { // Don't attempt to delete the directory if it isn't empty. if (!glob($node . DIRECTORY_SEPARATOR . '*')) { if (@rmdir($node) === false) { mtrace("Failed removing directory '{$node}'."); } } } if ($iter->isFile()) { if (@unlink($node) === false) { mtrace("Failed removing file '{$node}'."); } } } } }
public function buildArray() { $directory = new \RecursiveDirectoryIterator('assets'); $iterator = new \RecursiveIteratorIterator($directory, \RecursiveIteratorIterator::CHILD_FIRST); $tree = []; foreach ($iterator as $info) { //if (in_array($info->getFilename(), ['.', '..'])) continue; if ($iterator->getDepth() >= 2 || !$iterator->isDir() || $iterator->isDot()) { continue; } if ($iterator->getDepth() == 1) { $path = $info->isDir() ? [$iterator->getDepth() - 1 => $info->getFilename()] : [$info->getFilename()]; } else { $path = $info->isDir() ? [$info->getFilename() => []] : [$info->getFilename()]; } for ($depth = $iterator->getDepth() - 1; $depth >= 0; $depth--) { $path = [$iterator->getSubIterator($depth)->current()->getFilename() => $path]; } $tree = array_merge_recursive($tree, $path); } $data = array(); foreach ($tree as $category => $children) { foreach ($children as $index => $value) { $data[$category][] = $value; } } foreach ($data as $category => $children) { $parentId = $this->addEntry($category, '0'); foreach ($children as $index => $value) { $this->addEntry($value, $parentId); } } return $data; }
/** * @throws \Seitenbau\FileSystem\FileSystemException */ public static function copyDir($source, $destination) { if (!is_dir($source)) { throw new FileSystemException('Sourcedir "' . $source . '" does not exists'); } $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST); if (!self::createDirIfNotExists($destination)) { return false; } // Verzeichnis rekursiv durchlaufen while ($iterator->valid()) { if (!$iterator->isDot()) { if ($iterator->current()->isDir()) { // relativen Teil des Pfad auslesen $relDir = str_replace($source, '', $iterator->key()); // Ziel-Verzeichnis erstellen if (!self::createDirIfNotExists($destination . $relDir)) { return false; } } elseif ($iterator->current()->isFile()) { $destinationFile = $destination . str_replace($source, '', $iterator->key()); if (!copy($iterator->key(), $destinationFile)) { return false; } } } $iterator->next(); } return true; }
private function recursive_rmdir($dirname, $followLinks = false) { if (is_dir($dirname) && !is_link($dirname)) { if (!is_writable($dirname)) { throw new Exception('You do not have renaming permissions!'); } $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirname), RecursiveIteratorIterator::CHILD_FIRST); while ($iterator->valid()) { if (!$iterator->isDot()) { if (!$iterator->isWritable()) { throw new Exception(sprintf('Permission Denied: %s.', $iterator->getPathName())); } if ($iterator->isLink() && false === (bool) $followLinks) { $iterator->next(); } if ($iterator->isFile()) { unlink($iterator->getPathName()); } else { if ($iterator->isDir()) { rmdir($iterator->getPathName()); } } } $iterator->next(); } unset($iterator); // Fix for Windows. return rmdir($dirname); } else { throw new Exception(sprintf('Directory %s does not exist!', $dirname)); } }
private static function is_extension_changed($name) { $name = strtolower($name); $extension_build_dir = ROOT_PATH . DS . 'build' . DS . $name; $extension_path = ROOT_PATH . DS . 'extensions' . DS . $name . '.tar.gz'; if (file_exists($extension_build_dir) && file_exists($extension_path)) { $dir_iterator = new \RecursiveDirectoryIterator($extension_build_dir); /** * @var $iterator \RecursiveDirectoryIterator */ $iterator = new \RecursiveIteratorIterator($dir_iterator); $iterator->rewind(); while ($iterator->valid()) { if (!$iterator->isDot()) { $file = $extension_build_dir . DS . $iterator->getSubPathName(); $phar_file = "phar://{$extension_path}/" . $iterator->getSubPathName(); if (!file_exists($phar_file)) { return true; } else { $build_file_hash = md5(file_get_contents($file)); $phar_file_hash = md5(file_get_contents($phar_file)); if ($build_file_hash != $phar_file_hash) { return true; } } } $iterator->next(); } return false; } else { return false; } }
/** * Get Test Files * * @param null $directory * @param null $excludes * @return array */ public static function getTestFiles($directory = null, $excludes = null) { if (is_array($directory)) { $files = array(); foreach ($directory as $d) { $files = array_merge($files, self::getTestFiles($d, $excludes)); } return array_unique($files); } if ($excludes !== null) { $excludes = self::getTestFiles((array) $excludes); } if ($directory === null || $directory !== realpath($directory)) { $basePath = App::pluginPath('DebugKit') . 'Test' . DS . 'Case' . DS; $directory = str_replace(DS . DS, DS, $basePath . $directory); } $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); $files = array(); while ($it->valid()) { if (!$it->isDot()) { $file = $it->key(); if (preg_match('|Test\\.php$|', $file) && $file !== __FILE__ && !preg_match('|^All.+?\\.php$|', basename($file)) && ($excludes === null || !in_array($file, $excludes))) { $files[] = $file; } } $it->next(); } return $files; }
protected function assertDirContentsEquals($expDir, $actDir) { if (!file_exists($expDir)) { throw new RuntimeException("Expected dir not found: " . $expDir); } if (!file_exists($actDir)) { throw new RuntimeException("Actual dir not found: " . $actDir); } $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($expDir)); $expFiles = array(); while ($dir->valid()) { if (!$dir->isDot()) { array_push($expFiles, $dir->getSubPathName()); } $dir->next(); } $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($actDir)); $actFiles = array(); while ($dir->valid()) { if (!$dir->isDot()) { array_push($actFiles, $dir->getSubPathName()); } $dir->next(); } $this->assertEquals($expFiles, $actFiles); foreach ($expFiles as $key => $value) { $expFile = file_get_contents($expDir . DIRECTORY_SEPARATOR . $value); $actFile = file_get_contents($expDir . DIRECTORY_SEPARATOR . $actFiles[$key]); $this->assertEquals($expFile, $actFile); } }
/** * Remove recursively the directory named by dirname. * * @param string $dirname Path to the directory * @param boolean $followLinks Removes symbolic links if set to true * @return boolean True if success otherwise false * @throws Exception When the directory does not exist or permission denied */ public static function rmdir($dirname, $followLinks = false) { if (!is_dir($dirname) && !is_link($dirname)) { throw new Exception(sprintf('Directory %s does not exist', $dirname)); } if (!is_writable($dirname)) { throw new Exception('You do not have renaming permissions'); } $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dirname), RecursiveIteratorIterator::CHILD_FIRST); while ($iterator->valid()) { if (!$iterator->isDot()) { if (!$iterator->isWritable()) { throw new Exception(sprintf('Permission denied for %s', $iterator->getPathName())); } if ($iterator->isLink() && false === (bool) $followLinks) { $iterator->next(); continue; } if ($iterator->isFile()) { unlink($iterator->getPathName()); } else { if ($iterator->isDir()) { rmdir($iterator->getPathName()); } } } $iterator->next(); } unset($iterator); return rmdir($dirname); }
public static function GetDirectoryContent($Directory) { $Iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($Directory)); $FilesArray = array(); while ($Iterator->valid()) { if (!$Iterator->isDot()) { $FilesArray[] = array('FileLink' => $Iterator->key(), 'FileName' => $Iterator->getSubPathName()); } $Iterator->next(); } return $FilesArray; }
private function get_all_files() { $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->input_dir)); $result = array(); while ($iterator->valid()) { if (!$iterator->isDot()) { $result[$iterator->getSubPath()][] = $iterator->key(); } $iterator->next(); } return $result; }
public static function addTestDirectoryRecursive(PHPUnit_Framework_TestSuite $suite, $directory) { $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)); while ($it->valid()) { if (!$it->isDot()) { $file = $it->key(); if (preg_match('|Test\\.php$|', $file) && $file !== __FILE__) { $suite->addTestFile($file); } } $it->next(); } }
private function _getPluggableFiles() { $FilePaths = array(); $directory = new \RecursiveDirectoryIterator($this->directory); foreach ($F = new \RecursiveIteratorIterator($directory) as $filepath) { if (!$F->isDot() && $F->getDepth() === 1) { if ($F->getBasename() === $this->filename) { $FilePaths[] = $filepath; } } } return $FilePaths; }
protected function countThemePages($path) { $result = 0; $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); $it->setMaxDepth(1); while ($it->valid()) { if (!$it->isDot() && !$it->isDir() && $it->getExtension() == 'htm') { $result++; } $it->next(); } return $result; }
public static function DirectoryContent($Directory, $UserLanguage) { $ReadDirectory = $Directory . str_replace('.language', '', $UserLanguage); $Iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($ReadDirectory)); $FilesArray = array(); while ($Iterator->valid()) { if (!$Iterator->isDot()) { $FilesArray[] = array('FileLink' => $Iterator->key(), 'FileName' => $Iterator->getSubPathName(), 'SmallFileName' => strtolower(str_replace('.language', '', $Iterator->getSubPathName())), 'LinesCount' => File::CountLines($Iterator->key())); } $Iterator->next(); } return $FilesArray; }
/** * Do the job. * Throw exceptions on errors (the job will be retried). */ public function execute() { global $CFG; $tmpdir = $CFG->tempdir; // Default to last weeks time. $time = time() - $CFG->tempdatafoldercleanup * 3600; $dir = new \RecursiveDirectoryIterator($tmpdir); // Show all child nodes prior to their parent. $iter = new \RecursiveIteratorIterator($dir, \RecursiveIteratorIterator::CHILD_FIRST); // An array of the full path (key) and date last modified. $modifieddateobject = array(); // Get the time modified for each directory node. Nodes will be updated // once a file is deleted, so we need a list of the original values. for ($iter->rewind(); $iter->valid(); $iter->next()) { $node = $iter->getRealPath(); if (!is_readable($node)) { continue; } $modifieddateobject[$node] = $iter->getMTime(); } // Now loop through again and remove old files and directories. for ($iter->rewind(); $iter->valid(); $iter->next()) { $node = $iter->getRealPath(); if (!is_readable($node)) { continue; } // Check if file or directory is older than the given time. if ($modifieddateobject[$node] < $time) { if ($iter->isDir() && !$iter->isDot()) { // Don't attempt to delete the directory if it isn't empty. if (!glob($node . DIRECTORY_SEPARATOR . '*')) { if (@rmdir($node) === false) { mtrace("Failed removing directory '{$node}'."); } } } if ($iter->isFile()) { if (@unlink($node) === false) { mtrace("Failed removing file '{$node}'."); } } } else { // Return the time modified to the original date only for real files. if ($iter->isDir() && !$iter->isDot()) { touch($node, $modifieddateobject[$node]); } } } }
/** * Looks for files in "core" code pool */ protected function _checkCoreCodePool() { $files = array(); /** @var \RecursiveDirectoryIterator $dir */ $dir = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->_extensionPath)); while ($dir->valid()) { if (!$dir->isDot() && strpos($dir->getSubPath(), 'app/code/core') !== false) { $files[] = $dir->getSubPathname(); } $dir->next(); } if (!empty($files)) { $this->_addIssue(array('type' => 'corehack', 'comment' => 'Usage of "core" code pool', 'files' => $files, 'occurrences' => count($files))); } }
public static function removeDir($dir) { $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, PHP_VERSION_ID < 50300 ? 0 : FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST); foreach ($iterator as $filename => $fileInfo) { if ($iterator->isDot()) { continue; } if ($fileInfo->isDir()) { rmdir($filename); } else { unlink($filename); } } rmdir($dir); }
protected function trackPattern($pattern) { $directory = pathinfo($pattern, PATHINFO_DIRNAME); $extension = pathinfo($pattern, PATHINFO_EXTENSION); $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory)); while ($iterator->valid()) { $file = $iterator->key(); $iterator->next(); if ($iterator->isDot()) { continue; } if ($extension == pathinfo($file, PATHINFO_EXTENSION)) { $this->trackFile($file); } } }
protected function tearDown() { if (file_exists($this->directory)) { $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->directory), RecursiveIteratorIterator::CHILD_FIRST); foreach ($iterator as $filename => $fileInfo) { if (!$iterator->isDot()) { if ($fileInfo->isDir()) { rmdir($filename); } else { unlink($filename); } } } rmdir($this->directory); } }
public function addDirectory($path, $localpath = null, $exclude_hidden = true) { if ($localpath === null) { $localpath = $path; } $localpath = rtrim($localpath, '/\\'); $path = rtrim($path, '/\\'); $iter = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST); while ($iter->valid()) { if (!$iter->isDot() && !$iter->isDir()) { if (!$exclude_hidden || !$this->is_path_hidden($iter->key())) { $this->addFile($iter->key(), $localpath . DIRECTORY_SEPARATOR . $iter->getSubPathName()); } } $iter->next(); } }
private static function searchByContent(\RecursiveIteratorIterator $file, $query) { if (!$file->isDot() && is_readable($file->key())) { try { $handle = fopen($file->key(), 'r'); if ($handle) { while (!feof($handle)) { if (strpos(fgets($handle), $query) !== false) { return true; } } } } catch (ContextErrorException $e) { } } return false; }
public function execute() { if (is_file($this->getPath())) { return array(basename($this->getPath())); } /** @var \RecursiveIteratorIterator $it */ $it = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->getPath())); $result = array(); // iterate over the directory and its subdirectories $it->rewind(); while ($it->valid()) { if (!$it->isDot()) { $result[] = $it->getSubPathName(); } $it->next(); } return $result; }
/** * @static * @param string $rootDir * @param string $matchRegExp * @param Closure|string $fn * @param bool $isRecursive * @return int */ static function applyToEachFile($rootDir, $matchRegExp, $fn, $isRecursive = true) { $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootDir)); $count = 0; foreach ($iterator as $item) { if ($iterator->isDot()) { continue; } $path = $item->getPathName(); if (preg_match($matchRegExp, $path)) { ++$count; if (false === $fn($path)) { break; } } } return $count; }
public function getHelpers($module) { try { $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($module . '/Helper')); } catch (UnexpectedValueException $e) { Mage::Log($e->getMessage()); return ''; } $options = array(); while ($it->valid()) { if (!$it->isDot()) { $label = strtolower(str_replace('.php', '', str_replace('/', '_', $it->getSubPathName()))); $options[$it->key() . '=>' . $label] = $label; } $it->next(); } return $this->_toOptionsFromHash($options); }
/** *## Iterate and Process * All the magic happens here, whils it looks complicated this is essentially a directory iterator * that creates an array of filenames, with a corresponding Phrocco object that can look after rendering itself. * **/ public function process() { $dir_iterator = new PhroccoIterator($this->options["i"]); $iterator = new \RecursiveIteratorIterator($dir_iterator, \RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $file) { if (!$iterator->isDot() && in_array($iterator->getExtension(), $this->extensions[$this->options["l"]])) { $base_path = $this->options["i"]; $rpath = str_replace($base_path, "", $file->getPath()); $phrocco = new Phrocco($this->options["l"], $file); if (!$this->options["o"]) { $output_dir = $file->getPath(); } else { $output_dir = $this->options["o"]; } if ($rpath != $file->getPath()) { $output_dir .= "/" . $rpath; } /** *### Check that we can write * This block ensures that the output directory exists. If it doesn't we'll try and create it before giving up. **/ if (!is_writable($output_dir)) { try { mkdir($output_dir, 0777, true); } catch (Exception $e) { if (!is_writable($output_dir)) { throw new \Exception("Invalid Output Directory - Couldn't Create Because of Permissions"); } } } /** *### Build the documentation tree * This block builds the documentation file layout to mirror the code files scanned. **/ $file_out = $output_dir . "/" . $file->getBasename($iterator->getExtension()) . "html"; $phrocco->output_file = $file_out; $subpath = $iterator->getSubPath(); $phrocco->path = (!empty($subpath) ? "./" : '') . $subpath; $this->group[$file->getBasename()] = $phrocco; $subpath .= !empty($subpath) ? '/' : ''; $this->sources[] = array("url" => $subpath . $file->getBasename($iterator->getExtension()) . "html", "name" => $file->getBasename(), "level" => $iterator->getDepth(), "folder" => $iterator->getSubPath()); } } }
private function findControllers() { $path = constant('APPLICATION_PATH') . '/modules'; $found = []; $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); $it->rewind(); while ($it->valid()) { if (!$it->isDot()) { if (false !== strpos($it->getSubPathName(), 'Controller') && false == strpos($it->getSubPathName(), 'Abstract')) { //echo 'SubPathName: ' . $it->getSubPathName() . "\n"; //echo 'SubPath: ' . $it->getSubPath() . "\n"; //echo 'Key: ' . $it->key() . "\n\n"; $found[] = $it->key(); } } $it->next(); } return $found; }
public static function GetDirectoryContent($Directory, $SearchForFormat = null) { $Iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($Directory)); $FilesArray = array(); while ($Iterator->valid()) { if (!$Iterator->isDot()) { if ($SearchForFormat != null) { $Exploded = explode('.', $Iterator->getSubPathName()); if ($Exploded[1] == $SearchForFormat) { $FilesArray[] = array('FileLink' => $Iterator->key(), 'FileName' => $Iterator->getSubPathName()); } } else { $FilesArray[] = array('FileLink' => $Iterator->key(), 'FileName' => $Iterator->getSubPathName()); } } $Iterator->next(); } return $FilesArray; }
/** * Delete compiled template file * * @param string $resource_name template name * @param string $compile_id compile id * @param integer $exp_time expiration time * @return integer number of template files deleted */ function Smarty_Method_Clear_Compiled_Tpl($smarty, $resource_name = null, $compile_id = null, $exp_time = null) { $_compile_id = isset($compile_id) ? preg_replace('![^\\w\\|]+!', '_', $compile_id) : null; $_dir_sep = $smarty->use_sub_dirs ? DS : '^'; if (isset($resource_name)) { $_resource_part_1 = $resource_name . '.php'; $_resource_part_2 = $resource_name . '.cache' . '.php'; } else { $_resource_part = ''; } $_dir = $smarty->compile_dir; if ($smarty->use_sub_dirs && isset($_compile_id)) { $_dir .= $_compile_id . $_dir_sep; } if (isset($_compile_id)) { $_compile_id_part = $smarty->compile_dir . $_compile_id . $_dir_sep; } $_count = 0; $_compileDirs = new RecursiveDirectoryIterator($_dir); $_compile = new RecursiveIteratorIterator($_compileDirs, RecursiveIteratorIterator::CHILD_FIRST); foreach ($_compile as $_file) { if (strpos($_file, '.svn') !== false) { continue; } if ($_file->isDir()) { if (!$_compile->isDot()) { // delete folder if empty @rmdir($_file->getPathname()); } } else { if ((!isset($_compile_id) || strlen((string) $_file) > strlen($_compile_id_part) && substr_compare((string) $_file, $_compile_id_part, 0, strlen($_compile_id_part)) == 0) && (!isset($resource_name) || strlen((string) $_file) > strlen($_resource_part_1) && substr_compare((string) $_file, $_resource_part_1, -strlen($_resource_part_1), strlen($_resource_part_1)) == 0 || strlen((string) $_file) > strlen($_resource_part_2) && substr_compare((string) $_file, $_resource_part_2, -strlen($_resource_part_2), strlen($_resource_part_2)) == 0)) { if (isset($exp_time)) { if (time() - @filemtime($_file) >= $exp_time) { $_count += @unlink((string) $_file) ? 1 : 0; } } else { $_count += @unlink((string) $_file) ? 1 : 0; } } } } return $_count; }