Skip to content
Open
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
33 changes: 26 additions & 7 deletions src/DependencyInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ class DependencyInjector
// Post Resolver Callbacks
protected array $_postResolver = [];

protected ?ReflectionObserver $_reflectionObserver = null;
/**
* @var ReflectionObserver[]
*/
protected array $_reflectionObservers = [];

public function addReflectionObserver(ReflectionObserver $observer): ReflectionObserver
{
$this->_reflectionObservers[] = $observer;
return $observer;
}

public function setReflectionObserver(ReflectionObserver $observer): ReflectionObserver
{
$this->_reflectionObserver = $observer;
return $this->_reflectionObserver;
$this->_reflectionObservers = [$observer];
return $observer;
}

public function factory($abstract, string|callable $generator, $mode = self::MODE_MUTABLE)
Expand Down Expand Up @@ -247,11 +256,16 @@ protected function _resolveParameters(\ReflectionMethod $reflection, array $para
public function resolveMethod(object $object, ?string $method, ...$parameters): mixed
{
$reflection = new \ReflectionMethod($object, $method);
$this->_reflectionObserver?->observe($reflection);
if($this->_reflectionObserver instanceof ReflectionInterrupt && $this->_reflectionObserver->shouldInterruptMethod())

foreach ($this->_reflectionObservers as $observer)
{
return $this->_reflectionObserver->interruptMethod();
$observer->observe($reflection);
if($observer instanceof ReflectionInterrupt && $observer->shouldInterruptMethod())
{
return $observer->interruptMethod();
}
}

return $this->_postResolve($reflection->invokeArgs($object, $this->_resolveParameters($reflection, $parameters)));
}

Expand All @@ -266,7 +280,12 @@ public function resolveMethod(object $object, ?string $method, ...$parameters):
public function resolveObject(string $className, ...$parameters): object
{
$reflection = new \ReflectionClass($className);
$this->_reflectionObserver?->observe($reflection);

foreach ($this->_reflectionObservers as $observer)
{
$observer->observe($reflection);
}

$constructor = $reflection->getConstructor();
if($constructor)
{
Expand Down
20 changes: 20 additions & 0 deletions tests/DependencyInjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Packaged\Tests\DiContainer\Supporting\MethodCaller;
use Packaged\Tests\DiContainer\Supporting\NeedyObject;
use Packaged\Tests\DiContainer\Supporting\ResolvableObject;
use Packaged\Tests\DiContainer\Supporting\SecondObserver;
use Packaged\Tests\DiContainer\Supporting\ServiceInterface;
use Packaged\Tests\DiContainer\Supporting\ServiceOne;
use Packaged\Tests\DiContainer\Supporting\ServiceTwo;
Expand Down Expand Up @@ -344,4 +345,23 @@ public function testWatchResolveAttributes()
static::assertInstanceOf(AttributeWatcherTest::class, $wt2);
static::assertCount(3, $attributes->attributes());
}

public function testWatchWithMultipleObservers()
{
$di = new DependencyInjector();

// Resolve still functions without a watcher
$wt = $di->resolve(AttributeWatcherTest::class);
static::assertInstanceOf(AttributeWatcherTest::class, $wt);

// With resolver
$attributes = new AttributeWatcher();
$another = new SecondObserver();
$di->addReflectionObserver($attributes);
$di->addReflectionObserver($another);
$wt2 = $di->resolve(AttributeWatcherTest::class);
static::assertInstanceOf(AttributeWatcherTest::class, $wt2);
static::assertCount(3, $attributes->attributes());
static::assertCount(2, $another->attributes());
}
}
31 changes: 31 additions & 0 deletions tests/Supporting/SecondObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Packaged\Tests\DiContainer\Supporting;

use Packaged\DiContainer\ReflectionObserver;

class SecondObserver implements ReflectionObserver
{
protected array $_attributes = [];

public function attributes(): array
{
return $this->_attributes;
}

public function observe($reflection): void
{
if($reflection instanceof \ReflectionClass || $reflection instanceof \ReflectionMethod)
{
$attributes = $reflection->getAttributes();

foreach($attributes as $attribute)
{
if ($attribute->getName() != 'Packaged\Tests\DiContainer\abc')
{
$this->_attributes[] = $attribute;
}
}
}
}
}