diff --git a/bin/dependency-visualizer b/bin/dependency-visualizer index 2a3f79f..5e301e6 100755 --- a/bin/dependency-visualizer +++ b/bin/dependency-visualizer @@ -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; } @@ -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 { diff --git a/src/Logger.php b/src/Logger.php new file mode 100644 index 0000000..34d9e94 --- /dev/null +++ b/src/Logger.php @@ -0,0 +1,47 @@ +setFormatter(new LineFormatter(null, null, true)); + self::$logger->pushHandler($handler); + } else { + self::$logger->pushHandler(new NullHandler()); + } + } + + /** + * @param mixed[] $context + */ + public static function info(string $message, array $context = []): void + { + if (self::$logger === null) { + return; + } + self::$logger->info($message, $context); + } + + /** + * @param mixed[] $context + */ + public static function error(string $message, array $context = []): void + { + if (self::$logger === null) { + return; + } + self::$logger->error($message, $context); + } +} diff --git a/src/Visitor/ClassVisitor.php b/src/Visitor/ClassVisitor.php index 2fadbc5..bb05637 100644 --- a/src/Visitor/ClassVisitor.php +++ b/src/Visitor/ClassVisitor.php @@ -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; @@ -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) { @@ -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();