1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Composer Magento Installer |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace MagentoHackathon\Composer\Magento\Deploystrategy; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Hardlink deploy strategy |
10
|
|
|
*/ |
11
|
|
|
class Link extends DeploystrategyAbstract |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Creates a hardlink with lots of error-checking |
15
|
|
|
* |
16
|
|
|
* @param string $source |
17
|
|
|
* @param string $dest |
18
|
|
|
* @return bool |
19
|
|
|
* @throws \ErrorException |
20
|
|
|
*/ |
21
|
13 |
|
public function createDelegate($source, $dest) |
22
|
|
|
{ |
23
|
13 |
|
$sourcePath = $this->getSourceDir() . '/' . $this->removeTrailingSlash($source); |
24
|
13 |
|
$destPath = $this->getDestDir() . '/' . $this->removeTrailingSlash($dest); |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
// Create all directories up to one below the target if they don't exist |
28
|
13 |
|
$destDir = dirname($destPath); |
29
|
13 |
|
if (!file_exists($destDir)) { |
30
|
2 |
|
mkdir($destDir, 0777, true); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Handle source to dir link, |
34
|
|
|
// e.g. Namespace_Module.csv => app/locale/de_DE/ |
35
|
13 |
|
if (file_exists($destPath) && is_dir($destPath)) { |
36
|
6 |
|
if (basename($sourcePath) === basename($destPath)) { |
37
|
|
|
// copy/link each child of $sourcePath into $destPath |
38
|
1 |
View Code Duplication |
foreach (new \DirectoryIterator($sourcePath) as $item) { |
|
|
|
|
39
|
1 |
|
$item = (string) $item; |
40
|
1 |
|
if (!strcmp($item, '.') || !strcmp($item, '..')) { |
41
|
1 |
|
continue; |
42
|
|
|
} |
43
|
1 |
|
$childSource = $source . '/' . $item; |
44
|
1 |
|
$this->create($childSource, substr($destPath, strlen($this->getDestDir())+1)); |
45
|
|
|
} |
46
|
1 |
|
return true; |
47
|
|
View Code Duplication |
} else { |
|
|
|
|
48
|
6 |
|
$destPath .= '/' . basename($source); |
49
|
6 |
|
return $this->create($source, substr($destPath, strlen($this->getDestDir())+1)); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// From now on $destPath can't be a directory, that case is already handled |
54
|
|
|
|
55
|
|
|
// If file exists and force is not specified, throw exception unless FORCE is set |
56
|
13 |
View Code Duplication |
if (file_exists($destPath)) { |
|
|
|
|
57
|
|
|
if ($this->isForced()) { |
58
|
|
|
unlink($destPath); |
59
|
|
|
} else { |
60
|
|
|
throw new \ErrorException("Target $dest already exists (set extra.magento-force to override)"); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// File to file |
65
|
13 |
|
if (!is_dir($sourcePath)) { |
66
|
9 |
|
if (is_dir($destPath)) { |
67
|
|
|
$destPath .= '/' . basename($sourcePath); |
68
|
|
|
} |
69
|
9 |
|
return link($sourcePath, $destPath); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Copy dir to dir |
73
|
|
|
// First create destination folder if it doesn't exist |
74
|
4 |
|
if (file_exists($destPath)) { |
75
|
|
|
$destPath .= '/' . basename($sourcePath); |
76
|
|
|
} |
77
|
4 |
|
mkdir($destPath, 0777, true); |
78
|
|
|
|
79
|
4 |
|
$iterator = new \RecursiveIteratorIterator( |
80
|
4 |
|
new \RecursiveDirectoryIterator($sourcePath), |
81
|
4 |
|
\RecursiveIteratorIterator::SELF_FIRST |
82
|
|
|
); |
83
|
|
|
|
84
|
4 |
|
foreach ($iterator as $item) { |
85
|
4 |
|
$subDestPath = $destPath . '/' . $iterator->getSubPathName(); |
86
|
4 |
|
if ($item->isDir()) { |
87
|
4 |
|
if (! file_exists($subDestPath)) { |
88
|
4 |
|
mkdir($subDestPath, 0777, true); |
89
|
|
|
} |
90
|
|
|
} else { |
91
|
4 |
|
link($item, $subDestPath); |
92
|
4 |
|
$this->addDeployedFile($subDestPath); |
93
|
|
|
} |
94
|
4 |
|
if (!is_readable($subDestPath)) { |
95
|
4 |
|
throw new \ErrorException("Could not create $subDestPath"); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
4 |
|
return true; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.