-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update third party initializer provider to read composer.json values …
…intead of parsing entire vendor directory (#276)
- Loading branch information
Showing
7 changed files
with
106 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
fixture_src/VendorScanningInitializers/vendor/cspray/package/composer.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "cspray/package", | ||
"extra": { | ||
"$annotatedContainer": { | ||
"initializers": [ | ||
"Cspray\\AnnotatedContainerFixture\\VendorScanningInitializers\\FirstInitializer", | ||
"Cspray\\AnnotatedContainerFixture\\VendorScanningInitializers\\SecondInitializer", | ||
"Cspray\\AnnotatedContainerFixture\\VendorScanningInitializers\\ThirdInitializer" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/Bootstrap/ComposerJsonScanningThirdPartyInitializerProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Cspray\AnnotatedContainer\Bootstrap; | ||
|
||
use DirectoryIterator; | ||
use SplFileInfo; | ||
|
||
final class ComposerJsonScanningThirdPartyInitializerProvider implements ThirdPartyInitializerProvider { | ||
|
||
/** | ||
* @var list<class-string<ThirdPartyInitializer>>|null | ||
*/ | ||
private ?array $initializers = null; | ||
|
||
public function __construct( | ||
private readonly BootstrappingDirectoryResolver $resolver | ||
) { | ||
} | ||
|
||
public function getThirdPartyInitializers() : array { | ||
if ($this->initializers === null) { | ||
$this->initializers = $this->scanVendorDirectoryForInitializers(); | ||
sort($this->initializers); | ||
} | ||
|
||
return $this->initializers; | ||
} | ||
|
||
/** | ||
* @return list<class-string<ThirdPartyInitializer>> | ||
*/ | ||
private function scanVendorDirectoryForInitializers() : array { | ||
$packages = []; | ||
$vendorIterator = new DirectoryIterator($this->resolver->getVendorPath()); | ||
/** @var SplFileInfo $fileInfo */ | ||
foreach ($vendorIterator as $fileInfo) { | ||
if (!$fileInfo->isDir() || $fileInfo->isDot()) { | ||
continue; | ||
} | ||
|
||
$vendorName = basename($fileInfo->getPathname()); | ||
foreach (new DirectoryIterator($fileInfo->getPathname()) as $vendorPackageInfo) { | ||
if (!$vendorPackageInfo->isDir() || $vendorPackageInfo->isDot()) { | ||
continue; | ||
} | ||
$packages[] = sprintf('%s/%s', $vendorName, basename($vendorPackageInfo->getPathname())); | ||
} | ||
|
||
} | ||
|
||
$initializers = []; | ||
foreach ($packages as $package) { | ||
$packageComposerJson = sprintf('%s/%s/composer.json', $this->resolver->getVendorPath(), $package); | ||
$composerData = json_decode( | ||
file_get_contents($packageComposerJson), | ||
true, | ||
512, | ||
JSON_THROW_ON_ERROR | ||
); | ||
|
||
$packageInitializers = $composerData['extra']['$annotatedContainer']['initializers'] ?? []; | ||
foreach ($packageInitializers as $packageInitializer) { | ||
$initializers[] = $packageInitializer; | ||
} | ||
} | ||
return $initializers; | ||
} | ||
|
||
} |
97 changes: 0 additions & 97 deletions
97
src/Bootstrap/VendorScanningThirdPartyInitializerProvider.php
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters