-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathFileUtil.php
More file actions
308 lines (247 loc) · 8.99 KB
/
FileUtil.php
File metadata and controls
308 lines (247 loc) · 8.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/*!
* File Util Class
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
* Generic file related functions that are used throughout Pattern Lab
*
*/
namespace PatternLab;
use \PatternLab\Config;
use \PatternLab\Console;
use \PatternLab\Dispatcher;
use \PatternLab\Timer;
use \Symfony\Component\Filesystem\Filesystem;
use \Symfony\Component\Filesystem\Exception\IOExceptionInterface;
class FileUtil {
/**
* Check that a dir from the config exists and try to build it if needed
* @param {String} directory to be checked/built
* @param {String} the config path
* @param {String} the config option that would help them
*/
public static function checkPathFromConfig($path, $configPath, $configOption = "") {
if (!isset($path)) {
Console::writeError("please make sure ".$configOption." is set in <path>".Console::getHumanReadablePath($configPath)."</path> by adding '".$configOption."=some/path'. sorry, stopping pattern lab... :(");
} else if (!is_dir($path)) {
Console::writeWarning("i can't seem to find the directory <path>".Console::getHumanReadablePath($path)."</path>...");
self::makeDir($path);
Console::writeWarning("i created <path>".Console::getHumanReadablePath($path)."</path> just in case. you can edit this in <path>".Console::getHumanReadablePath($configPath)."</path> by editing ".$configOption."...");
}
}
/**
* Make a directory
* @param {String} directory to be made
*/
public static function makeDir($dir) {
$fs = new Filesystem();
try {
$fs->mkdir($dir);
} catch (IOExceptionInterface $e) {
Console::writeError("an error occurred while creating your directory at <path>".$e->getPath()."</path>...");
}
unset($fs);
}
/**
* Copies a file from the given source path to the given public path.
* THIS IS NOT FOR PATTERNS
* @param {String} the source file
* @param {String} the public file
*/
protected static function moveFile($s,$p) {
// default vars
$sourceDir = Config::getOption("sourceDir");
$publicDir = Config::getOption("publicDir");
$fs = new Filesystem();
$fs->copy($sourceDir.DIRECTORY_SEPARATOR.$s,$publicDir.DIRECTORY_SEPARATOR.$p);
}
/**
* Moves static files that aren't directly related to Pattern Lab
* @param {String} file name to be moved
* @param {String} copy for the message to be printed out
* @param {String} part of the file name to be found for replacement
* @param {String} the replacement
*/
public static function moveStaticFile($fileName,$copy = "", $find = "", $replace = "") {
self::moveFile($fileName,str_replace($find, $replace, $fileName));
Util::updateChangeTime();
if ($copy != "") {
Console::writeInfo($fileName." ".$copy."...");
}
}
/**
* Check to see if a given filename is in a directory that should be ignored
* @param {String} file name to be checked
*
* @return {Boolean} whether the directory should be ignored
*/
public static function ignoreDir($fileName) {
$id = Config::getOption("id");
foreach ($id as $dir) {
$pos = strpos(DIRECTORY_SEPARATOR.$fileName,DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR);
if ($pos !== false) {
return true;
}
}
return false;
}
/**
* Taken from Composer: https://github.com/composer/composer/blob/master/src/Composer/Util/Filesystem.php
*
* Normalize a path. This replaces backslashes with slashes, removes ending
* slash and collapses redundant separators and up-level references.
*
* @param string $path Path to the file or directory
* @return string
*/
public static function normalizePath($path) {
$parts = array();
$path = strtr($path, '\\', '/');
$prefix = '';
$absolute = false;
if (preg_match('{^([0-9a-z]+:(?://(?:[a-z]:)?)?)}i', $path, $match)) {
$prefix = $match[1];
$path = substr($path, strlen($prefix));
}
if (substr($path, 0, 1) === '/') {
$absolute = true;
$path = substr($path, 1);
}
$up = false;
foreach (explode('/', $path) as $chunk) {
if ('..' === $chunk && ($absolute || $up)) {
array_pop($parts);
$up = !(empty($parts) || '..' === end($parts));
} elseif ('.' !== $chunk && '' !== $chunk) {
$parts[] = $chunk;
$up = '..' !== $chunk;
}
}
return $prefix.($absolute ? '/' : '').implode('/', $parts);
}
/**
* Delete everything in export/
*/
public static function cleanExport() {
// default var
$exportDir = Config::getOption("exportDir");
if (is_dir($exportDir)) {
$files = scandir($exportDir);
foreach ($files as $file) {
if (($file == "..") || ($file == ".")) {
array_shift($files);
} else {
$key = array_keys($files,$file);
$files[$key[0]] = $exportDir.DIRECTORY_SEPARATOR.$file;
}
}
$fs = new Filesystem();
$fs->remove($files);
}
}
/**
* Delete patterns and user-created directories and files in public/
*/
public static function cleanPublic() {
// set-up the dispatcher
$dispatcherInstance = Dispatcher::getInstance();
// dispatch that the data gather has started
$dispatcherInstance->dispatch("fileUtil.cleanPublicStart");
// default var
$patternPublicDir = Config::getOption("patternPublicDir");
// make sure patterns exists before trying to clean it
if (is_dir($patternPublicDir)) {
// symfony finder doesn't support child first and I don't want to do array crap
$objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($patternPublicDir), \RecursiveIteratorIterator::CHILD_FIRST);
// make sure dots are skipped
$objects->setFlags(\FilesystemIterator::SKIP_DOTS);
// for each file figure out what to do with it
foreach ($objects as $name => $object) {
if ($object->isDir()) {
// if this is a directory remove it
rmdir($name);
} else if ($object->isFile() && ($object->getFilename() != "README")) {
// if this is a file remove it
unlink($name);
}
}
}
// scan source/ & public/ to figure out what directories might need to be cleaned up
$publicDir = Config::getOption("publicDir");
$sourceDir = Config::getOption("sourceDir");
$publicDirs = glob($publicDir.DIRECTORY_SEPARATOR."*",GLOB_ONLYDIR);
$sourceDirs = glob($sourceDir.DIRECTORY_SEPARATOR."*",GLOB_ONLYDIR);
// make sure some directories aren't deleted
$ignoreDirs = array("styleguide","patternlab-components");
foreach ($ignoreDirs as $ignoreDir) {
$key = array_search($publicDir.DIRECTORY_SEPARATOR.$ignoreDir,$publicDirs);
if ($key !== false){
unset($publicDirs[$key]);
}
}
// compare source dirs against public. remove those dirs w/ an underscore in source/ from the public/ list
foreach ($sourceDirs as $dir) {
$cleanDir = str_replace($sourceDir.DIRECTORY_SEPARATOR,"",$dir);
if ($cleanDir[0] == "_") {
$key = array_search($publicDir.DIRECTORY_SEPARATOR.str_replace("_","",$cleanDir),$publicDirs);
if ($key !== false){
unset($publicDirs[$key]);
}
}
}
// for the remaining dirs in public delete them and their files
foreach ($publicDirs as $dir) {
// symfony finder doesn't support child first and I don't want to do array crap
$objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir), \RecursiveIteratorIterator::CHILD_FIRST);
// make sure dots are skipped
$objects->setFlags(\FilesystemIterator::SKIP_DOTS);
foreach($objects as $name => $object) {
if ($object->isDir()) {
rmdir($name);
} else if ($object->isFile()) {
unlink($name);
}
}
rmdir($dir);
}
$dispatcherInstance->dispatch("fileUtil.cleanPublicEnd");
}
/**
* moves user-generated static files from public/ to export/
*/
public static function exportStatic() {
// default vars
$exportDir = Config::getOption("exportDir");
$publicDir = Config::getOption("publicDir");
$sourceDir = Config::getOption("sourceDir");
if (!is_dir($exportDir)) {
mkdir($exportDir);
}
if (is_dir($publicDir)) {
// decide which files in public should def. be ignored
$ignore = array("annotations","data","patterns","styleguide","index.html","latest-change.txt",".DS_Store",".svn","README");
$files = scandir($publicDir);
foreach ($files as $key => $file) {
if (($file == "..") || ($file == ".")) {
unset($files[$key]);
} else if (in_array($file,$ignore)) {
unset($files[$key]);
} else if (is_dir($publicDir.DIRECTORY_SEPARATOR.$file) && !is_dir($sourceDir.DIRECTORY_SEPARATOR.$file)) {
unset($files[$key]);
} else if (is_file($publicDir.DIRECTORY_SEPARATOR.$file) && !is_file($sourceDir.DIRECTORY_SEPARATOR.$file)) {
unset($files[$key]);
}
}
$fs = new Filesystem();
foreach ($files as $file) {
if (is_dir($publicDir.DIRECTORY_SEPARATOR.$file)) {
$fs->mirror($publicDir.DIRECTORY_SEPARATOR.$file,$exportDir.DIRECTORY_SEPARATOR.$file);
} else if (is_file($publicDir.DIRECTORY_SEPARATOR.$file)) {
$fs->copy($publicDir.DIRECTORY_SEPARATOR.$file,$exportDir.DIRECTORY_SEPARATOR.$file);
}
}
}
}
}