-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add AnnotatedWith attribute, and AnnotatedResolverFactory that scans …
…for attributes, allow to execute code before compilation via ContainerCompileInterface
- Loading branch information
1 parent
80e4914
commit 9894c8e
Showing
19 changed files
with
562 additions
and
54 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
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,31 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* | ||
* This file is part of Aura for PHP. | ||
* | ||
* @license http://opensource.org/licenses/MIT MIT | ||
* | ||
*/ | ||
|
||
namespace Aura\Di\Attribute; | ||
|
||
use Attribute; | ||
use Aura\Di\Injection\LazyAnnotatedWith; | ||
use Aura\Di\Injection\LazyInterface; | ||
|
||
#[Attribute(Attribute::TARGET_PARAMETER)] | ||
class AnnotatedWith implements AnnotatedInjectInterface | ||
{ | ||
private string $name; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
} | ||
|
||
public function inject(): LazyInterface | ||
{ | ||
return new LazyAnnotatedWith($this->name); | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* | ||
* This file is part of Aura for PHP. | ||
* | ||
* @license http://opensource.org/licenses/MIT MIT | ||
* | ||
*/ | ||
|
||
namespace Aura\Di\Attribute; | ||
|
||
interface DefineInterface | ||
{ | ||
public function define(\Reflector $reflector): mixed; | ||
} |
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
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
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,29 @@ | ||
<?php | ||
declare(strict_types=1); | ||
/** | ||
* | ||
* This file is part of Aura for PHP. | ||
* | ||
* @license http://opensource.org/licenses/MIT MIT | ||
* | ||
*/ | ||
namespace Aura\Di; | ||
|
||
/** | ||
* | ||
* An interface for a set of Container configuration instructions. | ||
* | ||
* @package Aura.Di | ||
* | ||
*/ | ||
interface ContainerCompileInterface extends ContainerConfigInterface | ||
{ | ||
/** | ||
* | ||
* Execute code after the Container is defined and before the Container is locked and compiled. | ||
* | ||
* @param Container $di The DI container. | ||
* | ||
*/ | ||
public function compile(Container $di): void; | ||
} |
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
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); | ||
/** | ||
* | ||
* This file is part of Aura for PHP. | ||
* | ||
* @license http://opensource.org/licenses/MIT MIT | ||
* | ||
*/ | ||
namespace Aura\Di\Injection; | ||
|
||
use Aura\Di\Resolver\Resolver; | ||
|
||
/** | ||
* | ||
* Returns a Container service when invoked. | ||
* | ||
* @package Aura.Di | ||
* | ||
*/ | ||
class LazyAnnotatedWith implements LazyInterface | ||
{ | ||
/** | ||
* | ||
* The name of the class. | ||
* | ||
* @var string | ||
* | ||
*/ | ||
protected string $className; | ||
|
||
/** | ||
* | ||
* Constructor. | ||
* | ||
* @param string $className The name to the class used for the annotation. | ||
* | ||
*/ | ||
public function __construct(string $className) | ||
{ | ||
$this->className = $className; | ||
} | ||
|
||
/** | ||
* | ||
* Invokes the closure to create the instance. | ||
* | ||
* @return array The list of the defined annotations. | ||
* | ||
*/ | ||
public function __invoke(Resolver $resolver): array | ||
{ | ||
$annotations = $resolver->annotations[$this->className] ?? []; | ||
|
||
foreach ($annotations as $key => $val) { | ||
if ($val instanceof LazyInterface) { | ||
$annotations[$key] = $val($resolver); | ||
} | ||
} | ||
|
||
return $annotations; | ||
} | ||
} |
Oops, something went wrong.