8000 PHP-DI 7: Drop support for `@Inject` annotations in favor of attributes by mnapoli · Pull Request #812 · PHP-DI/PHP-DI · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

PHP-DI 7: Drop support for @Inject annotations in favor of attributes #812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2022
Merged
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
3 changes: 2 additions & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
'php_unit_strict' => true,
'php_unit_strict' => false,
'phpdoc_add_missing_param_annotation' => false,
'phpdoc_align' => false,
'phpdoc_annotation_without_dot' => false,
Expand All @@ -49,6 +49,7 @@
'yoda_style' => false,
'native_function_invocation' => false,
'single_line_throw' => false,
'php_unit_method_casing' => false,
])
->setRiskyAllowed(true)
->setFinder($finder);
2 changes: 0 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"require-dev": {
"phpunit/phpunit": "^9.5",
"mnapoli/phpunit-easymock": "^1.3",
"doctrine/annotations": "~1.10",
"ocramius/proxy-manager": "^2.11.2",
"friendsofphp/php-cs-fixer": "^3",
"vimeo/psalm": "^4.6"
Expand All @@ -41,7 +40,6 @@
"psr/container-implementation": "^1.0"
},
"suggest": {
"doctrine/annotations": "Install it if you want to use annotations (version ^1.7)",
"ocramius/proxy-manager": "Install it if you want to use lazy injection (version ^2.3)"
}
}
2 changes: 1 addition & 1 deletion couscous.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ menu:
text: PHP definitions
url: doc/php-definitions.html
attributes:
text: PHP 8 attributes
text: PHP attributes
url: doc/attributes.html
annotations:
text: Annotations
Expand Down
2 changes: 1 addition & 1 deletion doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ title: Documentation index
* [Introduction](definition.md)
* [Autowiring](autowiring.md)
* [PHP definitions](php-definitions.md)
* [Annotations](annotations.md)
* [Attributes](attributes.md)
* [Definition extensions and overriding](definition-overriding.md)

### Frameworks integration
Expand Down
146 changes: 1 addition & 145 deletions doc/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,148 +5,4 @@ current_menu: annotations

# Annotations

**Since PHP 8, annotations are deprecated in favor of [PHP attributes](attributes.md).**

---

On top of [autowiring](autowiring.md) and [PHP configuration files](php-definitions.md), you can define injections using annotations.

Using annotations do not affect performances when [compiling the container](performances.md).

## Installation

