c890cd92262b1ec163bb6d9dc19d5959343afa1e
[pharext/pharext] / src / pharext / Tempfile.php
1 <?php
2
3 namespace pharext;
4
5 class Tempfile extends \SplFileInfo
6 {
7 private $handle;
8
9 function __construct($prefix) {
10 $tries = 0;
11 /* PharData needs a dot in the filename, sure */
12 $temp = sys_get_temp_dir() . "/";
13
14 $omask = umask(077);
15 do {
16 $path = $temp.uniqid($prefix).".tmp";
17 $this->handle = fopen($path, "x");
18 } while (!is_resource($this->handle) && $tries++ < 10);
19 umask($omask);
20
21 if (!is_resource($this->handle)) {
22 throw new \Exception("Could not create temporary file");
23 }
24
25 parent::__construct($path);
26 }
27
28 function __destruct() {
29 @unlink($this->getPathname());
30 }
31
32 function closeStream() {
33 fclose($this->handle);
34 }
35
36 function getStream() {
37 return $this->handle;
38 }
39 }