Skip to content
This repository was archived by the owner on Mar 16, 2024. It is now read-only.

Commit e80123a

Browse files
committed
Improve rendering logic
1 parent 0727994 commit e80123a

20 files changed

+164
-321
lines changed

composer.json

-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
"type": "project",
1010
"repositories": [
1111
{"type": "path", "url": "libs/content-renderer"},
12-
{"type": "path", "url": "libs/markdown-info"},
1312
{"type": "path", "url": "libs/github"}
1413
],
1514
"require": {
1615
"php": "^8.1",
1716
"local/github": "^1.0",
18-
"local/markdown-info": "^1.0",
1917
"local/content-renderer": "^1.0",
2018
"beberlei/doctrineextensions": "^1.3",
2119
"doctrine/inflector": "^2.0",

composer.lock

+3-48
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/content-renderer/composer.json

+8-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"php": "^8.1",
1414
"ext-simplexml": "*",
1515
"league/commonmark": "^2.3",
16+
"scrivo/highlight.php": "^9.18.1.9",
1617
"spatie/commonmark-highlighter": "^3.0"
1718
},
1819
"autoload": {
@@ -30,12 +31,13 @@
3031
"psr-4": {
3132
"App\\ContentRenderer\\Tests\\": "tests"
3233
}
33-
},"extra": {
34-
"branch-alias": {
35-
"dev-master": "1.x-dev",
36-
"dev-main": "1.x-dev"
37-
}
38-
},
34+
},
35+
"extra": {
36+
"branch-alias": {
37+
"dev-master": "1.x-dev",
38+
"dev-main": "1.x-dev"
39+
}
40+
},
3941
"config": {
4042
"sort-packages": true
4143
},

libs/content-renderer/src/ContentRendererInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
interface ContentRendererInterface
1414
{
1515
/**
16-
* @param string $original
16+
* @param string $markdown
1717
* @return ResultInterface
1818
*/
19-
public function render(string $original): ResultInterface;
19+
public function render(string $markdown): ResultInterface;
2020
}

libs/content-renderer/src/ContentRendererServiceProvider.php

