Passed
Push — master ( bb9a04...3de7c0 )
by Wanderson
02:24
created

Filesystem::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Win\Repositories;
4
5
class Filesystem
6
{
7
	const DS = DIRECTORY_SEPARATOR;
8
9
	/**
10
	 * Retorna array com arquivos e diretórios
11
	 * @param string $path
12
	 * @return string[]
13
	 */
14
	public function children($path = '')
15
	{
16
		return array_diff((array) scandir(BASE_PATH . "/$path"), ['..', '.']);
17
	}
18
19
	/**
20
	 * Retorna total de items
21
	 * @param string $path
22
	 * @return int
23
	 */
24
	public function count($path)
25
	{
26
		return count($this->children($path));
27
	}
28
29
	/**
30
	 * Cria o diretório
31
	 * @param string $folderPath
32
	 * @param int $chmod Permissão (base 8)
33
	 */
34
	public function create($folderPath, $chmod = 0755)
35
	{
36
		$path = BASE_PATH . "/$folderPath";
37
		if (!is_dir($path)) {
38
			$mask = umask(0);
39
			mkdir($path, $chmod, true);
40
			umask($mask);
41
		}
42
	}
43
44
	/**
45
	 * Renomeia o arquivo
46
	 * @param string $filePath
47
	 * @param string $newFilePath
48
	 * @return bool
49
	 */
50
	public function rename($filePath, $newFilePath)
51
	{
52
		return rename(BASE_PATH . "/$filePath", BASE_PATH . "/$newFilePath");
53
	}
54
55
	/**
56
	 * Move o arquivo
57
	 * @param string $filePath
58
	 * @param string $newFolder
59
	 */
60
	public function move($filePath, $newFolder)
61
	{
62
		return rename(BASE_PATH . "/$filePath", BASE_PATH . "/$newFolder");
63
	}
64
65
	/**
66
	 * Exclui o arquivo/diretório
67
	 * @param string $path Caminho do arquivo/diretório
68
	 * @return bool
69
	 */
70
	public function delete($path)
71
	{
72
		$fullPath = BASE_PATH . "/$path";
73
		if (is_dir($fullPath) && !is_link($fullPath)) {
74
			foreach ($this->children($path) as $child) {
75
				$this->delete("$path/$child");
76
			}
77
			rmdir($fullPath);
78
		} elseif (is_file($fullPath)) {
79
			unlink($fullPath);
80
		}
81
	}
82
83
	/**
84
	 * Salva o conteúdo no arquivo
85
	 * @param string $content
86
	 * @param string $mode
87
	 * @return bool
88
	 */
89
	public function write($filePath, $content, $mode = 'w')
90
	{
91
		$dir = pathinfo($filePath, PATHINFO_DIRNAME);
92
		$file = pathinfo($filePath, PATHINFO_BASENAME);
93
		$return = false;
94
95
		if ($dir) {
96
			$this->create($dir, 0777);
97
			$fp = fopen(BASE_PATH . "/{$dir}/{$file}", $mode);
98
			if (false !== $fp) {
99
				fwrite($fp, $content);
100
				$return = fclose($fp);
101
			}
102
		}
103
104
		return $return;
105
	}
106
107
	/**
108
	 * Retorna o conteúdo do arquivo
109
	 * @return string|false
110
	 */
111
	public function read($filePath)
112
	{
113
		if (!$this->exists($filePath)) {
114
			return false;
115
		}
116
		return file_get_contents(BASE_PATH . "/$filePath");
117
	}
118
119
	/**
120
	 * Retorna TRUE se o arquivo existe
121
	 * @param string $filePath
122
	 * @return bool
123
	 */
124
	public function exists($filePath)
125
	{
126
		$filePath = BASE_PATH . "/$filePath";
127
128
		return is_file($filePath) or is_dir($filePath);
129
	}
130
}
131