-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add autowire/autodiscovery/auto-initialization of interfaces to…
… classes (#501)
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Container; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS)] | ||
final class Autowire | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Container; | ||
|
||
use Tempest\Core\Discovery; | ||
use Tempest\Core\HandlesDiscoveryCache; | ||
use Tempest\Reflection\ClassReflector; | ||
|
||
/** | ||
* @property GenericContainer $container | ||
*/ | ||
final class AutowireDiscovery implements Discovery | ||
{ | ||
use HandlesDiscoveryCache; | ||
|
||
public function __construct( | ||
private readonly Container $container, | ||
) { | ||
} | ||
|
||
public function discover(ClassReflector $class): void | ||
{ | ||
$autowire = $class->getAttribute(Autowire::class); | ||
|
||
if ($autowire === null) { | ||
return; | ||
} | ||
|
||
if ($class->getAttribute(Singleton::class) !== null) { | ||
$this->discoverAsSingleton($class); | ||
} else { | ||
$this->discoverAsDefinition($class); | ||
} | ||
} | ||
|
||
private function discoverAsSingleton(ClassReflector $class): void | ||
{ | ||
$interfaces = $class->getReflection()->getInterfaceNames(); | ||
foreach ($interfaces as $interface) { | ||
$this->container->singleton($interface, fn (Container $container) => $container->get($class->getName())); | ||
} | ||
} | ||
|
||
private function discoverAsDefinition(ClassReflector $class): void | ||
{ | ||
$interfaces = $class->getReflection()->getInterfaceNames(); | ||
foreach ($interfaces as $interface) { | ||
$this->container->register($interface, fn (Container $container) => $container->get($class->getName())); | ||
} | ||
} | ||
|
||
public function createCachePayload(): string | ||
{ | ||
return serialize($this->container->getDefinitions()); | ||
} | ||
|
||
public function restoreCachePayload(Container $container, string $payload): void | ||
{ | ||
$this->container->setDefinitions(unserialize($payload)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters