Skip to content

Commit 7cca118

Browse files
committed
Can choose the path to the temp dir
1 parent 8a8d9ab commit 7cca118

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/FireFS/FireFS.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,14 @@ class FireFS
8585
*/
8686
protected $workingDir = '';
8787

88+
/**
89+
* The path to the directory used to store
90+
* temporary files.
91+
*
92+
* @var string
93+
*/
94+
protected $tempDir = '';
95+
8896
/**
8997
* Aliases
9098
*
@@ -109,7 +117,8 @@ public function __construct(string $rootPath = "./")
109117
else
110118
throw new \RuntimeException("The directory \"{$rootPath}\" can't be located.");
111119

112-
$this->setWorkingDir('/');
120+
$this->setWorkingDir('./');
121+
$this->setTempDir('./tmp');
113122
}
114123

115124
/**
@@ -124,6 +133,18 @@ public function setRootPath(string $rootPath)
124133
$this->rootPath = $this->cleanPath($rootPath);
125134
}
126135

136+
/**
137+
* Changes the path to the directory of
138+
* temporary files.
139+
*
140+
* @param string $path The path to the directory starting
141+
* to the root path.
142+
*/
143+
public function setTempDir(string $path)
144+
{
145+
$this->tempDir = $this->cleanPath($path);
146+
}
147+
127148
/**
128149
* Clean the path for bad directory name
129150
*
@@ -189,7 +210,8 @@ public function explodePath(string $path): array
189210
/**
190211
* Set the current working directory
191212
*
192-
* @param string $workingDir The path to the directory
213+
* @param string $workingDir The path to the directory starting
214+
* to the root path.
193215
*
194216
* @return void
195217
*/
@@ -870,20 +892,17 @@ public function toFileSystemPath(string $internalPath): string
870892
*/
871893
public function tmpfile(): string
872894
{
873-
$tmpDir = $this->toInternalPath($this->rootPath . "/tmp");
895+
$tmpDir = $this->toInternalPath($this->tempDir);
874896

875897
if (!$this->isDir($tmpDir)) {
876898
$this->mkdir($tmpDir, true);
877899
}
878900

879-
$tmpFile = tempnam($tmpDir, "tmp_");
901+
$tmpFile = tempnam($tmpDir, "tmp");
880902

881903
$fileManager = $this;
882-
$currentDir = getcwd();
883-
884-
register_shutdown_function(function () use ($tmpFile, $fileManager, $currentDir) {
885-
chdir($currentDir);
886904

905+
register_shutdown_function(function () use ($tmpFile, $fileManager) {
887906
if ($fileManager->exists($tmpFile)) {
888907
$fileManager->delete($tmpFile, true);
889908
}

0 commit comments

Comments
 (0)