Skip to content

Commit

Permalink
Merge pull request #8 from charonlab/feature/autowire
Browse files Browse the repository at this point in the history
  • Loading branch information
nulxrd committed Mar 2, 2024
2 parents 6d03e9f + 470c391 commit f2fd655
Show file tree
Hide file tree
Showing 25 changed files with 652 additions and 120 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
## charonlab/charon-container

A fast and minimal dependency injection container.
This project is an open-source implementation of the PHP-FIG PSR-11 standard, which
defines a common interface for dependency injection containers.
PSR-11 aims to provide a simple and efficient Inversion of Control (IoC) container for PHP applications.

## Features
- **PSR-11 Compliance**: Adheres to the PSR-11 standard, ensuring compatibility with other components and frameworks that rely on this specification.
- **Dependency Injection (DI)**: Facilitates the injection of dependencies into your application components, promoting a modular and maintainable code structure.
- **Service Container**: Acts as a service container, managing the instantiation and retrieval of objects throughout your application.
- **Simplicity and Performance**: Focuses on simplicity and performance, providing a lightweight solution for dependency management.

## Installation

Expand Down
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
},
"autoload-dev": {
"psr-4": {
"Charon\\Tests\\Performance\\": "tests/performance/",
"Charon\\Tests\\Unit\\": "tests/unit/"
"Charon\\Tests\\Container\\": "tests/"
}
},
"config": {
Expand Down
59 changes: 59 additions & 0 deletions docs/autowire.md
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'
]
);
```
15 changes: 9 additions & 6 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@
executionOrder="depends,defects"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="true"
failOnRisky="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
failOnNotice="true"
failOnWarning="true"
failOnRisky="true"
>
<coverage/>

<testsuites>
<testsuite name="unit test">
<directory>./tests/unit</directory>
</testsuite>
<testsuite name="performance test">
<directory>./tests/performance</directory>
<testsuite name="Charon Container Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<source restrictDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>
<php>
<ini name="error_reporting" value="24575"/>
</php>
</phpunit>
9 changes: 9 additions & 0 deletions psalm-baseline.xml
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>
4 changes: 3 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
addParamDefaultToDocblockType="true"
findUnusedBaselineEntry="true"
findUnusedCode="false"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src"/>
<directory name="tests"/>
<ignoreFiles>
<directory name=".github"/>
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
</psalm>
</psalm>
54 changes: 54 additions & 0 deletions src/BindableContainerInterface.php
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;
}
Loading

0 comments on commit f2fd655

Please sign in to comment.