scratch – Blame information for rev 120

Subversion Repositories:
Rev:
Rev Author Line No. Line
120 office 1 <?php
2  
3 /*
4 * This file is part of TemporaryFilesystem.
5 *
6 * (c) Romain Neutron <imprec@gmail.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11  
12 namespace Neutron\TemporaryFilesystem;
13  
14 use Symfony\Component\Filesystem\Filesystem;
15 use Symfony\Component\Filesystem\Exception\IOException as SfIOException;
16  
17 class TemporaryFilesystem implements TemporaryFilesystemInterface
18 {
19 /** @var Filesystem */
20 private $filesystem;
21  
22 public function __construct(Filesystem $filesystem)
23 {
24 $this->filesystem = $filesystem;
25 }
26  
27 /**
28 * {@inheritdoc}
29 */
30 public function createTemporaryDirectory($mode = 0777, $maxTry = 65536, $prefix = null)
31 {
32 $basePath = sys_get_temp_dir();
33  
34 while ($maxTry > 0) {
35 $dir = $basePath . DIRECTORY_SEPARATOR
36 . $prefix . base_convert(mt_rand(0x19A100, 0x39AA3FF), 10, 36);
37  
38 if (false === file_exists($dir)) {
39 try {
40 $this->filesystem->mkdir($dir, $mode);
41 } catch (SfIOException $e) {
42 throw new IOException('Unable to make directory', $e->getCode(), $e);
43 }
44  
45 return $dir;
46 }
47  
48 $maxTry --;
49 }
50  
51 throw new IOException('Unable to generate a temporary directory');
52 }
53  
54 /**
55 * {@inheritdoc}
56 */
57 public function createTemporaryFiles($quantity = 1, $prefix = null, $suffix = null, $extension = null, $maxTry = 65536)
58 {
59 if ($quantity < 1) {
60 throw new \InvalidArgumentException('Invalid temporary files quantity');
61 }
62  
63 $files = array();
64  
65 while ($quantity > 0) {
66 $files[] = $this->createEmptyFile(sys_get_temp_dir(), $prefix, $suffix, $extension, $maxTry);
67 $quantity --;
68 }
69  
70 return $files;
71 }
72  
73 /**
74 * {@inheritdoc}
75 */
76 public function createTemporaryFile($prefix = null, $suffix = null, $extension = null, $maxTry = 65536)
77 {
78 $files = $this->createTemporaryFiles(1, $prefix, $suffix, $extension, $maxTry);
79  
80 return array_pop($files);
81 }
82  
83 /**
84 * {@inheritdoc}
85 */
86 public function createEmptyFile($basePath, $prefix = null, $suffix = null, $extension = null, $maxTry = 65536)
87 {
88 if (false === is_dir($basePath) || false === is_writeable($basePath)) {
89 throw new IOException(sprintf('`%s` should be a writeable directory', $basePath));
90 }
91  
92 if ($suffix === null && $extension === null) {
93 if (false === $file = @tempnam($basePath, $prefix)) {
94 throw new IOException('Unable to generate a temporary filename');
95 }
96  
97 return $file;
98 }
99  
100 while ($maxTry > 0) {
101 $file = $basePath . DIRECTORY_SEPARATOR
102 . $prefix . base_convert(mt_rand(0x19A100, 0x39AA3FF), 10, 36) . $suffix
103 . ( $extension ? '.' . $extension : '');
104  
105 if (false === file_exists($file)) {
106 try {
107 $this->filesystem->touch($file);
108 } catch (SfIOException $e) {
109 throw new IOException('Unable to touch file', $e->getCode(), $e);
110 }
111  
112 return $file;
113 }
114  
115 $maxTry --;
116 }
117  
118 throw new IOException('Unable to generate a temporary filename');
119 }
120  
121 /**
122 * Creates a TemporaryFilesystem
123 *
124 * @return TemporaryFilesystem
125 */
126 public static function create()
127 {
128 return new static(new Filesystem());
129 }
130 }