Browse Source

Réécriture du context pour plus de clarté

master
Matthieu Marcillaud 1 year ago
parent
commit
c3354730f1
  1. 6
      src/Command/FromDirectory.php
  2. 12
      src/Command/FromFile.php
  3. 4
      src/Command/FromGit.php
  4. 2
      src/Command/MakeIndex.php
  5. 73
      src/Container.php
  6. 104
      src/Context.php
  7. 6
      src/Stage/CheckStage.php
  8. 7
      src/Stage/GitStage.php
  9. 8
      src/Stage/IndexStage.php
  10. 2
      src/Stage/JsonAutodocStage.php
  11. 4
      src/Stage/PackageStage.php
  12. 22
      src/Stage/PhpDocumentorConfigStage.php
  13. 6
      src/Stage/PhpDocumentorStage.php

6
src/Command/FromDirectory.php

@ -50,14 +50,14 @@ class FromDirectory extends Command
$source = $input->getArgument('source');
$io->text("Source: <info>$source</info>.");
$context->set('input_directory', $source);
$context->set('output_directory', $input->getOption('output'));
$context->directory->set('input', $source);
$context->directory->set('output', $input->getOption('output'));
if ($input->hasOption('prefix')) {
$context->set('default_prefix', $input->getOption('prefix'));
}
$context->set('topnav', $input->getOption('topnav'));
$context->set('topnav_spip', $input->getOption('topnav_spip'));
$context->set('phpdocumentor_force', $input->getOption('force'));
$context->phpdocumentor->options->set('force', $input->getOption('force'));
$this->pipeline($context);

12
src/Command/FromFile.php

@ -14,6 +14,7 @@ use Spip\Autodoc\Exception\AutodocException;
use Spip\Autodoc\Git;
use Spip\Autodoc\Stage\CheckStage;
use Spip\Autodoc\Stage\GitStage;
use Spip\Autodoc\Stage\IndexStage;
use Spip\Autodoc\Stage\JsonAutodocStage;
use Spip\Autodoc\Stage\PackageStage;
use Spip\Autodoc\Stage\PhpDocumentorConfigStage;
@ -70,7 +71,7 @@ https://git.spip.net/spip-contrib-extensions/champs_extras_core@master;cextras
}
if ($input->getOption('outputs')) {
$context->set('output_base_directory', $input->getOption('outputs'));
$context->directory->set('output_base', $input->getOption('outputs'));
}
if ($input->getOption('prefix')) {
$context->set('only_this_prefix', $input->getOption('outputs'));
@ -78,11 +79,11 @@ https://git.spip.net/spip-contrib-extensions/champs_extras_core@master;cextras
$context->set('topnav', $input->getOption('topnav'));
$context->set('topnav_spip', $input->getOption('topnav_spip'));
$context->set('phpdocumentor_force', $input->getOption('force'));
$context->phpdocumentor->options->set('force', $input->getOption('force'));
$autodoc = new AutodocFile(
source: $source,
cache_directory: $context->get('input_base_directory'),
cache_directory: $context->directory->get('input_base'),
logger: $context->get('logger'),
only_one_prefix: $input->getOption('prefix') ?: null,
);
@ -97,8 +98,9 @@ https://git.spip.net/spip-contrib-extensions/champs_extras_core@master;cextras
$this->pipeline($item);
}
// create index
// Créer l’index des documentations.
$index = new IndexStage();
$context = $index($context);
return Command::SUCCESS;
}

4
src/Command/FromGit.php

@ -61,12 +61,12 @@ class FromGit extends Command
throw new AutodocException("Source must be provide");
}
$context->set('output_directory', $input->getOption('output'));
$context->directory->set('output', $input->getOption('output'));
$context->set('default_prefix', $input->getOption('prefix') ?: pathinfo($source, \PATHINFO_FILENAME));
$context->set('topnav', $input->getOption('topnav'));
$context->set('topnav_spip', $input->getOption('topnav_spip'));
$context->set('phpdocumentor_force', $input->getOption('force'));
$context->phpdocumentor->options->set('force', $input->getOption('force'));
$branch = $input->getOption('branch');
$git = (new Git($source))->setBranch($branch);

2
src/Command/MakeIndex.php

@ -41,7 +41,7 @@ class MakeIndex extends Command
$io->title("Autodoc " . $this->getName());
if ($input->getOption('outputs')) {
$context->set('output_base_directory', $input->getOption('outputs'));
$context->directory->set('output_base', $input->getOption('outputs'));
}
$context->set('topnav', $input->getOption('topnav'));
$context->set('topnav_spip', $input->getOption('topnav_spip'));

73
src/Container.php

@ -0,0 +1,73 @@
<?php
namespace Spip\Autodoc;
class Container
{
public function __construct(private array $context = [])
{
}
public function __isset(string $key) {
return $this->has($key);
}
public function __get(string $key) {
return $this->get($key);
}
public function set(string $key, mixed $value): self
{
$this->context[$key] = $value;
return $this;
}
public function add(string $key, mixed $value): self
{
if (!$this->has($key)) {
$this->set($key, []);
}
$this->context[$key][] = $value;
return $this;
}
public function has(string $key): bool
{
return array_key_exists($key, $this->context);
}
public function get(string $key): mixed
{
if (!$this->has($key)) {
throw new \Exception(sprintf('key "%s" does not exist in context.', $key), 1);
}
return $this->context[$key];
}
public function empty(string $key): bool
{
if (!$this->has($key)) {
return true;
}
return empty($this->get($key));
}
public function unset(string $key): self
{
if ($this->has($key)) {
unset($this->context[$key]);
}
return $this;
}
public function reset(): self
{
$this->context = [];
return $this;
}
}

104
src/Context.php

@ -7,85 +7,45 @@ use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class Context
class Context extends Container
{
/** @var array<mixed> */
private array $context = [];
public function __construct(InputInterface $input, OutputInterface $output)
{
$this->set('io', new SymfonyStyle($input, $output));
$this->set('logger', new ConsoleLogger($output));
$this->init();
}
public function init(): void
{
$this->set('php', PHP_BINARY);
$this->set('cwd_directory', getcwd());
$this->set('phpdocumentor', $this->get('cwd_directory') . '/phpDocumentor.phar');
$this->set('templates_directory', dirname(__DIR__) . '/templates');
$this->set('phpdocumentor_config_directory', dirname(__DIR__) . '/phpdoc');
$this->set('var_directory', $this->get('cwd_directory') . '/var');
$this->set('config_directory', $this->get('var_directory') . '/config');
$this->set('cache_base_directory', $this->get('var_directory') . '/cache');
$this->set('input_base_directory', $this->get('var_directory') . '/input');
$this->set('output_base_directory', $this->get('var_directory') . '/output');
$this->set('default_prefix', 'default');
}
public function set(string $key, mixed $value): self
{
$this->context[$key] = $value;
return $this;
}
public function add(string $key, mixed $value): self
{
if (!$this->has($key)) {
$this->set($key, []);
}
$this->context[$key][] = $value;
return $this;
}
public function has(string $key): bool
{
return array_key_exists($key, $this->context);
}
public function get(string $key): mixed
{
if (!$this->has($key)) {
throw new \Exception(sprintf('key "%s" does not exist in context.', $key), 1);
}
return $this->context[$key];
}
public function empty(string $key): bool
{
if (!$this->has($key)) {
return true;
}
return empty($this->get($key));
}
public function unset(string $key): self
{
if ($this->has($key)) {
unset($this->context[$key]);
}
return $this;
}
public function reset(): self
{
$this->context = [];
return $this;
$this->set('only_this_prefix', null);
$this->set('topnav', null);
$this->set('topnav_spip', null);
$cwd = getcwd();
$var = $cwd . '/var';
$this->set('directory', new Container([
'cwd' => $cwd,
'var' => $var,
'config' => $var . '/config',
'cache_base' => $var . '/cache',
'input_base' => $var . '/input',
'output_base' => $var . '/output',
'cache' => null,
'input' => null,
'output' => null,
'templates' => dirname(__DIR__) . '/templates',
]));
$this->set('phpdocumentor', new Container([
'phar' => $this->directory->cwd . '/phpDocumentor.phar',
'config_directory' => dirname(__DIR__) . '/phpdoc',
'options' => new Container([
'force' => false,
])
]));
}
function __clone()
{
$this->set('directory', clone $this->directory);
$this->set('phpdocumentor', clone $this->phpdocumentor);
}
}

6
src/Stage/CheckStage.php

