-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Load data from composer.json if present
Load data like description, support links, composer name etc from the composer.json if one is found.
- Loading branch information
Showing
9 changed files
with
279 additions
and
3 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Guides\Event; | ||
|
||
use phpDocumentor\Guides\Settings\ProjectSettings; | ||
|
||
/** | ||
* This event is dispatched after the settings have been loaded but before they have | ||
* been overridden by input parameters of the CLI. | ||
*/ | ||
final class PostSettingsLoaded | ||
{ | ||
public function __construct(private readonly ProjectSettings $projectSettings) | ||
{ | ||
} | ||
|
||
public function getProjectSettings(): ProjectSettings | ||
{ | ||
return $this->projectSettings; | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Guides\Event; | ||
|
||
use phpDocumentor\Guides\Settings\ProjectSettings; | ||
|
||
/** | ||
* This event is dispatched after the settings have been overridden by input parameters of the CLI. | ||
* | ||
* This event can be used to determine or change the final project settings | ||
*/ | ||
final class PostSettingsOverridden | ||
{ | ||
public function __construct(private readonly ProjectSettings $projectSettings) | ||
{ | ||
} | ||
|
||
public function getProjectSettings(): ProjectSettings | ||
{ | ||
return $this->projectSettings; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
packages/guides/src/EventListener/LoadSettingsFromComposer.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,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Guides\EventListener; | ||
|
||
use phpDocumentor\Guides\Event\PostSettingsLoaded; | ||
use phpDocumentor\Guides\Settings\ComposerSettingsLoader; | ||
|
||
use function dirname; | ||
use function file_exists; | ||
use function getcwd; | ||
use function is_string; | ||
|
||
final class LoadSettingsFromComposer | ||
{ | ||
public function __construct(private readonly ComposerSettingsLoader $composerSettingsLoader) | ||
{ | ||
} | ||
|
||
public function __invoke(PostSettingsLoaded $event): void | ||
{ | ||
$workDir = getcwd(); | ||
if ($workDir === false) { | ||
return; | ||
} | ||
|
||
$composerjson = $this->findComposerJson($workDir); | ||
if (!is_string($composerjson)) { | ||
return; | ||
} | ||
|
||
$this->composerSettingsLoader->loadSettings($event->getProjectSettings(), $composerjson); | ||
} | ||
|
||
private function findComposerJson(string $currentDir): string|null | ||
{ | ||
// Navigate up the directory structure until finding the composer.json file | ||
while (!file_exists($currentDir . '/composer.json') && $currentDir !== '/') { | ||
$currentDir = dirname($currentDir); | ||
} | ||
|
||
// If found, return the path to the composer.json file; otherwise, return null | ||
return file_exists($currentDir . '/composer.json') ? $currentDir . '/composer.json' : null; | ||
} | ||
} |
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,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Guides\Settings; | ||
|
||
use function array_diff_key; | ||
use function array_merge; | ||
use function array_unique; | ||
use function file_get_contents; | ||
use function implode; | ||
use function is_array; | ||
use function is_string; | ||
use function json_decode; | ||
use function sprintf; | ||
use function strval; | ||
|
||
class ComposerSettingsLoader | ||
{ | ||
public function loadSettings(ProjectSettings $projectSettings, string $pathToComposerJson): void | ||
{ | ||
$jsonContents = file_get_contents($pathToComposerJson); | ||
if (!is_string($jsonContents)) { | ||
return; | ||
} | ||
|
||
$composerData = json_decode($jsonContents, true); | ||
if (!is_array($composerData)) { | ||
return; | ||
} | ||
|
||
if ($projectSettings->getComposerName() === '' && isset($composerData['name']) && is_string($composerData['name'])) { | ||
$projectSettings->setComposerName($composerData['name']); | ||
} | ||
|
||
if ($projectSettings->getDescription() === '' && isset($composerData['description']) && is_string($composerData['description'])) { | ||
$projectSettings->setDescription(strval($composerData['description'])); | ||
} | ||
|
||
if ($projectSettings->getVersion() === '' && isset($composerData['version']) && is_string($composerData['version'])) { | ||
$projectSettings->setVersion(strval($composerData['version'])); | ||
} | ||
|
||
if ($projectSettings->getComposerType() === '' && isset($composerData['type']) && is_string($composerData['type'])) { | ||
$projectSettings->setComposerType(strval($composerData['type'])); | ||
} | ||
|
||
if ($projectSettings->getLicense() === '' && isset($composerData['license'])) { | ||
if (is_string($composerData['license'])) { | ||
$projectSettings->setLicense($composerData['license']); | ||
} elseif (is_array($composerData['license'])) { | ||
$projectSettings->setLicense(sprintf('(%s)', implode(' or ', $composerData['license']))); | ||
} | ||
} | ||
|
||
if (isset($composerData['keywords']) && is_array($composerData['keywords'])) { | ||
$keywords = $projectSettings->getKeywords(); | ||
$keywords = array_unique(array_merge($keywords, $composerData['keywords'])); | ||
$projectSettings->setKeywords($keywords); | ||
} | ||
|
||
if (!isset($composerData['support']) || !is_array($composerData['support'])) { | ||
return; | ||
} | ||
|
||
$supportData = $projectSettings->getSupportData(); | ||
// Merge the arrays and keep only the keys that are not already present in $supportData | ||
$mergedSupportData = array_merge($supportData, array_diff_key($composerData['support'], $supportData)); | ||
$projectSettings->setSupportData($mergedSupportData); | ||
} | ||
} |
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