-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTemporaryFile.php
More file actions
84 lines (74 loc) · 1.97 KB
/
TemporaryFile.php
File metadata and controls
84 lines (74 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* TemporaryFile
*
* @author Ryuji AMANO <ryuji@ryus.co.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
App::uses('File', 'Utility');
App::uses('TemporaryFolder', 'Files.Utility');
App::uses('Security', 'Utility');
/**
* Class TemporaryFile
*/
class TemporaryFile extends File {
/**
* @var array このクラスで作成されたテンポラリファイルのリスト
*/
private static $__filePaths = [];
/**
* @var boola register_shutdown_functionに登録済みか
*/
private static $__isRegisteredShutdownFunction = false;
/**
* @var TemporaryFolder テンポラリファイルを配置するテンポラリフォルダ
*/
protected $_tmpFolder;
/**
* TemporaryFile constructor.
*
* @param string $folderPath テンポラリフォルダを作成するフォルダパス。指定されなければテンポラリフォルダを作成する
*/
public function __construct($folderPath = null) {
if ($folderPath === null) {
$this->_tmpFolder = new TemporaryFolder();
$folderPath = $this->_tmpFolder->path;
}
$fileName = Security::hash(mt_rand() . microtime(), 'md5');
$path = $folderPath . DS . $fileName;
self::$__filePaths[] = $path;
if (!self::$__isRegisteredShutdownFunction) {
register_shutdown_function([TemporaryFile::CLASS, 'deleteAll']);
self::$__isRegisteredShutdownFunction = true;
}
parent::__construct($path, true);
}
/**
* 削除
*
* @return void
*/
public function delete() {
$key = array_search($this->path, self::$__filePaths);
if ($key !== false) {
unset(self::$__filePaths[$key]);
if ($this->_tmpFolder instanceof TemporaryFolder) {
$this->_tmpFolder->delete();
}
}
parent::delete();
}
/**
* 全テンポラリファイル削除
*
* @return void
*/
public static function deleteAll() {
foreach (self::$__filePaths as $path) {
$file = new File($path);
$file->delete();
}
self::$__filePaths = [];
}
}