X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=src%2Fpharext%2FTempfile.php;h=e7205519ea784869a58c821d132609091a4720c3;hb=a3858b4c4a9efd4d360e20a268e073e50b034e8a;hp=13ac411af246ae2fe19e4d120a8bc2ab3ccfc14c;hpb=02e746254e0800bff88c45d41463bdb0b3aa69db;p=pharext%2Fpharext diff --git a/src/pharext/Tempfile.php b/src/pharext/Tempfile.php index 13ac411..e720551 100644 --- a/src/pharext/Tempfile.php +++ b/src/pharext/Tempfile.php @@ -2,37 +2,56 @@ namespace pharext; +/** + * Create a new temporary file + */ class Tempfile extends \SplFileInfo { + /** + * @var resource + */ 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; - $template = sys_get_temp_dir()."/$prefix."; - $omask = umask(077); do { - $path = $template.uniqid(); + $path = new Tempname($prefix, $suffix); $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); } - - function __destruct() { + + /** + * Unlink the file + */ + public function __destruct() { @unlink($this->getPathname()); } - function closeStream() { + /** + * Close the stream + */ + public function closeStream() { fclose($this->handle); } - function getStream() { + /** + * Retrieve the stream resource + * @return resource + */ + public function getStream() { return $this->handle; } }