1 : <?php
2 : /**
3 : * Some utility methods for vfsStream.
4 : *
5 : * @package bovigo_vfs
6 : * @version $Id: vfsStream.php 211 2010-10-06 16:33:05Z google@frankkleine.de $
7 : */
8 : /**
9 : * @ignore
10 : */
11 : require_once dirname(__FILE__) . '/vfsStreamWrapper.php';
12 : /**
13 : * Some utility methods for vfsStream.
14 : *
15 : * @package bovigo_vfs
16 : */
17 : class vfsStream
18 : {
19 : /**
20 : * url scheme
21 : */
22 : const SCHEME = 'vfs';
23 : /**
24 : * owner: root
25 : */
26 : const OWNER_ROOT = 0;
27 : /**
28 : * owner: user 1
29 : */
30 : const OWNER_USER_1 = 1;
31 : /**
32 : * owner: user 2
33 : */
34 : const OWNER_USER_2 = 2;
35 : /**
36 : * group: root
37 : */
38 : const GROUP_ROOT = 0;
39 : /**
40 : * group: user 1
41 : */
42 : const GROUP_USER_1 = 1;
43 : /**
44 : * group: user 2
45 : */
46 : const GROUP_USER_2 = 2;
47 : /**
48 : * initial umask setting
49 : *
50 : * @var int
51 : */
52 : protected static $umask = 0000;
53 :
54 : /**
55 : * prepends the scheme to the given URL
56 : *
57 : * @param string $path
58 : * @return string
59 : */
60 : public static function url($path)
61 : {
62 93 : return self::SCHEME . '://' . str_replace('\\', '/', $path);
63 : }
64 :
65 : /**
66 : * restores the path from the url
67 : *
68 : * @param string $url
69 : * @return string
70 : */
71 : public static function path($url)
72 : {
73 : // remove line feeds and trailing whitespaces
74 93 : $path = trim($url, " \t\r\n\0\x0B/");
75 93 : $path = substr($path, strlen(self::SCHEME . '://'));
76 93 : $path = str_replace('\\', '/', $path);
77 : // replace double slashes with single slashes
78 93 : $path = str_replace('//', '/', $path);
79 93 : return $path;
80 : }
81 :
82 : /**
83 : * sets new umask setting and returns previous umask setting
84 : *
85 : * If no value is given only the current umask setting is returned.
86 : *
87 : * @param int $umask optional
88 : * @return int
89 : * @since 0.8.0
90 : */
91 : public static function umask($umask = null)
92 : {
93 131 : $oldUmask = self::$umask;
94 131 : if (null !== $umask) {
95 15 : self::$umask = $umask;
96 15 : }
97 :
98 131 : return $oldUmask;
99 : }
100 :
101 : /**
102 : * helper method for setting up vfsStream in unit tests
103 : *
104 : * Instead of
105 : * vfsStreamWrapper::register();
106 : * vfsStreamWrapper::setRoot(vfsStream::newDirectory('root'));
107 : * you can simply do
108 : * vfsStream::setup()
109 : * which yields the same result. Additionally, the method returns the
110 : * freshly created root directory which you can use to make further
111 : * adjustments to it.
112 : *
113 : * @param string $rootDirName optional name of root directory
114 : * @param int $permissions optional file permissions of root directory
115 : * @return vfsStreamDirectory
116 : * @since 0.7.0
117 : */
118 : public static function setup($rootDirName = 'root', $permissions = null)
119 : {
120 12 : vfsStreamWrapper::register();
121 12 : $root = self::newDirectory($rootDirName, $permissions);
122 12 : vfsStreamWrapper::setRoot($root);
123 12 : return $root;
124 : }
125 :
126 : /**
127 : * returns a new file with given name
128 : *
129 : * @param string $name
130 : * @param int $permissions optional
131 : * @return vfsStreamFile
132 : */
133 : public static function newFile($name, $permissions = null)
134 : {
135 85 : return new vfsStreamFile($name, $permissions);
136 : }
137 :
138 : /**
139 : * returns a new directory with given name
140 : *
141 : * If the name contains slashes, a new directory structure will be created.
142 : * The returned directory will always be the parent directory of this
143 : * directory structure.
144 : *
145 : * @param string $name
146 : * @param int $permissions optional
147 : * @return vfsStreamDirectory
148 : */
149 : public static function newDirectory($name, $permissions = null)
150 : {
151 30 : if ('/' === $name{0}) {
152 5 : $name = substr($name, 1);
153 5 : }
154 :
155 30 : $firstSlash = strpos($name, '/');
156 30 : if (false === $firstSlash) {
157 30 : return new vfsStreamDirectory($name, $permissions);
158 : }
159 :
160 9 : $ownName = substr($name, 0, $firstSlash);
161 9 : $subDirs = substr($name, $firstSlash + 1);
162 9 : $directory = new vfsStreamDirectory($ownName, $permissions);
163 9 : self::newDirectory($subDirs, $permissions)->at($directory);
164 9 : return $directory;
165 : }
166 :
167 : /**
168 : * returns current user
169 : *
170 : * If the system does not support posix_getuid() the current user will be root (0).
171 : *
172 : * @return int
173 : */
174 : public static function getCurrentUser()
175 : {
176 155 : return function_exists('posix_getuid') ? posix_getuid() : self::OWNER_ROOT;
177 : }
178 :
179 : /**
180 : * returns current group
181 : *
182 : * If the system does not support posix_getgid() the current group will be root (0).
183 : *
184 : * @return int
185 : */
186 : public static function getCurrentGroup()
187 : {
188 155 : return function_exists('posix_getgid') ? posix_getgid() : self::GROUP_ROOT;
189 : }
190 : }
|