Skip to content

Commit

Permalink
[FEATURE] Load data from composer.json if present
Browse files Browse the repository at this point in the history
Load data like description, support links, composer name etc
from the composer.json if one is found.
  • Loading branch information
linawolf committed Jan 4, 2024
1 parent adde014 commit ff3749b
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 3 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
},
"sort-packages": true
},
"support": {
"issues": "https://github.com/phpDocumentor/guides/issues",
"source": "https://github.com/phpDocumentor/guides",
"docs": "https://docs.phpdoc.org/3.0/guide/guides/index.html"
},
"autoload-dev": {
"psr-4": {
"phpDocumentor\\Guides\\": ["packages/guides/tests/unit/", "tests/"],
Expand Down
16 changes: 15 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@
dev-main

====================
phpDocumentor Guides
|project|
====================

This library can be used for rendering of
`reStructuredText Markup <https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html>`__ and
`Markdown <https://daringfireball.net/projects/markdown/>`__.

Description
|description|
Source
`Source`_
Report issues
`Issues`_
Latest public documentation
|docs|

.. _Source: |source|
.. _Issues: |issues|



Tip: Render this documentation
==============================

Expand Down
17 changes: 15 additions & 2 deletions packages/guides-cli/src/Command/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
use phpDocumentor\Guides\Event\PostParseProcess;
use phpDocumentor\Guides\Event\PostRenderDocument;
use phpDocumentor\Guides\Event\PostRenderProcess;
use phpDocumentor\Guides\Event\PostSettingsLoaded;
use phpDocumentor\Guides\Event\PostSettingsOverridden;
use phpDocumentor\Guides\Event\PreParseDocument;
use phpDocumentor\Guides\Event\PreRenderDocument;
use phpDocumentor\Guides\Event\PreRenderProcess;
use phpDocumentor\Guides\Handlers\CompileDocumentsCommand;
use phpDocumentor\Guides\Handlers\ParseDirectoryCommand;
use phpDocumentor\Guides\Handlers\ParseFileCommand;
use phpDocumentor\Guides\Handlers\RenderCommand;
use phpDocumentor\Guides\Nodes\Inline\PlainTextInlineNode;
use phpDocumentor\Guides\Nodes\ProjectNode;
use phpDocumentor\Guides\Settings\ProjectSettings;
use phpDocumentor\Guides\Settings\SettingsManager;
Expand Down Expand Up @@ -199,10 +202,12 @@ static function (PostRenderProcess $event) use ($renderingProgressBar, $renderin
);
}

private function getSettingsOverridenWithInput(InputInterface $input): ProjectSettings
private function getSettingsOverriddenWithInput(InputInterface $input): ProjectSettings
{
$settings = $this->settingsManager->getProjectSettings();

$this->eventDispatcher->dispatch(new PostSettingsLoaded($settings));

if ($settings->isShowProgressBar()) {
$settings->setShowProgressBar($input->getOption('progress'));
}
Expand Down Expand Up @@ -244,12 +249,14 @@ private function getSettingsOverridenWithInput(InputInterface $input): ProjectSe
$settings->setTheme((string) $input->getOption('theme'));
}

$this->eventDispatcher->dispatch(new PostSettingsOverridden($settings));

return $settings;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$settings = $this->getSettingsOverridenWithInput($input);
$settings = $this->getSettingsOverriddenWithInput($input);
$inputDir = $this->getAbsolutePath($settings->getInput());
if (!is_dir($inputDir)) {
throw new RuntimeException(sprintf('Input directory "%s" was not found! ' . "\n" .
Expand All @@ -263,6 +270,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$settings->getCopyright() === '' ? null : $settings->getCopyright(),
$this->clock->now(),
);
$projectNode->addVariable('composer_name', new PlainTextInlineNode($settings->getComposerName()));
$projectNode->addVariable('description', new PlainTextInlineNode($settings->getDescription()));
$projectNode->addVariable('license', new PlainTextInlineNode($settings->getLicense()));
$projectNode->addVariable('issues', new PlainTextInlineNode($settings->getSupportData()['issues'] ?? ''));
$projectNode->addVariable('source', new PlainTextInlineNode($settings->getSupportData()['source'] ?? ''));
$projectNode->addVariable('docs', new PlainTextInlineNode($settings->getSupportData()['docs'] ?? ''));

$outputDir = $this->getAbsolutePath($settings->getOutput());
$sourceFileSystem = new Filesystem(new Local($settings->getInput()));
Expand Down
8 changes: 8 additions & 0 deletions packages/guides/resources/config/guides.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use phpDocumentor\Guides\Compiler\NodeTransformer;
use phpDocumentor\Guides\Compiler\NodeTransformers\CustomNodeTransformerFactory;
use phpDocumentor\Guides\Compiler\NodeTransformers\NodeTransformerFactory;
use phpDocumentor\Guides\Event\PostSettingsLoaded;
use phpDocumentor\Guides\EventListener\LoadSettingsFromComposer;
use phpDocumentor\Guides\Interlink\DefaultInventoryLoader;
use phpDocumentor\Guides\Interlink\DefaultInventoryRepository;
use phpDocumentor\Guides\Interlink\InventoryLoader;
Expand Down Expand Up @@ -46,6 +48,7 @@
use phpDocumentor\Guides\Renderer\UrlGenerator\ConfigurableUrlGenerator;
use phpDocumentor\Guides\Renderer\UrlGenerator\RelativeUrlGenerator;
use phpDocumentor\Guides\Renderer\UrlGenerator\UrlGeneratorInterface;
use phpDocumentor\Guides\Settings\ComposerSettingsLoader;
use phpDocumentor\Guides\Settings\SettingsManager;
use phpDocumentor\Guides\TemplateRenderer;
use phpDocumentor\Guides\Twig\AssetsExtension;
Expand Down Expand Up @@ -208,6 +211,11 @@
param('phpdoc.guides.base_template_paths'),
)

->set(LoadSettingsFromComposer::class)
->tag('event_listener', ['event' => PostSettingsLoaded::class])

->set(ComposerSettingsLoader::class)

->set(EnvironmentBuilder::class)
->arg('$extensions', tagged_iterator('twig.extension'))
->arg('$themeManager', service(ThemeManager::class))
Expand Down
23 changes: 23 additions & 0 deletions packages/guides/src/Event/PostSettingsLoaded.php
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;
}
}
24 changes: 24 additions & 0 deletions packages/guides/src/Event/PostSettingsOverridden.php
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 packages/guides/src/EventListener/LoadSettingsFromComposer.php
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;
}
}
71 changes: 71 additions & 0 deletions packages/guides/src/Settings/ComposerSettingsLoader.php
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);
}
}
72 changes: 72 additions & 0 deletions packages/guides/src/Settings/ProjectSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class ProjectSettings
/** @var array<string, string> */
private array $inventories = [];
private string $title = '';
private string $description = '';
/** @var list<string> */
private array $keywords = [];
private string $version = '';
private string $release = '';
private string $copyright = '';
Expand All @@ -25,6 +28,11 @@ class ProjectSettings
private bool $linksRelative = false;
private string $defaultCodeLanguage = '';
private int $maxMenuDepth = 0;
private string $composerName = '';
private string $composerType = '';
private string $license = '';
/** @var array<string, string> */
private array $supportData = [];

public function getTitle(): string
{
Expand Down Expand Up @@ -201,4 +209,68 @@ public function setMaxMenuDepth(int $maxMenuDepth): ProjectSettings

return $this;
}

public function getComposerName(): string
{
return $this->composerName;
}

public function setComposerName(string $composerName): void
{
$this->composerName = $composerName;
}

public function getComposerType(): string
{
return $this->composerType;
}

public function setComposerType(string $composerType): void
{
$this->composerType = $composerType;
}

public function getDescription(): string
{
return $this->description;
}

public function setDescription(string $description): void
{
$this->description = $description;
}

/** @return list<string> */
public function getKeywords(): array
{
return $this->keywords;
}

/** @param list<string> $keywords */
public function setKeywords(array $keywords): void
{
$this->keywords = $keywords;
}

/** @return array<string, string> */
public function getSupportData(): array
{
return $this->supportData;
}

/** @param array<string, string> $supportData */
public function setSupportData(array $supportData): void
{
$this->supportData = $supportData;
}

public function getLicense(): string
{
return $this->license;
}

public function setLicense(string $license): void
{
$this->license = $license;
}
}

0 comments on commit ff3749b

Please sign in to comment.