19
19
class MagentoBuilder
20
20
{
21
21
/**
22
- * @param \Closure $processOutput Handle client output
23
- * @param string $edition
24
- * @param string $version
25
- * @param string $outDir
26
- * @param string $magentoToken
27
- * @param string $magentoPrivateToken
28
- * @param string $githubToken
22
+ * @param \Closure $processOutput Handle client output
23
+ * @param string $edition
24
+ * @param string $version
25
+ * @param string $outDir
26
+ * @param string $magentoToken
27
+ * @param string $magentoPrivateToken
28
+ * @param string $githubToken
29
29
* @throws ClientExceptionInterface
30
30
* @throws RedirectionExceptionInterface
31
31
* @throws ServerExceptionInterface
@@ -39,19 +39,61 @@ public static function deploy(
39
39
$ magentoToken = "" ,
40
40
$ magentoPrivateToken = "" ,
41
41
$ githubToken = ""
42
- ) {
43
- $ composerAuth = sprintf (
42
+ )
43
+ {
44
+ $ composerAuth = self ::makeComposerAuth ($ magentoToken , $ magentoPrivateToken , $ githubToken );
45
+
46
+ self ::prepareDirectoriesForInstall ($ outDir );
47
+ self ::composerCreateProject ($ edition , $ version , $ composerAuth , $ processOutput );
48
+ self ::moveToOutDirectory ($ outDir );
49
+ self ::saveComposerAuth ($ outDir , $ composerAuth );
50
+ self ::putSampleNginx ($ outDir , $ version );
51
+ }
52
+
53
+ /**
54
+ * @param $magentoToken
55
+ * @param $magentoPrivateToken
56
+ * @param $githubToken
57
+ * @return string
58
+ */
59
+ private static function makeComposerAuth ($ magentoToken , $ magentoPrivateToken , $ githubToken )
60
+ {
61
+ return sprintf (
44
62
'{"http-basic": {"repo.magento.com": {"username": "%s","password": "%s"}}, "github-oauth": {"github.com": "%s"}} ' ,
45
63
$ magentoToken ,
46
64
$ magentoPrivateToken ,
47
65
$ githubToken
48
66
);
67
+ }
49
68
69
+ /**
70
+ * @param $outDir
71
+ * @return void
72
+ */
73
+ private static function prepareDirectoriesForInstall ($ outDir )
74
+ {
50
75
$ filesystem = new Filesystem ();
51
76
$ filesystem ->ensureDirectoryExists (self ::temporaryPath ());
52
77
$ filesystem ->ensureDirectoryExists ($ outDir );
53
78
$ filesystem ->cleanDirectory (self ::temporaryPath ());
79
+ }
54
80
81
+ /**
82
+ * @return string
83
+ */
84
+ private static function temporaryPath ()
85
+ {
86
+ return sys_get_temp_dir () . '/magento ' ;
87
+ }
88
+
89
+ /**
90
+ * @param $edition
91
+ * @param $version
92
+ * @param $composerAuth
93
+ * @param \Closure $processOutput
94
+ */
95
+ private static function composerCreateProject ($ edition , $ version , $ composerAuth , \Closure $ processOutput )
96
+ {
55
97
$ cmdInstall = 'composer create-project -n --no-install --repository=https://repo.magento.com/ ' . $ edition . ' . ' . $ version ;
56
98
$ createProject = Process::fromShellCommandline (
57
99
$ cmdInstall ,
@@ -70,33 +112,128 @@ function ($type, $buffer) use ($processOutput) {
70
112
if ($ createProject ->getExitCode () !== 0 ) {
71
113
throw new ProcessFailedException ($ createProject );
72
114
}
73
-
74
- $ filesystem ->copyDirectory (self ::temporaryPath (), $ outDir );
75
- self ::putSampleNginx ($ version , $ filesystem , $ outDir );
76
- $ filesystem ->put ($ outDir . '/auth.json ' , $ composerAuth );
77
115
}
78
116
79
117
/**
80
- * @return string
118
+ * @param $outDir
119
+ * @return void
81
120
*/
82
- protected static function temporaryPath ( )
121
+ private static function moveToOutDirectory ( $ outDir )
83
122
{
84
- return sys_get_temp_dir () . '/magento ' ;
123
+ $ filesystem = new Filesystem ();
124
+ $ filesystem ->copyDirectory (self ::temporaryPath (), $ outDir );
85
125
}
86
126
87
127
/**
88
- * @param $version
89
- * @param Filesystem $filesystem
128
+ * This is appropriate for ensuring Magento's NGINX is in place before a composer install has been executed
90
129
* @param $outDir
130
+ * @param $mageVersion
91
131
* @throws ClientExceptionInterface
92
132
* @throws RedirectionExceptionInterface
93
133
* @throws ServerExceptionInterface
94
134
* @throws TransportExceptionInterface
135
+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
95
136
*/
96
- protected static function putSampleNginx ($ version , Filesystem $ filesystem , $ outDir )
137
+ private static function putSampleNginx ($ outDir , $ mageVersion )
97
138
{
139
+ $ filesystem = new Filesystem ();
98
140
$ mageRepoClient = HttpClient::createForBaseUri ('https://raw.githubusercontent.com/magento/magento2/ ' );
99
- $ sampleNginx = $ mageRepoClient ->request ('GET ' , $ version . '/nginx.conf.sample ' )->getContent (true );
141
+ $ installedVersion = @self ::getComposerData ($ filesystem , $ outDir )['version ' ];
142
+ if (empty ($ installedVersion )){
143
+ $ installedVersion = $ mageVersion ;
144
+ }
145
+ $ sampleNginx = $ mageRepoClient ->request ('GET ' , $ installedVersion . '/nginx.conf.sample ' )->getContent (true );
100
146
$ filesystem ->put ($ outDir . '/nginx.conf.sample ' , $ sampleNginx , true );
101
147
}
148
+
149
+ /**
150
+ * @param $outDir
151
+ * @param $composerAuth
152
+ * @return void
153
+ */
154
+ private static function saveComposerAuth ($ outDir , $ composerAuth )
155
+ {
156
+ (new Filesystem ())->put ($ outDir . '/auth.json ' , $ composerAuth );
157
+ }
158
+
159
+ /**
160
+ * @param $processOutput
161
+ * @param string $version
162
+ * @param string $outDir
163
+ * @param string $githubUrl
164
+ * @param string $magentoToken
165
+ * @param string $magentoPrivateToken
166
+ * @param string $githubToken
167
+ * @throws ClientExceptionInterface
168
+ * @throws RedirectionExceptionInterface
169
+ * @throws ServerExceptionInterface
170
+ * @throws TransportExceptionInterface
171
+ */
172
+ public static function deployFromGitHub (
173
+ $ processOutput ,
174
+ $ version = '2.3 ' ,
175
+ $ outDir = 'new-magento ' ,
176
+ $ githubUrl = 'https://github.com/magento/magento2 ' ,
177
+ $ magentoToken = "" ,
178
+ $ magentoPrivateToken = "" ,
179
+ $ githubToken = ""
180
+ )
181
+ {
182
+ $ composerAuth = self ::makeComposerAuth ($ magentoToken , $ magentoPrivateToken , $ githubToken );
183
+ self ::prepareDirectoriesForInstall ($ outDir );
184
+ self ::gitClone ($ version , self ::authenticatedRepoUrl ($ githubToken , $ githubUrl ), $ processOutput );
185
+ self ::moveToOutDirectory ($ outDir );
186
+ self ::saveComposerAuth ($ outDir , $ composerAuth );
187
+ }
188
+
189
+ /**
190
+ * @param $branchOrTag
191
+ * @param $githubRepo
192
+ * @param \Closure $processOutput
193
+ */
194
+ private static function gitClone ($ branchOrTag , $ githubRepo , \Closure $ processOutput )
195
+ {
196
+ $ cmdGitClone = 'git clone --depth 1 --single-branch --branch ' . $ branchOrTag . ' ' . $ githubRepo . ' . ' ;
197
+
198
+ $ gitClone = Process::fromShellCommandline (
199
+ $ cmdGitClone ,
200
+ self ::temporaryPath ()
201
+ );
202
+
203
+ $ gitClone ->start (
204
+ function ($ type , $ buffer ) use ($ processOutput ) {
205
+ $ processOutput ($ type , $ buffer );
206
+ }
207
+ );
208
+ $ gitClone ->wait ();
209
+ if ($ gitClone ->getExitCode () !== 0 ) {
210
+ throw new ProcessFailedException ($ gitClone );
211
+ }
212
+ }
213
+
214
+ /**
215
+ * @param $githubToken
216
+ * @param $githubUrl
217
+ * @return string|string[]
218
+ */
219
+ private static function authenticatedRepoUrl ($ githubToken , $ githubUrl )
220
+ {
221
+ $ cloneUrl = rtrim (rtrim ($ githubUrl , '/ ' ), '.git ' ) . '.git ' ;
222
+ return str_replace (
223
+ 'https:// ' ,
224
+ 'https:// ' . $ githubToken . '@ ' ,
225
+ $ cloneUrl
226
+ );
227
+ }
228
+
229
+ /**
230
+ * @param Filesystem $filesystem
231
+ * @param $outDir
232
+ * @return mixed
233
+ * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
234
+ */
235
+ private static function getComposerData (Filesystem $ filesystem , $ outDir )
236
+ {
237
+ return json_decode ($ filesystem ->get ($ outDir . DIRECTORY_SEPARATOR . 'composer.json ' ), true );
238
+ }
102
239
}
0 commit comments