PluginProbe ʕ •ᴥ•ʔ
10Web Booster – Website speed optimization, Cache & Page Speed optimizer / trunk
10Web Booster – Website speed optimization, Cache & Page Speed optimizer vtrunk
2.33.6 2.33.5 2.33.0 2.30.5 2.30.7 2.30.9 2.31.10 2.31.8 2.32.11 2.32.21 2.32.3 2.32.4 2.32.7 2.6.31 2.6.40 2.6.42 2.6.7 2.7.37 2.7.44 2.7.47 2.8.18 2.8.19 2.8.32 2.8.34 2.8.35 2.9.23 2.9.24 2.9.25 2.9.27 v2.27.4 trunk 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.17 2.0.18 2.0.21 2.0.22 2.0.25 2.0.26 2.0.27 2.0.3 2.0.7 2.0.9 2.10.46 2.10.65 2.10.66 2.10.68 2.11.41 2.11.42 2.11.43 2.12.15 2.12.21 2.12.22 2.12.23 2.12.26 2.13.37 2.13.40 2.13.41 2.13.42 2.13.44 2.13.45 2.13.47 2.14.49 2.14.50 2.15.18 2.17.21 2.17.23 2.18.17 2.19.44 2.19.45 2.19.46 2.19.49 2.2.12 2.2.15 2.2.16 2.2.18 2.2.8 2.20.31 2.20.32 2.20.33 2.21.11 2.21.12 2.21.16 2.21.25 2.22.32 2.23.13 2.23.15 2.23.16 2.23.18 2.24.12 2.24.14 2.24.18 2.25.14 2.26.6 2.28.10 2.28.13 2.28.14 2.28.7 2.29.1 2.29.2 2.29.3 2.3.0 2.3.1 2.3.2 2.3.3 2.30.18
tenweb-speed-optimizer / vendor / makasim / temp-file / src / Makasim / File / TempFile.php
tenweb-speed-optimizer / vendor / makasim / temp-file / src / Makasim / File Last commit date
TempFile.php 4 years ago
TempFile.php
98 lines
1 <?php
2 namespace Makasim\File;
3
4 /**
5 * @author Kotlyar Maksim <kotlyar.maksim@gmail.com>
6 * @since 8/15/12
7 */
8 class TempFile extends \SplFileInfo
9 {
10 /**
11 * @var array of string paths array(path => path)
12 */
13 protected static $tempFiles = array();
14
15 /**
16 * {@inheritdoc}
17 */
18 public function __construct($fileName)
19 {
20 parent::__construct($fileName);
21
22 self::registerRemoveTempFilesHandler();
23 self::$tempFiles[$fileName] = $fileName;
24 }
25
26 /**
27 * Persist file so that it would not be removed at the end of the script execution.
28 *
29 * @return \SplFileInfo
30 */
31 public function persist()
32 {
33 unset(self::$tempFiles[(string) $this]);
34
35 return $this->getFileInfo();
36 }
37
38 /**
39 * Creates a temp file with unique filename.
40 *
41 * @param string $prefix
42 *
43 * @return TempFile
44 */
45 public static function generate($prefix = 'php-tmp-file', $suffix = '')
46 {
47 $filename = tempnam(sys_get_temp_dir(), $prefix);
48
49 if ($suffix) {
50 $i = 0;
51 do {
52 $newFilename = $filename . $i++ . $suffix;
53 } while (file_exists($newFilename));
54
55 rename($filename, $newFilename);
56 $filename = $newFilename;
57 }
58
59 return new static($filename);
60 }
61
62 /**
63 * Creates a temp file from an exist file keeping it safe.
64 *
65 * @param mixed $file
66 * @param string $prefix
67 *
68 * @return TempFile
69 */
70 public static function from($file, $prefix = 'php-tmp-file')
71 {
72 $tmpFile = static::generate($prefix);
73
74 copy($file, $tmpFile);
75
76 return $tmpFile;
77 }
78
79 private static function registerRemoveTempFilesHandler()
80 {
81 static $registered = false;
82 if ($registered) {
83 return;
84 }
85
86 $tempFiles = &self::$tempFiles;
87
88 register_shutdown_function(function() use (&$tempFiles) {
89 foreach ($tempFiles as $tempFile) {
90 if (file_exists($tempFile)) {
91 @unlink($tempFile);
92 }
93 }
94 });
95
96 $registered = true;
97 }
98 }