Skip to content

Commit

Permalink
Merge branch release/v2.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
roadiz-ci committed Dec 6, 2024
1 parent 4045a46 commit 70cb1bf
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 123 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-version: ['8.1', '8.2', '8.3']
php-version: ['8.2', '8.3']
steps:
- uses: shivammathur/setup-php@v2
with:
Expand All @@ -35,7 +35,5 @@ jobs:
${{ runner.os }}-php-${{ matrix.php-version }}-
- name: Install Dependencies
run: composer install --no-scripts --no-ansi --no-interaction --no-progress
- name: Run PHP Code Sniffer
run: vendor/bin/phpcs -p ./src
- name: Run PHPStan
run: vendor/bin/phpstan analyse --no-progress -c phpstan.neon
4 changes: 0 additions & 4 deletions Makefile

This file was deleted.

9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Roadiz sub-package which generates Markdown documentation skeleton based on your schema",
"type": "library",
"require": {
"php": ">=8.1",
"php": ">=8.2",
"roadiz/nodetype-contracts": "~1.1.2",
"symfony/translation": "6.4.*",
"symfony/http-foundation": "6.4.*"
Expand All @@ -23,13 +23,12 @@
}
},
"require-dev": {
"phpstan/phpstan": "^1.5.3",
"squizlabs/php_codesniffer": "^3.5"
"phpstan/phpstan": "^1.5.3"
},
"extra": {
"branch-alias": {
"dev-main": "2.3.x-dev",
"dev-develop": "2.4.x-dev"
"dev-main": "2.4.x-dev",
"dev-develop": "2.5.x-dev"
}
}
}
17 changes: 0 additions & 17 deletions phpcs.xml.dist

This file was deleted.

40 changes: 11 additions & 29 deletions src/Generators/AbstractFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,20 @@

abstract class AbstractFieldGenerator
{
protected NodeTypeFieldInterface $field;
protected TranslatorInterface $translator;
protected ParameterBag $nodeTypesBag;
protected MarkdownGeneratorFactory $markdownGeneratorFactory;

/**
* @param MarkdownGeneratorFactory $fieldGeneratorFactory
* @param NodeTypeFieldInterface $field
* @param ParameterBag $nodeTypesBag
* @param TranslatorInterface $translator
*/
public function __construct(
MarkdownGeneratorFactory $fieldGeneratorFactory,
NodeTypeFieldInterface $field,
ParameterBag $nodeTypesBag,
TranslatorInterface $translator
protected readonly MarkdownGeneratorFactory $markdownGeneratorFactory,
protected readonly NodeTypeFieldInterface $field,
protected readonly ParameterBag $nodeTypesBag,
protected readonly TranslatorInterface $translator,
) {
$this->field = $field;
$this->nodeTypesBag = $nodeTypesBag;
$this->translator = $translator;
$this->markdownGeneratorFactory = $fieldGeneratorFactory;
}

abstract public function getContents(): string;

/**
* @return string
*/
public function getIntroduction(): string
{
$lines = [
'### ' . $this->field->getLabel(),
'### '.$this->field->getLabel(),
];
if (!empty($this->field->getDescription())) {
$lines[] = $this->field->getDescription();
Expand All @@ -50,19 +32,19 @@ public function getIntroduction(): string
'',
'| | |',
'| --- | --- |',
'| **' . trim($this->translator->trans('docs.type')) . '** | ' . $this->translator->trans($this->field->getTypeName()) . ' |',
'| **' . trim($this->translator->trans('docs.technical_name')) . '** | `' . $this->field->getVarName() . '` |',
'| **' . trim($this->translator->trans('docs.universal')) . '** | *' . $this->markdownGeneratorFactory->getHumanBool($this->field->isUniversal()) . '* |',
'| **'.trim($this->translator->trans('docs.type')).'** | '.$this->translator->trans($this->field->getTypeName()).' |',
'| **'.trim($this->translator->trans('docs.technical_name')).'** | `'.$this->field->getVarName().'` |',
'| **'.trim($this->translator->trans('docs.universal')).'** | *'.$this->markdownGeneratorFactory->getHumanBool($this->field->isUniversal()).'* |',
]);

if (!empty($this->field->getGroupName())) {
$lines[] = '| **' . trim($this->translator->trans('docs.group')) . '** | ' . $this->field->getGroupName() . ' |';
$lines[] = '| **'.trim($this->translator->trans('docs.group')).'** | '.$this->field->getGroupName().' |';
}

if (!$this->field->isVisible()) {
$lines[] = '| **' . trim($this->translator->trans('docs.visible')) . '** | *' . $this->markdownGeneratorFactory->getHumanBool($this->field->isVisible()) . '* |';
$lines[] = '| **'.trim($this->translator->trans('docs.visible')).'** | *'.$this->markdownGeneratorFactory->getHumanBool($this->field->isVisible()).'* |';
}

return implode("\n", $lines) . "\n";
return implode("\n", $lines)."\n";
}
}
11 changes: 7 additions & 4 deletions src/Generators/ChildrenNodeFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public function getContents(): string
{
return implode("\n\n", [
$this->getIntroduction(),
'#### ' . $this->translator->trans('docs.available_children_blocks'),
$this->getAvailableChildren()
'#### '.$this->translator->trans('docs.available_children_blocks'),
$this->getAvailableChildren(),
]);
}

