Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7fa1707

Browse files
committed
refactoring
1 parent 8660b70 commit 7fa1707

File tree

10 files changed

+92
-59
lines changed

10 files changed

+92
-59
lines changed

src/Symfony/Component/ImportMaps/Command/AbstractCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Symfony\Component\ImportMaps\Provider;
1919

2020
/**
21+
* @experimental
22+
*
2123
* @author Kévin Dunglas <[email protected]>
2224
*/
2325
abstract class AbstractCommand extends Command

src/Symfony/Component/ImportMaps/Command/ExportCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Symfony\Component\ImportMaps\ImportMapManager;
1919

2020
/**
21+
* @experimental
22+
*
2123
* @author Kévin Dunglas <[email protected]>
2224
*/
2325
#[AsCommand(name: 'importmap:export', description: 'Exports the importmap JSON')]

src/Symfony/Component/ImportMaps/Command/RemoveCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use Symfony\Component\ImportMaps\Provider;
2121

2222
/**
23+
* @experimental
24+
*
2325
* @author Kévin Dunglas <[email protected]>
2426
*/
2527
#[AsCommand(name: 'importmap:remove', description: 'Removes JavaScript packages')]

src/Symfony/Component/ImportMaps/Command/RequireCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Symfony\Component\ImportMaps\Provider;
2323

2424
/**
25+
* @experimental
26+
*
2527
* @author Kévin Dunglas <[email protected]>
2628
*/
2729
#[AsCommand(name: 'importmap:require', description: 'Requires JavaScript packages')]

src/Symfony/Component/ImportMaps/Command/UpdateCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
use Symfony\Component\ImportMaps\Provider;
2020

2121
/**
22+
* @experimental
23+
*
2224
* @author Kévin Dunglas <[email protected]>
2325
*/
2426
#[AsCommand(name: 'importmap:update', description: 'Updates all JavaScript packages to their latest versions')]

src/Symfony/Component/ImportMaps/Env.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\ImportMaps;
1313

1414
/**
15+
* @experimental
16+
*
1517
* @author Kévin Dunglas <[email protected]>
1618
*/
1719
enum Env: string

src/Symfony/Component/ImportMaps/ImportMapManager.php

Lines changed: 75 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Symfony\Contracts\HttpClient\HttpClientInterface;
1919

