1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakeCMS Core |
4
|
|
|
* |
5
|
|
|
* This file is part of the of the simple cms based on CakePHP 3. |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* @package Core |
10
|
|
|
* @license MIT |
11
|
|
|
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php |
12
|
|
|
* @link https://github.com/CakeCMS/Core". |
13
|
|
|
* @author Sergey Kalistratov <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Core\Path; |
17
|
|
|
|
18
|
|
|
use Cake\Core\Configure; |
19
|
|
|
use JBZoo\Utils\FS; |
20
|
|
|
use JBZoo\Utils\Arr; |
21
|
|
|
use JBZoo\Path\Exception; |
22
|
|
|
use JBZoo\Path\Path as JBPAth; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Path |
26
|
|
|
* |
27
|
|
|
* @package Core\Path |
28
|
|
|
*/ |
29
|
|
|
class Path extends JBPAth |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
const LS_MODE_DIR = 'dir'; |
33
|
|
|
const LS_MODE_FILE = 'file'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Flag of result path (If true, is real path. If false, is relative path). |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $_isReal = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get a list of directories from a resource. |
44
|
|
|
* |
45
|
|
|
* @param string $resource |
46
|
|
|
* @param bool $recursive |
47
|
|
|
* @param null $filter |
48
|
|
|
* @return mixed |
49
|
|
|
*/ |
50
|
|
|
public function dirs($resource, $recursive = false, $filter = null) |
51
|
|
|
{ |
52
|
|
|
return $this->ls($resource, self::LS_MODE_DIR, $recursive, $filter); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get url to a file. |
57
|
|
|
* |
58
|
|
|
* @param string $source |
59
|
|
|
* @param bool $full |
60
|
|
|
* @return null|string |
61
|
|
|
*/ |
62
|
|
|
public function url($source, $full = true) |
63
|
|
|
{ |
64
|
|
|
$stamp = Configure::read('Asset.timestamp'); |
65
|
|
|
$url = parent::url($source, $full); |
66
|
|
|
|
67
|
|
|
if ($url !== null && $stamp && strpos($url, '?') === false) { |
68
|
|
|
$fullPath = $this->get($source); |
69
|
|
|
$url .= '?' . @filemtime($fullPath); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $url; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get path instance. |
77
|
|
|
* |
78
|
|
|
* @param string $key |
79
|
|
|
* @return Path |
80
|
|
|
* @throws Exception |
81
|
|
|
*/ |
82
|
|
|
public static function getInstance($key = 'default') |
83
|
|
|
{ |
84
|
|
|
if ((string) $key = '') { |
85
|
|
|
throw new Exception('Invalid object key'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (!Arr::key($key, self::$_objects)) { |
89
|
|
|
self::$_objects[$key] = new self(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return self::$_objects[$key]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get a list of files or diretories from a resource. |
97
|
|
|
* |
98
|
|
|
* @param string $resource |
99
|
|
|
* @param string $mode |
100
|
|
|
* @param bool $recursive |
101
|
|
|
* @param null $filter |
102
|
|
|
* @return array |
103
|
|
|
* @SuppressWarnings(PHPMD.ShortMethodName) |
104
|
|
|
*/ |
105
|
|
|
public function ls($resource, $mode = self::LS_MODE_FILE, $recursive = false, $filter = null) |
106
|
|
|
{ |
107
|
|
|
$files = []; |
108
|
|
|
list (, $paths, $path) = $this->_parse($resource); |
109
|
|
|
|
110
|
|
|
foreach ((array) $paths as $_path) { |
111
|
|
|
if (file_exists($_path . '/' . $path)) { |
112
|
|
|
foreach ($this->_list(FS::clean($_path . '/' . $path), '', $mode, $recursive, $filter) as $file) { |
113
|
|
|
if (!Arr::in($file, $files)) { |
114
|
|
|
$files[] = $file; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $files; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the list of files or directories in a given path. |
125
|
|
|
* |
126
|
|
|
* @param string $path |
127
|
|
|
* @param string $prefix |
128
|
|
|
* @param string $mode |
129
|
|
|
* @param bool $recursive |
130
|
|
|
* @param null $filter |
131
|
|
|
* @return array |
132
|
|
|
* @SuppressWarnings(PHPMD.CyclomaticComplexity) |
133
|
|
|
*/ |
134
|
|
|
protected function _list($path, $prefix = '', $mode = 'file', $recursive = false, $filter = null) |
135
|
|
|
{ |
136
|
|
|
$files = []; |
137
|
|
|
$ignore = ['.', '..', '.DS_Store', '.svn', '.git', '.gitignore', '.gitmodules', 'cgi-bin']; |
138
|
|
|
|
139
|
|
|
if ($scan = @scandir($path)) { |
140
|
|
|
foreach ($scan as $file) { |
141
|
|
|
// continue if ignore match |
142
|
|
|
if (Arr::in($file, $ignore)) { |
143
|
|
|
continue; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (is_dir($path . '/' . $file)) { |
147
|
|
|
// add dir |
148
|
|
View Code Duplication |
if ($mode === 'dir') { |
|
|
|
|
149
|
|
|
// continue if no regex filter match |
150
|
|
|
if ($filter && !preg_match($filter, $file)) { |
151
|
|
|
continue; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$files[] = $prefix . $file; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// continue if not recursive |
158
|
|
|
if (!$recursive) { |
159
|
|
|
continue; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// read subdirectory |
163
|
|
|
$files = array_merge( |
164
|
|
|
$files, |
165
|
|
|
$this->_list($path . '/' . $file, $prefix . $file . '/', $mode, $recursive, $filter) |
166
|
|
|
); |
167
|
|
|
|
168
|
|
View Code Duplication |
} else { |
|
|
|
|
169
|
|
|
// add file |
170
|
|
|
if ($mode === 'file') { |
171
|
|
|
// continue if no regex filter match |
172
|
|
|
if ($filter && !preg_match($filter, $file)) { |
173
|
|
|
continue; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$files[] = $prefix.$file; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $files; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.