Skip to content

Commit

Permalink
feat: 処理の進行がわかるようログを出力できるようにする
Browse files Browse the repository at this point in the history
  • Loading branch information
hirokinoue committed Feb 26, 2024
1 parent e96fdac commit e03bd15
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
25 changes: 9 additions & 16 deletions bin/dependency-visualizer
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ use Hirokinoue\DependencyVisualizer\Config\Config;
use Hirokinoue\DependencyVisualizer\Exporter\Exporter;
use Hirokinoue\DependencyVisualizer\Exporter\PlantUmlExporter;
use Hirokinoue\DependencyVisualizer\Exporter\StringExporter;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php'] as $autoload) {
if (file_exists($autoload)) {
$loadedFile = $autoload;
require $autoload;
break;
}
Expand All @@ -35,30 +32,26 @@ Config::initialize(\getcwd());

ini_set('memory_limit', Config::memoryLimit());

$logger = new Logger('dependency visualizer');
if ($options['log']) {
$handler = new StreamHandler(getcwd() . '/app.log');
$handler->setFormatter(new LineFormatter(null, null, true));
$logger->pushHandler($handler);
} else {
$logger->pushHandler(new NullHandler());
}
Logger::initialize($options['log']);

$logger->info('start analyzing', ['target' => $file]);
Logger::info('autoload file has been loaded', ['path' => $loadedFile ?? '']);
Logger::info('start analyzing', ['target' => $file]);

try {
$dependencyVisualizer = DependencyVisualizer::create($file);
$diagramUnit = $dependencyVisualizer->analyze();
} catch (\Exception $e) {
$logger->error($e->getMessage());
$logger->error('finish analyzing abnormally');
Logger::error($e->getMessage());
Logger::error('finish analyzing abnormally');
exit(1);
}

Logger::info('start drawing');

$exporter = exporterFactory($options);
echo $exporter->export($diagramUnit);

$logger->info('finish analyzing');
Logger::info('finish analyzing');
exit(0);

function exporterFactory($options): Exporter {
Expand Down
3 changes: 3 additions & 0 deletions src/Visitor/ClassVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Hirokinoue\DependencyVisualizer\ClassManipulator\ClassLoader;
use Hirokinoue\DependencyVisualizer\Config\Config;
use Hirokinoue\DependencyVisualizer\DiagramUnit;
use Hirokinoue\DependencyVisualizer\Logger;
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\NodeTraverser;
Expand All @@ -19,6 +20,7 @@ final class ClassVisitor extends NodeVisitorAbstract
public function __construct(DiagramUnit $diagramUnit) {
$diagramUnit->registerVisitedClass();
$this->diagramUnit = $diagramUnit;
Logger::info('instantiate ClassVisitor', ['name' => $diagramUnit->fullyQualifiedClassName()]);
}

public function enterNode(Node $node) {
Expand All @@ -36,6 +38,7 @@ public function enterNode(Node $node) {

$classFile = ClassLoader::create($node);
if ($classFile->isClass()) {
Logger::info('load class', ['name' => $classFile->className()]);
$ancestors = $this->diagramUnit->ancestors();
$ancestors[] = $node->toCodeString();

Expand Down

0 comments on commit e03bd15

Please sign in to comment.