Changeset View
Changeset View
Standalone View
Standalone View
src/filesystem/TempFile.php
| Show All 13 Lines | |||||
| * @task config Configuration | * @task config Configuration | ||||
| * @task internal Internals | * @task internal Internals | ||||
| */ | */ | ||||
| final class TempFile extends Phobject { | final class TempFile extends Phobject { | ||||
| private $dir; | private $dir; | ||||
| private $file; | private $file; | ||||
| private $preserve; | private $preserve; | ||||
| private $ignoreRemovalFailure; | |||||
| private $destroyed = false; | private $destroyed = false; | ||||
| /* -( Creating a Temporary File )------------------------------------------ */ | /* -( Creating a Temporary File )------------------------------------------ */ | ||||
| /** | /** | ||||
| * Create a new temporary file. | * Create a new temporary file. | ||||
| * | * | ||||
| Show All 29 Lines | /* -( Configuration )------------------------------------------------------ */ | ||||
| * @return this | * @return this | ||||
| * @task config | * @task config | ||||
| */ | */ | ||||
| public function setPreserveFile($preserve) { | public function setPreserveFile($preserve) { | ||||
| $this->preserve = $preserve; | $this->preserve = $preserve; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| /** | |||||
| * Normally, failure to remove the file is considered to be an exception, which | |||||
| * will fail the process with an error exit code. However, Windows loves to hold | |||||
| * file locks, preventing the deletion of files. | |||||
| * | |||||
| * @param bool True to ignore failure removing the file. | |||||
| * @return this | |||||
| * @task config | |||||
| */ | |||||
| public function setIgnoreRemovalFailure($ignore) { | |||||
| $this->ignoreRemovalFailure = $ignore; | |||||
| return $this; | |||||
| } | |||||
| /* -( Internals )---------------------------------------------------------- */ | /* -( Internals )---------------------------------------------------------- */ | ||||
| /** | /** | ||||
| * Get the path to the temporary file. Normally you can just use the object | * Get the path to the temporary file. Normally you can just use the object | ||||
| * in a string context. | * in a string context. | ||||
| * | * | ||||
| * @return string Absolute path to the temporary file. | * @return string Absolute path to the temporary file. | ||||
| Show All 14 Lines | public function __destruct() { | ||||
| if ($this->destroyed) { | if ($this->destroyed) { | ||||
| return; | return; | ||||
| } | } | ||||
| if ($this->preserve) { | if ($this->preserve) { | ||||
| return; | return; | ||||
| } | } | ||||
| Filesystem::remove($this->dir); | try { | ||||
| @Filesystem::remove($this->dir); | |||||
| // NOTE: tempnam() doesn't guarantee it will return a file inside the | // NOTE: tempnam() doesn't guarantee it will return a file inside the | ||||
| // directory you passed to the function, so we make sure to nuke the file | // directory you passed to the function, so we make sure to nuke the file | ||||
| // explicitly. | // explicitly. | ||||
| Filesystem::remove($this->file); | @Filesystem::remove($this->file); | ||||
| } catch (Exception $ex) { | |||||
| if (!$this->ignoreRemovalFailure) { | |||||
| throw $ex; | |||||
| } | |||||
| } | |||||
| $this->file = null; | $this->file = null; | ||||
| $this->dir = null; | $this->dir = null; | ||||
| $this->destroyed = true; | $this->destroyed = true; | ||||
| } | } | ||||
| } | } | ||||