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

Handle missing dependencies in setter methods with @Inject(optional=true) #292

Merged
merged 3 commits into from
Nov 28, 2024
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
3 changes: 1 addition & 2 deletions src/di/AspectBind.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(AopBind $bind)
/**
* Instantiate interceptors
*
* @return array<string, array<MethodInterceptor>>
* @return array<string, list<MethodInterceptor>>
*/
public function inject(Container $container): array
{
Expand All @@ -32,7 +32,6 @@ public function inject(Container $container): array
$interceptors = [];
foreach ($interceptorClassNames as $interceptorClassName) {
/** @var class-string $interceptorClassName */
/** @psalm-suppress MixedAssignment */
$interceptor = $container->getInstance($interceptorClassName);
assert($interceptor instanceof MethodInterceptor);
$interceptors[] = $interceptor;
Expand Down
2 changes: 2 additions & 0 deletions src/di/AssistedInjectInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function invoke(MethodInvocation $invocation)
}

/**
* @param MethodInvocation<object> $invocation
*
* @return array<string, mixed>
*/
private function getNamedArguments(MethodInvocation $invocation): array
Expand Down
3 changes: 2 additions & 1 deletion src/di/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
final class ContainerFactory
{
/**
* @param AbstractModule|non-empty-array<AbstractModule>|null $module Module(s)
* @param non-empty-string $classDir
* @param AbstractModule|non-empty-array<AbstractModule>|null $module Module(s)
*/
public function __invoke($module, string $classDir): Container
{
Expand Down
2 changes: 1 addition & 1 deletion src/di/Dependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function weaveAspects(CompilerInterface $compiler, array $pointcuts): voi

$class = $compiler->compile($className, $bind);
/** @psalm-suppress ArgumentTypeCoercion */
$this->newInstance->weaveAspects($class, $bind); // @phpstan-ignore-line
$this->newInstance->weaveAspects($class, $bind);
}

/** @inheritDoc */
Expand Down
11 changes: 11 additions & 0 deletions src/di/Exception/DirectoryNotWritable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Ray\Di\Exception;

use RuntimeException;

class DirectoryNotWritable extends RuntimeException implements ExceptionInterface
{
}
5 changes: 3 additions & 2 deletions src/di/Grapher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@

final class Grapher
{
/** @var string */
/** @var non-empty-string */
private $classDir;

/** @var Container */
private $container;

/**
* @param AbstractModule $module Binding module
* @param AbstractModule $module Binding module
* @param non-empty-string $classDir Class directory
*
* @throws AnnotationException
*/
Expand Down
12 changes: 10 additions & 2 deletions src/di/Injector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@

use Doctrine\Common\Annotations\AnnotationException;
use Ray\Aop\Compiler;
use Ray\Di\Exception\DirectoryNotWritable;
use Ray\Di\Exception\Untargeted;

use function assert;
use function file_exists;
use function is_dir;
use function is_writable;
use function spl_autoload_register;
use function sprintf;
use function str_replace;
use function sys_get_temp_dir;

class Injector implements InjectorInterface
{
/** @var string */
/** @var non-empty-string */
private $classDir;

/** @var Container */
Expand All @@ -30,7 +32,13 @@ class Injector implements InjectorInterface
*/
public function __construct($module = null, string $tmpDir = '')
{
$this->classDir = is_dir($tmpDir) ? $tmpDir : sys_get_temp_dir();
/** @var non-empty-string $classDir */
$classDir = is_dir($tmpDir) ? $tmpDir : sys_get_temp_dir();
if (! is_writable($classDir)) {
throw new DirectoryNotWritable($classDir); // @CodeCoverageIgnore
}

$this->classDir = $classDir;
$this->container = (new ContainerFactory())($module, $this->classDir);
// Bind injector (built-in bindings)
(new Bind($this->container, InjectorInterface::class))->toInstance($this);
Expand Down
8 changes: 7 additions & 1 deletion src/di/MethodInvocationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@
*/
final class MethodInvocationProvider implements ProviderInterface
{
/** @var ?MethodInvocation */
/** @var ?MethodInvocation<object> */
private $invocation;

/**
* @param MethodInvocation<object> $invocation
*/
public function set(MethodInvocation $invocation): void
{
$this->invocation = $invocation;
}

/**
* @return MethodInvocation<object>
*/
public function get(): MethodInvocation
{
if ($this->invocation === null) {
Expand Down
2 changes: 1 addition & 1 deletion src/di/MultiBinding/LazyTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public function __construct(string $class)
*/
public function __invoke(InjectorInterface $injector)
{
return $injector->getInstance($this->class); // @phpstan-ignore-line
return $injector->getInstance($this->class);
}
}
12 changes: 11 additions & 1 deletion src/di/SetterMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ public function setOptional(): void
/** @inheritDoc */
public function accept(VisitorInterface $visitor)
{
$visitor->visitSetterMethod($this->method, $this->arguments);
try {
$visitor->visitSetterMethod($this->method, $this->arguments);
} catch (Unbound $e) {
if ($this->isOptional) {
// Return when no dependency given and @Inject(optional=true) annotated to setter method.
return;
}

// Throw exception when no dependency given and @Inject(optional=false) annotated to setter method.
throw $e;
}
}
}
2 changes: 1 addition & 1 deletion src/di/SpyCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function compile(string $class, BindInterface $bind): string
return $class;
}

return $class . $this->getInterceptors($bind);
return $class . $this->getInterceptors($bind); // @phpstan-ignore-line
}

/**
Expand Down
Loading
Loading