2020
/**
21+
* @experimental
22+
*
2123
* @author Kévin Dunglas <[email protected]>
2224
*/
2325
final class ImportMapManager
@@ -60,8 +62,9 @@ public function getImportMap(): string
6062
{
6163
$this->loadImportMap();
6264

65+
$importmap = ['imports' => []];
6366
foreach ($this->importMap as $package => $data) {
64-
$importmap['imports'][$package] = isset($data['local']) ? $this->vendorUrl.$data['local'] : $data['url'];
67+
$importmap['imports'][$package] = isset($data['digest']) ? $this->vendorUrl.$data['digest'] : $data['url'];
6568
}
6669

6770
// Use JSON_UNESCAPED_SLASHES | JSON_HEX_TAG to prevent XSS
@@ -103,25 +106,12 @@ public function update(Env $env = Env::Production, ?Provider $provider = null):
103106
private function createImportMap(Env $env, ?Provider $provider, bool $update, array $require, array $remove): void
104107
{
105108
$this->loadImportMap();
109+
$this->removeFromImportMap($remove);
106110

107111
$install = [];
108-
109-
foreach ($remove as $packageName) {
110-
if (!isset($this->importMap[$packageName])) {
111-
continue;
112-
}
113-
114-
$localPath = $this->vendorDir.$this->importMap[$packageName]['local'];
115-
if ($this->filesystem->exists($localPath)) {
116-
$this->filesystem->remove($localPath);
117-
}
118-
119-
unset($this->importMap[$packageName]);
120-
}
121-
122112
$packages = [];
123113
foreach ($this->importMap ?? [] as $name => $data) {
124-
$packages[$name] = new PackageOptions((bool) ($data['local'] ?? false), $data['preload'] ?? false);
114+
$packages[$name] = new PackageOptions((bool) ($data['digest'] ?? false), $data['preload'] ?? false);
125115

126116
if (preg_match(self::PACKAGE_PATTERN, $data['url'], $matches)) {
127117
$constraint = ($matches['registry'] ?? null) ? "{$matches['registry']}:{$matches['package']}" : $matches['package'];
@@ -141,48 +131,79 @@ private function createImportMap(Env $env, ?Provider $provider, bool $update, ar
141131
}
142132
}
143133

144-
if ($install) {
145-
$json = [
146-
'install' => array_values($install),
147-
'flattenScope' => true,
148-
'provider' => $provider?->value ?? $this->provider->value,
149-
];
150-
151-
$json['env'] = ['browser', 'module', $env->value];
152-
153-
$response = $this->apiHttpClient->request('POST', '/generate', [
154-
'json' => $json,
155-
]);
156-
157-
$this->filesystem->mkdir($this->vendorDir);
158-
foreach ($response->toArray()['map']['imports'] as $packageName => $url) {
159-
$previousPackageData = $this->importMap[$packageName] ?? null;
160-
$this->importMap[$packageName] = ['url' => $url];
161-
162-
if ($packages[$packageName]->download) {
163-
$this->importMap[$packageName]['local'] = sprintf('%s-%s.js', rawurlencode($packageName), hash('xxh128', $url));
164-
165-
if (!isset($previousPackageData['local']) || $this->importMap[$packageName]['local'] !== $previousPackageData['local']) {
166-
if (isset($previousPackageData['local']) && $this->filesystem->exists($this->vendorDir.$previousPackageData['local'])) {
167-
$this->filesystem->remove($this->vendorDir.$previousPackageData['local']);
168-
}
169-
170-
$this->filesystem->dumpFile(
171-
$this->vendorDir.$this->importMap[$packageName]['local'],
172-
$this->httpClient->request('GET', $url)->getContent()
173-
);
174-
}
175-
}
176-
177-
if ($packages[$packageName]->preload) {
178-
$this->importMap[$packageName]['preload'] = true;
179-
}
180-
}
181-
}
134+
$this->jspmGenerate($env, $provider, $install, $packages);
182135

183136
$this->filesystem->dumpFile(
184137
$this->path,
185138
sprintf("<?php\n\nreturn %s;\n", VarExporter::export($this->importMap))
186139
);
187140
}
141+
142+
/**
143+
* @param string[] $remove
144+
*/
145+
private function removeFromImportMap(array $remove): void {
146+
foreach ($remove as $packageName) {
147+
if (!isset($this->importMap[$packageName])) {
148+
continue;
149+
}
150+
151+
$this->removeIfExists($this->vendorDir.$this->importMap[$packageName]['digest']);
152+
unset($this->importMap[$packageName]);
153+
}
154+
}
155+
156+
private function jspmGenerate(Env $env, ?Provider $provider, array $install, array $packages): void
157+
{
158+
if (!$install) {
159+
return;
160+
}
161+
162+
$json = [
163+
'install' => array_values($install),
164+
'flattenScope' => true,
165+
'provider' => $provider?->value ?? $this->provider->value,
166+
];
167+
168+
$json['env'] = ['browser', 'module', $env->value];
169+
170+
$response = $this->apiHttpClient->request('POST', '/generate', [
171+
'json' => $json,
172+
]);
173+
174+
$this->filesystem->mkdir($this->vendorDir);
175+
foreach ($response->toArray()['map']['imports'] as $packageName => $url) {
176+
$previousPackageData = $this->importMap[$packageName] ?? null;
177+
$this->importMap[$packageName] = ['url' => $url];
178+
179+
if ($packages[$packageName]->preload) {
180+
$this->importMap[$packageName]['preload'] = true;
181+
}
182+
183+
if (!$packages[$packageName]->download) {
184+
continue;
185+
}
186+
187+
$this->importMap[$packageName]['digest'] = sprintf('%s-%s.js', $packageName, hash('xxh128', $url));
188+
if ($this->importMap[$packageName]['digest'] === ($previousPackageData['digest'] ?? null)) {
189+
continue;
190+
}
191+
192+
if (isset($previousPackageData['digest'])) {
193+
$this->removeIfExists($this->vendorDir.$previousPackageData['digest']);
194+
}
195+
196+
$this->filesystem->dumpFile(
197+
$this->vendorDir.$this->importMap[$packageName]['digest'],
198+
$this->httpClient->request('GET', $url)->getContent(),
199+
);
200+
}
201+
}
202+
203+
private function removeIfExists(string $path): void
204+
{
205+
if ($this->filesystem->exists($path)) {
206+
$this->filesystem->remove($path);
207+
}
208+
}
188209
}

src/Symfony/Component/ImportMaps/PackageOptions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\ImportMaps;
1313

1414
/**
15+
* @experimental
16+
*
1517
* @author Kévin Dunglas <[email protected]>
1618
*/
1719
final class PackageOptions

src/Symfony/Component/ImportMaps/Provider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\ImportMaps;
1313

1414
/**
15+
* @experimental
16+
*
1517
* @author Kévin Dunglas <[email protected]>
1618
*/
1719
enum Provider: string

src/Symfony/Component/ImportMaps/Tests/App/importmap.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
return [
44
'@hotwired/stimulus' => [
55
'url' => 'https://ga.jspm.io/npm:@hotwired/[email protected]/dist/stimulus.js',
6-
'local' => '%40hotwired%2Fstimulus-997ad71d32657837726ccf3fe40b8b53.js',
7-
],
8-
'react' => [
9-
'url' => 'https://ga.jspm.io/npm:[email protected]/index.js',
10-
'local' => 'react-eb0995a457572b1c9bf63f3db91ddc58.js',
6+
'digest' => '@hotwired/stimulus-997ad71d32657837726ccf3fe40b8b53.js',
117
],
128
];

0 commit comments

Comments
 (0)