Skip to content

Commit 9a83cb1

Browse files
authored
Fix InstalledPackagesScoper with Composer v2 (#404)
1 parent a9f42a9 commit 9a83cb1

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed

src/Scoper/Composer/InstalledPackagesScoper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ public function scope(string $filePath, string $contents, string $prefix, array
4343

4444
$decodedJson = json_decode($contents, false);
4545

46-
$decodedJson = $this->prefixLockPackages((array) $decodedJson, $prefix, $whitelist);
46+
// compatibility with Composer 2
47+
if (isset($decodedJson->packages)) {
48+
$decodedJson->packages = $this->prefixLockPackages($decodedJson->packages, $prefix, $whitelist);
49+
} else {
50+
$decodedJson = $this->prefixLockPackages((array) $decodedJson, $prefix, $whitelist);
51+
}
4752

4853
return json_encode(
4954
$decodedJson,

tests/Scoper/Composer/InstalledPackagesScoperTest.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,132 @@ public function provideInstalledPackagesFiles(): Generator
325325
]
326326
}
327327
]
328+
JSON
329+
];
330+
yield [
331+
<<<'JSON'
332+
{
333+
"dev": true,
334+
"packages": [
335+
{
336+
"name": "beberlei/assert",
337+
"version": "v2.7.6",
338+
"version_normalized": "2.7.6.0",
339+
"source": {
340+
"type": "git",
341+
"url": "https://github.com/beberlei/assert.git",
342+
"reference": "8726e183ebbb0169cb6cb4832e22ebd355524563"
343+
},
344+
"dist": {
345+
"type": "zip",
346+
"url": "https://api.github.com/repos/beberlei/assert/zipball/8726e183ebbb0169cb6cb4832e22ebd355524563",
347+
"reference": "8726e183ebbb0169cb6cb4832e22ebd355524563",
348+
"shasum": ""
349+
},
350+
"require": {
351+
"ext-mbstring": "*",
352+
"php": ">=5.3"
353+
},
354+
"require-dev": {
355+
"friendsofphp/php-cs-fixer": "^2.1.1",
356+
"phpunit/phpunit": "^4|^5"
357+
},
358+
"time": "2017-05-04T02:00:24+00:00",
359+
"type": "library",
360+
"installation-source": "dist",
361+
"autoload": {
362+
"psr-4": {
363+
"Assert\\": "lib/Assert"
364+
},
365+
"files": [
366+
"lib/Assert/functions.php"
367+
]
368+
},
369+
"notification-url": "https://packagist.org/downloads/",
370+
"license": [
371+
"BSD-2-Clause"
372+
],
373+
"authors": [
374+
{
375+
"name": "Benjamin Eberlei",
376+
"email": "[email protected]",
377+
"role": "Lead Developer"
378+
},
379+
{
380+
"name": "Richard Quadling",
381+
"email": "[email protected]",
382+
"role": "Collaborator"
383+
}
384+
],
385+
"description": "Thin assertion library for input validation in business models.",
386+
"keywords": [],
387+
"platform": {}
388+
}
389+
]
390+
}
391+
392+
JSON
393+
,
394+
<<<'JSON'
395+
{
396+
"dev": true,
397+
"packages": [
398+
{
399+
"name": "beberlei\/assert",
400+
"version": "v2.7.6",
401+
"version_normalized": "2.7.6.0",
402+
"source": {
403+
"type": "git",
404+
"url": "https:\/\/github.com\/beberlei\/assert.git",
405+
"reference": "8726e183ebbb0169cb6cb4832e22ebd355524563"
406+
},
407+
"dist": {
408+
"type": "zip",
409+
"url": "https:\/\/api.github.com\/repos\/beberlei\/assert\/zipball\/8726e183ebbb0169cb6cb4832e22ebd355524563",
410+
"reference": "8726e183ebbb0169cb6cb4832e22ebd355524563",
411+
"shasum": ""
412+
},
413+
"require": {
414+
"ext-mbstring": "*",
415+
"php": ">=5.3"
416+
},
417+
"require-dev": {
418+
"friendsofphp\/php-cs-fixer": "^2.1.1",
419+
"phpunit\/phpunit": "^4|^5"
420+
},
421+
"time": "2017-05-04T02:00:24+00:00",
422+
"type": "library",
423+
"installation-source": "dist",
424+
"autoload": {
425+
"psr-4": {
426+
"Foo\\Assert\\": "lib\/Assert"
427+
},
428+
"files": [
429+
"lib\/Assert\/functions.php"
430+
]
431+
},
432+
"notification-url": "https:\/\/packagist.org\/downloads\/",
433+
"license": [
434+
"BSD-2-Clause"
435+
],
436+
"authors": [
437+
{
438+
"name": "Benjamin Eberlei",
439+
"email": "[email protected]",
440+
"role": "Lead Developer"
441+
},
442+
{
443+
"name": "Richard Quadling",
444+
"email": "[email protected]",
445+
"role": "Collaborator"
446+
}
447+
],
448+
"description": "Thin assertion library for input validation in business models.",
449+
"keywords": [],
450+
"platform": {}
451+
}
452+
]
453+
}
328454
JSON
329455
];
330456
}

0 commit comments

Comments
 (0)