Browse Source

On utilise twig pour générer le phpdoc.xml

master
Matthieu Marcillaud 9 months ago
parent
commit
7e6d1bab9a
  1. 2
      phpdoc/template/components/sidebar.html.twig
  2. 5
      src/Package.php
  3. 58
      src/Stage/PhpDocumentorConfigStage.php
  4. 33
      templates/phpdoc.xml.twig

2
phpdoc/template/components/sidebar.html.twig vendored

@ -66,6 +66,6 @@
{% block outro %}
<div class='phpdocumentor-sidebar__outro'>
<p class='phpdocumentor-sidebar__date'>Documentation générée le {{ 'now'|date('d m Y') }} à {{ 'now'|date('h\\hi') }}</p>
<p class='phpdocumentor-sidebar__date'>Documentation générée le {{ 'now'|date('d m Y', "Europe/Paris") }} à {{ 'now'|date('h\\hi', "Europe/Paris") }}</p>
</div>
{% endblock %}

5
src/Package.php

@ -31,6 +31,11 @@ class Package
$this->analyse();
}
public function __isset(string $name): bool
{
return isset($this->$name);
}
public function __get(string $name)
{
return $this->get($name);

58
src/Stage/PhpDocumentorConfigStage.php

@ -2,6 +2,7 @@
namespace Spip\Autodoc\Stage;
use autodoc\Plugin\Core\Transformer\Writer\Twig;
use Spip\Autodoc\Context;
use Spip\Autodoc\Exception\ContextException;
use Spip\Autodoc\Git;
@ -25,6 +26,14 @@ class PhpDocumentorConfigStage implements StageInterface
throw new ContextException(sprintf('Key "%s" needs to be defined and instance of Package.', 'package'));
}
$this->createDirectories($context);
$this->copyPhpdocTemplates($context);
$this->generateConfig($context);
return $context;
}
private function createDirectories(Context $context) {
$fs = new Filesystem();
$package = $context->get('package');
$prefix = $package->get('prefix') ?: $context->get('default_prefix');
@ -49,49 +58,42 @@ class PhpDocumentorConfigStage implements StageInterface
if (!$fs->exists($config_directory)) {
$fs->mkdir($config_directory);
}
}
private function copyPhpdocTemplates(Context $context) {
$fs = new Filesystem();
// copy templates in config directory
$fs->mirror(
$context->phpdocumentor->config_directory,
$config_directory . '/.phpdoc',
$context->directory->config . '/.phpdoc',
options: [
'delete' => true,
'override' => true,
]
);
}
private function generateConfig(Context $context) {
$repository = $context->has('git_asked') ? ($context->get('git_asked')->getRepositoryUrl() ?? '') : '';
$replacements = [
'@INPUT_DIRECTORY@' => $context->directory->input,
'@OUTPUT_DIRECTORY@' => $output_directory,
'@CACHE_DIRECTORY@' => $cache_directory,
'@PREFIX@' => $prefix,
'@DEFAULT_PACKAGE_NAME@' => $context->get('default_prefix'),
'@TITLE@' => $this->escape_xml($context->get('title')),
'@SLOGAN@' => $this->escape_xml($package->get('slogan')),
'@DESCRIPTION@' => $this->escape_xml($package->get('description')),
'@PRESENTATION@' => $this->escape_xml($context->get('presentation')),
'@VERSION@' => $package->get('version'),
'@REVISION@' => ($context->has('git') ? $context->get('git')->getCommit() : ''),
'@URL_PACKAGE@' => $package->get('url_package'),
'@URL_DOCUMENTATION@' => $package->get('url_documentation'),
'@URL_DEVELOPMENT@' => $package->get('url_development'),
'@URL_REPOSITORY@' => $repository,
'@GUIDES_ENABLED@' => ($package->get('with_rst_documentation') ? 'true' : 'false'),
];
$phpdocumentor_template_xml = $context->directory->templates . '/phpdoc.xml';
$template = file_get_contents($phpdocumentor_template_xml);
$config = str_replace(array_keys($replacements), $replacements, $template);
$config_file = $config_directory . '/phpdoc_' . $prefix . '.xml';
// 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', [
'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);
return $context;
}
private function escape_xml(string $content) {
return htmlspecialchars($content, \ENT_XML1, 'UTF-8');
}
}

33
templates/phpdoc.xml → templates/phpdoc.xml.twig

@ -6,14 +6,14 @@
xsi:noNamespaceSchemaLocation="https://docs.phpdoc.org/latest/phpdoc.xsd"
>
<paths>
<output>@OUTPUT_DIRECTORY@</output>
<cache>@CACHE_DIRECTORY@</cache>
<output>{{ directory.output }}</output>
<cache>{{ directory.cache }}</cache>
</paths>
<title>@TITLE@</title>
<title>{{ title|e('html') }}</title>
<version number="latest">
<api format="php">
<source dsn="@INPUT_DIRECTORY@">
<source dsn="{{ directory.input }}">
<path>.</path>
</source>
<output>api</output>
@ -28,7 +28,7 @@
<path>tmp/**/*</path>
<path>squelettes-dist/**/*</path>
</ignore>
<!-- default-package-name>@DEFAULT_PACKAGE_NAME@</default-package-name -->
<!-- default-package-name>...</default-package-name -->
<!-- include-source>true</include-source -->
<markers>
<marker>TODO</marker>
@ -36,23 +36,24 @@
</markers>
</api>
<guide format="rst">
<source dsn="@INPUT_DIRECTORY@">
<source dsn="{{ directory.input }}">
<path>docs</path>
</source>
<output>docs</output>
</guide>
</version>
<setting name="package.slogan" value="@SLOGAN@"/>
<setting name="package.description" value="@DESCRIPTION@"/>
<setting name="package.presentation" value="@PRESENTATION@"/>
<setting name="package.version" value="@VERSION@"/>
<setting name="package.revision" value="@REVISION@"/>
<setting name="package.url_package" value="@URL_PACKAGE@"/>
<setting name="package.url_documentation" value="@URL_DOCUMENTATION@"/>
<setting name="package.url_development" value="@URL_DEVELOPMENT@"/>
<setting name="package.url_repository" value="@URL_REPOSITORY@"/>
<setting name="package.prefix" value="{{ package.prefix }}"/>
<setting name="package.slogan" value="{{ package.slogan|e('html_attr') }}"/>
<setting name="package.description" value="{{ package.description|e('html_attr') }}"/>
<setting name="package.presentation" value="{{ package.presentation|e('html_attr') }}"/>
<setting name="package.version" value="{{ package.version }}"/>
<setting name="package.revision" value="{{ git.commit }}"/>
<setting name="package.url_package" value="{{ package.url_package }}"/>
<setting name="package.url_documentation" value="{{ package.url_documentation }}"/>
<setting name="package.url_development" value="{{ package.url_development }}" />
<setting name="package.url_repository" value="{{ git.repositoryurl }}"/>
<setting name="guides.enabled" value="@GUIDES_ENABLED@"/>
<setting name="guides.enabled" value="{{ package.with_rst_documentation ? 'true' : 'false' }}"/>
<template name="default" />
</phpdocumentor>
Loading…
Cancel
Save