Skip to content

Commit

Permalink
Check if "service" function exists
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Strahl authored and GromNaN committed Feb 17, 2022
1 parent d55a5f3 commit 25dd435
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
17 changes: 16 additions & 1 deletion src/Bundle/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,31 @@
use PrismaMedia\Metrics\Bundle\PrismaMediaMetricsBundle;
use PrismaMedia\Metrics\MetricAggregator;
use PrismaMedia\Metrics\MetricRenderer;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use function function_exists;
use function sprintf;

return function (ContainerConfigurator $configurator) {

$functionRef = sprintf('%s\ref', __NAMESPACE__);
$functionService = sprintf('%s\service', __NAMESPACE__);

$function = function_exists($functionService) ? $functionService : $functionRef;

if (!function_exists($function)) {
throw new LogicException(
sprintf('Function "%s" is missing and or not supported by the used Symfony version.', $function)
);
}

$configurator->services()
->set(MetricAggregator::class)
->args([tagged_iterator(PrismaMediaMetricsBundle::TAG_METRIC)])

->set(MetricRenderer::class)

->set(MetricsController::class)
->args([service(MetricAggregator::class), service(MetricRenderer::class)])
->args([$function(MetricAggregator::class), $function(MetricRenderer::class)])
->tag('controller.service_arguments')
;
};
2 changes: 1 addition & 1 deletion src/MetricRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function render(Metric $metric): string
}, array_keys($metric->getLabels()), $metric->getLabels())));
}

return sprintf("%s%s %s\n", $metric->getName(), $labels, $metric->getValue());
return sprintf("%s%s %s \n", $metric->getName(), $labels, $metric->getValue());
}
}
6 changes: 3 additions & 3 deletions tests/Bundle/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public function testEndpointMetrics(): void
$this->assertResponseIsSuccessful();

$expected =
"article_total{brand=\"Capital\"} 42\n".
"article_total{brand=\"Femme Actuelle\"} 876\n".
"article_total{brand=\"Télé-Loisirs\"} 381\n";
"article_total{brand=\"Capital\"} 42 \n".
"article_total{brand=\"Femme Actuelle\"} 876 \n".
"article_total{brand=\"Télé-Loisirs\"} 381 \n";
$this->assertSame($expected, $content);

$this->assertSame(200, $client->getResponse()->getStatusCode());
Expand Down

0 comments on commit 25dd435

Please sign in to comment.