Skip to content

Commit 8d9d951

Browse files
authored
Merge pull request #5 from tarsana/absolute-paths-detection
no need to specify if the path is absolute or relative
2 parents 70ea345 + cbc9c22 commit 8d9d951

File tree

14 files changed

+221
-223
lines changed

14 files changed

+221
-223
lines changed

README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
[![Build Status](https://travis-ci.org/tarsana/io.svg?branch=master)](https://travis-ci.org/tarsana/io)
44
[![Coverage Status](https://coveralls.io/repos/github/tarsana/io/badge.svg?branch=master)](https://coveralls.io/github/tarsana/io?branch=master)
55
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/63923f3f-955d-4058-bbd0-4730639709d4/mini.png)](https://insight.sensiolabs.com/projects/63923f3f-955d-4058-bbd0-4730639709d4)
6+
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://paypal.me/webneat)
67
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/tarsana/io/blob/master/LICENSE)
78

89
Simple classes to handle Input/Output operations.
@@ -29,27 +30,27 @@ $fs = new Tarsana\IO\Filesystem('path/to/fs/root/directory');
2930
Maybe you need to check if a specific path is a file
3031

3132
```php
32-
if ($fs->isFile('relative/path'))
33+
if ($fs->isFile('path'))
3334
```
3435

3536
or a directory
3637

3738
```php
38-
if ($fs->isDir('relative/path'))
39+
if ($fs->isDir('path'))
3940
```
4041

4142
or you just want to know it exists, no matter it's a file or directory
4243

4344
```php
44-
if ($fs->isAny('relative/path'))
45+
if ($fs->isAny('path'))
4546
```
4647

4748
What if you need to check multiple paths at once ?
4849

4950
```php
50-
if ($fs->areFiles(['relative/path1', 'relative/path2', 'relative/path3']))
51-
if ($fs->areDirs(['relative/path1', 'relative/path2', 'relative/path3']))
52-
if ($fs->areAny(['relative/path1', 'relative/path2', 'relative/path3']))
51+
if ($fs->areFiles(['path1', 'path2', 'path3']))
52+
if ($fs->areDirs(['path1', 'path2', 'path3']))
53+
if ($fs->areAny(['path1', 'path2', 'path3']))
5354
```
5455

5556
But what if you want to know the type of a path without having to do multiple checks ?
@@ -103,22 +104,22 @@ $collection->names(); // array of names of the files and directories
103104
Well, to handle a file, you should get it first
104105

105106
```php
106-
$file = $fs->file('relative/path/to/file');
107+
$file = $fs->file('path/to/file');
107108
```
108109

109110
Notice that this will throw an exception if the file is not found. If you want to create it when missing; specify `true` in the second argument
110111

111112
```php
112-
$file = $fs->file('relative/path/to/file', true);
113+
$file = $fs->file('path/to/file', true);
113114
```
114115

115116
You can also get or create multiple files at once
116117

117118
```php
118119
$files = $fs->files([
119-
'relative/path/to/file1',
120-
'relative/path/to/file2',
121-
'relative/path/to/file3'
120+
'path/to/file1',
121+
'path/to/file2',
122+
'path/to/file3'
122123
]); // specify the second argument as true if you want missing files to be created
123124

124125
foreach ($files->asArray() as $file) {
@@ -157,12 +158,12 @@ Notice that all setters return the same instance to enable call chaining.
157158
Just like the file, you can get a directory like that
158159

159160
```php
160-
$dir = $fs->dir('relative/path/to/dir'); // throws exception if the directory not found
161-
$dir = $fs->dir('relative/path/to/dir', true); // creates the directory if not found
161+
$dir = $fs->dir('path/to/dir'); // throws exception if the directory not found
162+
$dir = $fs->dir('path/to/dir', true); // creates the directory if not found
162163
$dirs = $fs->dirs([
163-
'relative/path/to/file1',
164-
'relative/path/to/file2',
165-
'relative/path/to/file3'
164+
'path/to/file1',
165+
'path/to/file2',
166+
'path/to/file3'
166167
]); // a collection containing directories
167168
```
168169

src/Filesystem.php

Lines changed: 41 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,11 @@ public function adapter()
7373
* if there are multiple matches and 'nothing' if no match found.
7474
*
7575
* @param string $pattern
76-
* @param boolean $isAbsolute
7776
* @return string
7877
*/
79-
public function whatIs($pattern, $isAbsolute = false)
78+
public function whatIs($pattern)
8079
{
81-
if (! $isAbsolute) {
80+
if (! $this->adapter->isAbsolute($pattern)) {
8281
$pattern = $this->rootPath . $pattern;
8382
}
8483

@@ -95,13 +94,12 @@ public function whatIs($pattern, $isAbsolute = false)
9594
* Checks if the given path is of the given type.
9695
*
9796
* @param string $path
98-
* @param boolean $isAbsolute
9997
* @param string $type
10098
* @return boolean
10199
*/
102-
protected function is($path, $isAbsolute, $type)
100+
protected function is($path, $type)
103101
{
104-
if (! $isAbsolute) {
102+
if (! $this->adapter->isAbsolute($path)) {
105103
$path = $this->rootPath . $path;
106104
}
107105
switch ($type) {
@@ -126,14 +124,13 @@ protected function is($path, $isAbsolute, $type)
126124
* Checks if the given paths are all of the given type.
127125
*
128126
* @param array $paths
129-
* @param boolean $areAbsolute
130127
* @param string $type
131128
* @return boolean
132129
*/
133-
protected function are($paths, $areAbsolute, $type)
130+
protected function are($paths, $type)
134131
{
135132
foreach ($paths as $path) {
136-
if (! $this->is($path, $areAbsolute, $type)) {
133+
if (! $this->is($path, $type)) {
137134
return false;
138135
}
139136
}
@@ -144,145 +141,133 @@ protected function are($paths, $areAbsolute, $type)
144141
* Checks if the given path is a file.
145142
*
146143
* @param string $path
147-
* @param boolean $isAbsolute
148144
* @return boolean
149145
*/
150-
public function isFile($path, $isAbsolute = false)
146+
public function isFile($path)
151147
{
152-
return $this->is($path, $isAbsolute, 'file');
148+
return $this->is($path, 'file');
153149
}
154150

155151
/**
156152
* Checks if all the given path are files.
157153
*
158154
* @param array $paths
159-
* @param boolean $areAbsolute
160155
* @return boolean
161156
*/
162-
public function areFiles($paths, $areAbsolute = false)
157+
public function areFiles($paths)
163158
{
164-
return $this->are($paths, $areAbsolute, 'file');
159+
return $this->are($paths, 'file');
165160
}
166161

167162
/**
168163
* Checks if the given path is a directory.
169164
*
170165
* @param string $path
171-
* @param boolean $isAbsolute
172166
* @return boolean
173167
*/
174-
public function isDir($path, $isAbsolute = false)
168+
public function isDir($path)
175169
{
176-
return $this->is($path, $isAbsolute, 'dir');
170+
return $this->is($path, 'dir');
177171
}
178172

179173
/**
180174
* Checks if all the given paths are directories.
181175
*
182176
* @param array $paths
183-
* @param boolean $areAbsolute
184177
* @return boolean
185178
*/
186-
public function areDirs($paths, $areAbsolute = false)
179+
public function areDirs($paths)
187180
{
188-
return $this->are($paths, $areAbsolute, 'dir');
181+
return $this->are($paths, 'dir');
189182
}
190183

191184
/**
192185
* Checks if the given path is a file or directory.
193186
*
194187
* @param string $path
195-
* @param boolean $isAbsolute
196188
* @return boolean
197189
*/
198-
public function isAny($path, $isAbsolute = false)
190+
public function isAny($path)
199191
{
200-
return $this->is($path, $isAbsolute, 'any');
192+
return $this->is($path, 'any');
201193
}
202194

203195
/**
204196
* Checks if all the given paths are files or directories.
205197
*
206198
* @param array $paths
207-
* @param boolean $areAbsolute
208199
* @return boolean
209200
*/
210-
public function areAny($paths, $areAbsolute = false)
201+
public function areAny($paths)
211202
{
212-
return $this->are($paths, $areAbsolute, 'any');
203+
return $this->are($paths, 'any');
213204
}
214205

215206
/**
216207
* Checks if the given path is readable.
217208
*
218209
* @param string $path
219-
* @param boolean $isAbsolute
220210
* @return boolean
221211
*/
222-
public function isReadable($path, $isAbsolute = false)
212+
public function isReadable($path)
223213
{
224-
return $this->is($path, $isAbsolute, 'readable');
214+
return $this->is($path, 'readable');
225215
}
226216

227217
/**
228218
* Checks if all the given paths are readable.
229219
*
230220
* @param string $paths
231-
* @param boolean $areAbsolute
232221
* @return boolean
233222
*/
234-
public function areReadable($paths, $areAbsolute = false)
223+
public function areReadable($paths)
235224
{
236-
return $this->are($paths, $areAbsolute, 'readable');
225+
return $this->are($paths, 'readable');
237226
}
238227

239228
/**
240229
* Checks if the given path is writable.
241230
*
242231
* @param string $path
243-
* @param boolean $isAbsolute
244232
* @return boolean
245233
*/
246-
public function isWritable($path, $isAbsolute = false)
234+
public function isWritable($path)
247235
{
248-
return $this->is($path, $isAbsolute, 'writable');
236+
return $this->is($path, 'writable');
249237
}
250238

251239
/**
252240
* Checks if all the given paths are writable.
253-
return $this->are($paths, $areAbsolute, 'readable');
241+
return $this->are($paths, 'readable');
254242
*
255243
* @param string $paths
256-
* @param boolean $areAbsolute
257244
* @return boolean
258245
*/
259-
public function areWritable($paths, $areAbsolute = false)
246+
public function areWritable($paths)
260247
{
261-
return $this->are($paths, $areAbsolute, 'writable');
248+
return $this->are($paths, 'writable');
262249
}
263250

264251
/**
265252
* Checks if the given path is executable.
266253
*
267254
* @param string $path
268-
* @param boolean $isAbsolute
269255
* @return boolean
270256
*/
271-
public function isExecutable($path, $isAbsolute = false)
257+
public function isExecutable($path)
272258
{
273-
return $this->is($path, $isAbsolute, 'executable');
259+
return $this->is($path, 'executable');
274260
}
275261

276262
/**
277263
* Checks if all the given paths are executable.
278264
*
279265
* @param string $paths
280-
* @param boolean $areAbsolute
281266
* @return boolean
282267
*/
283-
public function areExecutable($paths, $areAbsolute = false)
268+
public function areExecutable($paths)
284269
{
285-
return $this->are($paths, $areAbsolute, 'executable');
270+
return $this->are($paths, 'executable');
286271
}
287272

288273
/**
@@ -291,14 +276,13 @@ public function areExecutable($paths, $areAbsolute = false)
291276
*
292277
* @param string $path
293278
* @param boolean $createMissing
294-
* @param boolean $isAbsolute
295279
* @return Tarsana\IO\Filesystem\File
296280
*
297281
* @throws Tarsana\IO\Exceptions\FilesystemException
298282
*/
299-
public function file($path, $createMissing = false, $isAbsolute = false)
283+
public function file($path, $createMissing = false)
300284
{
301-
if (! $isAbsolute) {
285+
if (! $this->adapter->isAbsolute($path)) {
302286
$path = $this->rootPath . $path;
303287
}
304288
if (! $createMissing && ! $this->isFile($path, true)) {
@@ -337,14 +321,13 @@ public function files($paths = false, $createMissing = false)
337321
*
338322
* @param string $path
339323
* @param boolean $createMissing
340-
* @param boolean $isAbsolute
341324
* @return Tarsana\IO\Filesystem\Directory
342325
*
343326
* @throws Tarsana\IO\Exceptions\FilesystemException
344327
*/
345-
public function dir($path, $createMissing = false, $isAbsolute = false)
328+
public function dir($path, $createMissing = false)
346329
{
347-
if (! $isAbsolute) {
330+
if (! $this->adapter->isAbsolute($path)) {
348331
$path = $this->rootPath . $path;
349332
}
350333
if (! $createMissing && ! $this->isDir($path, true)) {
@@ -381,12 +364,11 @@ public function dirs($paths = false, $createMissing = false)
381364
* and returns a collection containing them.
382365
*
383366
* @param string $pattern
384-
* @param boolean $isAbsolute
385367
* @return Tarsana\IO\Filesystem\Collection
386368
*/
387-
public function find($pattern, $isAbsolute = false)
369+
public function find($pattern)
388370
{
389-
if (! $isAbsolute) {
371+
if (! $this->adapter->isAbsolute($pattern)) {
390372
$pattern = $this->rootPath . $pattern;
391373
}
392374
$list = new Collection;
@@ -404,12 +386,11 @@ public function find($pattern, $isAbsolute = false)
404386
* Removes a file or directory recursively.
405387
*
406388
* @param string $path
407-
* @param boolean $isAbsolute
408389
* @return Tarsana\IO\Filesystem
409390
*/
410-
public function remove($path, $isAbsolute = false)
391+
public function remove($path)
411392
{
412-
if (! $isAbsolute) {
393+
if (! $this->adapter->isAbsolute($path)) {
413394
$path = $this->rootPath . $path;
414395
}
415396
if ($this->isFile($path, true)) {
@@ -430,13 +411,12 @@ public function remove($path, $isAbsolute = false)
430411
* Removes an array of files or directories.
431412
*
432413
* @param array $paths
433-
* @param boolean $areAbsolute
434414
* @return Tarsana\IO\Filesystem
435415
*/
436-
public function removeAll($paths, $areAbsolute = false)
416+
public function removeAll($paths)
437417
{
438418
foreach ($paths as $path) {
439-
$this->remove($path, $areAbsolute);
419+
$this->remove($path);
440420
}
441421
return $this;
442422
}

0 commit comments

Comments
 (0)