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.
 
 
 

119 lines
4.4 KiB

<?php
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 */
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);
$this->copyCss($context);
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 = [];
$core = $this->find_plugins_dist_source($list);
// first sort by organization
// separe 'spip' and 'spip-core'
// separe plugins_dist into 'spip-dist'
foreach ($list as $project) {
$organization = basename(dirname($project->urls->repository ?: '')) ?: 'other';
if (in_array($project->urls->repository, $core)) {
$organization = 'spip-dist';
} elseif ($organization === 'spip' and $project->prefix !== 'spip') {
$organization = 'spip-core';
} elseif (strpos($project->urls->repository, 'git.spip.net/') === false) {
$organization = 'extern';
}
$groups[$organization][] = $project;
}
foreach ($groups as $organization => &$group) {
usort($group, function ($a, $b) {
return mb_strtolower($a->title) <=> mb_strtolower($b->title);
});
}
return $groups;
}
private function find_plugins_dist_source(array $list): array {
$plugins = [];
foreach ($list as $project) {
if (
$project->prefix === 'spip'
and !empty($project->core_plugins)
) {
foreach ($project->core_plugins as $plugin) {
$plugins[] = $plugin->source;
}
}
}
return $plugins;
}
private function createIndex(Context $context, array $groups) {
$organizations = [
'spip' => 'SPIP',
'spip-dist' => 'Plugins Dist',
'spip-core' => 'Core',
'spip-contrib-extensions' => 'Plugins',
'spip-contrib-outils' => 'Outils',
'spip-contrib-squelettes' => 'Squelettes',
'other' => 'Autres',
'extern' => 'Externes',
];
$others = array_diff(array_keys($groups), array_keys($organizations));
foreach ($others as $other) {
$organizations[$other] = $other;
}
$template = new Template($context->directory->templates, $context->directory->output_base);
$template->generate('index.html.twig', [
'header' => $context->header,
'groups' => $groups,
'organizations' => $organizations,
]);
}
private function copyCss(Context $context) {
$template = new Template($context->phpdocumentor->config_directory . '/template/css', $context->directory->output_base);
$template->generate('variables.css.twig');
$fs = new Filesystem();
$fs->copy($context->directory->templates . '/favicon.png', $context->directory->output_base . '/favicon.png');
$fs->copy($context->directory->templates . '/autodoc.png', $context->directory->output_base . '/autodoc.png');
$fs->copy($context->directory->templates . '/autodoc.css', $context->directory->output_base . '/autodoc.css');
}
}