Skip to content

Commit 4908784

Browse files
bug #769 Fix looking for keywords (nicolas-grekas)
This PR was merged into the 1.12-dev branch. Discussion ---------- Fix looking for keywords Fix #768 Commits ------- 9f47b85 Fix looking for keywords
2 parents 8798024 + 9f47b85 commit 4908784

File tree

7 files changed

+42
-36
lines changed

7 files changed

+42
-36
lines changed

src/Command/GenerateIdCommand.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818

1919
class GenerateIdCommand extends Command
2020
{
21-
public function __construct(/* cannot be type-hinted */ $flex = null)
21+
public function __construct()
2222
{
2323
// No-op to support downgrading to v1.12.x
24-
2524
parent::__construct();
2625
}
2726

src/Flex.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,9 @@ private function synchronizePackageJson(string $rootDir)
593593
$synchronizer = new PackageJsonSynchronizer($rootDir, $vendorDir);
594594

595595
if ($synchronizer->shouldSynchronize()) {
596-
$packagesNames = array_column($this->composer->getLocker()->getLockData()['packages'] ?? [], 'name');
596+
$lockData = $this->composer->getLocker()->getLockData();
597597

598-
if ($synchronizer->synchronize($packagesNames)) {
598+
if ($synchronizer->synchronize($lockData['packages'] ?? []) || $synchronizer->synchronize($lockData['packages-dev'] ?? [])) {
599599
$this->io->writeError('<info>Synchronizing package.json with PHP packages</>');
600600
$this->io->writeError('<warning>Don\'t forget to run npm install --force or yarn install --force to refresh your JavaScript dependencies!</>');
601601
$this->io->writeError('');
@@ -774,8 +774,8 @@ public function updateAutoloadFile()
774774
file_put_contents($autoloadFile, <<<EOPHP
775775
<?php
776776
777-
if (\PHP_VERSION_ID < $version) {
778-
echo sprintf("Fatal Error: composer.lock was created for PHP version $platform or higher but the current PHP version is %d.%d.%d.\\n", PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION);
777+
if (PHP_VERSION_ID < $version) {
778+
echo sprintf("Fatal Error: composer.lock was created for PHP version $platform or higher but the current PHP version is %d.%d.%s.\\n", PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION);
779779
exit(1);
780780
}
781781
$code

src/InformationOperation.php

-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ public function getPackage()
2727
return $this->package;
2828
}
2929

30-
/**
31-
* {@inheritdoc}
32-
*/
3330
public function getJobType()
3431
{
3532
return 'information';

src/PackageJsonSynchronizer.php

+15-14
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ public function shouldSynchronize(): bool
3737
return $this->rootDir && file_exists($this->rootDir.'/package.json');
3838
}
3939

40-
public function synchronize(array $packagesNames): bool
40+
public function synchronize(array $phpPackages): bool
4141
{
4242
// Remove all links and add again only the existing packages
4343
$didAddLink = $this->removePackageJsonLinks((new JsonFile($this->rootDir.'/package.json'))->read());
4444

45-
foreach ($packagesNames as $packageName) {
46-
$didAddLink = $this->addPackageJsonLink($packageName) || $didAddLink;
45+
foreach ($phpPackages as $phpPackage) {
46+
$didAddLink = $this->addPackageJsonLink($phpPackage) || $didAddLink;
4747
}
4848

49-
$this->registerPeerDependencies($packagesNames);
49+
$this->registerPeerDependencies($phpPackages);
5050

5151
// Register controllers and entrypoints in controllers.json
52-
$this->registerWebpackResources($packagesNames);
52+
$this->registerWebpackResources($phpPackages);
5353

5454
return $didAddLink;
5555
}
@@ -76,14 +76,14 @@ private function removePackageJsonLinks(array $packageJson): bool
7676
return $didRemoveLink;
7777
}
7878

79-
private function addPackageJsonLink(string $phpPackage): bool
79+
private function addPackageJsonLink(array $phpPackage): bool
8080
{
8181
if (!$packageJson = $this->resolvePackageJson($phpPackage)) {
8282
return false;
8383
}
8484

8585
$manipulator = new JsonManipulator(file_get_contents($this->rootDir.'/package.json'));
86-
$manipulator->addSubNode('devDependencies', '@'.$phpPackage, 'file:'.substr($packageJson->getPath(), 1 + \strlen($this->rootDir), -13));
86+
$manipulator->addSubNode('devDependencies', '@'.$phpPackage['name'], 'file:'.substr($packageJson->getPath(), 1 + \strlen($this->rootDir), -13));
8787

8888
$content = json_decode($manipulator->getContents(), true);
8989

@@ -112,10 +112,11 @@ private function registerWebpackResources(array $phpPackages)
112112
if (!$packageJson = $this->resolvePackageJson($phpPackage)) {
113113
continue;
114114
}
115+
$name = '@'.$phpPackage['name'];
115116

116117
foreach ($packageJson->read()['symfony']['controllers'] ?? [] as $controllerName => $defaultConfig) {
117118
// If the package has just been added (no config), add the default config provided by the package
118-
if (!isset($previousControllersJson['controllers']['@'.$phpPackage][$controllerName])) {
119+
if (!isset($previousControllersJson['controllers'][$name][$controllerName])) {
119120
$config = [];
120121
$config['enabled'] = $defaultConfig['enabled'];
121122
$config['fetch'] = $defaultConfig['fetch'] ?? 'eager';
@@ -124,13 +125,13 @@ private function registerWebpackResources(array $phpPackages)
124125
$config['autoimport'] = $defaultConfig['autoimport'];
125126
}
126127

127-
$newControllersJson['controllers']['@'.$phpPackage][$controllerName] = $config;
128+
$newControllersJson['controllers'][$name][$controllerName] = $config;
128129

129130
continue;
130131
}
131132

132133
// Otherwise, the package exists: merge new config with uer config
133-
$previousConfig = $previousControllersJson['controllers']['@'.$phpPackage][$controllerName];
134+
$previousConfig = $previousControllersJson['controllers'][$name][$controllerName];
134135

135136
$config = [];
136137
$config['enabled'] = $previousConfig['enabled'];
@@ -145,7 +146,7 @@ private function registerWebpackResources(array $phpPackages)
145146
}
146147
}
147148

148-
$newControllersJson['controllers']['@'.$phpPackage][$controllerName] = $config;
149+
$newControllersJson['controllers'][$name][$controllerName] = $config;
149150
}
150151

151152
foreach ($packageJson->read()['symfony']['entrypoints'] ?? [] as $entrypoint => $filename) {
@@ -191,11 +192,11 @@ public function registerPeerDependencies(array $phpPackages)
191192
file_put_contents($this->rootDir.'/package.json', $manipulator->getContents());
192193
}
193194

194-
private function resolvePackageJson(string $phpPackage): ?JsonFile
195+
private function resolvePackageJson(array $phpPackage): ?JsonFile
195196
{
196-
$packageDir = $this->rootDir.'/'.$this->vendorDirname.'/'.$phpPackage;
197+
$packageDir = $this->rootDir.'/'.$this->vendorDirname.'/'.$phpPackage['name'];
197198

198-
if (!\in_array('symfony-ux', json_decode(file_get_contents($packageDir.'/composer.json'), true)['keywords'] ?? [], true)) {
199+
if (!\in_array('symfony-ux', $phpPackage['keywords'] ?? [], true)) {
199200
return null;
200201
}
201202

tests/Fixtures/packageJson/vendor/symfony/existing-package/composer.json

-5
This file was deleted.

tests/Fixtures/packageJson/vendor/symfony/new-package/composer.json

-5
This file was deleted.

tests/PackageJsonSynchronizerTest.php

+22-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ public function testSynchronizeNoPackage()
6464

6565
public function testSynchronizeExistingPackage()
6666
{
67-
$this->synchronizer->synchronize(['symfony/existing-package']);
67+
$this->synchronizer->synchronize([
68+
[
69+
'name' => 'symfony/existing-package',
70+
'keywords' => ['symfony-ux'],
71+
],
72+
]);
6873

6974
// Should keep existing package references and config
7075
$this->assertSame(
@@ -107,7 +112,16 @@ public function testSynchronizeExistingPackage()
107112

108113
public function testSynchronizeNewPackage()
109114
{
110-
$this->synchronizer->synchronize(['symfony/existing-package', 'symfony/new-package']);
115+
$this->synchronizer->synchronize([
116+
[
117+
'name' => 'symfony/existing-package',
118+
'keywords' => ['symfony-ux'],
119+
],
120+
[
121+
'name' => 'symfony/new-package',
122+
'keywords' => ['symfony-ux'],
123+
],
124+
]);
111125

112126
// Should keep existing package references and config and add the new package, while keeping the formatting
113127
$this->assertSame(
@@ -159,7 +173,12 @@ public function testSynchronizeNewPackage()
159173

160174
public function testArrayFormattingHasNotChanged()
161175
{
162-
$this->synchronizer->synchronize(['symfony/existing-package']);
176+
$this->synchronizer->synchronize([
177+
[
178+
'name' => 'symfony/existing-package',
179+
'keywords' => ['symfony-ux'],
180+
],
181+
]);
163182

164183
// Should keep existing array formatting
165184
$this->assertSame(

0 commit comments

Comments
 (0)