From d921c412b5ada518e15dd5cbbe009f228341a99d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Goetz?= Date: Sun, 19 Jul 2015 22:38:37 +0200 Subject: [PATCH] Add format and processor as configuration options for live mode --- global.json | 3 ++ libs/Daux.php | 45 +++++++++++++++++++++++++ libs/Format/Base/LiveGenerator.php | 13 +++++++ libs/Format/HTML/Generator.php | 10 ++++-- libs/Generator/Command.php | 54 ++++++++++-------------------- libs/Server/Server.php | 30 ++++++++++++++--- 6 files changed, 111 insertions(+), 44 deletions(-) create mode 100644 libs/Format/Base/LiveGenerator.php diff --git a/global.json b/global.json index 3e0f769..7e7428a 100644 --- a/global.json +++ b/global.json @@ -8,6 +8,9 @@ "image": "", "languages": {}, + "format": "html", + "processor": "", + "ignore": { "files": [], "folders": [] diff --git a/libs/Daux.php b/libs/Daux.php index 52e2e6b..da32b42 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -233,4 +233,49 @@ class Daux return array_replace($default, $extended); } + + + public function getProcessorClass() + { + $processor = $this->getParams()['processor']; + + if (empty($processor)) { + return null; + } + + $class = "\\Todaymade\\Daux\\Extension\\" . $processor; + if (!class_exists($class)) { + throw new \RuntimeException("Class '$class' not found. We cannot use it as a Processor"); + } + + //TODO :: check that it implements processor + + return $class; + } + + /** + * @return \Todaymade\Daux\Format\Base\Generator + */ + public function getGenerator() + { + $generators = $this->getGenerators(); + + $format = $this->getParams()['format']; + + if (!array_key_exists($format, $generators)) { + throw new \RuntimeException("The format '$format' doesn't exist, did you forget to set your processor ?"); + } + + $class = $generators[$format]; + if (!class_exists($class)) { + throw new \RuntimeException("Class '$class' not found. We cannot use it as a Generator"); + } + + $interface = 'Todaymade\Daux\Format\Base\Generator'; + if (!in_array('Todaymade\Daux\Format\Base\Generator', class_implements($class))) { + throw new \RuntimeException("The class '$class' does not implement the '$interface' interface"); + } + + return new $class($this); + } } diff --git a/libs/Format/Base/LiveGenerator.php b/libs/Format/Base/LiveGenerator.php new file mode 100644 index 0000000..eedaea7 --- /dev/null +++ b/libs/Format/Base/LiveGenerator.php @@ -0,0 +1,13 @@ +getPath()); diff --git a/libs/Generator/Command.php b/libs/Generator/Command.php index ff22c15..d301a4f 100644 --- a/libs/Generator/Command.php +++ b/libs/Generator/Command.php @@ -19,7 +19,7 @@ class Command extends SymfonyCommand ->setDescription('Generate documentation') ->addOption('configuration', 'c', InputArgument::OPTIONAL, 'Configuration file') ->addOption('format', 'f', InputArgument::OPTIONAL, 'Output format, html or confluence', 'html') - ->addOption('processor', 'p', InputArgument::OPTIONAL, 'Manipulations on the tree', 'none') + ->addOption('processor', 'p', InputArgument::OPTIONAL, 'Manipulations on the tree') ->addOption('destination', 'd', InputArgument::OPTIONAL, $description, 'static'); } @@ -30,50 +30,30 @@ class Command extends SymfonyCommand $width = $this->getApplication()->getTerminalDimensions()[0]; - $processor = $input->getOption('processor'); - if (!empty($processor) && $processor != 'none') { - $this->prepareProcessor($processor, $daux, $output, $width); - } + // Instiantiate the processor if one is defined + $this->prepareProcessor($daux, $input, $output, $width); // Improve the tree with a processor $daux->getProcessor()->manipulateTree($daux->tree); - $this->launchGeneration($daux, $input, $output, $width); + // Set the format if requested + if ($input->getOption('format')) { + $daux->getParams()['format'] = $input->getOption('format'); + } + + // Generate the documentation + $daux->getGenerator()->generateAll($input, $output, $width); } - protected function prepareProcessor($processor, Daux $daux, OutputInterface $output, $width) + protected function prepareProcessor(Daux $daux, InputInterface $input, OutputInterface $output, $width) { - $class = "\\Todaymade\\Daux\\Extension\\" . $processor; - if (class_exists($class)) { + if ($input->getOption('processor')) { + $daux->getParams()['processor'] = $input->getOption('processor'); + } + + $class = $daux->getProcessorClass(); + if ($class) { $daux->setProcessor(new $class($daux, $output, $width)); - } elseif (file_exists($processor)) { - include $processor; } } - - protected function launchGeneration(Daux $daux, InputInterface $input, OutputInterface $output, $width) - { - $generators = $daux->getGenerators(); - - $format = strtolower($input->getOption('format')); - if (empty($format)) { - $format = 'html'; - } - - if (!array_key_exists($format, $generators)) { - throw new \RuntimeException("The format '$format' doesn't exist, did you forget to set your processor ?"); - } - - $class = $generators[$format]; - if (!class_exists($class)) { - throw new \RuntimeException("Class '$class' not found. We cannot use it as a Generator"); - } - - $interface = 'Todaymade\Daux\Format\Base\Generator'; - if (!in_array('Todaymade\Daux\Format\Base\Generator', class_implements($class))) { - throw new \RuntimeException("the class '$class' does not implement the '$interface' interface"); - } - - (new $class($daux))->generateAll($input, $output, $width); - } } diff --git a/libs/Server/Server.php b/libs/Server/Server.php index b6c457b..b5992a6 100644 --- a/libs/Server/Server.php +++ b/libs/Server/Server.php @@ -1,8 +1,10 @@ initialize(); + + $class = $daux->getProcessorClass(); + if ($class) { + $daux->setProcessor(new $class($daux, new NullOutput(), 0)); + } + + // Improve the tree with a processor + $daux->getProcessor()->manipulateTree($daux->tree); + + $server = new static($daux); + try { - $daux->initialize(); - $server = new static($daux); + $page = $server->handle($_REQUEST); } catch (NotFoundException $e) { @@ -117,9 +130,16 @@ class Server throw new NotFoundException('The Page you requested is yet to be made. Try again later.'); } - // TODO :: make it possible to replace the generator in live code - $generator = new Generator($this->daux); - return $generator->generateOne($file, $this->params); + $generator = $this->daux->getGenerator(); + + if (!$generator instanceof LiveGenerator) { + throw new \RuntimeException( + "The generator '" . get_class($generator) . "' does not implement the interface " . + "'Todaymade\\Daux\\Format\\Base\\LiveGenerator' and thus doesn't support live rendering." + ); + } + + return $this->daux->getGenerator()->generateOne($file, $this->params); } public function getRequest()