/** * Renames the current directory * * This operation will NOT be performed until the filesystem transaction * has been committed, if a transaction is in progress. Any non-Flourish * code (PHP or system) will still see this directory (and all contained * files/dirs) as existing with the old paths until that point. * * @param string $new_dirname The new full path to the directory or a new name in the current parent directory * @param boolean $overwrite If the new dirname already exists, TRUE will cause the file to be overwritten, FALSE will cause the new filename to change * @return void */ public function rename($new_dirname, $overwrite) { $this->tossIfDeleted(); if (!$this->getParent()->isWritable()) { throw new fEnvironmentException('The directory, %s, can not be renamed because the directory containing it is not writable', $this->directory); } // If the dirname does not contain any folder traversal, rename the dir in the current parent directory if (preg_match('#^[^/\\\\]+$#D', $new_dirname)) { $new_dirname = $this->getParent()->getPath() . $new_dirname; } $info = fFilesystem::getPathInfo($new_dirname); if (!file_exists($info['dirname'])) { throw new fProgrammerException('The new directory name specified, %s, is inside of a directory that does not exist', $new_dirname); } if (file_exists($new_dirname)) { if (!is_writable($new_dirname)) { throw new fEnvironmentException('The new directory name specified, %s, already exists, but is not writable', $new_dirname); } if (!$overwrite) { $new_dirname = fFilesystem::makeUniqueName($new_dirname); } } else { $parent_dir = new fDirectory($info['dirname']); if (!$parent_dir->isWritable()) { throw new fEnvironmentException('The new directory name specified, %s, is inside of a directory that is not writable', $new_dirname); } } rename($this->directory, $new_dirname); // Make the dirname absolute $new_dirname = fDirectory::makeCanonical(realpath($new_dirname)); // Allow filesystem transactions if (fFilesystem::isInsideTransaction()) { fFilesystem::rename($this->directory, $new_dirname); } fFilesystem::updateFilenameMapForDirectory($this->directory, $new_dirname); }
/** * Renames the current file * * If the filename already exists and the overwrite flag is set to false, * a new filename will be created. * * This operation will be reverted if a filesystem transaction is in * progress and is later rolled back. * * @param string $new_filename The new full path to the file or a new filename in the current directory * @param boolean $overwrite If the new filename already exists, `TRUE` will cause the file to be overwritten, `FALSE` will cause the new filename to change * @return fFile The file object, to allow for method chaining */ public function rename($new_filename, $overwrite) { $this->tossIfDeleted(); if (!$this->getParent()->isWritable()) { throw new fEnvironmentException('The file, %s, can not be renamed because the directory containing it is not writable', $this->file); } // If the filename does not contain any folder traversal, rename the file in the current directory if (preg_match('#^[^/\\\\]+$#D', $new_filename)) { $new_filename = $this->getParent()->getPath() . $new_filename; } $info = fFilesystem::getPathInfo($new_filename); if (!file_exists($info['dirname'])) { throw new fProgrammerException('The new filename specified, %s, is inside of a directory that does not exist', $new_filename); } // Make the filename absolute $new_filename = fDirectory::makeCanonical(realpath($info['dirname'])) . $info['basename']; if ($this->file == $new_filename && $overwrite) { return $this; } if (file_exists($new_filename) && !$overwrite) { $new_filename = fFilesystem::makeUniqueName($new_filename); } if (file_exists($new_filename)) { if (!is_writable($new_filename)) { throw new fEnvironmentException('The new filename specified, %s, already exists, but is not writable', $new_filename); } if (fFilesystem::isInsideTransaction()) { fFilesystem::recordWrite(new fFile($new_filename)); } // Windows requires that the existing file be deleted before being replaced unlink($new_filename); } else { $new_dir = new fDirectory($info['dirname']); if (!$new_dir->isWritable()) { throw new fEnvironmentException('The new filename specified, %s, is inside of a directory that is not writable', $new_filename); } } rename($this->file, $new_filename); // Allow filesystem transactions if (fFilesystem::isInsideTransaction()) { fFilesystem::recordRename($this->file, $new_filename); } fFilesystem::updateFilenameMap($this->file, $new_filename); return $this; }
/** * Converts PHP short tags to long tags when short tags are turned off * * Please note that this only affects PHP files that are **directly** * evaluated with ::place() or ::inject(). It will not affect PHP files that * have been evaluated via `include` or `require` statements inside of the * directly evaluated PHP files. * * This functionality will be inherited by all child fTemplating objects * that do not have their own explicit short tag settings. * * @param string $mode The compilation mode - `'development'` means that file modification times will be checked on each load, `'production'` means that the cache files will only be regenerated when missing * @param fDirectory|string $cache_directory The directory to cache the compiled files into - this directory should not be accessible from the web * @return void */ public function enablePHPShortTags($mode, $cache_directory) { // This does not need to be enabled if short tags are on if (ini_get('short_open_tag') && strtolower(ini_get('short_open_tag')) != 'off') { return; } $valid_modes = array('development', 'production'); if (!in_array($mode, $valid_modes)) { throw new fProgrammerException('The mode specified, %1$s, is invalid. Must be one of: %2$s.', $mode, join(', ', $valid_modes)); } $cache_directory = $cache_directory instanceof fDirectory ? $cache_directory->getPath() : $cache_directory; if (!is_writable($cache_directory)) { throw new fEnvironmentException('The cache directory specified, %s, is not writable', $cache_directory); } $this->short_tag_mode = $mode; $this->short_tag_directory = fDirectory::makeCanonical($cache_directory); }
/** * Sets the directory the ImageMagick binary is installed in and tells the class to use ImageMagick even if GD is installed * * @param string $directory The directory ImageMagick is installed in * @return void */ public static function setImageMagickDirectory($directory) { $directory = fDirectory::makeCanonical($directory); self::checkImageMagickBinary($directory); self::$imagemagick_dir = $directory; self::$processor = 'imagemagick'; }