You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

88 lines
3.0 KiB

<?php
namespace Spip\Autodoc\Stage;
use Spip\Autodoc\Context;
use Spip\Autodoc\Exception\ContextException;
/** Create index.html page with links of all available documentations */
class IndexStage implements StageInterface {
public function __invoke(Context $context): Context
{
$context->add('stages', $this::class);
$context->get('logger')->debug("Pass: ".$this::class);
if (
$context->directory->empty('output_base')
or !is_dir($context->directory->output_base)
) {
throw new ContextException(sprintf('Key "%s" needs to be a valid directory.', 'output_base_directory'));
}
$list = $this->load($context->directory->output_base);
$list = $this->sort($list);
$this->createIndex($context, $list);
return $context;
}
/** @return array<string, stdClass> */
private function load(string $output_base_directory): array {
$list = [];
$projects = glob($output_base_directory . '/*/autodoc.json');
foreach ($projects as $project) {
$dir = basename(dirname($project));
$json = json_decode(file_get_contents($project));
$json->path = $dir;
$list[] = $json;
}
return $list;
}
/** @return array<string, stdClass> */
private function sort(array $list): array {
$groups = [];
foreach ($list as $project) {
$organization = basename(dirname($project->urls->repository ?: '')) ?: 'other';
if ($organization === 'spip' and $project->prefix !== 'spip') {
$organization = 'spip-core';
}
$groups[$organization][] = $project;
}
foreach ($groups as $organization => &$group) {
usort($group, function ($a, $b) {
return $a->title <=> $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',
'spip-core' => 'Core',
'spip-contrib-extensions' => 'Plugins',
'spip-contrib-outils' => 'Outils',
'spip-contrib-squelettes' => 'Squelettes',
'other' => 'Autres',
];
$others = array_diff(array_keys($groups), array_keys($organizations));
foreach ($others as $other) {
$organizations[$other] = $other;
}
$index = $twig->render('index.html.twig', [
'title' => 'Documentation automatique de SPIP et ses plugins',
'groups' => $groups,
'organizations' => $organizations,
]);
file_put_contents($context->directory->output_base . '/index.html', $index);
return $context;
}
}