1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jellyfish\Transfer; |
4
|
|
|
|
5
|
|
|
use Jellyfish\Filesystem\FilesystemInterface; |
6
|
|
|
use Jellyfish\Finder\FinderFactoryInterface; |
7
|
|
|
use Jellyfish\Transfer\Generator\FactoryRegistryGenerator; |
8
|
|
|
use Jellyfish\Transfer\Generator\FactoryRegistryGeneratorInterface; |
9
|
|
|
use SplFileInfo; |
10
|
|
|
|
11
|
|
|
class TransferCleaner implements TransferCleanerInterface |
12
|
|
|
{ |
13
|
|
|
protected const EXCLUDED_FILE = 'factory-registry.php'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $targetDirectory; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Jellyfish\Filesystem\FilesystemInterface |
22
|
|
|
*/ |
23
|
|
|
protected $filesystem; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \Jellyfish\Finder\FinderFactoryInterface |
27
|
|
|
*/ |
28
|
|
|
protected $finderFactory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param \Jellyfish\Finder\FinderFactoryInterface $finderFactory |
32
|
|
|
* @param \Jellyfish\Filesystem\FilesystemInterface $filesystem |
33
|
|
|
* @param string $targetDirectory |
34
|
|
|
*/ |
35
|
|
|
public function __construct( |
36
|
|
|
FinderFactoryInterface $finderFactory, |
37
|
|
|
FilesystemInterface $filesystem, |
38
|
|
|
string $targetDirectory |
39
|
|
|
) |
40
|
|
|
{ |
41
|
|
|
$this->finderFactory = $finderFactory; |
42
|
|
|
$this->targetDirectory = $targetDirectory; |
43
|
|
|
$this->filesystem = $filesystem; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return \Jellyfish\Transfer\TransferCleanerInterface |
48
|
|
|
*/ |
49
|
|
|
public function clean(): TransferCleanerInterface |
50
|
|
|
{ |
51
|
|
|
if (!$this->canClean()) { |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->cleanDirectory($this->targetDirectory); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
protected function canClean(): bool |
62
|
|
|
{ |
63
|
|
|
return $this->filesystem->exists($this->targetDirectory); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $directory |
68
|
|
|
* |
69
|
|
|
* @return \Jellyfish\Transfer\TransferCleanerInterface |
70
|
|
|
*/ |
71
|
|
|
protected function cleanDirectory(string $directory): TransferCleanerInterface |
72
|
|
|
{ |
73
|
|
|
$finder = $this->finderFactory->create(); |
74
|
|
|
|
75
|
|
|
$iterator = $finder->in($directory)->depth(1)->getIterator(); |
76
|
|
|
|
77
|
|
|
foreach ($iterator as $item) { |
78
|
|
|
if (!($item instanceof SplFileInfo)) { |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$itemRealPath = $item->getRealPath(); |
83
|
|
|
|
84
|
|
|
if (!$this->canRemove($itemRealPath)) { |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($item->isDir()) { |
89
|
|
|
$this->cleanDirectory($itemRealPath); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->filesystem->remove($itemRealPath); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $realPathOfItem |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
protected function canRemove(string $realPathOfItem): bool |
104
|
|
|
{ |
105
|
|
|
$realPathOfFactoryRegistry = $this->targetDirectory . static::EXCLUDED_FILE; |
106
|
|
|
|
107
|
|
|
return $realPathOfFactoryRegistry !== $realPathOfItem; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|