projects
/
pharext
/
pharext
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
|
github
raw
|
inline
| side by side
fix name handling
[pharext/pharext]
/
src
/
pharext
/
Tempfile.php
diff --git
a/src/pharext/Tempfile.php
b/src/pharext/Tempfile.php
index c890cd92262b1ec163bb6d9dc19d5959343afa1e..2ec68369adeacde9836240b0639d8121568facb2 100644
(file)
--- a/
src/pharext/Tempfile.php
+++ b/
src/pharext/Tempfile.php
@@
-2,38
+2,58
@@
namespace pharext;
namespace pharext;
+/**
+ * Create a new temporary file
+ */
class Tempfile extends \SplFileInfo
{
class Tempfile extends \SplFileInfo
{
+ /**
+ * @var resource
+ */
private $handle;
private $handle;
-
- function __construct($prefix) {
+
+ /**
+ * @param string $prefix uniqid() prefix
+ * @param string $suffix e.g. file extension
+ * @throws \pharext\Exception
+ */
+ public function __construct($prefix, $suffix = ".tmp") {
$tries = 0;
$tries = 0;
- /* PharData needs a dot in the filename, sure */
- $temp = sys_get_temp_dir() . "/";
-
$omask = umask(077);
do {
$omask = umask(077);
do {
- $path =
$temp.uniqid($prefix).".tmp"
;
+ $path =
new Tempname($prefix, $suffix)
;
$this->handle = fopen($path, "x");
} while (!is_resource($this->handle) && $tries++ < 10);
umask($omask);
if (!is_resource($this->handle)) {
$this->handle = fopen($path, "x");
} while (!is_resource($this->handle) && $tries++ < 10);
umask($omask);
if (!is_resource($this->handle)) {
- throw new
\
Exception("Could not create temporary file");
+ throw new Exception("Could not create temporary file");
}
parent::__construct($path);
}
}
parent::__construct($path);
}
-
- function __destruct() {
- @unlink($this->getPathname());
+
+ /**
+ * Unlink the file
+ */
+ public function __destruct() {
+ if (is_file($this->getPathname())) {
+ @unlink($this->getPathname());
+ }
}
}
-
- function closeStream() {
+
+ /**
+ * Close the stream
+ */
+ public function closeStream() {
fclose($this->handle);
}
fclose($this->handle);
}
- function getStream() {
+ /**
+ * Retrieve the stream resource
+ * @return resource
+ */
+ public function getStream() {
return $this->handle;
}
}
return $this->handle;
}
}