1 | <?php |
||
9 | class Bootstrap |
||
10 | { |
||
11 | /** |
||
12 | * String inserted as a PHP comment, before and after the patch code. |
||
13 | */ |
||
14 | const PATCH_MARK = 'AUTOLOADER PATCH'; |
||
15 | |||
16 | /** |
||
17 | * @var ProjectConfig |
||
18 | */ |
||
19 | private $config; |
||
20 | |||
21 | /** |
||
22 | * @var IOInterface |
||
23 | */ |
||
24 | private $io; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $mageClassFilePath; |
||
30 | |||
31 | /** |
||
32 | * @param string $mageClassFilePath Path to the Mage.php file which the patch will be applied on. |
||
33 | * @param ProjectConfig $config |
||
34 | */ |
||
35 | 7 | private function __construct($mageClassFilePath, ProjectConfig $config) |
|
36 | { |
||
37 | 7 | $this->setMageClassFilePath($mageClassFilePath); |
|
38 | 7 | $this->config = $config; |
|
39 | 7 | } |
|
40 | |||
41 | /** |
||
42 | * @param ProjectConfig $config |
||
43 | * @return $this |
||
44 | */ |
||
45 | 7 | public static function fromConfig(ProjectConfig $config) |
|
49 | |||
50 | /** |
||
51 | * @return ProjectConfig |
||
52 | */ |
||
53 | 7 | private function getConfig() |
|
57 | |||
58 | /** |
||
59 | * @return string |
||
60 | * @throws \DomainException |
||
61 | */ |
||
62 | 5 | private function getMageClassFilePath() |
|
63 | { |
||
64 | 5 | $mageFileCheck = true; |
|
65 | |||
66 | 5 | if (!is_file($this->mageClassFilePath)) { |
|
67 | 3 | $message = "{$this->mageClassFilePath} is not a file."; |
|
68 | 3 | $mageFileCheck = false; |
|
69 | 5 | } elseif (!is_readable($this->mageClassFilePath)) { |
|
70 | $message = "{$this->mageClassFilePath} is not readable."; |
||
71 | $mageFileCheck = false; |
||
72 | 2 | } elseif (!is_writable($this->mageClassFilePath)) { |
|
73 | $message = "{$this->mageClassFilePath} is not writable."; |
||
74 | $mageFileCheck = false; |
||
75 | } |
||
76 | |||
77 | 5 | if (!$mageFileCheck) { |
|
78 | 3 | throw new \DomainException($message); |
|
79 | } |
||
80 | |||
81 | 2 | return $this->mageClassFilePath; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Path to the Mage.php file which the patch will be applied on. |
||
86 | * |
||
87 | * @param string $mageClassFilePath |
||
88 | */ |
||
89 | 7 | private function setMageClassFilePath($mageClassFilePath) |
|
93 | |||
94 | /** |
||
95 | * @return bool |
||
96 | */ |
||
97 | 5 | private function isPatchAlreadyApplied() |
|
101 | |||
102 | /** |
||
103 | * @return bool |
||
104 | */ |
||
105 | 7 | public function canApplyPatch() |
|
106 | { |
||
107 | // check the config first |
||
108 | 7 | if (!$this->getConfig()->mustApplyBootstrapPatch()) { |
|
109 | 2 | $message = "<comment>Magento autoloader patching skipped because of configuration flag</comment>"; |
|
110 | 2 | $result = false; |
|
111 | 7 | } elseif ($this->isPatchAlreadyApplied()) { |
|
112 | 2 | $message = "<comment>{$this->getMageClassFilePath()} was already patched</comment>"; |
|
113 | 2 | $result = false; |
|
114 | 2 | } else { |
|
115 | 2 | $result = true; |
|
116 | 2 | $message = "<info>Autoloader patch to {$this->getMageClassFilePath()} was applied successfully</info>"; |
|
117 | } |
||
118 | |||
119 | 4 | $this->getIo()->write($message); |
|
120 | |||
121 | 4 | return $result; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * @return bool |
||
126 | */ |
||
127 | 7 | public function patch() |
|
131 | |||
132 | /** |
||
133 | * @return string |
||
134 | */ |
||
135 | protected function getAppPath() |
||
139 | |||
140 | /** |
||
141 | * @return bool |
||
142 | */ |
||
143 | 2 | protected function writeComposerAutoloaderPatch() |
|
144 | { |
||
145 | 2 | $mageFileContent = file($this->getMageClassFilePath()); |
|
146 | |||
147 | 2 | $mageFileBootstrapPart = ''; |
|
148 | 2 | $mageFileClassDeclarationPart = ''; |
|
149 | 2 | $isBootstrapPart = true; |
|
150 | |||
151 | 2 | foreach ($mageFileContent as $row) { |
|
152 | 2 | if ($isBootstrapPart) { |
|
153 | 2 | $mageFileBootstrapPart .= $row; |
|
154 | 2 | } else { |
|
155 | 2 | $mageFileClassDeclarationPart .= $row; |
|
156 | } |
||
157 | 2 | if (strpos($row, 'Varien_Autoload') === 0) { |
|
158 | 2 | $isBootstrapPart = false; |
|
159 | 2 | } |
|
160 | 2 | } |
|
161 | |||
162 | $mageFileReplacement = $mageFileBootstrapPart . PHP_EOL |
||
163 | 2 | . $this->getAutoloaderPatchString() . PHP_EOL |
|
164 | 2 | . $mageFileClassDeclarationPart; |
|
165 | |||
166 | 2 | return file_put_contents($this->getMageClassFilePath(), $mageFileReplacement) !== false; |
|
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param IOInterface $io |
||
171 | */ |
||
172 | 2 | public function setIo(IOInterface $io) |
|
176 | |||
177 | /** |
||
178 | * @return IOInterface |
||
179 | */ |
||
180 | 4 | public function getIo() |
|
187 | |||
188 | /** |
||
189 | * @return string |
||
190 | */ |
||
191 | 2 | private function getAutoloaderPatchString() |
|
210 | } |
||
211 |