1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MagentoHackathon\Composer\Magento\UnInstallStrategy; |
4
|
|
|
|
5
|
|
|
use Composer\Util\Filesystem; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class UnInstallStrategy |
9
|
|
|
* @package MagentoHackathon\Composer\Magento\UnInstallStrategy |
10
|
|
|
* @author Aydin Hassan <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class UnInstallStrategy implements UnInstallStrategyInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var Filesystem |
17
|
|
|
*/ |
18
|
|
|
protected $fileSystem; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The root dir for uninstalling from. Should be project root. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $rootDir; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Filesystem $fileSystem |
29
|
|
|
* @param string $rootDir |
30
|
|
|
*/ |
31
|
3 |
|
public function __construct(Filesystem $fileSystem, $rootDir) |
32
|
|
|
{ |
33
|
3 |
|
$this->fileSystem = $fileSystem; |
34
|
3 |
|
$this->rootDir = $rootDir; |
35
|
3 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* UnInstall the extension given the list of install files |
39
|
|
|
* |
40
|
|
|
* @param array $files |
41
|
|
|
*/ |
42
|
3 |
|
public function unInstall(array $files) |
43
|
|
|
{ |
44
|
3 |
|
foreach ($files as $file) { |
45
|
3 |
|
$file = $this->rootDir . $file; |
46
|
|
|
|
47
|
|
|
/* |
48
|
|
|
because of different reasons the file can be already gone. |
49
|
|
|
example: |
50
|
|
|
- file got deployed by multiple modules(should only happen with copy force) |
51
|
|
|
- user did things |
52
|
|
|
|
53
|
|
|
when the file is a symlink, but the target is already gone, file_exists returns false |
54
|
|
|
*/ |
55
|
|
|
|
56
|
3 |
|
if (is_link($file)) { |
57
|
1 |
|
$this->fileSystem->unlink($file); |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
if (file_exists($file)) { |
61
|
2 |
|
$this->fileSystem->remove($file); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
$parentDir = dirname($file); |
65
|
3 |
|
while (is_dir($parentDir) |
66
|
3 |
|
&& $this->fileSystem->isDirEmpty($parentDir) |
67
|
3 |
|
&& $parentDir !== $this->rootDir |
68
|
|
|
) { |
69
|
2 |
|
$this->fileSystem->removeDirectory($parentDir); |
70
|
2 |
|
$parentDir = dirname($parentDir); |
71
|
|
|
} |
72
|
|
|
} |
73
|
3 |
|
} |
74
|
|
|
} |
75
|
|
|
|