Skip to content

Commit

Permalink
refactor: autoload extensions (#1877)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed Jan 11, 2024
1 parent 0aa432f commit 8b908f2
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 43 deletions.
14 changes: 8 additions & 6 deletions config/default.php
Expand Up @@ -118,7 +118,7 @@
'capture' => 'before', // part to capture, `before` or `after` the separator (`before` by default)
],
],
'generators' => [ // list of pages generators, ordered by weight
'generators' => [ // list of pages generators class, ordered by weight
10 => 'Cecil\Generator\DefaultPages',
20 => 'Cecil\Generator\VirtualPages',
30 => 'Cecil\Generator\ExternalBody',
Expand Down Expand Up @@ -258,7 +258,9 @@
'dir' => 'resources/translations', // internal translations directory
],
],
'extensions' => [], // Twig extensions
'extensions' => [ // list of Twig extensions class
//'Name' => 'Cecil\Renderer\Extension\Class',
],
],
// themes
'themes' => [
Expand Down Expand Up @@ -369,10 +371,10 @@
'vocabulary' => ['html'],
'term' => ['html', 'atom'],
],
'postprocessors' => [ // list of output post processors
-1 => 'Cecil\Renderer\PostProcessor\GeneratorMetaTag',
-2 => 'Cecil\Renderer\PostProcessor\HtmlExcerpt',
-3 => 'Cecil\Renderer\PostProcessor\MarkdownLink',
'postprocessors' => [ // list of output post processors class
'GeneratorMetaTag' => 'Cecil\Renderer\PostProcessor\GeneratorMetaTag',
'HtmlExcerpt' => 'Cecil\Renderer\PostProcessor\HtmlExcerpt',
'MarkdownLink' => 'Cecil\Renderer\PostProcessor\MarkdownLink',
],
],
'cache' => [
Expand Down
4 changes: 3 additions & 1 deletion docs/1-Quick Start.fr.md
Expand Up @@ -42,7 +42,9 @@ curl -LO https://cecil.app/cecil.phar

Vous pouvez également [télécharger Cecil](https://cecil.app/download/) manuellement.

> [PHP](https://php.net/manual/fr/install.php) 8.1+ est requis.
:::important
[PHP](https://php.net/manual/fr/install.php) 8.1+ est requis.
:::

### Créer un nouveau site

Expand Down
4 changes: 3 additions & 1 deletion docs/1-Quick Start.md
Expand Up @@ -36,7 +36,9 @@ curl -LO https://cecil.app/cecil.phar

You can also [download Cecil](https://cecil.app/download/) manually.

> [PHP](https://php.net/manual/en/install.php) 8.1+ is required.
:::important
[PHP](https://php.net/manual/en/install.php) 8.1+ is required.
:::

### Create a new website

Expand Down
41 changes: 20 additions & 21 deletions docs/7-Extend.md
@@ -1,23 +1,31 @@
<!--
description: "Extend Cecil."
date: 2023-04-17
updated: 2023-12-11
updated: 2024-01-11
-->
# Extend

There is several way to extend Cecil:

[toc]

## Pages Generator

A Generator help you to create pages without Markdown files (with data from a database or an API for example) or alter existing pages.
A Generator help you to create pages without Markdown files (with data from an API or a database for example) or alter existing pages.

Just add your Generator to the [`pages.generators`](4-Configuration.md#generators) list, and create a new class in the `Cecil\Generator` namespace.
Just add your Generator PHP class name to the [`pages.generators`](4-Configuration.md#generators) list, and create a new class in the `Cecil\Generator` namespace.

**Example:**

_/generators/Cecil/Generator/DummyPage.php_
_configuration_

```yaml
pages:
generators:
# priority: class name
35: Cecil\Generator\Database
99: Cecil\Generator\DummyPage
```

_/extensions/Cecil/Generator/DummyPage.php_

```php
<?php
Expand All @@ -44,7 +52,7 @@ class DummyPage extends AbstractGenerator implements GeneratorInterface
}
```

_/generators/Cecil/Generator/Database.php_
_/extensions/Cecil/Generator/Database.php_

```php
<?php
Expand Down Expand Up @@ -75,16 +83,7 @@ class Database extends AbstractGenerator implements GeneratorInterface
}
```

_configuration_

```yaml
pages:
generators:
35: Cecil\Generator\Database
99: Cecil\Generator\DummyPage
```

## Renderer extension
## Renderer (Twig) extension

You can add custom [functions](3-Templates.md#functions) and [filters](3-Templates.md#filters):

Expand Down Expand Up @@ -119,15 +118,15 @@ layouts:
MyExtension: Cecil\Renderer\Extension\MyExtension
```

## Output post processor
## Output Post Processor

You can post process page output.

Just add your post processor to the `output.postprocessors` list, and create a new class in the `Cecil\Renderer\PostProcessor` namespace.
Just add your Post Processor PHP class name to the `output.postprocessors` list, and create a new class in the `Cecil\Renderer\PostProcessor` namespace.

**Example:**

_/postprocessors/Cecil/Renderer/PostProcessor/MyProcessor.php_
_/extensions/Cecil/Renderer/PostProcessor/MyProcessor.php_

```php
<?php
Expand All @@ -151,5 +150,5 @@ _configuration_
```yaml
output:
postprocessors:
- Cecil\Renderer\PostProcessor\MyProcessor
MyProcessor: Cecil\Renderer\PostProcessor\MyProcessor
```
2 changes: 2 additions & 0 deletions src/Builder.php
Expand Up @@ -111,6 +111,8 @@ public function __construct($config = null, LoggerInterface $logger = null)
if (getenv('CECIL_DEBUG') == 'true' || (bool) $this->getConfig()->get('debug')) {
$this->debug = true;
}
// autoloads local extensions
Util::autoload($this, 'extensions');
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/Renderer/Twig.php
Expand Up @@ -14,6 +14,7 @@
namespace Cecil\Renderer;

use Cecil\Builder;
use Cecil\Exception\RuntimeException;
use Cecil\Renderer\Extension\Core as CoreExtension;
use Cecil\Util;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
Expand Down Expand Up @@ -108,7 +109,7 @@ public function __construct(Builder $builder, $templatesPath)
// intl
$this->twig->addExtension(new IntlExtension());
if (\extension_loaded('intl')) {
$this->builder->getLogger()->debug('Intl extension is loaded');
$this->builder->getLogger()->debug('PHP Intl extension is loaded');
}
// filters fallback
$this->twig->registerUndefinedFilterCallback(function ($name) {
Expand All @@ -131,10 +132,13 @@ public function __construct(Builder $builder, $templatesPath)
}
// loads custom extensions
if ($this->builder->getConfig()->has('layouts.extensions')) {
Util::autoload($builder, 'extensions');
foreach ((array) $this->builder->getConfig()->get('layouts.extensions') as $name => $class) {
$this->twig->addExtension(new $class($this->builder));
$this->builder->getLogger()->debug(sprintf('Extension "%s" (%s) added', $name, $class));
try {
$this->twig->addExtension(new $class($this->builder));
$this->builder->getLogger()->debug(sprintf('Twig extension "%s" added', $name));
} catch (\Exception | \Error $e) {
$this->builder->getLogger()->error(sprintf('Unable to add Twig extension "%s": %s', $class, $e->getMessage()));
}
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/Step/Pages/Generate.php
Expand Up @@ -46,11 +46,10 @@ public function init(array $options): void
public function process(): void
{
$generatorManager = new GeneratorManager($this->builder);
Util::autoload($this->builder, 'generators'); // loads custom generators
$generators = (array) $this->config->get('pages.generators');
array_walk($generators, function ($generator, $priority) use ($generatorManager) {
if (!class_exists($generator)) {
$message = sprintf('Unable to load generator "%s".', $generator);
$message = sprintf('Unable to load generator "%s" (priority: %s).', $generator, $priority);
$this->builder->getLogger()->error($message);

return;
Expand Down
7 changes: 3 additions & 4 deletions src/Step/Pages/Render.php
Expand Up @@ -85,15 +85,14 @@ public function process(): void

// renders each page
$count = 0;
Util::autoload($this->builder, 'postprocessors');
$postprocessors = [];
foreach ($this->config->get('output.postprocessors') as $postprocessor) {
foreach ($this->config->get('output.postprocessors') as $name => $postprocessor) {
if (!class_exists($postprocessor)) {
$this->builder->getLogger()->error(sprintf('Can\'t load output post processor "%s"', $postprocessor));
$this->builder->getLogger()->error(sprintf('Unable to load output post processor "%s"', $postprocessor));
break;
}
$postprocessors[] = new $postprocessor($this->builder);
$this->builder->getLogger()->debug(sprintf('Output post processor "%s" loaded', $postprocessor));
$this->builder->getLogger()->debug(sprintf('Output post processor "%s" loaded', $name));
}
/** @var Page $page */
foreach ($pages as $page) {
Expand Down
4 changes: 2 additions & 2 deletions src/Util.php
Expand Up @@ -119,14 +119,14 @@ public static function autoload(Builder $builder, string $dir): void
{
spl_autoload_register(function ($className) use ($builder, $dir) {
$classFile = Util::joinFile($builder->getConfig()->getSourceDir(), $dir, "$className.php");
if (file_exists($classFile)) {
if (is_readable($classFile)) {
require $classFile;
return;
}
// in themes
foreach ($builder->getConfig()->getTheme() ?? [] as $theme) {
$classFile = Util::joinFile($builder->getConfig()->getThemeDirPath($theme, $dir), "$className.php");
if (file_exists($classFile)) {
if (is_readable($classFile)) {
require $classFile;
return;
}
Expand Down
5 changes: 3 additions & 2 deletions tests/fixtures/website/config.php
Expand Up @@ -98,7 +98,7 @@
],
'pages' => [
'generators' => [
99 => 'Cecil\Generator\Test',
99 => 'Cecil\Generator\TestError',
100 => 'Cecil\Generator\TitleReplace',
],
'default' => [
Expand Down Expand Up @@ -226,7 +226,8 @@
],
'layouts' => [
'extensions' => [
'Test' => 'Cecil\Renderer\Extension\Test',
'Test' => 'Cecil\Renderer\Extension\Test',
'Test error' => 'Cecil\Renderer\Extension\TestError',
],
],
'cache' => [
Expand Down

0 comments on commit 8b908f2

Please sign in to comment.