Browse Source

Utiliser un Template pour centraliser Twig, et ajouter l’extension string de twig

master
Matthieu Marcillaud 9 months ago
parent
commit
1dd54497a7
  1. 3
      composer.json
  2. 152
      composer.lock
  3. 11
      src/Stage/IndexStage.php
  4. 15
      src/Stage/PhpDocumentorConfigStage.php
  5. 40
      src/Template.php
  6. 72
      templates/index.html.twig

3
composer.json

@ -11,7 +11,8 @@
"symfony/filesystem": "^6.0",
"psr/log": "^3.0",
"league/pipeline": "^1.0",
"twig/twig": "^3.3"
"twig/twig": "^3.3",
"twig/string-extra": "^3.3"
},
"require-dev": {
"symfony/var-dumper": "^6.0"

152
composer.lock generated

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "6aa60094df2e34eb31f7dcfdf9de5c60",
"content-hash": "7734389bfcaaa78252be9969fc93ed6b",
"packages": [
{
"name": "league/pipeline",
@ -815,6 +815,156 @@
],
"time": "2021-12-08T15:13:44+00:00"
},
{
"name": "symfony/translation-contracts",
"version": "v3.0.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/translation-contracts.git",
"reference": "1b6ea5a7442af5a12dba3dbd6d71034b5b234e77"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/translation-contracts/zipball/1b6ea5a7442af5a12dba3dbd6d71034b5b234e77",
"reference": "1b6ea5a7442af5a12dba3dbd6d71034b5b234e77",
"shasum": ""
},
"require": {
"php": ">=8.0.2"
},
"suggest": {
"symfony/translation-implementation": ""
},
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "3.0-dev"
},
"thanks": {
"name": "symfony/contracts",
"url": "https://github.com/symfony/contracts"
}
},
"autoload": {
"psr-4": {
"Symfony\\Contracts\\Translation\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Generic abstractions related to translation",
"homepage": "https://symfony.com",
"keywords": [
"abstractions",
"contracts",
"decoupling",
"interfaces",
"interoperability",
"standards"
],
"support": {
"source": "https://github.com/symfony/translation-contracts/tree/v3.0.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2021-09-07T12:43:40+00:00"
},
{
"name": "twig/string-extra",
"version": "v3.3.5",
"source": {
"type": "git",
"url": "https://github.com/twigphp/string-extra.git",
"reference": "03608ae2e9c270a961e8cf1b75751e8635ad3e3c"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/twigphp/string-extra/zipball/03608ae2e9c270a961e8cf1b75751e8635ad3e3c",
"reference": "03608ae2e9c270a961e8cf1b75751e8635ad3e3c",
"shasum": ""
},
"require": {
"php": ">=7.2.5",
"symfony/string": "^5.0|^6.0",
"symfony/translation-contracts": "^1.1|^2|^3",
"twig/twig": "^2.7|^3.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^4.4.9|^5.0.9|^6.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.2-dev"
}
},
"autoload": {
"psr-4": {
"Twig\\Extra\\String\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com",
"homepage": "http://fabien.potencier.org",
"role": "Lead Developer"
}
],
"description": "A Twig extension for Symfony String",
"homepage": "https://twig.symfony.com",
"keywords": [
"html",
"string",
"twig",
"unicode"
],
"support": {
"source": "https://github.com/twigphp/string-extra/tree/v3.3.5"
},
"funding": [
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/twig/twig",
"type": "tidelift"
}
],
"time": "2022-01-02T10:02:25+00:00"
},
{
"name": "twig/twig",
"version": "v3.3.7",

11
src/Stage/IndexStage.php

@ -4,6 +4,7 @@ namespace Spip\Autodoc\Stage;
use Spip\Autodoc\Context;
use Spip\Autodoc\Exception\ContextException;
use Spip\Autodoc\Template;
use Symfony\Component\Filesystem\Filesystem;
/** Create index.html page with links of all available documentations */
@ -53,17 +54,13 @@ class IndexStage implements StageInterface {
}
foreach ($groups as $organization => &$group) {
usort($group, function ($a, $b) {
return $a->title <=> $b->title;
return mb_strtolower($a->title) <=> mb_strtolower($b->title);
});
}
return $groups;
}
private function createIndex(Context $context, array $groups) {
// charger Twig, générer la page et l'enregitrer
$loader = new \Twig\Loader\FilesystemLoader($context->directory->templates);
$twig = new \Twig\Environment($loader);
$twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone('Europe/Paris');
$organizations = [
'spip' => 'SPIP',
@ -78,13 +75,13 @@ class IndexStage implements StageInterface {
$organizations[$other] = $other;
}
$index = $twig->render('index.html.twig', [
$template = new Template($context->directory->templates, $context->directory->output_base);
$template->generate('index.html.twig', [
'title' => 'SPIP Code',
'subtitle' => 'Documentation du code de SPIP et ses plugins',
'groups' => $groups,
'organizations' => $organizations,
]);
file_put_contents($context->directory->output_base . '/index.html', $index);
}
private function copyCss(Context $context) {

15
src/Stage/PhpDocumentorConfigStage.php

@ -7,6 +7,7 @@ use Spip\Autodoc\Context;
use Spip\Autodoc\Exception\ContextException;
use Spip\Autodoc\Git;
use Spip\Autodoc\Package;
use Spip\Autodoc\Template;
use Symfony\Component\Filesystem\Filesystem;
/**
@ -76,21 +77,17 @@ class PhpDocumentorConfigStage implements StageInterface
private function generateConfig(Context $context) {
// charger Twig, générer la page et l'enregitrer
$loader = new \Twig\Loader\FilesystemLoader($context->directory->templates);
$twig = new \Twig\Environment($loader);
$twig->getExtension(\Twig\Extension\CoreExtension::class)->setTimezone('Europe/Paris');
$config = $twig->render('phpdoc.xml.twig', [
$template = new Template($context->directory->templates, $context->directory->config);
$content = $template->render('phpdoc.xml.twig', [
'title' => $context->title,
'presentation' => $context->presentation,
'package' => $context->package,
'directory' => $context->directory,
'git' => $context->git,
]);
$config_file = $context->directory->config . '/phpdoc_' . $context->package->prefix . '.xml';
file_put_contents($config_file, $config);
$context->phpdocumentor->set('config', $config_file);
$file = 'phpdoc_' . $context->package->prefix . '.xml';
$template->write($file, $content);
$context->phpdocumentor->set('config', $context->directory->config . '/' . $file);
return $context;
}

40
src/Template.php

@ -0,0 +1,40 @@
<?php
namespace Spip\Autodoc;
use Twig\Loader\FilesystemLoader;
use Twig\Environment as Twig;
use Twig\Extension\CoreExtension;
use Twig\Extra\String\StringExtension;
class Template {
private Twig $twig;
public function __construct(private string $template_directory, private string $output_directory)
{
$this->twig = $this->instance();
}
private function instance(): Twig {
$loader = new FilesystemLoader($this->template_directory);
$twig = new Twig($loader);
$twig->getExtension(CoreExtension::class)->setTimezone('Europe/Paris');
$twig->getExtension(CoreExtension::class)->setTimezone('Europe/Paris');
$twig->addExtension(new StringExtension());
return $twig;
}
public function render(string $twig_file, array $data = []): string {
return $this->twig->render($twig_file, $data);
}
public function generate(string $twig_file, array $data = []) {
$content = $this->render($twig_file, $data);
$file = preg_replace('/[.]twig$/', '', $twig_file);
$this->write($file, $content);
}
public function write(string $file, string $content) {
file_put_contents($this->output_directory . '/' . $file, $content);
}
}

72
templates/index.html.twig

@ -38,40 +38,54 @@
<main class="main">
<div class='container'>
<div class="content" id="content">
<div class="tabset">
<input type="radio" name="tabset" id="tab0" aria-controls="tous" checked>
<label for="tab0">Tous</label>
{% for organization, name in organizations %}
{% if groups[organization] %}
<h2>{{ name }}</h2>
<table class="table">
<thead>
<tr><th>Nom</th><th>Description</th><th>Autres liens</th></tr>
</thead>
<tbody>
{% for package in groups[organization] %}
<tr>
<th><a href="{{ package.path ~ '/'}}">
{{ package.title }}</a></th>
<td>{{ package.slogan }}</td>
<td class="links">
{%- if package.urls.package %}
<a href="{{ package.urls.package }}" class="spip_out">Annuaire</a>
{%- endif %}
{%- if package.urls.documentation %}
<a href="{{ package.urls.documentation }}" class="spip_out">Documentation</a>
{%- endif %}
{%- if package.urls.repository %}
<a href="{{ package.urls.repository }}" class="spip_out">Forge</a>
{%- endif %}
{%- if package.urls.development and (package.urls.development != package.urls.repository) %}
<a href="{{ package.urls.development }}" class="spip_out">Développement</a>
{%- endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
<input type="radio" name="tabset" id="tab{{ loop.index }}" aria-controls="{{ name|slug }}" checked>
<label for="tab{{ loop.index }}">{{ name }}</label>
{% endif %}
{% endfor %}
<div class="tab-panels">
{% for organization, name in organizations %}
{% if groups[organization] %}
<section id="marzen" class="tab-panel">
<h2>{{ name }}</h2>
<table class="table">
<thead>
<tr><th>Nom</th><th>Description</th><th>Autres liens</th></tr>
</thead>
<tbody>
{% for package in groups[organization] %}
<tr>
<th><a href="{{ package.path ~ '/'}}">
{{ package.title }}</a></th>
<td>{{ package.slogan }}</td>
<td class="links">
{%- if package.urls.package %}
<a href="{{ package.urls.package }}" class="spip_out">Annuaire</a>
{%- endif %}
{%- if package.urls.documentation %}
<a href="{{ package.urls.documentation }}" class="spip_out">Documentation</a>
{%- endif %}
{%- if package.urls.repository %}
<a href="{{ package.urls.repository }}" class="spip_out">Forge</a>
{%- endif %}
{%- if package.urls.development and (package.urls.development != package.urls.repository) %}
<a href="{{ package.urls.development }}" class="spip_out">Développement</a>
{%- endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</section>
{% endif %}
{% endfor %}
</div>
</div>
</div><!-- #content -->
</div><!-- .container -->
</main>

Loading…
Cancel
Save