/** * @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; }
function array_flatten(array $array, $key_separator = '/') { $result = array(); // a stack of strings $keys = array(); $prev_depth = -1; $iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($array), RecursiveIteratorIterator::SELF_FIRST); // rewind() is necessary for ($iter->rewind(); $iter->valid(); $iter->next()) { $curr_depth = $iter->getDepth(); $diff_depth = $prev_depth - $curr_depth + 1; //##### TODO: It would be nice to do this with a single function. while ($diff_depth > 0) { array_pop($keys); --$diff_depth; } /* Note: http://bugs.php.net/bug.php?id=52425 array_shift/array_pop: add parameter that tells how many to elements to pop */ array_push($keys, $iter->key()); if (is_scalar($iter->current())) { $result[implode($key_separator, $keys)] = $iter->current(); } $prev_depth = $curr_depth; } return $result; }
public function rmdir($path) { if (!$this->isDeletable($path)) { return false; } try { $it = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($this->getSourcePath($path)), \RecursiveIteratorIterator::CHILD_FIRST ); /** * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach * This bug is fixed in PHP 5.5.9 or before * See #8376 */ $it->rewind(); while ($it->valid()) { /** * @var \SplFileInfo $file */ $file = $it->current(); if (in_array($file->getBasename(), array('.', '..'))) { $it->next(); continue; } elseif ($file->isDir()) { rmdir($file->getPathname()); } elseif ($file->isFile() || $file->isLink()) { unlink($file->getPathname()); } $it->next(); } return rmdir($this->getSourcePath($path)); } catch (\UnexpectedValueException $e) { return false; } }
public static function findDirectories($rootDir, $multiMap = true) { if (!is_dir($rootDir)) { throw new \LogicException($rootDir . ' is not a directory'); } $elements = array(); $d = @dir($rootDir); if (!$d) { throw new \LogicException('Directory is not readable'); } $dirs = array(); $it = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($rootDir, \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST); while ($it->valid()) { if (!$it->isDir()) { $it->next(); } else { $cur = $it->current(); if ($cur->isDir()) { if ($multiMap) { $dirs[$cur->getPath()][] = $cur->getFilename(); } else { $dirs[] = $cur->getPath(); } } $it->next(); } } return $dirs; }
function get_directory_list($settings = false) { $directory = !empty($settings['dir']) ? $settings['dir'] : CLIENT_DIR . "/"; $array = array(); $array['dirs'] = array(); $array['host'] = array(); $array['root'] = array(); if (!is_dir($directory)) { return false; } $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), RecursiveIteratorIterator::CHILD_FIRST); // Loop through directories while ($dir->valid()) { try { $file = $dir->current(); ob_start(); echo $file; $data = ob_get_contents(); ob_end_clean(); $data = trim($data); if (basename($data) != '.' && basename($data) != '..') { $array['host'][] = $data; $array['root'][] = str_replace(ROOT_DIR, "", $data); if (is_dir($data) && !in_array($data . "/", $array['dirs'])) { $array['dirs'][] = $data . "/"; } } unset($data); $dir->next(); } catch (UnexpectedValueException $e) { continue; } } return isset($array) ? $array : false; }
function current() { if ($this->rit_flags & self::BYPASS_CURRENT) { return parent::current(); } else { return $this->getPrefix() . $this->getEntry() . $this->getPostfix(); } }
public function testIterate() { $sitemap = new Sitemap("h-1", "home", null); $n1 = $sitemap->getRoot()->addChild(new Node("h-1-1", "n1", null)); $n2 = $n1->addChild(new Node("h-1-1-1", "n2", null)); $n3 = $n1->addChild(new Node("h-1-1-2", "n3", null)); $it = new \RecursiveIteratorIterator($sitemap->getIterator(), \RecursiveIteratorIterator::SELF_FIRST); $it->rewind(); $this->assertEquals($sitemap->getRoot(), $it->current(), "Se esperaba que el primer elemento del iterador sea la raiz."); $it->next(); $this->assertEquals($n1, $it->current(), "Se esperaba que el segundo elemento del iterador sea el primer hijo de la raiz."); $it->next(); $this->assertEquals($n2, $it->current(), "Se esperaba que el tercer elemento del iterador sea el primer hijo del primer hijo de la raiz."); $it->next(); $this->assertEquals($n3, $it->current(), "Se esperaba que el cuarto elemento del iterador sea el segundo hijo del primer hijo de la raiz."); $it->next(); $this->assertFalse($it->valid(), "Se esperaba que el luego del cuarto elemento el iterador quede inválido."); }
/** * Get the current element * * @see http://www.php.net/RecursiveIteratorIterator * @return mixed */ public function current() { $current = parent::current(); if (is_array($current)) { $this->namespaceStack[] = $this->key(); $this->nodeType = self::NODE_PARENT; } else { $this->nodeType = self::NODE_ITEM; } return $current; }
function current() { $current = parent::current(); $key = parent::key(); $retValue = "<td style='width:150px;border:1px solid black;'>\n\t\t\t \t\t \t<input "; if ($key == 'Id') { $retValue .= "readonly "; } $retValue .= "type='text' name='" . htmlspecialchars($key) . "' value='" . htmlspecialchars($current) . "'>\n\t\t\t \t\t</td>"; return $retValue; }
public function valid() { $valid = $this->iterator->valid(); while ($valid) { $filename = $this->iterator->current()->getPathname(); $ext = Fn::fileExtension($filename); if ($ext !== 'original-filename' && is_file($filename . '.original-filename')) { $this->currentFilename = $filename; break; } $this->iterator->next(); $valid = $this->iterator->valid(); } return $valid; }
/** * Searches value inside a multidimensional array, returning its index * * Original function by "giulio provasi" (link below) * * @param mixed|array $haystack * The haystack to search * * @param mixed $needle * The needle we are looking for * * @param mixed|optional $index * Allow to define a specific index where the data will be searched * * @return integer|string * If given needle can be found in given haystack, its index will * be returned. Otherwise, -1 will * * @see http://www.php.net/manual/en/function.array-search.php#97645 */ public static function search($haystack, $needle, $index = NULL) { if (is_null($haystack)) { return -1; } $arrayIterator = new \RecursiveArrayIterator($haystack); $iterator = new \RecursiveIteratorIterator($arrayIterator); while ($iterator->valid()) { if ((isset($index) and $iterator->key() == $index or !isset($index)) and $iterator->current() == $needle) { return $arrayIterator->key(); } $iterator->next(); } return -1; }
/** * Finder method to be able to find resources by context name * and attributes. Example usage: * * <code> * * </code> * * @param Zend_Tool_Project_Profile_Resource_SearchConstraints|string|array $searchParameters * @return Zend_Tool_Project_Profile_Resource */ public function search($searchConstraints) { if (!$searchConstraints instanceof Zend_Tool_Project_Profile_Resource_SearchConstraints) { $searchConstraints = new Zend_Tool_Project_Profile_Resource_SearchConstraints($searchConstraints); } $this->rewind(); $riIterator = new RecursiveIteratorIterator($this, RecursiveIteratorIterator::SELF_FIRST); $foundResource = false; $currentConstraint = $searchConstraints->getConstraint(); $foundDepth = 0; while ($currentResource = $riIterator->current()) { // if current depth is less than found depth, end if ($riIterator->getDepth() < $foundDepth) { break; } if (strtolower($currentResource->getName()) == strtolower($currentConstraint->name)) { $paramsMatch = true; // @todo check to ensure params match (perhaps) if (count($currentConstraint->params) > 0) { $currentResourceAttributes = $currentResource->getAttributes(); foreach ($currentConstraint->params as $paramName => $paramValue) { if (!isset($currentResourceAttributes[$paramName]) || $currentResourceAttributes[$paramName] != $paramValue) { $paramsMatch = false; break; } } } if ($paramsMatch) { $foundDepth = $riIterator->getDepth(); if (($currentConstraint = $searchConstraints->getConstraint()) == null) { $foundResource = $currentResource; break; } } } $riIterator->next(); } return $foundResource; }
function UnlinkDPL() { global $TopDirectory; try { $TopDirectoryCnt = count($TopDirectory); //print "TopDirectoryCnt: " . $TopDirectoryCnt . $NL; foreach ($TopDirectory as $Dir => $RM) { $it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($Dir)); while ($it->valid()) { if ($it->isFile()) { $ext = pathinfo($it->current(), PATHINFO_EXTENSION); if ($ext == "dpl") { unlink($it->getPathName()); } } $it->next(); } } } catch (Exception $e) { echo $e->getMessage(); } }
public function calculate_wp_default_path() { global $phpbb_root_path; //find wp install $wp_path = $phpbb_root_path; $i = 0; $found = false; do { //http://php.net/manual/en/class.recursivedirectoryiterator.php#114504 $directory = new \RecursiveDirectoryIterator($wp_path, \FilesystemIterator::FOLLOW_SYMLINKS); //OH WAIT? this PIECE OF SHI*T doesnt work for recursive directory that arent the parent. GOD. WHY. $filter = new \RecursiveCallbackFilterIterator($directory, function ($current, $key, $iterator) { //in case we take the time to exclude the self from the previous loop //well, it's ez, but im lazy return true; }); $iterator = new \RecursiveIteratorIterator($filter); //directory dependance of the callback request us to ... redefine the whole goddam thing each loop. cmon.. $files = array(); $iterator->rewind(); while ($iterator->valid() || !$found) { $info = $iterator->current(); $iterator->next(); //alasfiltering must be done here cause filter doesnt filter.meh. if (strpos($info->getFilename(), 'wp-config.php') === 0) { $files[] = $info->getPath(); } //actually, yeah, we stop once we found one. $found = true; } //We got up 1 lvl in hierarchy $wp_path = $wp_path . '../'; $i++; } while ($i < 2 || !$found); return !empty($files) ? $files[0] : ""; }
protected function parseContents(\SimpleXMLIterator $iterator, FileCollection $files) { // Load files $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST); $iterator->rewind(); $lastDepth = -1; $path = array(); while ($iterator->valid()) { if ($lastDepth > $iterator->getDepth()) { array_pop($path); while ($lastDepth != $iterator->getDepth()) { array_pop($path); $lastDepth--; } } elseif ($lastDepth === $iterator->getDepth()) { array_pop($path); } $elt = $iterator->current(); if ($elt->getName() === 'file') { $path[] = (string) $elt['name']; $filePath = implode('/', $path); $file = new File($filePath, $filePath, $elt['role']); // Find file tasks $tasks = $elt->children('http://pear.php.net/dtd/tasks-1.0'); foreach ($tasks as $task) { $options = current((array) $task->attributes()); $file->addTask($task->getName(), $options); } $files[] = $file; } elseif ($elt->getName() === 'dir') { $path[] = ((string) $elt['name'] === '/' ? '' : $elt['name']) . (isset($elt['baseinstalldir']) && (string) $elt['baseinstalldir'] !== '/' ? '/' . $elt['baseinstalldir'] : ''); } $lastDepth = $iterator->getDepth(); $iterator->next(); } }
/** * get the size from a given root folder * @param \OC\Files\View $view file view on the root folder * @return integer size of the folder */ private static function calculateSize($view) { $root = \OCP\Config::getSystemValue('datadirectory') . $view->getAbsolutePath(''); if (!file_exists($root)) { return 0; } $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($root), \RecursiveIteratorIterator::CHILD_FIRST); $size = 0; /** * RecursiveDirectoryIterator on an NFS path isn't iterable with foreach * This bug is fixed in PHP 5.5.9 or before * See #8376 */ $iterator->rewind(); while ($iterator->valid()) { $path = $iterator->current(); $relpath = substr($path, strlen($root) - 1); if (!$view->is_dir($relpath)) { $size += $view->filesize($relpath); } $iterator->next(); } return $size; }
/** * Take an array of mixed strings and arrays, assuming that they are all * relevant to ONE command and create a string that conforms to the Redis * protocol * @return string */ protected function protocol(array $args) { $iter1 = new \RecursiveArrayIterator($args); $iter2 = new \RecursiveIteratorIterator($iter1); $cmd = ""; $i = 0; for ($iter2->rewind(); $iter2->valid(); $iter2->next()) { if (is_null($iter2->current())) { continue; } ++$i; $cmd .= "\$" . strlen($iter2->current()); $cmd .= $this->DELIM; $cmd .= $iter2->current(); $cmd .= $this->DELIM; } $command = sprintf("*%d%s%s", $i, $this->DELIM, $cmd); return $command; }
function current() { echo __METHOD__ . "\n"; return parent::current(); }
function current() { //return "<td style='width:150px;border:1px solid black;'>" .parent::current(). "</td>"; Esta era la linea original return parent::current(); }
/** * Folder equivalent to copyFileWithinStorage(). * * @param \TYPO3\CMS\Core\Resource\Folder $folderToCopy * @param \TYPO3\CMS\Core\Resource\Folder $targetFolder * @param string $newFolderName * @return boolean */ public function copyFolderWithinStorage(\TYPO3\CMS\Core\Resource\Folder $folderToCopy, \TYPO3\CMS\Core\Resource\Folder $targetFolder, $newFolderName) { // This target folder path already includes the topmost level, i.e. the folder this method knows as $folderToCopy. // We can thus rely on this folder being present and just create the subfolder we want to copy to. $targetFolderPath = $this->getAbsolutePath($targetFolder) . $newFolderName . '/'; mkdir($targetFolderPath); $sourceFolderPath = $this->getAbsolutePath($folderToCopy); /** @var $iterator RecursiveDirectoryIterator */ $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($sourceFolderPath)); while ($iterator->valid()) { /** @var $current RecursiveDirectoryIterator */ $current = $iterator->current(); $itemSubPath = $iterator->getSubPathname(); if ($current->isDir() && !($itemSubPath === '..' || $itemSubPath === '.')) { mkdir($targetFolderPath . $itemSubPath); } elseif ($current->isFile()) { $result = copy($sourceFolderPath . $itemSubPath, $targetFolderPath . $itemSubPath); if ($result === FALSE) { throw new \TYPO3\CMS\Core\Resource\Exception\FileOperationErrorException('Copying file "' . $sourceFolderPath . $itemSubPath . '" to "' . $targetFolderPath . $itemSubPath . '" failed.', 1330119452); } } $iterator->next(); } return TRUE; }
/** * Folder equivalent to copyFileWithinStorage(). * * @param string $sourceFolderIdentifier * @param string $targetFolderIdentifier * @param string $newFolderName * * @return bool * @throws Exception\FileOperationErrorException */ public function copyFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName) { // This target folder path already includes the topmost level, i.e. the folder this method knows as $folderToCopy. // We can thus rely on this folder being present and just create the subfolder we want to copy to. $newFolderIdentifier = $this->canonicalizeAndCheckFolderIdentifier($targetFolderIdentifier . '/' . $newFolderName); $sourceFolderPath = $this->getAbsolutePath($sourceFolderIdentifier); $targetFolderPath = $this->getAbsolutePath($newFolderIdentifier); mkdir($targetFolderPath); /** @var $iterator \RecursiveDirectoryIterator */ $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($sourceFolderPath), \RecursiveIteratorIterator::SELF_FIRST); // Rewind the iterator as this is important for some systems e.g. Windows $iterator->rewind(); while ($iterator->valid()) { /** @var $current \RecursiveDirectoryIterator */ $current = $iterator->current(); $fileName = $current->getFilename(); $itemSubPath = GeneralUtility::fixWindowsFilePath($iterator->getSubPathname()); if ($current->isDir() && !($fileName === '..' || $fileName === '.')) { GeneralUtility::mkdir($targetFolderPath . '/' . $itemSubPath); } elseif ($current->isFile()) { $result = copy($sourceFolderPath . '/' . $itemSubPath, $targetFolderPath . '/' . $itemSubPath); if ($result === false) { // rollback GeneralUtility::rmdir($targetFolderIdentifier, true); throw new Exception\FileOperationErrorException('Copying file "' . $sourceFolderPath . $itemSubPath . '" to "' . $targetFolderPath . $itemSubPath . '" failed.', 1330119452); } } $iterator->next(); } GeneralUtility::fixPermissions($targetFolderPath, true); return true; }
/** * Find a value also in nested arrays/objects * * @param mixed $needle The value to search for * * @return string The key of that value * * @since 1.0.0 */ public function searchRecursive($needle) { $aIt = new RecursiveArrayIterator($this); $it = new RecursiveIteratorIterator($aIt); while ($it->valid()) { if ($it->current() == $needle) { return $aIt->key(); } $it->next(); } return false; }
public function current() { return new FileResource(parent::current()->getPathname()); }
function current() { return "<td style='width: 150px; border: 1px solid black;'>" . parent::current() . "</td>"; }
public static function copyDir($source, $target) { if (!file_exists($source)) { return; } try { $done = array(); $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST); while ($iterator->valid()) { if ($iterator->isDot()) { $iterator->next(); continue; } $file = $iterator->current(); $targetPath = $target . DIRECTORY_SEPARATOR . $iterator->getSubPathName(); $iterator->next(); // It seems RecursiveIteratorIterator can return the save file twice if (isset($done[$targetPath])) { continue; } $done[$targetPath] = true; // Copy directory or file if ($file->isDir()) { if (!mkdir($targetPath, $file->getPerms())) { throw new IOException('Unable to copy directory, failed to create directory ' . $targetPath . ' - ' . $file->getPathname()); } } elseif (!copy($file->getPathName(), $targetPath)) { throw new IOException('Unable to copy directory, failed to copy the file'); } if (!chmod($targetPath, $file->getPerms())) { throw new IOException('Unable to copy directory, failed to set permissions'); } if (!touch($targetPath, $file->getMTime(), $file->getATime())) { throw new IOException('Unable to copy directory, touch failed'); } } } catch (\UnexpectedValueException $e) { throw new IOException('directory copy failed: ' . $e->getMessage(), $e); } }
function array_get($array, $searched, $index) { $aIt = new RecursiveArrayIterator($array); $it = new RecursiveIteratorIterator($aIt); while ($it->valid()) { if ((isset($index) and $it->key() == $index or !isset($index)) and $it->current() == $searched) { $c = $aIt->current(); return $c; // return $c[$key]; } $it->next(); } return FALSE; }
function current() { return "<td>" . parent::current() . "</td>"; }
/** * current * * @access public * @return void */ public function current() { $sub_path = $this->getSubPathname(); while (parent::valid() && isset($this->__ignore_files[$sub_path])) { parent::next(); $sub_path = $this->getSubPathname(); } return parent::current(); }
/** * performs a search in a nested array * @param array $haystack the array to be searched * @param string $needle the search string * @param string $index optional, only search this key name * @return mixed the key of the matching field, otherwise false * * performs a search in a nested array * * taken from http://www.php.net/manual/en/function.array-search.php#97645 */ public static function recursiveArraySearch($haystack, $needle, $index = null) { $aIt = new RecursiveArrayIterator($haystack); $it = new RecursiveIteratorIterator($aIt); while ($it->valid()) { if ((isset($index) and $it->key() == $index or !isset($index)) and $it->current() == $needle) { return $aIt->key(); } $it->next(); } return false; }
/** * * Search for needle in a recursive array * @author http://www.php.net/manual/en/function.array-search.php#97645 * * @param $haystack * @param $needle * @param $index */ function rarray_search($needle, $haystack, $index = null) { $aIt = new RecursiveArrayIterator($haystack); $it = new RecursiveIteratorIterator($aIt); // Tar bort ".www" om det finns för bättre jämföring $needle = preg_replace('/\bwww./', '', $needle); while($it->valid()) { // Tar bort ".www" om det finns för bättre jämföring $current = preg_replace('/\bwww./', '', $it->current()); if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($current == $needle)) { return $aIt->key(); } $it->next(); } return FALSE; }