missing file
authorMichael Wallner <mike@php.net>
Mon, 9 Mar 2015 14:48:29 +0000 (15:48 +0100)
committerMichael Wallner <mike@php.net>
Mon, 9 Mar 2015 14:48:29 +0000 (15:48 +0100)
src/pharext/Tempfile.php [new file with mode: 0644]

diff --git a/src/pharext/Tempfile.php b/src/pharext/Tempfile.php
new file mode 100644 (file)
index 0000000..13ac411
--- /dev/null
@@ -0,0 +1,38 @@
+<?php 
+
+namespace pharext;
+
+class Tempfile extends \SplFileInfo
+{
+       private $handle;
+       
+       function __construct($prefix) {
+               $tries = 0;
+               $template = sys_get_temp_dir()."/$prefix.";
+               
+               $omask = umask(077);
+               do {
+                       $path = $template.uniqid();
+                       $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");
+               }
+               
+               parent::__construct($path);
+       }
+       
+       function __destruct() {
+               @unlink($this->getPathname());
+       }
+
+       function closeStream() {
+               fclose($this->handle);
+       }
+
+       function getStream() {
+               return $this->handle;
+       }
+}