Expand All @@ -25,20 +25,23 @@ protected function getChildrenNodeTypes(): array
if (null !== $this->field->getDefaultValues()) {
return array_filter(array_map(function (string $nodeTypeName) {
$nodeType = $this->nodeTypesBag->get(trim($nodeTypeName));

return $nodeType instanceof NodeTypeInterface ? $nodeType : null;
}, explode(',', $this->field->getDefaultValues())));
}

return [];
}

protected function getAvailableChildren(): string
{
return implode("\n", array_map(function (NodeTypeInterface $nodeType) {
$nodeTypeGenerator = $this->markdownGeneratorFactory->createForNodeType($nodeType);

return implode("\n", [
'* **' . trim($nodeTypeGenerator->getMenuEntry()) . '** ',
'* **'.trim($nodeTypeGenerator->getMenuEntry()).'** ',
$nodeType->getDescription(),
]);
}, $this->getChildrenNodeTypes())) . "\n";
}, $this->getChildrenNodeTypes()))."\n";
}
}
2 changes: 1 addition & 1 deletion src/Generators/CommonFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class CommonFieldGenerator extends AbstractFieldGenerator
public function getContents(): string
{
return implode("\n\n", [
$this->getIntroduction()
$this->getIntroduction(),
]);
}
}
6 changes: 3 additions & 3 deletions src/Generators/DefaultValuedFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ public function getContents(): string
{
return implode("\n\n", [
$this->getIntroduction(),
$this->getDefaultValues()
$this->getDefaultValues(),
]);
}

private function getDefaultValues(): string
{
return implode("\n", array_map(function (string $value) {
return implode("\n", [
'* **' . trim($this->translator->trans(trim($value))) . '** `' . $value . '`',
'* **'.trim($this->translator->trans(trim($value))).'** `'.$value.'`',
]);
}, explode(',', $this->field->getDefaultValues() ?? ''))) . "\n";
}, explode(',', $this->field->getDefaultValues() ?? '')))."\n";
}
}
24 changes: 10 additions & 14 deletions src/Generators/DocumentationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,14 @@

class DocumentationGenerator
{
private ParameterBag $nodeTypesBag;
private TranslatorInterface $translator;
private MarkdownGeneratorFactory $markdownGeneratorFactory;
private ?array $reachableTypeGenerators = null;
private ?array $nonReachableTypeGenerators = null;

/**
* @param ParameterBag $nodeTypesBag
* @param TranslatorInterface $translator
*/
public function __construct(ParameterBag $nodeTypesBag, TranslatorInterface $translator)
{
$this->nodeTypesBag = $nodeTypesBag;
$this->translator = $translator;
public function __construct(
private readonly ParameterBag $nodeTypesBag,
private readonly TranslatorInterface $translator,
) {
$this->markdownGeneratorFactory = new MarkdownGeneratorFactory($nodeTypesBag, $translator);
}

Expand Down Expand Up @@ -65,6 +59,7 @@ public function getReachableTypeGenerators(): array
return $this->markdownGeneratorFactory->createForNodeType($nodeType);
}, $this->getReachableTypes());
}

return $this->reachableTypeGenerators;
}

Expand All @@ -78,6 +73,7 @@ public function getNonReachableTypeGenerators(): array
return $this->markdownGeneratorFactory->createForNodeType($nodeType);
}, $this->getNonReachableTypes());
}

return $this->nonReachableTypeGenerators;
}

Expand All @@ -103,10 +99,10 @@ public function getNavBar(): string
}

return implode("\n", [
'* ' . $this->translator->trans('docs.pages'),
" * " . implode("\n * ", $pages),
'* ' . $this->translator->trans('docs.blocks'),
" * " . implode("\n * ", $blocks)
'* '.$this->translator->trans('docs.pages'),
' * '.implode("\n * ", $pages),
'* '.$this->translator->trans('docs.blocks'),
' * '.implode("\n * ", $blocks),
]);
}
}
16 changes: 3 additions & 13 deletions src/Generators/MarkdownGeneratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Contracts\Translation\TranslatorInterface;

final class MarkdownGeneratorFactory
final readonly class MarkdownGeneratorFactory
{
public function __construct(
private readonly ParameterBag $nodeTypesBag,
private readonly TranslatorInterface $translator
private ParameterBag $nodeTypesBag,
private TranslatorInterface $translator,
) {
}

Expand All @@ -22,11 +22,6 @@ public function getHumanBool(bool $bool): string
return $bool ? $this->translator->trans('docs.yes') : $this->translator->trans('docs.no');
}

