1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: joshgulledge |
5
|
|
|
* Date: 3/13/18 |
6
|
|
|
* Time: 1:11 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace LCI\Blend\Helpers; |
10
|
|
|
|
11
|
|
|
use FilesystemIterator; |
12
|
|
|
use RecursiveDirectoryIterator; |
13
|
|
|
use RecursiveIteratorIterator; |
14
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
|
17
|
|
|
trait Files |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @param string $source ~ full path of source |
21
|
|
|
* @param string $destination ~ full path of destination |
22
|
|
|
* @param int $file_count |
23
|
|
|
*/ |
24
|
|
|
public function copyDirectory($source, $destination, $file_count = 4000) |
25
|
|
|
{ |
26
|
|
|
$destination = rtrim($destination, '\/\\'); |
27
|
|
|
if (!is_dir($destination)) { |
28
|
|
|
$created = mkdir($destination, 0700); |
29
|
|
|
echo PHP_EOL . ' **** '. __LINE__ . 'Attempted to create: '. $destination. ($created ? 'Y' : 'N').' **** '. PHP_EOL; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** @var \RecursiveDirectoryIterator $directoryIterator */ |
33
|
|
|
$directoryIterator = new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS); |
34
|
|
|
|
35
|
|
|
if (isset($this->output) && $this->output instanceof OutputInterface) { |
36
|
|
|
$this->output->writeln('Now copying extracted files to '.$destination.' File count: '.$file_count); |
37
|
|
|
$progress = new ProgressBar($this->output, $file_count); |
38
|
|
|
$progress->start(); |
39
|
|
|
|
40
|
|
|
$progress->setRedrawFrequency(10); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** @var RecursiveIteratorIterator $recursiveIteratorIterator */ |
44
|
|
|
$recursiveIteratorIterator = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::SELF_FIRST); |
45
|
|
|
|
46
|
|
|
/** @var \DirectoryIterator $item */ |
47
|
|
|
foreach ($recursiveIteratorIterator as $item) { |
48
|
|
|
if ($item->isDir()) { |
49
|
|
|
if (is_dir($destination.DIRECTORY_SEPARATOR.$recursiveIteratorIterator->getSubPathName())) { |
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
$created = mkdir($destination.DIRECTORY_SEPARATOR.$recursiveIteratorIterator->getSubPathName()); |
53
|
|
|
echo PHP_EOL . ' **** '. __LINE__ . 'Attempted to create: '. $destination. ($created ? 'Y' : 'N').' **** '. PHP_EOL; |
54
|
|
|
|
55
|
|
|
} else { |
56
|
|
|
copy($item, $destination.DIRECTORY_SEPARATOR.$recursiveIteratorIterator->getSubPathName()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (isset($progress) && $progress instanceof ProgressBar) { |
60
|
|
|
$progress->advance(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (isset($progress) && $progress instanceof ProgressBar) { |
66
|
|
|
// ensures that the progress bar is at 100% |
67
|
|
|
$progress->finish(); |
68
|
|
|
$this->output->writeln(''); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $directory |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function deleteDirectory($directory) |
78
|
|
|
{ |
79
|
|
|
$directory = rtrim($directory, '\/\\'); |
80
|
|
|
if (!empty($directory) && file_exists($directory)) { |
81
|
|
|
|
82
|
|
|
/** @var \RecursiveDirectoryIterator $directoryIterator */ |
83
|
|
|
$directoryIterator = new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS); |
84
|
|
|
|
85
|
|
|
/** @var RecursiveIteratorIterator $recursiveIteratorIterator */ |
86
|
|
|
$recursiveIteratorIterator = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::CHILD_FIRST); |
87
|
|
|
|
88
|
|
|
/** @var $file */ |
89
|
|
|
foreach ($recursiveIteratorIterator as $file) { |
90
|
|
|
$file->isDir() ? rmdir($file) : unlink($file); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return rmdir($directory); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |