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.
 
 
 

104 lines
4.1 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'));
}
if (
!$context->has('phpdocumentor_template_xml')
or !$context->get('phpdocumentor_template_xml')
or !file_exists($context->get('phpdocumentor_template_xml'))
) {
throw new ContextException(sprintf('Key "%s" needs to be defined with valid file.', 'phpdocumentor_template_xml'));
}
$fs = new Filesystem();
$package = $context->get('package');
$prefix = $package->get('prefix') ?: $context->get('default_prefix');
if (!$context->has('output_directory') or !$context->get('output_directory')) {
$context->set('output_directory', $context->get('output_base_directory') . '/' . $prefix);
}
$output_directory = $context->get('output_directory');
if (!$fs->exists($output_directory)) {
$fs->mkdir($output_directory);
}
if (!$context->has('cache_directory') or !$context->get('cache_directory')) {
$context->set('cache_directory', $context->get('cache_base_directory') . '/' . $prefix);
}
$cache_directory = $context->get('cache_directory');
if (!$fs->exists($cache_directory)) {
$fs->mkdir($cache_directory);
}
$config_directory = $context->get('config_directory');
if (!$fs->exists($config_directory)) {
$fs->mkdir($config_directory);
}
// copy templates in config directory
$fs->mirror(
$context->get('templates_directory'),
$config_directory . '/.phpdoc',
options: [
'delete' => true,
'override' => true,
]
);
$repository = $context->has('git_asked') ? ($context->get('git_asked')->getRepositoryUrl() ?? '') : '';
$replacements = [
'@INPUT_DIRECTORY@' => $context->get('input_directory'),
'@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'),
];
$template = file_get_contents($context->get('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->set('phpdocumentor_config', $config_file);
return $context;
}
private function escape_xml(string $content) {
return htmlspecialchars($content, \ENT_XML1, 'UTF-8');
}
}