Skip to content
This repository was archived by the owner on Aug 14, 2022. It is now read-only.

Commit 5d92fbf

Browse files
committed
Updated to 1.1.3 version
1 parent b147de4 commit 5d92fbf

File tree

5 files changed

+162
-4
lines changed

5 files changed

+162
-4
lines changed

Diff for: CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# CHANGELOG
22

3+
## 1.1.3 - 2017-09-03
4+
5+
* Added `Josantonius\File\File::exists()` method.
6+
* Added `Josantonius\File\File::delete()` method.
7+
* Added `Josantonius\File\File::createDir()` method.
8+
* Added `Josantonius\File\File::deleteEmptyDir()` method.
9+
* Added `Josantonius\File\File::deleteDirRecursively()` method.
10+
* Added `Josantonius\File\File::getFilesFromDir()` method.
11+
312
## 1.1.2 - 2017-07-16
413
* Deleted `Josantonius\File\Exception\FileException` class.
514
* Deleted `Josantonius\File\Exception\Exceptions` abstract class.

Diff for: README-ES.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Biblioteca PHP para manejo de archivos.
1414
- [Métodos disponibles](#métodos-disponibles)
1515
- [Uso](#uso)
1616
- [Tests](#tests)
17+
- [TODO](#-todo)
1718
- [Contribuir](#contribuir)
1819
- [Repositorio](#repositorio)
1920
- [Licencia](#licencia)
@@ -65,7 +66,12 @@ use Josantonius\File\File;
6566
Métodos disponibles en esta biblioteca:
6667

6768
```php
68-
File::searchString();
69+
File::exists();
70+
File::delete();
71+
File::createDir();
72+
File::deleteEmptyDir();
73+
File::deleteDirRecursively();
74+
File::getFilesFromDir();
6975
```
7076
### Uso
7177

@@ -102,6 +108,11 @@ Métodos de prueba disponibles en esta biblioteca:
102108
FileTest::testSearchString();
103109
```
104110

111+
## ☑ TODO
112+
113+
- [ ] Completar tests
114+
- [ ] Completar archivos README
115+
105116
### Contribuir
106117
1. Comprobar si hay incidencias abiertas o abrir una nueva para iniciar una discusión en torno a un fallo o función.
107118
1. Bifurca la rama del repositorio en GitHub para iniciar la operación de ajuste.

Diff for: README.md

+13-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ PHP library for file management.
1414
- [Available Methods](#available-methods)
1515
- [Usage](#usage)
1616
- [Tests](#tests)
17+
- [TODO](#-todo)
1718
- [Contribute](#contribute)
1819
- [Repository](#repository)
1920
- [License](#license)
@@ -65,7 +66,12 @@ use Josantonius\File\File;
6566
Available methods in this library:
6667

6768
```php
68-
File::searchString();
69+
File::exists();
70+
File::delete();
71+
File::createDir();
72+
File::deleteEmptyDir();
73+
File::deleteDirRecursively();
74+
File::getFilesFromDir();
6975
```
7076
### Usage
7177

@@ -97,6 +103,12 @@ $loader->addPsr4('Josantonius\\File\\Tests\\', __DIR__ . '/vendor/josantonius/fi
97103
use Josantonius\File\Tests\FileTest;
98104

99105
```
106+
107+
## ☑ TODO
108+
109+
- [ ] Expand tests
110+
- [ ] Expand README files
111+
100112
Available test methods in this library:
101113

102114
```php

Diff for: composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "josantonius/file",
3-
"version": "1.1.2",
3+
"version": "1.1.3",
44
"type": "library",
55
"description": "PHP library for file management.",
66
"keywords": [

Diff for: src/File.php

+127-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,132 @@
1818
*/
1919
class File {
2020

21+
/**
22+
* Directory separator.
23+
*
24+
* @since 1.1.3
25+
*
26+
* @var string
27+
*/
28+
const DS = DIRECTORY_SEPARATOR;
29+
30+
/**
31+
* Check if a file exists in a path or url.
32+
*
33+
* @since 1.1.3
34+
*
35+
* @param string $file → path or file url
36+
*
37+
* @return boolean
38+
*/
39+
public static function exists($file) {
40+
41+
if (filter_var($file, FILTER_VALIDATE_URL)) {
42+
43+
$stream = stream_context_create(['http' => ['method' => 'HEAD']]);
44+
45+
$content = fopen($file, 'r', null, $stream);
46+
47+
$headers = stream_get_meta_data($content);
48+
49+
fclose($fp);
50+
51+
$status = substr($headers['wrapper_data'][0], 9, 3);
52+
53+
return ($status >= 200 && $status < 400);
54+
}
55+
56+
return (file_exists($file) && is_file($file));
57+
}
58+
59+
/**
60+
* Delete file.
61+
*
62+
* @since 1.1.3
63+
*
64+
* @param string $file → file path
65+
*
66+
* @return boolean
67+
*/
68+
public static function delete($file) {
69+
70+
return (self::exists($file) && unlink($file));
71+
}
72+
73+
/**
74+
* Create directory.
75+
*
76+
* @since 1.1.3
77+
*
78+
* @param string $path → path where to create directory
79+
*
80+
* @return boolean
81+
*/
82+
public static function createDir($path) {
83+
84+
return (!is_dir($path) && mkdir($path, 0777, true));
85+
}
86+
87+
/**
88+
* Delete empty directory.
89+
*
90+
* @since 1.1.3
91+
*
92+
* @param string $path → path to delete
93+
*
94+
* @return boolean
95+
*/
96+
public static function deleteEmptyDir($path) {
97+
98+
return (is_dir($path) && rmdir($path));
99+
}
100+
101+
/**
102+
* Delete directory recursively.
103+
*
104+
* @since 1.1.3
105+
*
106+
* @param string $_path → path to delete
107+
*
108+
* @return boolean
109+
*/
110+
public static function deleteDirRecursively($_path) {
111+
112+
if (!$path = self::getFilesFromDir($_path)) { return false; }
113+
114+
foreach($path as $file) {
115+
116+
if ($file->isFile()) {
117+
118+
if (!self::delete($file->getRealPath())) { return false; }
119+
120+
} else if (!$file->isDot() && $file->isDir()) {
121+
122+
self::deleteDirRecursively($file->getRealPath());
123+
124+
self::deleteEmptyDir($file->getRealPath());
125+
}
126+
}
127+
128+
return self::deleteEmptyDir($_path);
129+
}
130+
131+
/**
132+
* Get files from directory.
133+
*
134+
* @since 1.1.3
135+
*
136+
* @param string $path → path where get files
137+
*
138+
* @return object|false →
139+
*/
140+
public static function getFilesFromDir($path) {
141+
142+
if (!is_dir($path)) { return false; }
143+
144+
return new \DirectoryIterator(rtrim($path, self::DS) . self::DS);
145+
}
146+
21147
/**
22148
* Search for part of a string in a file.
23149
*
@@ -26,7 +152,7 @@ class File {
26152
* @param string $search → word or term to search
27153
* @param string $pathfile → full path to the file
28154
*
29-
* @return bool
155+
* @return boolean
30156
*/
31157
public static function searchString($search, $pathfile) {
32158

0 commit comments

Comments
 (0)