Commit 025cb747 authored by Tobias Munk's avatar Tobias Munk

updated docs application commands (WIP)

parent 13758650
......@@ -17,7 +17,9 @@ Event::on(
return [
'aliases' => [
'schmunk42/markdocs/commands' => __DIR__.'/../src/modules/docs/commands'
'schmunk42/markdocs/commands' => __DIR__.'/../src/modules/docs/commands',
'schmunk42/markdocs/components' => __DIR__.'/../src/modules/docs/components',
'schmunk42/markdocs/helpers' => __DIR__.'/../src/modules/docs/helpers'
],
'controllerMap' => [
'markdocs' => DocsController::class,
......
......@@ -9,62 +9,55 @@
namespace schmunk42\markdocs\commands;
use schmunk42\markdocs\helpers\DocsHelper;
use yii\console\Controller;
use yii\helpers\FileHelper;
use yii\helpers\Markdown;
class DocsController extends Controller
{
public $verbose = false;
private $_basePath;
private $_toc;
public function actionIndex($path)
public function options($actionID)
{
return ['verbose'];
}
public function actionCheckToc($path)
{
$this->_basePath = \Yii::getAlias($path);
$this->stdout('Docs'.PHP_EOL);
$tocLinks = $this->parseToc();
$tocLinks = DocsHelper::parseToc($path, 'README.md');
$files = FileHelper::findFiles($path, ['only' => ['*.md']]);
foreach ($files AS $file) {
#\Yii::trace("Processing {$file}...");
$this->stdout("Processing {$file}...".PHP_EOL);
\Yii::trace("Processing {$file}...");
$links[] = str_replace($path, '', $file);
}
#var_dump($links, $tocLinks);
$notInToc = array_diff($links, $tocLinks);
$brokenLinks = array_diff($tocLinks, $links);
var_dump($notInToc, $brokenLinks);
}
private function markdownFileAsDomDocument($file)
public function actionEnvList($path)
{
$html = Markdown::process(file_get_contents($file));
if (!$html) {
$this->stdout("EMPTY!");
return false;
$files = FileHelper::findFiles($path, ['only' => ['*.php'], 'except' => ['tests/']]);
$list = DocsHelper::findEnvVariables($files);
foreach ($list as $var => $occurences) {
$this->stdout("- `{$var}` ");
if ($this->verbose) {
$this->stdout("[".count($occurences)."] ");
foreach ($occurences as $occurence) {
$this->stdout($occurence['file'].":".$occurence['line'].";");
}
}
$this->stdout(PHP_EOL);
}
$doc = new \DOMDocument();
libxml_use_internal_errors(true);
$doc->strictErrorChecking = false;
$doc->loadHtml($html);
echo $html;
return $doc;
}
private function getMarkdownFiles()
{
}
private function parseToc($tocFile = 'README.md')
{
echo $this->_basePath.'/'.$tocFile;
$xpath = new \DOMXPath($this->markdownFileAsDomDocument($this->_basePath.'/'.$tocFile));
$list = $xpath->query('//a');
foreach ($list AS $element) {
$links[] = $element->getAttribute('href');
}
return $links;
}
}
\ No newline at end of file
<?php
namespace schmunk42\markdocs\components;
use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use yii\base\ErrorException;
/**
* @link http://www.diemeisterei.de/
* @copyright Copyright (c) 2017 diemeisterei GmbH, Stuttgart
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class EnvVisitor extends NodeVisitorAbstract
{
private $_envVariables = [];
public function leaveNode(Node $node)
{
if ($node instanceof Node\Expr\FuncCall) {
try {
if (isset($node->name->parts[0]) && $node->name->parts[0] === 'getenv') {
$value = $node->args[0]->value;
$this->_envVariables[$value->value] = ['line' => $value->getAttribute('startLine')];
}
} catch (ErrorException $e) {
var_dump($node);
}
}
}
public function getEnvVariables()
{
return $this->_envVariables;
}
}
\ No newline at end of file
<?php
/**
* @link http://www.diemeisterei.de/
* @copyright Copyright (c) 2017 diemeisterei GmbH, Stuttgart
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace schmunk42\markdocs\helpers;
use PhpParser\Lexer;
use PhpParser\Parser;
use schmunk42\markdocs\components\EnvVisitor;
use yii\helpers\FileHelper;
use yii\helpers\Markdown;
class DocsHelper
{
public static function findEnvVariables($files)
{
$traverser = new \PhpParser\NodeTraverser();
$parser = new Parser(new Lexer);
foreach ($files as $file) {
$envVisitior = new EnvVisitor();
$traverser->addVisitor($envVisitior);
$code = file_get_contents($file);
try {
$ast = $parser->parse($code);
$traverser->traverse($ast);
$occurrences = $envVisitior->getEnvVariables();
foreach ($occurrences as $var => $occurrence) {
$list[$var][] = ['file' => $file, 'line' => $occurrence['line']];
}
} catch (\Error $error) {
\Yii::error("Parse error: {$error->getMessage()}");
return;
}
}
ksort($list);
return $list;
}
public static function getSourceFiles($path)
{
return FileHelper::findFiles($path, ['only' => ['*.php']]);
}
public static function parseToc($basePath, $tocFile = 'README.md')
{
echo $basePath.'/'.$tocFile;
$xpath = new \DOMXPath(self::markdownFileAsDomDocument($basePath.'/'.$tocFile));
$list = $xpath->query('//a');
foreach ($list AS $element) {
$links[] = $element->getAttribute('href');
}
return $links;
}
private static function markdownFileAsDomDocument($file)
{
$html = Markdown::process(file_get_contents($file));
if (!$html) {
return false;
}
$doc = new \DOMDocument();
libxml_use_internal_errors(true);
$doc->strictErrorChecking = false;
$doc->loadHtml($html);
return $doc;
}
private function getMarkdownFiles()
{
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment