-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
Bootstrap.php
210 lines (179 loc) · 5.19 KB
/
Bootstrap.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
<?php
namespace MagentoHackathon\Composer\Magento\Patcher;
use Composer\IO\NullIO;
use Composer\IO\IOInterface;
use MagentoHackathon\Composer\Magento\ProjectConfig;
class Bootstrap
{
/**
* String inserted as a PHP comment, before and after the patch code.
*/
const PATCH_MARK = 'AUTOLOADER PATCH';
/**
* @var ProjectConfig
*/
private $config;
/**
* @var IOInterface
*/
private $io;
/**
* @var string
*/
private $mageClassFilePath;
/**
* @param string $mageClassFilePath Path to the Mage.php file which the patch will be applied on.
* @param ProjectConfig $config
*/
private function __construct($mageClassFilePath, ProjectConfig $config)
{
$this->setMageClassFilePath($mageClassFilePath);
$this->config = $config;
}
/**
* @param ProjectConfig $config
* @return $this
*/
public static function fromConfig(ProjectConfig $config)
{
return new self($config->getMagentoRootDir() . '/app/Mage.php', $config);
}
/**
* @return ProjectConfig
*/
private function getConfig()
{
return $this->config;
}
/**
* @return string
* @throws \DomainException
*/
private function getMageClassFilePath()
{
$mageFileCheck = true;
if (!is_file($this->mageClassFilePath)) {
$message = "{$this->mageClassFilePath} is not a file.";
$mageFileCheck = false;
} elseif (!is_readable($this->mageClassFilePath)) {
$message = "{$this->mageClassFilePath} is not readable.";
$mageFileCheck = false;
} elseif (!is_writable($this->mageClassFilePath)) {
$message = "{$this->mageClassFilePath} is not writable.";
$mageFileCheck = false;
}
if (!$mageFileCheck) {
throw new \DomainException($message);
}
return $this->mageClassFilePath;
}
/**
* Path to the Mage.php file which the patch will be applied on.
*
* @param string $mageClassFilePath
*/
private function setMageClassFilePath($mageClassFilePath)
{
$this->mageClassFilePath = $mageClassFilePath;
}
/**
* @return bool
*/
private function isPatchAlreadyApplied()
{
return strpos(file_get_contents($this->getMageClassFilePath()), self::PATCH_MARK) !== false;
}
/**
* @return bool
*/
public function canApplyPatch()
{
// check the config first
if (!$this->getConfig()->mustApplyBootstrapPatch()) {
$message = "<comment>Magento autoloader patching skipped because of configuration flag</comment>";
$result = false;
} elseif ($this->isPatchAlreadyApplied()) {
$message = "<comment>{$this->getMageClassFilePath()} was already patched</comment>";
$result = false;
} else {
$result = true;
$message = "<info>Autoloader patch to {$this->getMageClassFilePath()} was applied successfully</info>";
}
$this->getIo()->write($message);
return $result;
}
/**
* @return bool
*/
public function patch()
{
return $this->canApplyPatch() ? $this->writeComposerAutoloaderPatch() : false;
}
/**
* @return string
*/
protected function getAppPath()
{
return $this->getConfig()->getMagentoRootDir() . '/app';
}
/**
* @return bool
*/
protected function writeComposerAutoloaderPatch()
{
$mageFileContent = file($this->getMageClassFilePath());
$mageFileBootstrapPart = '';
$mageFileClassDeclarationPart = '';
$isBootstrapPart = true;
foreach ($mageFileContent as $row) {
if ($isBootstrapPart) {
$mageFileBootstrapPart .= $row;
} else {
$mageFileClassDeclarationPart .= $row;
}
if (strpos($row, 'Varien_Autoload') === 0) {
$isBootstrapPart = false;
}
}
$mageFileReplacement = $mageFileBootstrapPart . PHP_EOL
. $this->getAutoloaderPatchString() . PHP_EOL
. $mageFileClassDeclarationPart;
return file_put_contents($this->getMageClassFilePath(), $mageFileReplacement) !== false;
}
/**
* @param IOInterface $io
*/
public function setIo(IOInterface $io)
{
$this->io = $io;
}
/**
* @return IOInterface
*/
public function getIo()
{
if (!$this->io) {
$this->io = new NullIO;
}
return $this->io;
}
/**
* @return string
*/
private function getAutoloaderPatchString()
{
$patchMark = self::PATCH_MARK;
// get the vendor folder name from Config, in case it's changed
$vendorFolderName = basename($this->getConfig()->getVendorDir());
$autoloadPhp = $vendorFolderName . '/autoload.php';
return <<<PATCH
/** $patchMark **/
if (file_exists(\$autoloaderPath = BP . DS . '{$autoloadPhp}') ||
file_exists(\$autoloaderPath = BP . DS . '../{$autoloadPhp}')
) {
require \$autoloaderPath;
}
/** $patchMark **/
PATCH;
}
}