17
17
*
18
18
* @author Włodzimierz Gajda <gajdaw@gajdaw.pl>
19
19
*/
20
- class FtpSplFileInfo extends \ SplFileInfo
20
+ class FtpSplFileInfo extends SplFileInfo
21
21
{
22
22
23
- const TYPE_UNKNOWN = 1 ;
23
+ const TYPE_UNKNOWN = 1 ;
24
24
const TYPE_DIRECTORY = 2 ;
25
- const TYPE_FILE = 3 ;
26
- const TYPE_LINK = 4 ;
25
+ const TYPE_FILE = 3 ;
26
+ const TYPE_LINK = 4 ;
27
27
28
- private $ type = self ::TYPE_DIRECTORY ;
28
+ private $ type = self ::TYPE_FILE ;
29
+ private $ basePath = '' ;
29
30
30
- /**
31
- * The name of a directory is always .:
32
- *
33
- * Directory: /lorem/ipsum/dolor
34
- * Path: /lorem/ipsum/dolor
35
- * Filename: .
36
- *
37
- *
38
- * File: /lorem/ipsum/dolor/sit.txt
39
- * Path: /lorem/ipsum/dolor
40
- * Filename: sit.txt
41
- *
42
- */
43
- private $ path = '/ ' ;
44
- private $ filename = '. ' ;
45
-
46
- /**
47
- * Constructor.
48
- *
49
- * Examples:
50
- *
51
- * $e = new FtpSplFileInfo('/this/is/a/dir');
52
- * $e = new FtpSplFileInfo('/and/this/is/a/file.txt', self::TYPE_FILE);
53
- *
54
- * @param type $item The name of the file or directory
55
- * @param type $type The type of the first parameter: directory or file
56
- */
57
- public function __construct ($ item = '/ ' , $ type = self ::TYPE_DIRECTORY )
31
+ public function __construct ($ file , $ relativePath , $ relativePathname , $ basePath )
58
32
{
59
- $ this ->setType ($ type );
60
-
61
- if ($ type === self ::TYPE_DIRECTORY ) {
62
- $ this ->filename = '. ' ;
63
- $ this ->path = $ item ;
64
- } else if ($ type === self ::TYPE_FILE ) {
65
- $ tmp = self ::parseFile ($ item );
66
- $ this ->filename = $ tmp ['filename ' ];
67
- $ this ->path = $ tmp ['path ' ];
68
- }
69
- parent ::__construct ($ this ->getFullFilename ());
33
+ parent ::__construct ($ file , $ relativePath , $ relativePathname );
34
+ $ this ->setBasePath ($ basePath );
70
35
}
71
36
72
- /**
73
- * Checks if the current object is a directory.
74
- *
75
- * @return Boolean true if the current object represents a directory
76
- *
77
- */
78
- public function isDir ()
37
+ public function setBasePath ($ basePath )
79
38
{
80
- return self :: TYPE_DIRECTORY === $ this ->type ;
39
+ $ this ->basePath = $ basePath ;
81
40
}
82
41
83
- /**
84
- * Checks if the current object is a file.
85
- *
86
- * @return Boolean true if the current object represents a file
87
- *
88
- */
89
- public function isFile ()
42
+ public function getBasePath ()
90
43
{
91
- return self :: TYPE_FILE === $ this ->type ;
44
+ return $ this ->basePath ;
92
45
}
93
46
94
- /**
95
- * Returns the full name of the current object.
96
- *
97
- * Example outputs:
98
- * /
99
- * /pub
100
- * /pub/some/dir
101
- * /some/file.txt
102
- * /other.yml
103
- *
104
- * @return string full absolute name of a current object
105
- *
106
- */
107
- public function getFullFilename ()
47
+ public function getRealpath ()
108
48
{
109
- if ($ this ->isDir ()) {
110
- return $ this ->getPath ();
111
- }
112
- if ($ this ->path === '/ ' && $ this ->filename === '. ' ) {
113
- return '/ ' ;
114
- }
115
- if ($ this ->path === '/ ' ) {
116
- return '/ ' . $ this ->filename ;
117
- }
118
-
119
- return $ this ->path . '/ ' . $ this ->filename ;
49
+ return $ this ->getPathname ();
120
50
}
121
51
122
- /**
123
- * Current object represents a directory.
124
- * Returns the absolute path of the $item from the current dir.
125
- *
126
- * Example:
127
- *
128
- * Current dir:
129
- * path: /some/dir
130
- * name: .
131
- *
132
- * Item:
133
- * info.xml
134
- *
135
- * Result:
136
- * /some/dir/info.xml
137
- *
138
- * @return string
139
- *
140
- */
141
52
public function getItemname ($ item )
142
53
{
143
- if ($ this ->path === '/ ' ) {
144
- return '/ ' . $ item ;
54
+ if (0 === strpos ($ item , '/ ' )) {
55
+ throw new \InvalidArgumentException (sprintf ('Item can not be absolute path: "%s". ' , $ item ));
56
+ }
57
+ $ result = $ this ->getRealpath ();
58
+ $ len = strlen ($ result );
59
+ if ($ result [$ len - 1 ] != '/ ' ) {
60
+ $ result .= '/ ' ;
145
61
}
146
62
147
- return $ this ->path . '/ ' . $ item ;
148
- }
149
-
150
- /**
151
- * Returns filename.
152
- *
153
- * @return string
154
- *
155
- */
156
- public function getFilename ()
157
- {
158
- return $ this ->filename ;
63
+ return $ result . $ item ;
159
64
}
160
65
161
66
/**
@@ -179,13 +84,25 @@ public function getType()
179
84
}
180
85
181
86
/**
182
- * Returns path.
87
+ * Checks if the current object is a directory.
88
+ *
89
+ * @return Boolean true if the current object represents a directory
183
90
*
184
- * @return string the path of the current object
185
91
*/
186
- public function getPath ()
92
+ public function isDir ()
93
+ {
94
+ return self ::TYPE_DIRECTORY === $ this ->type ;
95
+ }
96
+
97
+ /**
98
+ * Checks if the current object is a file.
99
+ *
100
+ * @return Boolean true if the current object represents a file
101
+ *
102
+ */
103
+ public function isFile ()
187
104
{
188
- return $ this ->path ;
105
+ return self :: TYPE_FILE === $ this ->type ;
189
106
}
190
107
191
108
/**
@@ -200,7 +117,6 @@ public static function parseRawListItem($item)
200
117
{
201
118
if ($ item === '' ) {
202
119
return self ::TYPE_UNKNOWN ;
203
- //throw new \InvalidArgumentException('$item is null!');
204
120
}
205
121
switch ($ item [0 ]) {
206
122
@@ -218,53 +134,4 @@ public static function parseRawListItem($item)
218
134
}
219
135
}
220
136
221
- /**
222
- * Parse the name of the file into pair: path/name.
223
- * The name must be absolute (i.e. it must start with /).
224
- *
225
- * Example:
226
- *
227
- * /abc/defgh/ij.txt
228
- *
229
- * path: /abc/defgh
230
- * filename: ij.txt
231
- *
232
- * @param type $file the name to parse
233
- *
234
- * @return array
235
- *
236
- * @throws \InvalidArgumentException
237
- */
238
- public static function parseFile ($ file = '/ ' )
239
- {
240
- if (0 !== strpos ($ file , '/ ' )) {
241
- throw new \InvalidArgumentException (sprintf ('File must start with /. It doesnt: "%s" ' , $ file ));
242
- }
243
-
244
- if (strlen ($ file ) < 2 ) {
245
- throw new \InvalidArgumentException (sprintf ('The name must contain at least two characters. It doesnt: "%s" ' , $ file ));
246
- }
247
-
248
- $ found = strrpos ($ file , '/ ' );
249
- $ tmpPath = substr ($ file , 0 , $ found );
250
- $ tmpName = substr ($ file , $ found + 1 );
251
- if ($ tmpPath === '' ) {
252
- $ tmpPath = '/ ' ;
253
- }
254
- return array (
255
- 'filename ' => $ tmpName ,
256
- 'path ' => $ tmpPath
257
- );
258
- }
259
-
260
- /**
261
- * Returns full filename
262
- *
263
- * @return string full filename of the current object
264
- */
265
- public function __toString ()
266
- {
267
- return $ this ->getFullFilename ();
268
- }
269
-
270
137
}
0 commit comments