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