Skip to content

Commit

Permalink
Make console logger available in runtime configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
DZunke committed May 6, 2024
1 parent e5826ce commit 7d3793e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
],
"require": {
"php": "^8.2",
"psr/log": "^3.0",
"symfony/console": "^7.0",
"symfony/event-dispatcher": "^7.0",
"symfony/yaml": "^7.0"
Expand Down
4 changes: 4 additions & 0 deletions panaly
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use Panaly\Storage\Handler as StorageHandler;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Logger\ConsoleLogger;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
use Symfony\Component\Console\Style\SymfonyStyle;
Expand All @@ -34,10 +35,13 @@ include $_composer_autoload_path ?? __DIR__ . '/vendor/autoload.php';
$io->title('Project Analyzer');

$runtimeConfiguration = new Configuration\RuntimeConfiguration();
$runtimeConfiguration->setLogger(new ConsoleLogger($io));

$configurationFile = (new Configuration\ConfigurationFileLoader())->loadFromFile(
$runtimeConfiguration->getFileProvider(),
$input->getOption('config')
);
$runtimeConfiguration->getLogger()->info('Config file "' . $input->getOption('config') .'" loaded.');

// The configuration file is parsed, so allow to change the configuration when there is any need for it, before the plugins are loaded
$runtimeConfiguration->getEventDispatcher()->dispatch($event = new ConfigurationLoaded($configurationFile));
Expand Down
14 changes: 14 additions & 0 deletions src/Collector/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,26 @@ public function __construct(

public function collect(): Result
{
$this->runtimeConfiguration->getLogger()->debug('Start collecting metrics.');

$result = new Result();
foreach ($this->configurationFile->metricGroups as $executingGroup) {
$this->runtimeConfiguration->getLogger()->debug(
'Collecting group "{group}".',
['group' => $executingGroup->identifier],
);

$title = $executingGroup->title;
assert($title !== ''); // Ensured by validation of the object

$group = new Group($title);

foreach ($executingGroup->metrics as $executingMetric) {
$this->runtimeConfiguration->getLogger()->debug(
'Calculate "{metric}" in group "{group}".',
['group' => $executingGroup->identifier, 'metric' => $executingMetric->identifier],
);

$metricHandler = $this->runtimeConfiguration->getMetric($executingMetric->metric);

$event = new BeforeMetricCalculate($executingMetric, $executingMetric->options);
Expand All @@ -47,6 +59,8 @@ public function collect(): Result
$result->addGroup($group);
}

$this->runtimeConfiguration->getLogger()->debug('Finished collecting metrics.');

return $result;
}
}
2 changes: 2 additions & 0 deletions src/Configuration/PluginLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ public function load(ConfigurationFile $configurationFile, RuntimeConfiguration
{
foreach ($configurationFile->plugins as $plugin) {
try {
$runtimeConfiguration->getLogger()->debug('Plugin "' . $plugin->class . '" loading.');
$loadedPlugin = new $plugin->class();
assert($loadedPlugin instanceof Plugin); // Ensured by configuration validation
} catch (Throwable $e) {
throw PluginLoadingFailed::instantiationFailed($plugin->class, $e);
}

$loadedPlugin->initialize($configurationFile, $runtimeConfiguration, $plugin->options);
$runtimeConfiguration->getLogger()->debug('Plugin "' . $plugin->class . '" initialized.', $plugin->options);

$loadedPluginMetrics = $loadedPlugin->getAvailableMetrics($plugin->options);
array_walk(
Expand Down
14 changes: 14 additions & 0 deletions src/Configuration/RuntimeConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Panaly\Plugin\Plugin\Reporting;
use Panaly\Plugin\Plugin\Storage;
use Panaly\Provider\FileProvider;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

Expand All @@ -19,6 +21,7 @@ class RuntimeConfiguration
{
private EventDispatcherInterface $eventDispatcher;
private FileProvider $fileProvider;
private LoggerInterface $logger;

/** @var list<Plugin> */
private array $loadedPlugins = [];
Expand All @@ -33,6 +36,17 @@ public function __construct()
{
$this->eventDispatcher = new EventDispatcher();
$this->fileProvider = new FileProvider();
$this->logger = new NullLogger();
}

public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
}

public function getLogger(): LoggerInterface
{
return $this->logger;
}

public function getEventDispatcher(): EventDispatcherInterface
Expand Down
7 changes: 7 additions & 0 deletions src/Reporting/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ public function __construct(

public function handle(Result $result): void
{
$this->runtimeConfiguration->getLogger()->debug('Handling reporting engines.');

foreach ($this->configurationFile->reporting as $executeReport) {
$this->runtimeConfiguration->getLogger()->debug(
'Handling reporting "{report}".',
['report' => $executeReport->identifier],
);

$reportHandler = $this->runtimeConfiguration->getReporting($executeReport->identifier);
$reportHandler->report($result, $executeReport->options);
}
Expand Down
7 changes: 7 additions & 0 deletions src/Storage/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ public function __construct(

public function handle(Result $result): void
{
$this->runtimeConfiguration->getLogger()->debug('Handling storage engines.');

foreach ($this->configurationFile->storage as $executeStorage) {
$this->runtimeConfiguration->getLogger()->debug(
'Handling storage "{storage}".',
['storage' => $executeStorage->identifier],
);

$storageHandler = $this->runtimeConfiguration->getStorage($executeStorage->identifier);
$storageHandler->store($result, $executeStorage->options);
}
Expand Down

0 comments on commit 7d3793e

Please sign in to comment.