13ac411af246ae2fe19e4d120a8bc2ab3ccfc14c
[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 $template = sys_get_temp_dir()."/$prefix.";
12
13 $omask = umask(077);
14 do {
15 $path = $template.uniqid();
16 $this->handle = fopen($path, "x");
17 } while (!is_resource($this->handle) && $tries++ < 10);
18 umask($omask);
19
20 if (!is_resource($this->handle)) {
21 throw new \Exception("Could not create temporary file");
22 }
23
24 parent::__construct($path);
25 }
26
27 function __destruct() {
28 @unlink($this->getPathname());
29 }
30
31 function closeStream() {
32 fclose($this->handle);
33 }
34
35 function getStream() {
36 return $this->handle;
37 }
38 }