Skip to content

Commit

Permalink
Update third party initializer provider to read composer.json values …
Browse files Browse the repository at this point in the history
…intead of parsing entire vendor directory (#276)
  • Loading branch information
cspray authored Feb 11, 2023
1 parent d002a86 commit f4a7e1c
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 113 deletions.
4 changes: 2 additions & 2 deletions bin/annotated-container
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<?php declare(strict_types=1);

use Cspray\AnnotatedContainer\Bootstrap\RootDirectoryBootstrappingDirectoryResolver;
use Cspray\AnnotatedContainer\Bootstrap\VendorScanningThirdPartyInitializerProvider;
use Cspray\AnnotatedContainer\Bootstrap\ComposerJsonScanningThirdPartyInitializerProvider;
use Cspray\AnnotatedContainer\Cli\Command\BuildCommand;
use Cspray\AnnotatedContainer\Cli\Command\CacheClearCommand;
use Cspray\AnnotatedContainer\Cli\Command\HelpCommand;
Expand All @@ -23,7 +23,7 @@ $directoryResolver = new RootDirectoryBootstrappingDirectoryResolver($dir);
$commandExecutor = new CommandExecutor();

$commandExecutor->setDefaultCommand(new HelpCommand($commandExecutor));
$commandExecutor->addCommand(new InitCommand($directoryResolver, new VendorScanningThirdPartyInitializerProvider($directoryResolver)));
$commandExecutor->addCommand(new InitCommand($directoryResolver, new ComposerJsonScanningThirdPartyInitializerProvider($directoryResolver)));
$commandExecutor->addCommand(new BuildCommand($directoryResolver));
$commandExecutor->addCommand(new CacheClearCommand($directoryResolver));

Expand Down
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"
]
}
}
}
27 changes: 18 additions & 9 deletions known-issues.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69">
<file src="src/Bootstrap/ComposerJsonScanningThirdPartyInitializerProvider.php">
<MixedArrayAccess occurrences="1">
<code>$composerData['extra']</code>
</MixedArrayAccess>
<MixedAssignment occurrences="4">
<code>$composerData</code>
<code>$initializers[]</code>
<code>$packageInitializer</code>
<code>$packageInitializers</code>
</MixedAssignment>
<MixedReturnTypeCoercion occurrences="2">
<code>$initializers</code>
<code>list&lt;class-string&lt;ThirdPartyInitializer&gt;&gt;</code>
</MixedReturnTypeCoercion>
<UndefinedMethod occurrences="1">
<code>isDot</code>
</UndefinedMethod>
</file>
<file src="src/Bootstrap/ServiceWiringObserver.php">
<LessSpecificReturnStatement occurrences="1">
<code>$services</code>
Expand All @@ -8,11 +26,6 @@
<code>array</code>
</MoreSpecificReturnType>
</file>
<file src="src/Bootstrap/VendorScanningThirdPartyInitializerProvider.php">
<MixedArrayAssignment occurrences="1">
<code>$data-&gt;targets[]</code>
</MixedArrayAssignment>
</file>
<file src="src/Cli/Command/BuildCommand.php">
<MixedArrayAccess occurrences="1">
<code>$composer['extra']['annotatedContainer']['configFile']</code>
Expand Down Expand Up @@ -88,10 +101,6 @@
<MixedArgumentTypeCoercion occurrences="1">
<code>$activeProfiles</code>
</MixedArgumentTypeCoercion>
<MixedReturnTypeCoercion occurrences="2">
<code>$this-&gt;profiles</code>
<code>array</code>
</MixedReturnTypeCoercion>
</file>
<file src="src/ContainerFactory/AurynContainerFactory.php">
<MixedArgument occurrences="7">
Expand Down
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 src/Bootstrap/VendorScanningThirdPartyInitializerProvider.php

This file was deleted.

6 changes: 3 additions & 3 deletions test/Unit/Cli/Command/InitCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Cspray\AnnotatedContainer\Unit\Cli\Command;

use Cspray\AnnotatedContainer\Bootstrap\VendorScanningThirdPartyInitializerProvider;
use Cspray\AnnotatedContainer\Bootstrap\ComposerJsonScanningThirdPartyInitializerProvider;
use Cspray\AnnotatedContainer\Cli\Command\InitCommand;
use Cspray\AnnotatedContainer\Cli\Exception\ComposerConfigurationNotFound;
use Cspray\AnnotatedContainer\Cli\Exception\InvalidOptionType;
Expand Down Expand Up @@ -33,7 +33,7 @@ protected function setUp() : void {
VirtualFilesystem::newDirectory('vendor')->at($this->vfs);
$this->subject = new InitCommand(
$resolver = new FixtureBootstrappingDirectoryResolver(),
new VendorScanningThirdPartyInitializerProvider($resolver)
new ComposerJsonScanningThirdPartyInitializerProvider($resolver)
);
$this->stdout = new InMemoryOutput();
$this->stderr = new InMemoryOutput();
Expand Down Expand Up @@ -266,7 +266,7 @@ public function testInitDefaultFileComposerJsonPresentCreatesConfigurationFile()
$input = new StubInput([], ['init']);
$subject = new InitCommand(
$resolver = new FixtureBootstrappingDirectoryResolver(true),
new VendorScanningThirdPartyInitializerProvider($resolver)
new ComposerJsonScanningThirdPartyInitializerProvider($resolver)
);
$exitCode = $subject->handle($input, $this->output);

Expand Down
4 changes: 2 additions & 2 deletions test/Unit/VendorScanningThirdPartyInitializerProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Cspray\AnnotatedContainer\Unit;

use Cspray\AnnotatedContainer\Bootstrap\RootDirectoryBootstrappingDirectoryResolver;
use Cspray\AnnotatedContainer\Bootstrap\VendorScanningThirdPartyInitializerProvider;
use Cspray\AnnotatedContainer\Bootstrap\ComposerJsonScanningThirdPartyInitializerProvider;
use Cspray\AnnotatedContainerFixture\VendorScanningInitializers\FirstInitializer;
use Cspray\AnnotatedContainerFixture\VendorScanningInitializers\SecondInitializer;
use Cspray\AnnotatedContainerFixture\VendorScanningInitializers\ThirdInitializer;
Expand All @@ -15,7 +15,7 @@ public function testVendorScanningProviderIncludesCorrectClasses() : void {
$directoryResolver = new RootDirectoryBootstrappingDirectoryResolver(
__DIR__ . '/../../fixture_src/VendorScanningInitializers'
);
$subject = new VendorScanningThirdPartyInitializerProvider($directoryResolver);
$subject = new ComposerJsonScanningThirdPartyInitializerProvider($directoryResolver);


self::assertSame([
Expand Down

0 comments on commit f4a7e1c

Please sign in to comment.