コード例 #1
0
 /**
  * Creates a new empty file using touch().
  *
  * @param mixed sfFilebasePluginFile | string $path
  * @param integer $perms
  * @return sfFilebasePluginFile $new_file
  */
 public function touch($path, $perms = null)
 {
     // Wrap around, because isDir() returs false on non-existing files.
     $path = new sfFilebasePluginFile($this->getFilebaseFile($path), $this);
     $dest = new sfFilebasePluginDirectory($path->getPath(), $this);
     if (!$dest->fileExists()) {
         throw new sfFilebasePluginException(sprintf('Destination directory %s does not exist.', $dest->getPathname()));
     }
     if (!$dest->isWritable()) {
         throw new sfFilebasePluginException(sprintf('Destination directory %s is write protected.', $dest->getPathname()));
     }
     if (!$this->isInFilebase($dest)) {
         throw new sfFilebasePluginException(sprintf('Destination directory %s does not belong to filebase %s, access forbidden due to security issues.', $dest->getPathname(), $this->getPathname()));
     }
     if ($path->fileExists()) {
         throw new sfFilebasePluginException(sprintf('File %s already exists', $path->getPathname()));
     }
     if (!@touch($path->getPathname())) {
         throw new sfFilebasePluginException(sprintf('Error creating file %s', $path->getPathname()));
     }
     if ($perms !== null) {
         //  Chmodde file
         $path->chmod($perms);
     }
     return $path;
 }
コード例 #2
0
 /**
  * Returns a new (child of) sfFilebasePluginFile. It returns an instance,
  * not regarding if the file exists or not. So you can fetch an instance
  * and create the underlying file by sfFilebasePlugin-methods.
  *
  * This method checks if file is a directory, an image
  * a link ... but beware, some attributes can only
  * be calculated if the file really exists in file system.
  *
  * For example, if you want to retrieve a directory that does
  * not exists, an instance of sfFilebasePluginFile is recieved.
  *
  * This behavior is simple and logically correct, but you have to keep it in
  * mind: sfFilebasePlugin cannot forecast what files you want to create. It
  * is probably more confusing for windows users where files are mostly
  * identified by its extension.
  *
  * If the dir exists, you'll get a sfFilebasePluginDirectory.
  *
  * @example $filebase->getFilebaseFile('path/to/directory') retrieves a dir
  *          if it exists in FS, or a sfFilebaseFile if not.
  *
  * @example Creating a new file:
  *          $new_file = $filebase->getFilebaseFile('path/to/new/file.txt');
  *          $filebase->touch($new_file);
  *          In Short: $filebase->touch('path/to/new/file.txt');
  *
  * @example Short way retrieving files:
  *          foreach ($filebase AS $file) ...
  *          with ArrayAccess:
  *          $file = $filebase['/path/to/file'];
  *
  * @todo Implement (sym)link handling.
  * @param string | sfFilebasePluginFile  $filename
  * @return sfFilebasePluginFile
  */
 public function getFilebaseFile($filename)
 {
     if (is_string($filename)) {
         //$filename = sfFilebasePluginUtil::unifySlashes($filename);
         if (strlen($filename) > 0) {
             if (sfFilebasePluginUtil::isAbsolutePathname($filename)) {
                 $filename = new sfFilebasePluginFile($filename, $this->filebase);
             } else {
                 $filename = new sfFilebasePluginFile($this->getPathname() . '/' . $filename, $this->filebase);
             }
         } else {
             $filename = $this;
         }
     }
     if ($filename instanceof sfFilebasePluginFile) {
         // returns only true if file exists, so beware of that
         if ($filename->isLink()) {
             throw new sfFilebasePluginException(sprintf('Error retrieving link %s: Link handling is not yet implemented', $filename->getPathname()));
             //return new sfFilebasePluginLink($filename->getPathname());
         } elseif ($filename->isDir()) {
             $filename = new sfFilebasePluginDirectory($filename->getPathname(), $this->filebase);
         } elseif ($this->filebase->getIsSupportedImage($filename)) {
             $filename = new sfFilebasePluginImage($filename->getPathname(), $this->filebase);
         }
         return $filename;
     }
     throw new sfFilebasePluginException(sprintf('File %s must be of type [string] or instanceof FilebaseFile', $filename));
 }