Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable dependency injection in check constructors #157

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Checks/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ abstract class Check

protected bool $shouldRun = true;

final public function __construct()
public function __construct()
{
}

public static function new(): static
{
$instance = new static();
$instance = app(static::class);

$instance->everyMinute();

Expand Down
10 changes: 10 additions & 0 deletions tests/Checks/DependencyInjectionCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Psr\Log\LoggerInterface;
use Spatie\Health\Tests\TestClasses\DependencyInjectionCheck;

it('will use dependency injection to resolve constructor arguments', function () {
$check = DependencyInjectionCheck::new();

expect($check->getLogger())->toBeInstanceOf(LoggerInterface::class);
});
34 changes: 34 additions & 0 deletions tests/TestClasses/DependencyInjectionCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Spatie\Health\Tests\TestClasses;

use Psr\Log\LoggerInterface;
use Spatie\Health\Checks\Check;
use Spatie\Health\Checks\Result;
use Spatie\Health\Enums\Status;

class DependencyInjectionCheck extends Check
{
protected LoggerInterface $logger;

public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;

parent::__construct();
}

public function run(): Result
{
$this->logger->info('Dependency injection worked');

return new Result(
Status::ok(),
);
}

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