Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

This library allows to easily detect the PHP stack (Wordpress, Laravel, Symfony…) and the version used, when parsing a directory or ar Github remote repository.

The library is still in an early stage and subject to changes, but it follows SemVer. As long as you stick to a major version, you should be safe.

Supported Stacks for now:

- Bolt CMS
Expand Down
2 changes: 2 additions & 0 deletions lib/DTO/NodeConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public function __construct(
public ?string $version,
public ?string $requirements,
public NodePackageManagerType $packageManager,
/** @var array<string, mixed> */
public array $packageJsonContent,
) {
}
}
6 changes: 5 additions & 1 deletion lib/DTO/PhpConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
{
public function __construct(
public ?PhpVersion $phpVersion,
/** @var string[] $requiredExtensions */
/** @var string[] */
public array $requiredExtensions = [],
/** @var array<string, mixed>|null */
public ?array $composerJsonContent = null,
/** @var array<string, mixed>|null */
public ?array $composerLockContent = null,
) {
}
}
1 change: 1 addition & 0 deletions lib/NodeConfigurationDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function getNodeConfiguration(
$this->getNodeVersion($baseUri, $subFolder),
$this->getVersionRequirements($baseUri, $subFolder),
$this->getPackageManagerType($baseUri, $subFolder),
$packageJsonConfig,
);
}

Expand Down
23 changes: 17 additions & 6 deletions lib/PhpConfigurationDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,31 @@ public function __construct(private ComposerConfigProvider $composerConfigProvid

public function getPhpConfiguration(string $baseUri, ?string $subFolder = null): PhpConfiguration
{
$composerConfig = $this->composerConfigProvider->getComposerConfig(
$composerJsonConfig = $this->composerConfigProvider->getComposerConfig(
ComposerConfigType::JSON,
$baseUri,
$subFolder
);

if (null === $composerConfig) {
return new PhpConfiguration(null, []);
if (null === $composerJsonConfig) {
return new PhpConfiguration(null, [], null, null);
}

$phpVersion = $this->getPhpVersion($composerConfig);
$requiredExtensions = $this->getRequiredExtensions($composerConfig);
$phpVersion = $this->getPhpVersion($composerJsonConfig);
$requiredExtensions = $this->getRequiredExtensions($composerJsonConfig);

return new PhpConfiguration($phpVersion, $requiredExtensions);
$composerLockConfig = $this->composerConfigProvider->getComposerConfig(
ComposerConfigType::LOCK,
$baseUri,
$subFolder
);

return new PhpConfiguration(
$phpVersion,
$requiredExtensions,
$composerJsonConfig->content,
$composerLockConfig?->content
);
}

private function getPhpVersion(ComposerConfig $config): ?PhpVersion
Expand Down
117 changes: 117 additions & 0 deletions tests/Unit/DetectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,34 @@ public function it_detects_no_node_configuration(): void
$this->assertNull($fullConfig->nodeConfiguration);
}

/** @test */
public function it_returns_package_json_content_if_found()
{
$fullConfig = $this->sut->getFullConfiguration(
sprintf('%s/../fixtures/%s', __DIR__, 'node-configuration/full-package-json')
);

$this->assertNotNull($fullConfig);
$this->assertIsArray($fullConfig->nodeConfiguration->packageJsonContent);
$this->assertCount(6, array_keys($fullConfig->nodeConfiguration->packageJsonContent));
$this->assertSame(
[
'private',
'type',
'scripts',
'dependencies',
'optionalDependencies',
'devDependencies',
],
array_keys($fullConfig->nodeConfiguration->packageJsonContent)
);

$this->assertCount(
8,
$fullConfig->nodeConfiguration->packageJsonContent['dependencies']
);
}

/**
* @test
*/
Expand Down Expand Up @@ -334,6 +362,95 @@ public function it_detects_no_extension_if_not_in_require()
$this->assertSame([], $fullConfig->phpConfiguration->requiredExtensions);
}

/** @test */
public function it_returns_null_if_no_composer_json_found()
{
$fullConfig = $this->sut->getFullConfiguration(
sprintf('%s/../fixtures/%s', __DIR__, 'php-config/detector/no-composer-config')
);

$this->assertNotNull($fullConfig);
$this->assertNotNull($fullConfig->phpConfiguration);
$this->assertNull($fullConfig->phpConfiguration->composerJsonContent);
}

/** @test */
public function it_returns_composer_json_content_if_found()
{
$fullConfig = $this->sut->getFullConfiguration(
sprintf('%s/../fixtures/%s', __DIR__, 'composer-config/composer-json')
);

$this->assertNotNull($fullConfig);
$this->assertNotNull($fullConfig->phpConfiguration);
$this->assertIsArray($fullConfig->phpConfiguration->composerJsonContent);
$this->assertCount(13, array_keys($fullConfig->phpConfiguration->composerJsonContent));
$this->assertSame(
[
'type',
'license',
'minimum-stability',
'prefer-stable',
'require',
'require-dev',
'config',
'autoload',
'autoload-dev',
'replace',
'scripts',
'conflict',
'extra',
],
array_keys($fullConfig->phpConfiguration->composerJsonContent)
);
}

/** @test */
public function it_returns_null_if_no_composer_lock_found()
{
$fullConfig = $this->sut->getFullConfiguration(
sprintf('%s/../fixtures/%s', __DIR__, 'php-config/detector/no-composer-config')
);

$this->assertNotNull($fullConfig);
$this->assertNotNull($fullConfig->phpConfiguration);
$this->assertNull($fullConfig->phpConfiguration->composerLockContent);
}

/** @test */
public function it_returns_composer_lock_content_if_both_composer_config_files_are_found()
{
$fullConfig = $this->sut->getFullConfiguration(
sprintf('%s/../fixtures/%s', __DIR__, 'composer-config/composer-both')
);

$this->assertNotNull($fullConfig);
$this->assertNotNull($fullConfig->phpConfiguration);
$this->assertIsArray($fullConfig->phpConfiguration->composerLockContent);
$this->assertCount(11, array_keys($fullConfig->phpConfiguration->composerLockContent));
$this->assertSame(
[
'_readme',
'content-hash',
'packages',
'aliases',
'minimum-stability',
'stability-flags',
'prefer-stable',
'prefer-lowest',
'platform',
'platform-dev',
'plugin-api-version',
],
array_keys($fullConfig->phpConfiguration->composerLockContent)
);

$this->assertCount(
3,
$fullConfig->phpConfiguration->composerLockContent['packages']
);
}

public static function packagesDataProvider(): array
{
return [
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/composer-config/composer-both/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "test/package"
}
Loading
Loading