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.7 KiB

<?php
namespace Spip\Autodoc\Stage;
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'));
}
$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);
}
// copy templates in config directory
$fs->mirror(
$context->phpdocumentor->config_directory,
$config_directory . '/.phpdoc',
options: [
'delete' => true,
'override' => true,
]
);
$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';
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');
}
}