+16-32
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,24 @@ class ContentRendererServiceProvider extends ServiceProvider
2828
public function register(): void
2929
{
3030
// Boot Default
31-
$this->registerDefaultRenderer();
32-
33-
// Boot Factory
34-
$this->app->singleton(Factory::class, function (Application $app) {
35-
$factory = new Factory($app->make(ContentRendererInterface::class));
36-
37-
$factory->extend(Type::MENU, static function () use ($app) {
38-
return new MenuRenderer($app->make(DispatcherInterface::class));
39-
});
40-
41-
$factory->extend(Type::MENU_TRANSLATION, static function () use ($app) {
42-
return new MenuTranslationRenderer($app->make(DispatcherInterface::class));
43-
});
44-
45-
$factory->extend(Type::DOCUMENTATION, static function () use ($app) {
46-
return new DocumentationRenderer($app->make(DispatcherInterface::class));
47-
});
48-
49-
$factory->extend(Type::DOCUMENTATION_TRANSLATION, static function () use ($app) {
50-
return new DocumentationTranslationRenderer($app->make(DispatcherInterface::class));
51-
});
52-
53-
return $factory;
31+
$this->app->singleton(DefaultRenderer::class);
32+
$this->app->singleton(MenuRenderer::class);
33+
$this->app->singleton(MenuTranslationRenderer::class);
34+
$this->app->singleton(DocumentationRenderer::class);
35+
$this->app->singleton(DocumentationTranslationRenderer::class);
36+
37+
$this->app->singleton(Factory::class, function (Application $app): Factory {
38+
$default = $app->make(ContentRendererInterface::class);
39+
40+
return new Factory($default, value(static function () use ($app): iterable {
41+
yield Type::MENU => $app->make(MenuRenderer::class);
42+
yield Type::MENU_TRANSLATION => $app->make(MenuTranslationRenderer::class);
43+
yield Type::DOCUMENTATION => $app->make(DocumentationRenderer::class);
44+
yield Type::DOCUMENTATION_TRANSLATION => $app->make(DocumentationTranslationRenderer::class);
45+
}));
5446
});
5547

56-
$this->app->alias(Factory::class, FactoryInterface::class);
57-
}
58-
59-
/**
60-
* @return void
61-
*/
62-
private function registerDefaultRenderer(): void
63-
{
64-
$this->app->singleton(DefaultRenderer::class);
6548
$this->app->alias(DefaultRenderer::class, ContentRendererInterface::class);
49+
$this->app->alias(Factory::class, FactoryInterface::class);
6650
}
6751
}

libs/content-renderer/src/Event/Rendered.php

-36
This file was deleted.

libs/content-renderer/src/Event/Rendering.php

-16
This file was deleted.

libs/content-renderer/src/Factory.php

+6-31
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,22 @@
1616
*/
1717
final class Factory implements FactoryInterface
1818
{
19-
/**
20-
* @var array<non-empty-string, RendererResolver>
21-
*/
22-
private array $registered = [];
23-
2419
/**
2520
* @var array<non-empty-string, ContentRendererInterface>
2621
*/
27-
private array $resolved = [];
22+
private array $renderers = [];
2823

2924
/**
3025
* @param ContentRendererInterface $default
26+
* @param iterable<Type, ContentRendererInterface> $renderers
3127
*/
3228
public function __construct(
3329
private readonly ContentRendererInterface $default,
30+
iterable $renderers = [],
3431
) {
35-
}
36-
37-
/**
38-
* @param Type $type
39-
* @param RendererResolver $resolver
40-
* @return $this
41-
*/
42-
public function extend(Type $type, callable $resolver): self
43-
{
44-
$this->registered[$type->name] = $resolver;
45-
46-
return $this;
47-
}
48-
49-
/**
50-
* @param Type|null $type
51-
* @return ContentRendererInterface
52-
*/
53-
private function resolve(?Type $type): ContentRendererInterface
54-
{
55-
if (isset($this->registered[$type->name])) {
56-
return $this->registered[$type->name]();
32+
foreach ($renderers as $type => $renderer) {
33+
$this->renderers[$type->name] = $renderer;
5734
}
58-
59-
return $this->default;
6035
}
6136

6237
/**
@@ -68,6 +43,6 @@ public function create(Type $type = null): ContentRendererInterface
6843
return $this->default;
6944
}
7045

71-
return $this->resolved[$type->name] ??= $this->resolve($type);
46+
return $this->renderers[$type->name] ??= $this->default;
7247
}
7348
}

libs/content-renderer/src/FactoryInterface.php

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
namespace App\ContentRenderer;
1313

14-
use App\ContentRenderer\ContentRendererInterface;
15-
1614
interface FactoryInterface
1715
{
1816
/**

libs/content-renderer/src/Renderer/DefaultRenderer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
namespace App\ContentRenderer\Renderer;
1313

14-
class DefaultRenderer extends Renderer
14+
final class DefaultRenderer extends Renderer
1515
{
1616
}

libs/content-renderer/src/Renderer/DocumentationRenderer.php

+4-11
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,17 @@
1616
use App\ContentRenderer\Extension\QuotesFormatter;
1717
use App\ContentRenderer\Extension\RemoveEmptyParagraphs;
1818
use App\ContentRenderer\Extension\RemoveStyleTags;
19-
use Illuminate\Contracts\Events\Dispatcher;
20-
use League\CommonMark\Util\HtmlFilter;
2119
use League\CommonMark\Extension\CommonMark\Node\Block\FencedCode;
2220
use League\CommonMark\Extension\CommonMark\Node\Block\IndentedCode;
21+
use League\CommonMark\Util\HtmlFilter;
2322
use Spatie\CommonMarkHighlighter\FencedCodeRenderer;
2423
use Spatie\CommonMarkHighlighter\IndentedCodeRenderer;
2524

2625
class DocumentationRenderer extends Renderer
2726
{
28-
/**
29-
* @param Dispatcher $dispatcher
30-
*/
31-
public function __construct(
32-
Dispatcher $dispatcher,
33-
) {
34-
parent::__construct($dispatcher, [
35-
'html_input' => HtmlFilter::ALLOW,
36-
]);
27+
public function __construct()
28+
{
29+
parent::__construct(['html_input' => HtmlFilter::ALLOW]);
3730

3831
$this->env->addExtension(new ImportHeaderClasses());
3932
$this->env->addExtension(new QuotesFormatter());

libs/content-renderer/src/Renderer/DocumentationTranslationRenderer.php

+4-9
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,12 @@
1212
namespace App\ContentRenderer\Renderer;
1313

1414
use App\ContentRenderer\Extension\RemoveCommitRelation;
15-
use Illuminate\Contracts\Events\Dispatcher;
1615

17-
class DocumentationTranslationRenderer extends DocumentationRenderer
16+
final class DocumentationTranslationRenderer extends DocumentationRenderer
1817
{
19-
/**
20-
* @param Dispatcher $dispatcher
21-
*/
22-
public function __construct(
23-
Dispatcher $dispatcher,
24-
) {
25-
parent::__construct($dispatcher);
18+
public function __construct()
19+
{
20+
parent::__construct();
2621

2722
$this->env->addExtension(new RemoveCommitRelation());
2823
}

libs/content-renderer/src/Renderer/MenuRenderer.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@
1717

1818
class MenuRenderer extends Renderer
1919
{
20-
/**
21-
* @param Dispatcher $dispatcher
22-
*/
23-
public function __construct(
24-
Dispatcher $dispatcher,
25-
) {
26-
parent::__construct($dispatcher);
27-
20+
public function __construct()
21+
{
22+
parent::__construct();
23+
2824
$this->env->addExtension(new MenuTitlesNormalizer());
2925
$this->env->addExtension(new ExternalLinks(['/api'], 'https://laravel.com', 'external-link'));
3026
}

0 commit comments

Comments
 (0)