1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 10.07.16 at 15:40 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonphp\resource; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* File system management class. |
10
|
|
|
* @package samsonphp\resource |
11
|
|
|
*/ |
12
|
|
|
class FileManager implements FileManagerInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Wrapper for reading file. |
16
|
|
|
* |
17
|
|
|
* @param string $file Full path to file |
18
|
|
|
* |
19
|
|
|
* @return string Asset content |
20
|
|
|
*/ |
21
|
1 |
|
public function read($file) |
22
|
|
|
{ |
23
|
1 |
|
return file_get_contents($file); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Wrapper for writing file. |
28
|
|
|
* |
29
|
|
|
* @param string $asset Full path to file |
30
|
|
|
* |
31
|
|
|
* @param string $content Asset content |
32
|
|
|
*/ |
33
|
1 |
|
public function write($asset, $content) |
34
|
|
|
{ |
35
|
1 |
|
$path = dirname($asset); |
36
|
1 |
|
if (!file_exists($path)) { |
37
|
1 |
|
$this->mkdir($path); |
38
|
1 |
|
} |
39
|
|
|
|
40
|
1 |
|
file_put_contents($asset, $content); |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create folder. |
45
|
|
|
* |
46
|
|
|
* @param string $path Full path to asset |
47
|
|
|
* |
48
|
|
|
* @throws \Exception |
49
|
|
|
*/ |
50
|
1 |
|
public function mkdir($path) |
51
|
|
|
{ |
52
|
|
|
// Create cache path |
53
|
1 |
|
if (!$this->exists($path)) { |
54
|
|
|
try { |
55
|
1 |
|
mkdir($path, 0777, true); |
56
|
1 |
|
} catch (\Exception $e) { |
57
|
|
|
throw new \Exception($e->getMessage() . ' ' . $path); |
58
|
|
|
} |
59
|
1 |
|
} |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* If path(file or folder) exists. |
64
|
|
|
* |
65
|
|
|
* @param string $path Path for validating existence |
66
|
|
|
* |
67
|
|
|
* @return bool True if path exists |
68
|
|
|
*/ |
69
|
1 |
|
public function exists($path) |
70
|
|
|
{ |
71
|
1 |
|
return file_exists($path); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Wrapper for touching file. |
76
|
|
|
* |
77
|
|
|
* @param string $asset Full path to file |
78
|
|
|
* @param int $timestamp Timestamp |
79
|
|
|
*/ |
80
|
|
|
public function touch($asset, $timestamp) |
81
|
|
|
{ |
82
|
|
|
// Sync cached file with source file |
83
|
|
|
touch($asset, $timestamp); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Remove path/file recursively. |
88
|
|
|
* |
89
|
|
|
* @param string $path Path to be removed |
90
|
|
|
*/ |
91
|
|
|
public function remove($path) |
92
|
|
|
{ |
93
|
|
|
if (is_dir($path)) { |
94
|
|
|
// Get folder content |
95
|
|
|
foreach (glob($path . '*', GLOB_MARK) as $file) { |
96
|
|
|
// Recursion |
97
|
|
|
$this->remove($file); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Remove folder after all internal sub-folders are clear |
101
|
|
|
rmdir($path); |
102
|
|
|
} elseif (is_file($path)) { |
103
|
|
|
unlink($path); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get last file modification timestamp. |
109
|
|
|
* |
110
|
|
|
* @param string $file Path to file |
111
|
|
|
* |
112
|
|
|
* @return int File modification timestamp |
113
|
|
|
*/ |
114
|
|
|
public function lastModified($file) |
115
|
|
|
{ |
116
|
|
|
return filemtime($file); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Recursively scan collection of paths to find files with passed |
121
|
|
|
* extensions. Method is based on linux find command so this method |
122
|
|
|
* can face difficulties on other OS. |
123
|
|
|
* |
124
|
|
|
* |
125
|
|
|
* @param array $paths Paths for files scanning |
126
|
|
|
* @param array $extensions File extension filter |
127
|
|
|
* @param array $excludeFolders Path patterns for excluding |
128
|
|
|
* |
129
|
|
|
* @return array Found files |
130
|
|
|
*/ |
131
|
1 |
|
public function scan(array $paths, array $extensions, array $excludeFolders = []) |
132
|
|
|
{ |
133
|
|
|
// Generate LINUX command to gather resources as this is 20 times faster |
134
|
1 |
|
$files = []; |
135
|
|
|
|
136
|
|
|
// Generate exclusion conditions |
137
|
|
|
$exclude = implode(' ', array_map(function ($value) { |
138
|
1 |
|
return '-not -path ' . $value . ' '; |
139
|
1 |
|
}, $excludeFolders)); |
140
|
|
|
|
141
|
|
|
// Generate filters |
142
|
1 |
|
$filters = implode('-o ', array_map(function ($value) use ($exclude) { |
143
|
1 |
|
return '-type f -name "*.' . $value . '" ' . $exclude; |
144
|
1 |
|
}, $extensions)); |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Firstly was implemented as single "find" command call with multiple paths but |
148
|
|
|
* Ubuntu sort files not alphabetically which forced to use piping with sort command |
149
|
|
|
* and calling each part separately |
150
|
|
|
* TODO: Probably there is a command which will achieve this with one call |
151
|
|
|
*/ |
152
|
1 |
|
foreach ($paths as $path) { |
153
|
|
|
// Scan path excluding folder patterns |
154
|
1 |
|
$tempFiles = []; |
155
|
1 |
|
exec('find ' . $path . ' ' . $filters . ' | sort ', $tempFiles); |
156
|
|
|
|
157
|
1 |
|
$files = array_merge($files, $tempFiles); |
158
|
1 |
|
} |
159
|
|
|
|
160
|
|
|
// TODO: Why some paths have double slashes? Investigate speed of realpath, maybe // changing if quicker |
161
|
1 |
|
return array_map('realpath', $files); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|