doc: Make path to module documentations configureable

refs #7196
This commit is contained in:
Eric Lippmann 2014-12-09 14:30:10 +01:00
parent e901e545c3
commit c7e6252aca
1 changed files with 56 additions and 25 deletions

View File

@ -9,6 +9,40 @@ use Icinga\Module\Doc\Exception\DocException;
class Doc_ModuleController extends DocController class Doc_ModuleController extends DocController
{ {
/**
* Get the path to a module documentation
*
* @param string $module The name of the module
* @param string $default The default path
* @param bool $suppressErrors Whether to not throw an exception if the module documentation is not
* available
*
* @return string|null Path to the documentation or null if the module documentation is not
* available and errors are suppressed
*
* @throws Zend_Controller_Action_Exception If the module documentation is not available and errors are not
* suppressed
*/
protected function getPath($module, $default, $suppressErrors = false)
{
if (($path = $this->Config()->get('documentation', 'modules')) !== null) {
$path = str_replace('{module}', $module, $path);
if (is_dir($path)) {
return $path;
}
}
if (is_dir($default)) {
return $default;
}
if ($suppressErrors) {
return null;
}
throw new Zend_Controller_Action_Exception(
sprintf($this->translate('Documentation for module \'%s\' is not available'), $module),
404
);
}
/** /**
* List modules which are enabled and having the 'doc' directory * List modules which are enabled and having the 'doc' directory
*/ */
@ -16,10 +50,10 @@ class Doc_ModuleController extends DocController
{ {
$moduleManager = Icinga::app()->getModuleManager(); $moduleManager = Icinga::app()->getModuleManager();
$modules = array(); $modules = array();
foreach (Icinga::app()->getModuleManager()->listEnabledModules() as $enabledModule) { foreach (Icinga::app()->getModuleManager()->listEnabledModules() as $module) {
$docDir = $moduleManager->getModuleDir($enabledModule, '/doc'); $path = $this->getPath($module, $moduleManager->getModuleDir($module, '/doc'), true);
if (is_dir($docDir)) { if ($path !== null) {
$modules[] = $enabledModule; $modules[] = $module;
} }
} }
$this->view->modules = $modules; $this->view->modules = $modules;
@ -63,16 +97,15 @@ class Doc_ModuleController extends DocController
*/ */
public function tocAction() public function tocAction()
{ {
$moduleName = $this->getParam('moduleName'); $module = $this->getParam('moduleName');
$this->assertModuleEnabled($moduleName); $this->assertModuleEnabled($module);
$this->view->moduleName = $moduleName; $this->view->moduleName = $module;
$moduleManager = Icinga::app()->getModuleManager();
try { try {
return $this->renderToc( return $this->renderToc(
$moduleManager->getModuleDir($moduleName, '/doc'), $this->getPath($module, Icinga::app()->getModuleManager()->getModuleDir($module, '/doc')),
$moduleName, $module,
'doc/module/chapter', 'doc/module/chapter',
array('moduleName' => $moduleName) array('moduleName' => $module)
); );
} catch (DocException $e) { } catch (DocException $e) {
throw new Zend_Controller_Action_Exception($e->getMessage(), 404); throw new Zend_Controller_Action_Exception($e->getMessage(), 404);
@ -88,24 +121,23 @@ class Doc_ModuleController extends DocController
*/ */
public function chapterAction() public function chapterAction()
{ {
$moduleName = $this->getParam('moduleName'); $module = $this->getParam('moduleName');
$this->assertModuleEnabled($moduleName); $this->assertModuleEnabled($module);
$chapterId = $this->getParam('chapterId'); $chapterId = $this->getParam('chapterId');
if ($chapterId === null) { if ($chapterId === null) {
throw new Zend_Controller_Action_Exception( throw new Zend_Controller_Action_Exception(
$this->translate('Missing parameter \'chapterId\''), sprintf($this->translate('Missing parameter \'%s\''), 'chapterId'),
404 404
); );
} }
$this->view->moduleName = $moduleName; $this->view->moduleName = $module;
$moduleManager = Icinga::app()->getModuleManager();
try { try {
return $this->renderChapter( return $this->renderChapter(
$moduleManager->getModuleDir($moduleName, '/doc'), $this->getPath($module, Icinga::app()->getModuleManager()->getModuleDir($module, '/doc')),
$chapterId, $chapterId,
$this->_helper->url->url(array('moduleName' => $moduleName), 'doc/module/toc'), $this->_helper->url->url(array('moduleName' => $module), 'doc/module/toc'),
'doc/module/chapter', 'doc/module/chapter',
array('moduleName' => $moduleName) array('moduleName' => $module)
); );
} catch (DocException $e) { } catch (DocException $e) {
throw new Zend_Controller_Action_Exception($e->getMessage(), 404); throw new Zend_Controller_Action_Exception($e->getMessage(), 404);
@ -119,14 +151,13 @@ class Doc_ModuleController extends DocController
*/ */
public function pdfAction() public function pdfAction()
{ {
$moduleName = $this->getParam('moduleName'); $module = $this->getParam('moduleName');
$this->assertModuleEnabled($moduleName); $this->assertModuleEnabled($module);
$moduleManager = Icinga::app()->getModuleManager();
return $this->renderPdf( return $this->renderPdf(
$moduleManager->getModuleDir($moduleName, '/doc'), $this->getPath($module, Icinga::app()->getModuleManager()->getModuleDir($module, '/doc')),
$moduleName, $module,
'doc/module/chapter', 'doc/module/chapter',
array('moduleName' => $moduleName) array('moduleName' => $module)
); );
} }
} }