8000 PHP-DI 7.0 by mnapoli · Pull Request #726 · 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.0 #726

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 60 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
1c0a168
Drop support for phpdoc types with @Inject by moving to PHP 7.4+
mnapoli Aug 1, 2020
dde4627
Remove the obsolete `ignorePhpDocErrors` option
mnapoli Aug 1, 2020
0f3b69f
Fix code formatting
mnapoli Aug 1, 2020
15afdad
Improve type declarations following the PHP 7.4 requirement
mnapoli Aug 1, 2020
3d9ac0c
Fix coding style
mnapoli Aug 1, 2020
4cdea6c
Remove useless config files
mnapoli Aug 1, 2020
a74041b
Update dependency versions to fix tests
mnapoli Aug 1, 2020
3585442
Improve support for PHP 8
mnapoli Aug 2, 2020
15ef9d5
Inject via PHP 8 attributes
mnapoli Sep 28, 2020
4aeb4ac
Merge pull request #738 from PHP-DI/php8-attributes
mnapoli Oct 5, 2020
b7d3d8f
Fix PHP 8 attributes 🤦
mnapoli Oct 5, 2020
4b4e31f
Merge branch 'master' into v7
mnapoli Oct 12, 2020
0ea9d81
Update CI for v7
mnapoli Oct 12, 2020
2a8bb30
Support PHP 8 attributes at the parameter level
mnapoli Oct 22, 2020
f31f16a
Merge pull request #741 from PHP-DI/v7-php8-parameter-attributes
mnapoli Oct 22, 2020
a1be38d
Test and document using PHP 8 named arguments for config
mnapoli Feb 27, 2021
b6de725
Require PHP 8.0 and use stricter types/fix warnings
mnapoli Feb 27, 2021
95a1a77
Fix psalm warnings
mnapoli Feb 27, 2021
18b192f
Require PHP 8
mnapoli Feb 27, 2021
91d2f1a
Psalm templates
mnapoli Feb 27, 2021
20873db
More psalm fixes
mnapoli Feb 27, 2021
bc862ff
Replace phpstan with psalm
mnapoli Feb 27, 2021
2017dc5
Fix code formatting
mnapoli Feb 27, 2021
849a06a
Merge pull request #766 from PHP-DI/php8-only
mnapoli Mar 6, 2021
da0912c
Fixes after upping the requirement to PHP 8
mnapoli Mar 6, 2021
ad2ba80
Merge branch 'master' into v7
mnapoli Mar 6, 2021
d842943
Fix minimum stability
mnapoli Mar 6, 2021
9df6976
Require psr/container 1.1 to strictly type parameters
mnapoli Mar 6, 2021
ec8fbdb
Remove now useless test
mnapoli Mar 6, 2021
4291647
Run APCu tests in CI
mnapoli Mar 6, 2021
902cc91
Support psr/container 2.0
mnapoli Mar 6, 2021
efa8ccd
Run APCu tests in CI
mnapoli Mar 6, 2021
957f926
Allow to create the container with definitions
mnapoli Apr 2, 2021
afb1ca7
Remove obsolete ContainerBuilder::buildDevContainer()
mnapoli Apr 2, 2021
99b0104
Improve the "Getting started" documentation
mnapoli Apr 2, 2021
50819b5
Fix CS
mnapoli Apr 2, 2021
9eaa05e
Merge pull request #769 from PHP-DI/simpler-container-creation
mnapoli Apr 4, 2021
6cb9fa2
Merge master into v7
mnapoli Apr 9, 2022
d01fdf9
Simplify method
mnapoli Apr 9, 2022
aa0ea10
Various code fixes and improvements (PHPStorm warnings)
mnapoli Apr 9, 2022
236f434
Upgrade php-cs-fixer
mnapoli Apr 9, 2022
1117da7
Force strict types
mnapoli Apr 9, 2022
891bd5e
Improve code style
mnapoli Apr 9, 2022
c3891b0
Drop support for `@Inject` annotations in favor of attributes
mnapoli Apr 9, 2022
8d35559
Merge pull request #812 from PHP-DI/drop-annotations
mnapoli Apr 12, 2022
35f16fc
Merge branch 'master' into v7
mnapoli Jul 18, 2022
5458204
Merge remote-tracking branch 'origin/v7' into v7
mnapoli Jul 18, 2022
a5b25de
Merge branch 'master' into v7
mnapoli Aug 21, 2022
6e8d9f5
Fix build
mnapoli Aug 21, 2022
a326519
Merge branch 'master' into v7
mnapoli Jan 8, 2023
8948ffb
Prepare the blog post for the 7.0 release
mnapoli Jan 8, 2023
921a1c8
Remove non-analyzed vendor directory from Psalm ignores
szepeviktor Jan 8, 2023
f540193
Merge pull request #837 from szepeviktor/patch-3
mnapoli Jan 9, 2023
be4a41a
Fix typo
jdreesen Jan 9, 2023
39b2557
Fix typo
jdreesen Jan 9, 2023
ea93a1f
Merge pull request #839 from jdreesen/patch-1
mnapoli Jan 9, 2023
d3e0057
Correct type hints to prevent tooling like PHPStan from raising issues.
ldebrouwer-rmn Jan 9, 2023
6011611
Update src/Definition/Helper/CreateDefinitionHelper.php
ldebrouwer Jan 9, 2023
7a17403
Update src/Definition/Helper/CreateDefinitionHelper.php
ldebrouwer Jan 9, 2023
6d9eab0
Merge pull request #841 from ldebrouwer/ldebrouwer/correct-type-hints
mnapoli Jan 9, 2023
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
8000 Diff view
27 changes: 13 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:
timeout-minutes: 15
strategy:
matrix:
php: [ '7.4', '8.0', '8.1' ]
php: [ '8.0', '8.1' ]
dependency-version: [ '' ]
include:
- php: '7.4'
- php: '8.0'
dependency-version: '--prefer-lowest'
steps:
- name: Checkout
Expand All @@ -30,6 +30,9 @@ jobs:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none
# Enable apcu
extensions: apcu
ini-values: apc.enable_cli=1
- name: Cache Composer dependencies
uses: actions/cache@v2
with:
Expand All @@ -41,10 +44,7 @@ jobs:
run: composer update ${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-progress --no-suggest
- name: 'Install PHP dependencies on PHP 8.1 (TODO: remove that)'
if: matrix.php == '8.1'
run: |
# Install Composer 2
composer self-update --snapshot
composer update --ignore-platform-reqs ${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-progress --no-suggest
run: composer update --ignore-platform-reqs ${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-progress --no-suggest
- name: PHPUnit
run: vendor/bin/phpunit

Expand All @@ -57,8 +57,8 @@ jobs:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
tools: composer, cs2pr
php-version: 8.0
tools: composer:v2, cs2pr
coverage: none
- name: Cache Composer dependencies
uses: actions/cache@v2
Expand All @@ -71,17 +71,16 @@ jobs:
- name: PHP CS Fixer
run: vendor/bin/php-cs-fixer fix --dry-run --format=checkstyle | cs2pr

phpstan:
name: PHPStan
psalm:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
tools: composer, cs2pr
php-version: 8.0
tools: composer:v2, cs2pr
coverage: none
- name: Cache Composer dependencies
uses: actions/cache@v2
Expand All @@ -91,5 +90,5 @@ jobs:
restore-keys: php-composer-locked-
- name: Install PHP dependencies
run: composer install --no-interaction --no-progress --no-suggest
- name: PHPStan
run: vendor/bin/phpstan analyse --error-format=checkstyle | cs2pr
- name: Psalm
run: vendor/bin/psalm --output-format=checkstyle | cs2pr
91 changes: 48 additions & 43 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,55 @@
$finder = PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->notPath('Compiler/Template.php');
$config = new PhpCsFixer\Config;

return (new PhpCsFixer\Config)
->setRules([
'@PSR2' => true,
'@PHP70Migration' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'braces' => [
'allow_single_line_closure' => true,
],
'concat_space' => [
'spacing' => 'one',
],
//'declare_strict_types' => true,
'heredoc_to_nowdoc' => true,
'linebreak_after_opening_tag' => true,
'new_with_braces' => false,
'multiline_whitespace_before_semicolons' => false,
'no_php4_constructor' => true,
'no_unreachable_default_argument_value' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
'php_unit_strict' => true,
'phpdoc_add_missing_param_annotation' => false,
'phpdoc_align' => false,
'phpdoc_annotation_without_dot' => false,
'phpdoc_separation' => false,
'phpdoc_to_comment' => false,
'phpdoc_var_without_name' => true,
'pow_to_exponentiation' => true,
'unary_operator_spaces' => false,
'return_type_declaration' => [
'space_before' => 'one',
],
'semicolon_after_instruction' => true,
'strict_comparison' => true,
'strict_param' => true,
'yoda_style' => false,
'native_function_invocation' => false,
'single_line_throw' => false,
'blank_line_between_import_groups' => false,
return $config->setRules([
'@PSR2' => true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want PSR12 here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not interested in PSR-12 honestly. This is a rename of the file that exists on master.

'@PHP70Migration' => true,
'@PHP71Migration' => true,
'@PHP73Migration' => true,
'@PHP74Migration' => true,
'@PHP80Migration' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'braces' => [
'allow_single_line_closure' => true,
],
'concat_space' => [
'spacing' => 'one',
],
'declare_strict_types' => true,
'heredoc_to_nowdoc' => true,
'linebreak_after_opening_tag' => true,
'new_with_braces' => false,
'multiline_whitespace_before_semicolons' => false,
'no_php4_constructor' => true,
'no_unreachable_default_argument_value' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_imports' => true,
'php_unit_strict' => false,
'phpdoc_add_missing_param_annotation' => false,
'phpdoc_align' => false,
'phpdoc_annotation_without_dot' => false,
'phpdoc_separation' => false,
'phpdoc_to_comment' => false,
'phpdoc_var_without_name' => true,
'pow_to_exponentiation' => true,
'unary_operator_spaces' => false,
'return_type_declaration' => [
'space_before' => 'one',
],
'semicolon_after_instruction' => true,
'strict_comparison' => true,
'strict_param' => true,
'yoda_style' => false,
'native_function_invocation' => false,
'single_line_throw' => false,
'php_unit_method_casing' => false,
'blank_line_between_import_groups' => false,
'global_namespace_import' => false,
])
])
->setRiskyAllowed(true)
->setFinder($finder);
16 changes: 6 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,25 @@
},
"scripts": {
"test": "phpunit",
"format-code": "php-cs-fixer fix --allow-risky=yes",
"phpstan": "phpstan analyse -l 5 -c phpstan.neon src"
"format-code": "php-cs-fixer fix --allow-risky=yes"
},
"require": {
"php": ">=7.4.0",
"psr/container": "^1.0",
"php": ">=8.0",
"psr/container": "^1.1 || ^2.0",
"php-di/invoker": "^2.0",
"php-di/phpdoc-reader": "^2.0.1",
"laravel/serializable-closure": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"mnapoli/phpunit-easymock": "^1.2",
"doctrine/annotations": "~1.10",
"mnapoli/phpunit-easymock": "^1.3",
"ocramius/proxy-manager": "^2.11.2",
"friendsofphp/php-cs-fixer": "^3",
"phpstan/phpstan": "^1.9"
"vimeo/psalm": "^4.6"
},
"provide": {
"psr/container-implementation": "^1.0"
},
"suggest": {
"doctrine/annotations": "Install it if you want to use annotations (version ~1.2)",
"ocramius/proxy-manager": "Install it if you want to use lazy injection (version ~2.0)"
"ocramius/proxy-manager": "Install it if you want to use lazy injection (version ^2.3)"
}
}
3 changes: 3 additions & 0 deletions couscous.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ menu:
php-definitions:
text: PHP definitions
url: doc/php-definitions.html
attributes:
text: PHP attributes
url: doc/attributes.html
annotations:
text: Annotations
url: doc/annotations.html
Expand Down
3 changes: 2 additions & 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 All @@ -45,6 +45,7 @@ title: Documentation index
* [Migration from PHP-DI 3.x to 4.0](migration/4.0.md)
* [Migration from PHP-DI 4.x to 5.0](migration/5.0.md)
* [Migration from PHP-DI 5.x to 6.0](migration/6.0.md)
* [Migration from PHP-DI 6.x to 7.0](migration/7.0.md)

### Internals

Expand Down
140 changes: 1 addition & 139 deletions doc/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,142 +5,4 @@ current_menu: annotations

# Annotations

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/), [PHPUnit](http://www.phpunit.de/)…

## @Inject

`@Inject` lets you define where PHP-DI should inject something, and what it should inject. You can optionally combine it with `@var` and `@param` phpdoc tags to define what should be injected.

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`.*

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

```php
class Example
{
/**
* Annotation combined with phpdoc:
*
* @Inject
* @var Foo
*/
private $property1;

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

/**
* Annotation combined with phpdoc:
*
* @Inject
* @param Foo $param1
* @param Bar $param2
*/
public function __construct($param1, $param2)
{
}

/**
* Annotation combined with the type-hint:
*
* @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:
*
* @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")`
- when using `@Inject` in combination with `@var` or `@param`, make sure the class name is correctly imported if using namespaces (a good IDE will show warnings if not)
- `@Inject` is not meant to be used on the method to call with [`Container::call()`](container.md#call) (it will be ignored)

## 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).**
Loading
0