public function copy(Directory $destination, $fileOverwrite = true) { $self = $this->getPathname(); $dest = $destination->getPathname() . DIRECTORY_SEPARATOR . $this->getBasename(); foreach ($this->getIterator() as $file) { $old = $file->getPathname(); $new = substr($old, strlen($self)); $new = $dest . $new; $base = dirname($new); if ($file instanceof Directory) { static::create($new); continue; } if (!is_file($new) || $fileOverwrite) { if (!@copy($old, $new)) { throw new RuntimeException('File ' . $old . ' could not be copied to ' . $new . '.'); } } elseif (is_file($new) && !$fileOverwrite) { throw new RuntimeException('File ' . $new . ' already exists.'); } } return $this; }
public function testDirectoryCreateOpenAndDelete() { if (!$this->testWritable()) { return; } try { $file = Directory::create($this->getTestDir()); } catch (Exception $e) { $this->assert(false, "The directory could not be created with message: {$e->getMessage()}."); } try { $file = Directory::open($file->getPathname()); } catch (Exception $e) { $this->assert(false, "The directory could not be opened with message: {$e->getMessage()}."); } try { $file->delete(); } catch (Exception $e) { $this->assert(false, "The directory {$file->getPathname()} could not be deleted with message: {$e->getMessage()}."); } }
public static function create($file, $mask = self::MASK) { // the file must not exist if (is_file($file)) { throw new LogicException("The file {$file} already exists."); } // the directory it is supposed to be in $dir = dirname($file); // create the directory if it doesn't exist if (!is_dir($dir)) { Directory::create($dir); } // create the file in the directory file_put_contents($file, ''); // set permissions chmod($file, $mask); // open and return return static::open($file); }