Skip to content

Commit

Permalink
bug #16 Check if "service" function exists (Marcel Strahl, Dropelikeit)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 2.x branch.

Discussion
----------

Check if "service" function exists

The package claims to be compatible with Symfony 4.4, unfortunately the package uses the `service` function, which is supposed to replace the `ref` function in the `ContainerConfigurator` as of Symfony 5.1.
This PR is intended to restore compatibility with Symfony 4.4.

Closes #15

Commits
-------

25dd435 Check if "service" function exists
  • Loading branch information
GromNaN committed Feb 17, 2022
2 parents d55a5f3 + 25dd435 commit 4fd2bf0
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 4fd2bf0

Please sign in to comment.