Annotations **are disabled by default**. To be able to use them, you first need to install the [Doctrine Annotations](http://doctrine-common.readthedocs.org/en/latest/reference/annotations.html) library using Composer:

```
composer require doctrine/annotations
```

Then you need to [configure the `ContainerBuilder`](container-configuration.md) to use them:

```php
$containerBuilder->useAnnotations(true);
```

Annotations are written in PHP docblock comments. They are used by a lot of modern libraries and frameworks, like [Doctrine](http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/index.html), [Symfony](http://symfony.com/) and more.

## @Inject

`@Inject` lets you define where PHP-DI should inject something, and optionally what it should inject.

It can be used on:

- the constructor (constructor injection)
- methods (setter/method injection)
- properties (property injection)

*Note: property injections occur after the constructor is executed, so any injectable property will be null inside `__construct`.*

**Since PHP-DI 7, `@Inject` will ignore types declared in phpdoc. Only types specified in PHP code will be considered.**

Here is an example of all possible uses of the `@Inject` annotation:

```php
class Example
{
/**
* Annotation combined with a type on the property:
*
* @Inject
*/
private Foo $property1;

/**
* Explicit definition of the entry to inject:
*
* @Inject("db.host")
*/
private $property2;

/**
* Annotation specifying exactly what to inject:
*
* @Inject({"db.host", "db.name"})
*/
public function __construct($param1, $param2)
{
}

/**
* Annotation combined with PHP types:
*
* @Inject
*/
public function method1(Foo $param)
{
}

/**
* Explicit definition of the entries to inject:
*
* @Inject({"db.host", "db.name"})
*/
public function method2($param1, $param2)
{
}

/**
* Explicit definition of parameters by their name
* (types are used for the other parameters):
*
* @Inject({"param2" = "db.host"})
*/
public function method3(Foo $param1, $param2)
{
}
}
```

*Note: importing annotations with `use DI\Annotation\Inject;` is optional.*

### Troubleshooting @Inject

- you must use double quotes (`"`) instead of single quotes(`'`), for example: `@Inject("foo")`
- what's inside `@Inject()` must be in quotes, even if it's a class name: `@Inject("Acme\Blog\ArticleRepository")`
- `@Inject` is not meant to be used on the method to call with [`Container::call()`](container.md#call) (it will be ignored)
- **Since PHP-DI 7, `@Inject` will ignore types declared in phpdoc. Only types specified in PHP code will be considered.**

Note that `@Inject` is implicit on all constructors.

## Injectable

The `@Injectable` annotation lets you set options on injectable classes:

```php
/**
* @Injectable(lazy=true)
*/
class Example
{
}
```

**The `@Injectable` annotation is optional: by default, all classes are injectable.**

## Limitations

There are things that can't be defined with annotations:

- values (instead of classes)
- mapping interfaces to implementations
- defining entries with an anonymous function

For that, you can combine annotations with [definitions in PHP](php-definitions.md).

## Troubleshooting

Since annotations are in PHP docblocks, the opcache option `opcache.save_comments` must be set to `1`. If it is set to `0`, comments will be stripped from the source code and annotations will not work.

The default value for this option is `1` so everything should work by default.

To check the value of this option, you can run the following command:

```
$ php -i | grep "opcache.save_comments"
```

Furthermore, please mind that annotations are case-sensitive. You should write `@Inject` and `@Injectable` instead of `@inject` and `@injectable` to avoid bugs on certain systems.
**Since PHP-DI 7, annotations have been replaced by [PHP attributes](attributes.md).**
4 changes: 2 additions & 2 deletions doc/container-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PHP-DI's container is preconfigured for "plug and play". You can start using it
$container = new Container();
```

By default, [Autowiring](definition.md) will be enabled. [Annotations](annotations.md) are disabled by default.
By default, [Autowiring](definition.md) will be enabled. [Attributes](attributes.md) are disabled by default.

To register [definitions using an array](php-definitions.md):

Expand Down Expand Up @@ -51,7 +51,7 @@ If you want to use PHP-DI's container as a simple container (no autowiring or an
```php
$builder = new \DI\ContainerBuilder();
$builder->useAutowiring(false);
$builder->useAnnotations(false);
$builder->useAttributes(false);

$container = $builder->build();
```
Expand Down
6 changes: 2 additions & 4 deletions doc/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,7 @@ Example:
```php
class UserController extends BaseController
{
/**
* @Inject
*/
#[Inject]
private SomeService $someService;

public function __construct()
Expand All @@ -207,7 +205,7 @@ class UserController extends BaseController
```

As you might have guessed, you can't use constructor injection with this method.
But other kind of injections (property or setter) will work, whether you use annotations
But other kind of injections (property or setter) will work, whether you use attributes
or whether you configured your object in a definition file.

## Extending the container
Expand Down
16 changes: 8 additions & 8 deletions doc/definition-overriding.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ current_menu: definition-overriding

# Definition extensions and overriding

A simple application usually takes advantage of one or two *definition sources*: autowiring (or annotations) + a definition file/array.
A simple application usually takes advantage of one or two *definition sources*: autowiring (with or without attributes) + a definition file/array.

However in more complex applications or modular systems you might want to have multiple definition files (e.g. one per modules/bundles/plugins/…). In this case, PHP-DI provides a clear and powerful system to **override and/or extend definitions**.

Expand All @@ -14,7 +14,7 @@ However in more complex applications or modular systems you might want to have m
From the lowest priority to the highest:

- autowiring if enabled
- annotations if enabled
- attributes if enabled
- PHP definitions (file or array) in the order they were added
- definitions added straight in the container with `$container->set()`

Expand All @@ -29,21 +29,21 @@ class Foo
}
```

PHP-DI would inject an instance of `Bar` using autowiring. Annotations have a higher priority, we can use it to override the definition:
PHP-DI would inject an instance of `Bar` using autowiring. Attributes have a higher priority, we can use it to override the definition:

```php
use DI\Attribute\Inject;

class Foo
{
/**
* @Inject({"my.specific.service"})
*/
#[Inject(['my.specific.service'])]
public function __construct(Bar $param1)
{
}
}
```

You can go even further by overriding annotations and autowiring using file-based definitions:
You can go even further by overriding attributes and autowiring using file-based definitions:

```php
return [
Expand All @@ -61,7 +61,7 @@ If we had another definition file (registered after this one), we could override

`DI\create()` overrides completely any previous definition or even autowiring. It doesn't allow extending another definition. See the "decorators" section below if you want to do that.

If an object is built using autowiring (or annotations), you can override specific parameters with `DI\autowire()`:
If an object is built using autowiring (with or without attributes), you can override specific parameters with `DI\autowire()`:

```php
class Foo
Expand Down
8 changes: 4 additions & 4 deletions doc/definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ current_menu: definition-introduction
To let PHP-DI know what to inject and where, you have several options:

- use [autowiring](autowiring.md)
- use [annotations](annotations.md)
- use [attributes](attributes.md)
- use [PHP definitions](php-definitions.md)

You can also use several or all these options at the same time if you want to.
Expand All @@ -17,7 +17,7 @@ If you combine several sources, there are priorities that apply. From the highes

- Explicit definition on the container (i.e. defined with `$container->set()`)
- PHP file definitions (if you add several configuration files, then the last one can override entries from the previous ones)
- Annotations
- PHP attributes
- Autowiring

Read more in the [Definition overriding documentation](definition-overriding.md)
Expand All @@ -27,9 +27,9 @@ Read more in the [Definition overriding documentation](definition-overriding.md)

See the dedicated documentation about [autowiring](autowiring.md).

## Annotations
## Attributes

See the dedicated documentation about [annotations](annotations.md).
See the dedicated documentation about [attributes](attributes.md).

## PHP configuration

Expand Down
2 changes: 1 addition & 1 deletion doc/frameworks/silex.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ $app->run();

## Benefits

Using PHP-DI in Silex allows you to use all the awesome features of PHP-DI to wire your dependencies (using the definition files, autowiring, annotations, …).
Using PHP-DI in Silex allows you to use all the awesome features of PHP-DI to wire your dependencies (using the definition files, autowiring, attributes, …).

Another big benefit of the PHP-DI integration is the ability to use dependency injection inside controllers, middlewares and param converters:

Expand Down
2 changes: 1 addition & 1 deletion doc/frameworks/slim.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class UserController
$app->delete('/user/{id}', [UserController::class, 'delete']);
```

Dependencies can then be injected in your controller using [autowiring, PHP-DI config files or even annotations](../definition.md).
Dependencies can then be injected in your controller using [autowiring, PHP-DI config files or even PHP attributes](../definition.md).

### Controller parameters

Expand Down
12 changes: 5 additions & 7 deletions doc/frameworks/symfony2.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ class ProductController
Example with property injection:

```php
use DI\Attribute\Inject;

class ProductController
{
/**
* @Inject
*/
#[Inject]
private ProductService $productService;

public function clearAction()
Expand Down Expand Up @@ -148,17 +148,15 @@ services:

### Service name aliases

PHP-DI can also work with autowiring or annotations. These rely on the fact that the service name
PHP-DI can also work with autowiring or PHP attributes. These rely on the fact that the service name
is the class name (or interface name), e.g. you reference the entity manager by its class name
instead of `doctrine.orm.entity_manager`.

If you want to enjoy autowiring or annotations, you can simplify your life and write simple aliases
If you want to enjoy autowiring or attributes, you can simplify your life and write simple aliases
like these:

```php
return [
'Psr\Log\LoggerInterface' => DI\get('logger'),
// PHP 5.5 notation:
ObjectManager::class => DI\get('doctrine.orm.entity_manager'),
];
```
Expand Down
Loading
0