@ -11,17 +11,17 @@ class CheckStage implements StageInterface
{
$context->add('stages', $this::class);
$context->get('logger')->debug("Pass: " . $this::class);
$context->get('logger')->debug("Cwd: " . $context->get('cwd_directory'));
$context->get('logger')->debug("Cwd: " . $context->directory->cwd);
$context->get('logger')->debug("Php: " . PHP_BINARY);
// prévenance : on s’assure que le script est lancé dans un lieu prévu pour
// (on ne crée pas à la volée le répertoire var si manquant)
$var_directory = $context->get('var_directory');
$var_directory = $context->directory->var;
if (!is_dir($var_directory) or !is_writable($var_directory)) {
throw new ContextException(sprintf('Var directory "%s" needs to be created and writable.', $var_directory));
}
$phpdocumentor = $context->get('phpdocumentor');
$phpdocumentor = $context->phpdocumentor->phar;
if (!$phpdocumentor or !file_exists($phpdocumentor)) {
throw new ContextException(sprintf('Can’t find "phpDocumentor.phar" in "%s".', $phpdocumentor));
}

7
src/Stage/GitStage.php

@ -30,16 +30,15 @@ class GitStage implements StageInterface
return $context;
}
/**
* Télécharge ou met à jour la source Git
**/
private function download(Context $context): Git
{
if (!$context->has('input_directory')) {
$context->set('input_directory', $context->get('input_base_directory') . '/' . $context->get('default_prefix'));
if ($context->directory->empty('input')) {
$context->directory->set('input', $context->directory->get('input_base') . '/' . $context->get('default_prefix'));
}
$input_directory = $context->get('input_directory');
$input_directory = $context->directory->input;
$fs = new Filesystem();
if (!$fs->exists($input_directory)) {

8
src/Stage/IndexStage.php

@ -13,15 +13,13 @@ class IndexStage implements StageInterface {
$context->get('logger')->debug("Pass: ".$this::class);
if (
!$context->get('output_base_directory')
or !is_dir($context->get('output_base_directory'))
$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'));
}
$output_base_directory = $context->get('output_base_directory');
$list = $this->load($output_base_directory);
$list = $this->load($context->directory->output_base);
$sorted = $this->sort($list);
dump($sorted);

2
src/Stage/JsonAutodocStage.php

@ -27,7 +27,7 @@ class JsonAutodocStage implements StageInterface
throw new ContextException(sprintf('Stage "%s" needs to be done after stage "%s".', 'JsonAutodocStage', 'PhpDocumentorStage'));
}
$output_directory = $context->get('output_directory');
$output_directory = $context->directory->output;
/** @var Package */
$package = $context->get('package');
// FIXME: This should be a real class. (and encoded / decoded with symfony/serializer ?)

4
src/Stage/PackageStage.php

@ -17,10 +17,10 @@ class PackageStage implements StageInterface
$context->add('stages', $this::class);
$context->get('logger')->debug("Pass: ".$this::class);
if (!$context->has('input_directory')) {
if ($context->directory->empty('input')) {
throw new ContextException(sprintf('Key "%s" needs to be defined.', 'input_directory'));
}
$input_directory = $context->get('input_directory');
$input_directory = $context->directory->input;
if (!$input_directory or !is_dir($input_directory) or !is_readable($input_directory)) {
throw new ContextException(sprintf('Key "%s" needs to be a readable directory.', 'input_directory'));
}

22
src/Stage/PhpDocumentorConfigStage.php

@ -29,30 +29,30 @@ class PhpDocumentorConfigStage implements StageInterface
$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);
if ($context->directory->empty('output')) {
$context->directory->set('output', $context->directory->output_base . '/' . $prefix);
}
$output_directory = $context->get('output_directory');
$output_directory = $context->directory->output;
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);
if ($context->directory->empty('cache')) {
$context->directory->set('cache', $context->directory->cache_base . '/' . $prefix);
}
$cache_directory = $context->get('cache_directory');
$cache_directory = $context->directory->cache;
if (!$fs->exists($cache_directory)) {
$fs->mkdir($cache_directory);
}
$config_directory = $context->get('config_directory');
$config_directory = $context->directory->config;
if (!$fs->exists($config_directory)) {
$fs->mkdir($config_directory);
}
// copy templates in config directory
$fs->mirror(
$context->get('phpdocumentor_config_directory'),
$context->phpdocumentor->config_directory,
$config_directory . '/.phpdoc',
options: [
'delete' => true,
@ -63,7 +63,7 @@ class PhpDocumentorConfigStage implements StageInterface
$repository = $context->has('git_asked') ? ($context->get('git_asked')->getRepositoryUrl() ?? '') : '';
$replacements = [
'@INPUT_DIRECTORY@' => $context->get('input_directory'),
'@INPUT_DIRECTORY@' => $context->directory->input,
'@OUTPUT_DIRECTORY@' => $output_directory,
'@CACHE_DIRECTORY@' => $cache_directory,
@ -81,12 +81,12 @@ class PhpDocumentorConfigStage implements StageInterface
'@URL_REPOSITORY@' => $repository,
'@GUIDES_ENABLED@' => ($package->get('with_rst_documentation') ? 'true' : 'false'),
];
$phpdocumentor_template_xml = $context->get('templates_directory') . '/phpdoc.xml';
$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->set('phpdocumentor_config', $config_file);
$context->phpdocumentor->set('config', $config_file);
return $context;
}

6
src/Stage/PhpDocumentorStage.php

@ -13,10 +13,10 @@ class PhpDocumentorStage implements StageInterface
$context->add('stages', $this::class);
$context->get('logger')->debug("Pass: ".$this::class);
$cmd = $context->get('php') . ' ' . Path::makeRelative($context->get('phpdocumentor'), $context->get('cwd_directory'));
$cmd .= ' -c ' . Path::makeRelative($context->get('phpdocumentor_config'), $context->get('cwd_directory'));
$cmd = $context->get('php') . ' ' . Path::makeRelative($context->phpdocumentor->phar, $context->directory->cwd);
$cmd .= ' -c ' . Path::makeRelative($context->phpdocumentor->config, $context->directory->cwd);
if ($context->has('phpdocumentor_force') and $context->get('phpdocumentor_force')) {
if ($context->phpdocumentor->options->force) {
$cmd .= ' --force';
}
// --sourcecode

Loading…
Cancel
Save