-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from charonlab/feature/autowire
- Loading branch information
Showing
25 changed files
with
652 additions
and
120 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
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,59 @@ | ||
## Interface Binding | ||
|
||
You can bind an interface to a specific implementation using the `bind` method: | ||
```php | ||
$container->bind('YourVendor\Interfaces\SomeInterface', 'YourVendor\Implementations\SomeImplementation'); | ||
``` | ||
|
||
Now, whenever you request YourVendor\Interfaces\SomeInterface from the container, it will resolve to YourVendor\Implementations\SomeImplementation. | ||
|
||
## Shared Binding | ||
|
||
To mark an implementation as shared (singleton), you can use the `shared` method: | ||
```php | ||
// Bind an interface to a concrete implementation as a singleton | ||
$container->singleton('YourVendor\Interfaces\SomeInterface', 'YourVendor\Implementations\SomeImplementation'); | ||
``` | ||
|
||
This ensures that the same instance of the implementation is returned on subsequent requests. | ||
|
||
## Autowiring | ||
|
||
This container supports autowiring, allowing automatic resolution of dependencies without manual configuration. | ||
|
||
```php | ||
use YourVendor\Container\Container; | ||
|
||
// Create a new container instance | ||
$container = new Container(); | ||
|
||
// Register a class without explicit bindings | ||
$dependency = $container->get('YourVendor\SomeClass'); | ||
$dependency->doSomething(); | ||
``` | ||
|
||
## Parameter Resolution | ||
|
||
You can also resolve dependencies with parameters: | ||
|
||
```php | ||
// Create a new container instance | ||
$container = new Container(); | ||
|
||
// Define a class with constructor parameters | ||
class SomeClassWithParameters | ||
{ | ||
public function __construct($param1, $param2) | ||
{ | ||
// ... | ||
} | ||
} | ||
|
||
// Resolve the class with specified parameters | ||
$dependency = $container->make('SomeClassWithParameters', | ||
[ | ||
'param1' => 'value1', | ||
'param2' => 'value2' | ||
] | ||
); | ||
``` |
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,9 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<files psalm-version="dev-master@b940c7e92d4616837ab1f09244ba94e77c5bb100"> | ||
<file src="src/Container.php"> | ||
<MixedReturnStatement> | ||
<code><![CDATA[$concrete($this)]]></code> | ||
<code><![CDATA[$parameter->getDefaultValue()]]></code> | ||
</MixedReturnStatement> | ||
</file> | ||
</files> |
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,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the charonlab/charon-container. | ||
* | ||
* Copyright (C) 2023-2024 Charon Lab Development Team | ||
* | ||
* This software may be modified and distributed under the terms | ||
* of the MIT license. See the LICENSE.md file for details. | ||
*/ | ||
|
||
namespace Charon\Container; | ||
|
||
use Closure; | ||
|
||
interface BindableContainerInterface | ||
{ | ||
/** | ||
* Binds a given concrete with the container. | ||
* | ||
* @param class-string|string $abstract | ||
* The container alias. | ||
* @param \Closure|null|string $concrete | ||
* The binding. | ||
* @param bool $shared | ||
* Sets a shared binding. | ||
* | ||
* @return void | ||
*/ | ||
public function bind(string $abstract, Closure|null|string $concrete, bool $shared = false): void; | ||
|
||
/** | ||
* Binds a given concrete with the container as shared instance. | ||
* | ||
* @param class-string|string $abstract | ||
* The container alias. | ||
* @param \Closure|null|string $concrete | ||
* The binding. | ||
* | ||
* @return void | ||
*/ | ||
public function shared(string $abstract, Closure|null|string $concrete): void; | ||
|
||
/** | ||
* Check if a given abstract is a shared instance. | ||
* | ||
* @param class-string|string $abstract | ||
* The container alias. | ||
* | ||
* @return bool | ||
* Returns TRUE if given abstract is shared, otherwise FALSE. | ||
*/ | ||
public function isShared(string $abstract): bool; | ||
} |
Oops, something went wrong.