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.
 
 
 

58 lines
1.8 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);
$sorted = $this->sort($list);
dump($sorted);
return $context;
}
/** @return array<string, stdClass> */
public 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));
$list[$dir] = $json;
}
return $list;
}
/** @return array<string, stdClass> */
public 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;
}
}