/**
* @param NodeTypeInterface $nodeType
*
* @return NodeTypeGenerator
*/
public function createForNodeType(NodeTypeInterface $nodeType): NodeTypeGenerator
{
return new NodeTypeGenerator(
Expand All @@ -36,11 +31,6 @@ public function createForNodeType(NodeTypeInterface $nodeType): NodeTypeGenerato
);
}

/**
* @param NodeTypeFieldInterface $field
*
* @return AbstractFieldGenerator
*/
public function createForNodeTypeField(NodeTypeFieldInterface $field): AbstractFieldGenerator
{
return match (true) {
Expand Down
4 changes: 2 additions & 2 deletions src/Generators/NodeReferencesFieldGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public function getContents(): string
{
return implode("\n\n", [
$this->getIntroduction(),
'#### ' . $this->translator->trans('docs.available_referenced_nodes'),
$this->getAvailableChildren()
'#### '.$this->translator->trans('docs.available_referenced_nodes'),
$this->getAvailableChildren(),
]);
}
}
40 changes: 12 additions & 28 deletions src/Generators/NodeTypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,15 @@
use RZ\Roadiz\Contracts\NodeType\NodeTypeInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

/**
* @package RZ\Roadiz\Documentation\Generators
*/
class NodeTypeGenerator
{
protected TranslatorInterface $translator;
protected MarkdownGeneratorFactory $markdownGeneratorFactory;
protected NodeTypeInterface $nodeType;
protected array $fieldGenerators;
protected array $fieldGenerators = [];

/**
* @param NodeTypeInterface $nodeType
* @param TranslatorInterface $translator
* @param MarkdownGeneratorFactory $markdownGeneratorFactory
*/
public function __construct(
NodeTypeInterface $nodeType,
TranslatorInterface $translator,
MarkdownGeneratorFactory $markdownGeneratorFactory
protected NodeTypeInterface $nodeType,
protected TranslatorInterface $translator,
protected MarkdownGeneratorFactory $markdownGeneratorFactory,
) {
$this->nodeType = $nodeType;
$this->fieldGenerators = [];
$this->translator = $translator;
$this->markdownGeneratorFactory = $markdownGeneratorFactory;

/** @var NodeTypeFieldInterface $field */
foreach ($this->nodeType->getFields() as $field) {
$this->fieldGenerators[] = $this->markdownGeneratorFactory->createForNodeTypeField($field);
Expand All @@ -41,7 +25,7 @@ public function __construct(

public function getMenuEntry(): string
{
return '[' . $this->nodeType->getLabel() . '](' . $this->getPath() . ')';
return '['.$this->nodeType->getLabel().']('.$this->getPath().')';
}

public function getType(): string
Expand All @@ -51,22 +35,22 @@ public function getType(): string

public function getPath(): string
{
return $this->getType() . '/' . $this->nodeType->getName() . '.md';
return $this->getType().'/'.$this->nodeType->getName().'.md';
}

public function getContents(): string
{
return implode("\n\n", [
$this->getIntroduction(),
'## ' . $this->translator->trans('docs.fields'),
$this->getFieldsContents()
'## '.$this->translator->trans('docs.fields'),
$this->getFieldsContents(),
]);
}

protected function getIntroduction(): string
{
$lines = [
'# ' . $this->nodeType->getLabel(),
'# '.$this->nodeType->getLabel(),
];
if (!empty($this->nodeType->getDescription())) {
$lines[] = $this->nodeType->getDescription();
Expand All @@ -75,14 +59,14 @@ protected function getIntroduction(): string
'',
'| | |',
'| --- | --- |',
'| **' . trim($this->translator->trans('docs.technical_name')) . '** | `' . $this->nodeType->getName() . '` |',
'| **'.trim($this->translator->trans('docs.technical_name')).'** | `'.$this->nodeType->getName().'` |',
]);

if ($this->nodeType->isPublishable()) {
$lines[] = '| **' . trim($this->translator->trans('docs.publishable')) . '** | *' . $this->markdownGeneratorFactory->getHumanBool($this->nodeType->isPublishable()) . '* |';
$lines[] = '| **'.trim($this->translator->trans('docs.publishable')).'** | *'.$this->markdownGeneratorFactory->getHumanBool($this->nodeType->isPublishable()).'* |';
}
if (!$this->nodeType->isVisible()) {
$lines[] = '| **' . trim($this->translator->trans('docs.visible')) . '** | *' . $this->markdownGeneratorFactory->getHumanBool($this->nodeType->isVisible()) . '* |';
$lines[] = '| **'.trim($this->translator->trans('docs.visible')).'** | *'.$this->markdownGeneratorFactory->getHumanBool($this->nodeType->isVisible()).'* |';
}

return implode("\n", $lines);
Expand Down

0 comments on commit 70cb1bf

Please sign in to comment.