Skip to content
This repository was archived by the owner on Jun 6, 2022. It is now read-only.

Commit 6e0034a

Browse files
Merge branch 'develop'
2 parents 7f1bf84 + 1517097 commit 6e0034a

File tree

4 files changed

+265
-50
lines changed

4 files changed

+265
-50
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"description": "Magento 2 Commerce/Open Source Downloader",
44
"require": {
55
"php": "~5.5.0|~5.6.0|~7.0.0|~7.1.0|~7.2.0|~7.3.0",
6+
"ext-json": "*",
67
"symfony/console": "^5.0",
78
"illuminate/validation": "^7.9",
89
"symfony/http-client": "^5.0",

src/Builder/MagentoBuilder.php

+157-20
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
class MagentoBuilder
2020
{
2121
/**
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
2929
* @throws ClientExceptionInterface
3030
* @throws RedirectionExceptionInterface
3131
* @throws ServerExceptionInterface
@@ -39,19 +39,61 @@ public static function deploy(
3939
$magentoToken = "",
4040
$magentoPrivateToken = "",
4141
$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(
4462
'{"http-basic": {"repo.magento.com": {"username": "%s","password": "%s"}}, "github-oauth": {"github.com": "%s"}}',
4563
$magentoToken,
4664
$magentoPrivateToken,
4765
$githubToken
4866
);
67+
}
4968

69+
/**
70+
* @param $outDir
71+
* @return void
72+
*/
73+
private static function prepareDirectoriesForInstall($outDir)
74+
{
5075
$filesystem = new Filesystem();
5176
$filesystem->ensureDirectoryExists(self::temporaryPath());
5277
$filesystem->ensureDirectoryExists($outDir);
5378
$filesystem->cleanDirectory(self::temporaryPath());
79+
}
5480

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+
{
5597
$cmdInstall = 'composer create-project -n --no-install --repository=https://repo.magento.com/ ' . $edition . ' . ' . $version;
5698
$createProject = Process::fromShellCommandline(
5799
$cmdInstall,
@@ -70,33 +112,128 @@ function ($type, $buffer) use ($processOutput) {
70112
if ($createProject->getExitCode() !== 0) {
71113
throw new ProcessFailedException($createProject);
72114
}
73-
74-
$filesystem->copyDirectory(self::temporaryPath(), $outDir);
75-
self::putSampleNginx($version, $filesystem, $outDir);
76-
$filesystem->put($outDir . '/auth.json', $composerAuth);
77115
}
78116

79117
/**
80-
* @return string
118+
* @param $outDir
119+
* @return void
81120
*/
82-
protected static function temporaryPath()
121+
private static function moveToOutDirectory($outDir)
83122
{
84-
return sys_get_temp_dir() . '/magento';
123+
$filesystem = new Filesystem();
124+
$filesystem->copyDirectory(self::temporaryPath(), $outDir);
85125
}
86126

87127
/**
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
90129
* @param $outDir
130+
* @param $mageVersion
91131
* @throws ClientExceptionInterface
92132
* @throws RedirectionExceptionInterface
93133
* @throws ServerExceptionInterface
94134
* @throws TransportExceptionInterface
135+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
95136
*/
96-
protected static function putSampleNginx($version, Filesystem $filesystem, $outDir)
137+
private static function putSampleNginx($outDir, $mageVersion)
97138
{
139+
$filesystem = new Filesystem();
98140
$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);
100146
$filesystem->put($outDir . '/nginx.conf.sample', $sampleNginx, true);
101147
}
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+
}
102239
}

src/Config/Github.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ public static function load()
1717
return [
1818
'user' => [
1919
'auth_url' => 'https://api.github.com/user'
20-
]
20+
],
21+
'magento'=> [
22+
'Open Source' => 'https://github.com/magento/magento2/'
23+
],
2124
];
2225
}
2326
}

0 commit comments

Comments
 (0)