silverstripe-docsviewer/code/tasks/CheckDocsSourcesTask.php

79 lines
1.9 KiB
PHP
Raw Normal View History

<?php
/**
* Check status of sources dirs
*/
2016-12-02 03:31:18 +01:00
class CheckDocsSourcesTask extends BuildTask
{
protected $errors = 0;
protected $description = "Check validity of all docs source files registered";
2017-08-08 05:50:24 +02:00
public function start()
2016-12-02 03:31:18 +01:00
{
2017-08-08 05:50:24 +02:00
if (!Director::is_cli()) {
echo "<ul>";
}
}
2017-08-08 05:50:24 +02:00
public function end()
2016-12-02 03:31:18 +01:00
{
2017-08-08 05:50:24 +02:00
if (Director::is_cli()) {
echo "\nTotal errors: {$this->errors}\n";
} else {
echo "</ul>";
echo "<p>Total errors: {$this->errors}</p>";
}
}
2017-08-08 05:50:24 +02:00
public function showError($error)
2016-12-02 03:31:18 +01:00
{
$this->errors++;
2017-08-08 05:50:24 +02:00
if (Director::is_cli()) {
echo "\n$error";
} else {
echo "<li>" . Convert::raw2xml($error) . "</li>";
}
}
/**
* Validate all source files
*
2016-12-02 03:31:18 +01:00
* @param SS_HTTPRequest $request
* @throws Exception
*/
public function run($request)
{
$this->start();
$registered = Config::inst()->get('DocumentationManifest', 'register_entities');
foreach ($registered as $details) {
// validate the details provided through the YAML configuration
$required = array('Path', 'Title');
// Check required configs
foreach ($required as $require) {
if (!isset($details[$require])) {
$this->showError("$require is a required key in DocumentationManifest.register_entities");
}
}
// Check path is loaded
$path = $this->getRealPath($details['Path']);
if (!$path || !is_dir($path)) {
$this->showError($details['Path'] . ' is not a valid documentation directory');
}
}
$this->end();
}
public function getRealPath($path)
{
if (!Director::is_absolute($path)) {
$path = Controller::join_links(BASE_PATH, $path);
}
return $path;
}
}