-
Notifications
You must be signed in to change notification settings - Fork 2
/
magelinker.php
240 lines (193 loc) · 6.51 KB
/
magelinker.php
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
<?php
const DS = DIRECTORY_SEPARATOR;
const DRY_RUN = false;
const SHOW_PARAMS = false;
if (SHOW_PARAMS || isset($_REQUEST['debug'])) {
echoParams();
}
$magePath = getcwd();
$modulePath = null;
$isTbtModule = false;
if (isset($_REQUEST['module'])) {
$modulePath = $_REQUEST['module'];
}
if (isset($_REQUEST['istbtmodule'])) {
$isTbtModule = true;
}
?>
<style>
.textBox {
width:300px;
}
</style>
<h1>Mage Linker [Beta]</h1>
<p><b>Mage Path: </b><?php echo $magePath; ?></p>
<form name="input" method="post" onsubmit="if(echo dirname($magePath) == this.module.value) { if(!confirm('You sure you want to link everything from <?php echo dirname($magePath) . DS; ?>?')) return false; }">
<b>Module Path: </b><input type="text" class="textBox" name="module" value="<?php echo dirname($magePath) . DS; ?>" /><br/><br/>
<input type="checkbox" name="istbtmodule" value="checked" checked="checked"/>Is TBT module<br /><br/>
<input type="submit" value="Submit" />
</form>
<br/>
<?php
// Done if no module specified
if (!$modulePath) {
return;
}
if (DRY_RUN) {
echo '<p><b>DRY RUN (no linking will be done)</b></p>';
}
$linker = new MageLinker($magePath, $modulePath, $isTbtModule);
$linker->link();
echo '<p><b>Done.</b></p>';
return;
/***************************************************
* HELPER FUNCTIONS START HERE
***************************************************/
function echoParams() {
echo p('POST: ' . print_r($_POST, true));
echo p('GET: ' . print_r($_GET));
echo p('REQUEST: ' . print_r($_REQUEST));
}
function p($line)
{
echo '<pre>' . $line . '</pre>';
}
/***************************************************
* CLASSES START HERE
***************************************************/
class MageLinker {
public $magePath;
public $modulePath;
public $isTbtModule;
/* A list of directories which are shared by many modules.
* These directory trees will be built if they do not exist
* rather than symlinked. This prevents a messy symlinking
* ordeal if for example, the first module symlinks the
* first TBT directory.
*/
private $tbtSharedDirs = array (
'/app/code/community/TBT',
'/app/design/adminhtml/base/default/layout',
'/app/design/adminhtml/base/default/template',
'/js/tbt',
'/skin/adminhtml/base/default/css',
'/skin/adminhtml/base/default/images',
'/skin/adminhtml/default/default/css',
'/skin/adminhtml/default/default/images',
'/skin/frontend/base/default/css',
'/skin/frontend/base/default/fonts',
'/skin/frontend/base/default/images',
);
function __construct($magePath, $modulePath, $isTbtModule = false)
{
$this->magePath = $this->unixPath($magePath);
$this->modulePath = $this->unixPath($modulePath);
$this->isTbtModule = $isTbtModule;
}
public function link()
{
$this->recursiveLink($this->modulePath, $this->magePath);
}
function recursiveLink($target, $link)
{
// Base case: if target is a file, just link it and we're done
if (!is_dir($target)) {
$this->createLink($target, $link);
return true;
}
// Base case: if target is a directory, only link if the link direcotry path does not exist
if (!file_exists($link)) {
// Only link if not a TBT Shared directory. (eg. 'app/code/community/TBT/')
if (!$this->isTbtModule || !$this->isTbtSharedDir($link)) {
$this->createLink($target, $link, true);
return true;
}
// If it is a TBT Shared directory, just create the dir and continue
if (DRY_RUN || mkdir($link)) {
$this->printl('Created dir: ' . $link);
} else {
$this->printl('Failed to create dir: ' . $link);
}
}
// 'ls'
$files = scandir($target);
// Recurse on each file in the directory
foreach($files as $file) {
// Ignore '.', '..', and hidden files
if ($file[0] == '.') {
continue;
}
$newTarget = $target . '/' . $file;
$newLink = $link . '/' . $file;
$this->recursiveLink($newTarget, $newLink);
}
return true;
}
function createLink($target, $link, $isDir = false, $force = false)
{
// Skip if target does not exist
if (!file_exists($target)) {
$this->printl('Target does not exist: ' . $target);
return false;
}
// Remove old symlink if 'force' option is true
if (file_exists($link)) {
if ($force) {
$this->printl('Removing old link: ' . $link);
unlink($link);
} else {
$this->printl('Link already exists (use force option to override): ' . $link);
return false;
}
}
// Create symlink
$mklink = 'mklink' . ($isDir ? ' /D' : '');
$command = $mklink . ' ' . $this->safePath($link) . ' ' . $this->safePath($target);
$output = '';
$exitCode = 0;
if (DRY_RUN) {
$output = $command;
} else {
$output = exec($command, $output, $exitCode);
}
echo $this->printl($exitCode . ': ' . $output);
return $exitCode === 0;
}
protected function isTbtSharedDir($path)
{
foreach ($this->tbtSharedDirs as $sharedPath) {
$fullMagePath = $this->magePath . $sharedPath;
if (strpos($fullMagePath, $path) !== false) {
// return true if the path in contained in a TBT Shared path
return true;
}
}
return false;
}
protected function endsWithTbtSharedPath($path)
{
foreach ($this->tbtSharedDirs as $createDir) {
if ($this->endsWith($path, $createDir)) {
return true;
}
}
return false;
}
protected function endsWith($string, $ending)
{
return substr_compare($string, $ending, -strlen($ending), strlen($ending)) === 0;
}
protected function safePath($path)
{
return str_replace('/', DS, $path);
}
protected function unixPath($path)
{
return str_replace('\\', '/', $path);
}
public function printl($line)
{
echo '<pre>' . $line . '</pre>';
}
}
?>