8000 [AssetMapper] Make importmap:install command download the same version · symfony/symfony@8db0a73 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8db0a73

Browse files
jmschefabpot
authored andcommitted
[AssetMapper] Make importmap:install command download the same version
1 parent 11adee9 commit 8db0a73

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/asset_mapper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
abstract_arg('importmap.php path'),
144144
abstract_arg('vendor directory'),
145145
service('asset_mapper.importmap.resolver'),
146+
service('http_client'),
146147
])
147148
->alias(ImportMapManager::class, 'asset_mapper.importmap.manager')
148149

src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313

1414
use Symfony\Component\AssetMapper\AssetDependency;
1515
use Symfony\Component\AssetMapper\AssetMapperInterface;
16-
use Symfony\Component\AssetMapper\Exception\RuntimeException;
1716
use Symfony\Component\AssetMapper\ImportMap\Resolver\PackageResolverInterface;
1817
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;
18+
use Symfony\Component\HttpClient\HttpClient;
1919
use Symfony\Component\VarExporter\VarExporter;
20+
use Symfony\Contracts\HttpClient\HttpClientInterface;
2021

2122
/**
2223
* @author Kévin Dunglas <kevin@dunglas.dev>
@@ -55,14 +56,17 @@ class ImportMapManager
5556
private array $importMapEntries;
5657
private array $modulesToPreload;
5758
private string $json;
59+
private readonly HttpClientInterface $httpClient;
5860

5961
public function __construct(
6062
private readonly AssetMapperInterface $assetMapper,
6163
private readonly PublicAssetsPathResolverInterface $assetsPathResolver,
6264
private readonly string $importMapConfigPath,
6365
private readonly string $vendorDir,
6466
private readonly PackageResolverInterface $resolver,
67+
HttpClientInterface $httpClient = null,
6568
) {
69+
$this->httpClient = $httpClient ?? HttpClient::create();
6670
}
6771

6872
public function getModulesToPreload(): array
@@ -112,31 +116,27 @@ public function update(): array
112116
/**
113117
* Downloads all missing downloaded packages.
114118
*
115-
* @return ImportMapEntry[] The downloaded packages
119+
* @return string[] The downloaded packages
116120
*/
117121
public function downloadMissingPackages(): array
118122
{
119123
$entries = $this->loadImportMapEntries();
120-
$packagesToDownload = [];
124+
$downloadedPackages = [];
121125

122126
foreach ($entries as $entry) {
123127
if (!$entry->isDownloaded || $this->assetMapper->getAsset($entry->path)) {
124128
continue;
125129
}
126130

127-
$parts = self::parsePackageName($entry->url);
128-
129-
$packagesToDownload[] = new PackageRequireOptions(
130-
$parts['package'],
131-
$parts['version'] ?? throw new RuntimeException(sprintf('Cannot get a version for the "%s" package.', $parts['package'])),
132-
true,
133-
$entry->preload,
134-
$parts['alias'] ?? $parts['package'],
135-
isset($parts['registry']) && $parts['registry'] ? $parts['registry'] : null,
131+
$this->downloadPackage(
132+
$entry->importName,
133+
$this->httpClient->request('GET', $entry->url)->getContent(),
136134
);
135+
136+
$downloadedPackages[] = $entry->importName;
137137
}
138138

139-
return $this->require($packagesToDownload);
139+
return $downloadedPackages;
140140
}
141141

142142
/**

src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapManagerTest.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@
2626
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolver;
2727
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;
2828
use Symfony\Component\Filesystem\Filesystem;
29+
use Symfony\Contracts\HttpClient\HttpClientInterface;
30+
use Symfony\Contracts\HttpClient\ResponseInterface;
2931

3032
class ImportMapManagerTest extends TestCase
3133
{
3234
private Filesystem $filesystem;
3335
private AssetMapperInterface $assetMapper;
3436
private PackageResolverInterface&MockObject $packageResolver;
37+
private HttpClientInterface&MockObject $httpClient;
3538

3639
protected function setUp(): void
3740
{
@@ -378,12 +381,14 @@ public function testDownloadMissingPackages()
378381
$rootDir = __DIR__.'/../fixtures/download';
379382
$manager = $this->createImportMapManager(['assets' => ''], $rootDir);
380383

381-
$this->packageResolver->expects($this->once())
382-
->method('resolvePackages')
383-
->willReturn([
384-
self::resolvedPackage('@hotwired/stimulus', 'https://cdn.jsdelivr.net/npm/stimulus@3.2.1/+esm', true, content: 'contents of stimulus.js'),
385-
])
386-
;
384+
$response = $this->createMock(ResponseInterface::class);
385+
$response->expects($this->once())
386+
->method('getContent')
387+
->willReturn('contents of stimulus.js');
388+
389+
$this->httpClient->expects($this->once())
390+
->method('request')
391+
->willReturn($response);
387392

388393
$downloadedPackages = $manager->downloadMissingPackages();
389394
$actualImportMap = require $rootDir.'/importmap.php';
@@ -506,13 +511,15 @@ private function createImportMapManager(array $dirs, string $rootDir, string $pu
506511

507512
$mapper = $this->createAssetMapper($pathResolver, $dirs, $rootDir);
508513
$this->packageResolver = $this->createMock(PackageResolverInterface::class);
514+
$this->httpClient = $this->createMock(HttpClientInterface::class);
509515

510516
return new ImportMapManager(
511517
$mapper,
512518
$pathResolver,
513519
$rootDir.'/importmap.php',
514520
$rootDir.'/assets/vendor',
515521
$this->packageResolver,
522+
$this->httpClient,
516523
);
517524
}
518525

0 commit comments

Comments
 (0)
0