Skip to content

Commit 8a2d0b0

Browse files
authored
Merge pull request #19 from OpenConext/feaure/autowiring-simplification-and-backticks
Symplify autowiring and remove backticks
2 parents ffc43a2 + e83bde5 commit 8a2d0b0

19 files changed

Lines changed: 108 additions & 289 deletions
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
open_conext_monitor:
2-
resource: "@OpenConextMonitorBundle/Resources/config/routing.yml"
3-
prefix: /
4-
5-
2+
resource: "@OpenConextMonitorBundle/src/Controller"
3+
type: attribute
4+
prefix: /

README.md

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A Symfony 5/6/7 bundle that adds an /internal/health and /internal/info endpoint
66

77
The endpoints return JSON responses. The `/internal/info` endpoint tries to give as much information about the currently installed
88
version of the application as possible. This information is based on the build path of the installation. But also
9-
includes the Symfony environment that is currently active and whether or not the debugger is enabled.
9+
includes the Symfony environment that is currently active and whether the debugger is enabled.
1010

1111
The `/internal/health` endpoint reports on the health of the application. This information could be used for example by a load
1212
balancer. Example output:
@@ -46,9 +46,11 @@ When a health check failed the HTTP Response status code will be 503. And the JS
4646
* Include the routing configuration in `config/routes.yaml` by adding:
4747
```yaml
4848
open_conext_monitor:
49-
resource: "@OpenConextMonitorBundle/Resources/config/routing.yml"
50-
prefix: /
49+
resource: "@OpenConextMonitorBundle/src/Controller"
50+
type: attribute
51+
prefix: /
5152
```
53+
_Note: this is currently done by the bundle itself, with an external dependency called https://github.com/endroid/installer_
5254
5355
* Add security exceptions in `config/packages/security.yaml` (if this is required at all)
5456
```yaml
@@ -78,24 +80,18 @@ use OpenConext\MonitorBundle\Value\HealthReport;
7880
7981
class ApiHealthCheck implements HealthCheckInterface
8082
{
81-
/**
82-
* @var MyService
83-
*/
84-
private $testService;
85-
86-
public function __construct(MyService $service)
83+
public function __construct(private readonly MyService $service)
8784
{
88-
$this->testService = $service;
8985
}
9086
9187
public function check(HealthReportInterface $report): HealthReportInterface
9288
{
93-
if (!$this->testService->everythingOk()) {
89+
if (!$this->service->everythingOk()) {
9490
// Return a HealthReport with a DOWN status when there are indications the application is not functioning as
9591
// intended. You can provide an optional message that is displayed alongside the DOWN status.
9692
return HealthReport::buildStatusDown('Not everything is allright.');
9793
}
98-
// By default return the report that was passed along as a parameter to the check method
94+
// By default, return the report that was passed along as a parameter to the check method
9995
return $report;
10096
}
10197
}
@@ -104,35 +100,20 @@ class ApiHealthCheck implements HealthCheckInterface
104100
registered health checkers. If everything was OK, just return the report that was passed to the method.
105101
106102
### Register the checker
107-
To actually include the home made checker simply tag it with 'surfnet.monitor.health_check'
108-
109-
Example service definition in `services.yml`
110-
111-
```yaml
112-
services:
113-
acme.monitor.my_custom_health_check:
114-
class: Acme\AppBundle\HealthCheck\MyCustomHealthCheck
115-
arguments:
116-
- @test_service
117-
tags:
118-
- { name: surfnet.monitor.health_check }
119-
```
103+
By implementing the `HealthCheckInterface` you can register your own health check.
104+
This interface is tagged automatically, so you don't have to do it yourself.
120105

121106
## Overriding a default HealthCheck
122107
To run a custom query with the DoctrineConnectionHealthCheck you will need to override it in your own project.
123108

124-
For example in your ACME bunde that is using the monitor bundle:
109+
For example in your ACME bundle that is using the monitor bundle:
125110

126-
`services.yml`
111+
`services.yaml`
127112
```yaml
128-
# Override the service, service names can be found in `/src/Resources/config/services.yml`
129-
openconext.monitor.database_health_check:
113+
# Override the service in `/src/config/services.yaml`
114+
OpenConext\MonitorBundle\HealthCheck\DoctrineConnectionHealthCheck:
130115
# Point to your own implementation of the check
131116
class: Acme\GreatSuccessBundle\HealthCheck\DoctrineConnectionHealthCheck
132-
# Do not forget to apply the correct tag
133-
tags:
134-
- { name: openconext.monitor.health_check }
135-
136117
```
137118

138119
The rest of the service configuration is up to your own needs. You can inject arguments, factory calls and other service features as need be.

composer.json

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
{
22
"name": "openconext/monitor-bundle",
33
"type": "symfony-bundle",
4-
"description": "A Symfony 5/6/7 bundle that facilitates health and info endpoints to a Symfony application.",
4+
"description": "A Symfony 6/7 bundle that facilitates health and info endpoints to a Symfony application.",
55
"keywords": ["SURFnet", "StepUp", "OpenConext", "monitoring", "health"],
66
"license": "Apache-2.0",
77
"minimum-stability": "stable",
88
"require": {
99
"php": ">=8.2, <9.0-dev",
10-
"doctrine/orm": "^2.9",
11-
"doctrine/dbal": "^3.1",
10+
"doctrine/dbal": "^3.1|^4.0",
1211
"endroid/installer": "^1.4",
13-
"symfony/dependency-injection": "^5.4|^6.3|^7.0",
14-
"symfony/framework-bundle": "^5.4|^6.3|^7.0",
12+
"symfony/dependency-injection": "^6.3|^7.0",
13+
"symfony/framework-bundle": "^6.3|^7.0",
1514
"webmozart/assert": "^1.10"
1615
},
1716
"require-dev": {
1817
"php-parallel-lint/php-parallel-lint": "^1.3",
1918
"phpmd/phpmd": "^2.13",
20-
"matthiasnoback/symfony-config-test": "^4.3",
21-
"phpdocumentor/reflection-docblock": "^5.2",
22-
"phpunit/php-token-stream": "^3.1.3|^4.0.4",
2319
"phpunit/phpunit": "^9.6|^10.4",
2420
"sebastian/phpcpd": "^4.1|^5.0|^6.0",
2521
"squizlabs/php_codesniffer": "^3.6",
@@ -28,21 +24,21 @@
2824
},
2925
"autoload": {
3026
"psr-4": {
31-
"OpenConext\\MonitorBundle\\": "src"
27+
"OpenConext\\MonitorBundle\\": "src/"
3228
}
3329
},
3430
"autoload-dev": {
3531
"psr-4": {
36-
"App\\Tests\\": "src/Tests/"
32+
"App\\Tests\\": "tests/"
3733
}
3834
},
3935
"scripts": {
4036
"tests": {
41-
"docheader": "vendor/bin/docheader check src/",
42-
"phpcs": "vendor/bin/phpcs src --report=full --standard=phpcs.xml --extensions=php --warning-severity=0",
43-
"phpcpd": "vendor/bin/phpcpd src --exclude=src/Tests/*",
37+
"docheader": "vendor/bin/docheader check src/ tests/",
38+
"phpcs": "vendor/bin/phpcs src tests --report=full --standard=phpcs.xml --extensions=php --warning-severity=0",
39+
"phpcpd": "vendor/bin/phpcpd src",
4440
"phpunit": "vendor/bin/phpunit --coverage-text",
45-
"phpmd": "vendor/bin/phpmd src text phpmd.xml --exclude 'src/Tests/*'"
41+
"phpmd": "vendor/bin/phpmd src text phpmd.xml"
4642
},
4743
"post-update-cmd": [
4844
"@tests"

config/services.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
_defaults:
3+
autowire: true # Automatically injects dependencies in your services.
4+
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
5+
6+
OpenConext\MonitorBundle\:
7+
resource: '../src/'
8+
exclude: '../src/{Entity,Repository,Tests,Resources,DependencyInjection,OpenConextMonitorBundle.php}'

phpunit.xml

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,29 @@
1313
>
1414

1515
<php>
16-
<ini name="error_reporting" value="-1" />
17-
<server name="APP_ENV" value="test" force="true" />
18-
<server name="SHELL_VERBOSITY" value="-1" />
19-
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
20-
<server name="SYMFONY_PHPUNIT_VERSION" value="9" />
21-
22-
<server name="KERNEL_CLASS" value="App\Tests\App\AppKernel"/>
23-
<server name="KERNEL_DIR" value="./Tests/App"/>
24-
<ini name="zend.enable_gc" value="0"/>
16+
<ini name="error_reporting" value="-1"/>
17+
<server name="APP_ENV" value="test" force="true"/>
18+
<server name="SHELL_VERBOSITY" value="-1"/>
19+
<server name="SYMFONY_PHPUNIT_REMOVE" value=""/>
20+
<server name="SYMFONY_PHPUNIT_VERSION" value="9"/>
21+
<ini name="zend.enable_gc" value="0"/>
2522
</php>
2623

27-
<coverage processUncoveredFiles="true">
28-
<include>
29-
<directory suffix=".php">src</directory>
30-
</include>
31-
<exclude>
32-
<directory>.github</directory>
33-
<directory>src/Tests</directory>
34-
</exclude>
35-
</coverage>
36-
<testsuites>
37-
<testsuite name="Test Suite">
38-
<directory>src/Tests</directory>
39-
</testsuite>
40-
</testsuites>
41-
<listeners>
42-
<listener class="\Mockery\Adapter\Phpunit\TestListener"/>
43-
</listeners>
24+
<coverage processUncoveredFiles="true">
25+
<include>
26+
<directory suffix=".php">src</directory>
27+
</include>
28+
<exclude>
29+
<directory>.github</directory>
30+
<directory>tests</directory>
31+
</exclude>
32+
</coverage>
33+
<testsuites>
34+
<testsuite name="Test Suite">
35+
<directory>tests</directory>
36+
</testsuite>
37+
</testsuites>
38+
<listeners>
39+
<listener class="\Mockery\Adapter\Phpunit\TestListener"/>
40+
</listeners>
4441
</phpunit>

src/Controller/HealthController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use OpenConext\MonitorBundle\HealthCheck\HealthCheckChain;
2222
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2323
use Symfony\Component\HttpFoundation\JsonResponse;
24+
use Symfony\Component\Routing\Attribute\Route;
2425

2526
/**
2627
* Display the health state of the application.
@@ -35,6 +36,8 @@ public function __construct(
3536
) {
3637
}
3738

39+
#[Route('/health', name: 'monitor.health', methods: ['GET'])]
40+
#[Route('/internal/health', name: 'monitor.internal_health', methods: ['GET'])]
3841
public function __invoke(): JsonResponse
3942
{
4043
$statusResponse = $this->healthChecker->check();

src/Controller/InfoController.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
namespace OpenConext\MonitorBundle\Controller;
2020

2121
use OpenConext\MonitorBundle\Value\BuildInformationFactory;
22-
use OpenConext\MonitorBundle\Value\BuildPathFactory;
2322
use OpenConext\MonitorBundle\Value\Information;
2423
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
24+
use Symfony\Component\DependencyInjection\Attribute\Autowire;
2525
use Symfony\Component\HttpFoundation\JsonResponse;
26-
use Symfony\Component\HttpFoundation\Response;
26+
use Symfony\Component\Routing\Attribute\Route;
2727

2828
/**
2929
* Display specific information about the application.
@@ -43,21 +43,31 @@
4343
class InfoController extends AbstractController
4444
{
4545
private array $systemInfo = [];
46+
private readonly string $buildPath;
4647

4748
public function __construct(
48-
private readonly string $buildPath,
49+
#[Autowire(param: 'kernel.project_dir')]
50+
private readonly string $projectDir,
51+
#[Autowire(param: 'kernel.environment')]
4952
private readonly string $environment,
53+
#[Autowire(param: 'kernel.debug')]
5054
private readonly bool $debuggerEnabled,
55+
#[Autowire(env: 'default::string:OPENCONEXT_APP_VERSION')]
5156
private readonly ?string $version,
57+
#[Autowire(env: 'default::string:OPENCONEXT_GIT_SHA')]
5258
private readonly ?string $revision,
59+
#[Autowire(env: 'default::string:OPENCONEXT_COMMIT_DATE')]
5360
private readonly ?string $commitDate,
5461
) {
62+
$this->buildPath = basename(realpath($this->projectDir));
5563

5664
if (function_exists('opcache_get_status')) {
5765
$this->systemInfo['opcache'] = opcache_get_status(false);
5866
}
5967
}
6068

69+
#[Route('/info', name: 'monitor.info', methods: ['GET'])]
70+
#[Route('/internal/info', name: 'monitor.internal_info', methods: ['GET'])]
6171
public function __invoke(): JsonResponse
6272
{
6373
$buildInformation = BuildInformationFactory::build(

src/DependencyInjection/Compiler/HealthCheckPass.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/DependencyInjection/OpenConextMonitorExtension.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)