Ejemplo n.º 1
0
 /**
  * Set the file access mode used by the file handler to open the file.
  * This will reopen the file handler if the file access mode changed and it was opened already.
  *
  * @param FileAccessMode|string $fileMode The file access mode instance or the file access mode as a string that
  * should be used by the file handler to open the file.
  *
  * @return bool True on success, false on failure.
  */
 public function setFileAccessMode($fileMode)
 {
     // Convert $fileMode into a FileAccessMode instance
     if (($fileMode = FileAccessMode::asFileAccessMode($fileMode)) === null) {
         return false;
     }
     // Set the file access mode that should be used
     $this->fileMode = $fileMode;
     // Check whether the file handler is opened and whether the file access mode should be changed.
     // If so, reopen the file handler with the proper file access mode
     if ($this->handler !== null) {
         if ($this->handler->isOpened() && !$fileMode->equals($this->handler->getFileAccessMode(), false)) {
             return $this->handler->reopen($this->fileMode);
         }
     }
     // File access mode changed successfully, return the result
     return true;
 }
Ejemplo n.º 2
0
 /**
  * Reopen the currently opened file handle. The file handle must be opened in order to reopen it.
  *
  * @param FileAccessMode|string $mode [optional] The file access mode used by the handler as file access mode
  * instance or as PHPs file access mode string. See PHPs fopen() function for more details. This file access mode
  * will be used if the handle needs to be reopened. If set to null, the current file access mode will be used.
  * @param resource|null $context [optional] See PHPs fopen() function for more details.
  * The context of the current opened file handle will be used if not specified.
  *
  * @return bool True on success, false on failure. False will also be returned if the file handle wasn't opened.
  *
  * @see fopen();
  */
 public function reopen($mode = null, $context = null)
 {
     // Make sure the file handle is opened
     if (!$this->isOpened()) {
         return false;
     }
     // Try to convert $mode into a FileAccessMode instance if set
     $mode = FileAccessMode::asFileAccessMode($mode);
     // TODO: Store the current context!
     // Store the current file mode and context if not set
     if ($mode === null) {
         $mode = $this->fileMode;
     }
     if ($context === null) {
         $context = null;
     }
     // Close the file handle, return false on error
     if (!$this->close()) {
         return false;
     }
     // Reopen the file handle, return the result
     return $this->open($mode, $context);
 }