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.
 
 
 

97 lines
3.2 KiB

<?php
namespace Spip\Autodoc\Stage;
use autodoc\Plugin\Core\Transformer\Writer\Twig;
use Spip\Autodoc\Context;
use Spip\Autodoc\Exception\ContextException;
use Spip\Autodoc\Git;
use Spip\Autodoc\Package;
use Symfony\Component\Filesystem\Filesystem;
/**
* Prepare phpdoc.xml for this documentation.
*/
class PhpDocumentorConfigStage implements StageInterface
{
public function __invoke(Context $context): Context
{
$context->add('stages', $this::class);
$context->get('logger')->debug("Pass: " . $this::class);
if (
!$context->has('package')
or !$context->get('package') instanceof Package
) {
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');
if ($context->directory->empty('output')) {
$context->directory->set('output', $context->directory->output_base . '/' . $prefix);
}
$output_directory = $context->directory->output;
if (!$fs->exists($output_directory)) {
$fs->mkdir($output_directory);
}
if ($context->directory->empty('cache')) {
$context->directory->set('cache', $context->directory->cache_base . '/' . $prefix);
}
$cache_directory = $context->directory->cache;
if (!$fs->exists($cache_directory)) {
$fs->mkdir($cache_directory);
}
$config_directory = $context->directory->config;
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,
$context->directory->config . '/.phpdoc',
options: [
'delete' => true,
'override' => true,
]
);
}
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', [
'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;
}
}