Skip to content

Commit

Permalink
Merge master into v7
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Apr 9, 2022
2 parents 9eaa05e + ae0f1b3 commit 6cb9fa2
Show file tree
Hide file tree
Showing 24 changed files with 127 additions and 30 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
timeout-minutes: 15
strategy:
matrix:
php: [ '8.0' ]
php: [ '8.0', '8.1' ]
dependency-version: [ '' ]
include:
- php: '8.0'
Expand All @@ -40,7 +40,11 @@ jobs:
key: php-${{ matrix.php }}-composer-locked-${{ hashFiles('composer.lock') }}
restore-keys: php-${{ matrix.php }}-composer-locked-
- name: Install PHP dependencies
if: matrix.php != '8.1'
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: composer update --ignore-platform-reqs ${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-progress --no-suggest
- name: PHPUnit
run: vendor/bin/phpunit

Expand Down
4 changes: 4 additions & 0 deletions change-log.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log

## 6.1.0

- [#791](https://github.com/PHP-DI/PHP-DI/issues/791) Support PHP 8.1, remove support for PHP 7.2

## 6.0.2

- Fix potential regression introduced when fixing [#582](https://github.com/PHP-DI/PHP-DI/issues/582)
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
"php": ">=8.0",
"psr/container": "^1.1 || ^2.0",
"php-di/invoker": "^2.0",
"opis/closure": "^3.5.5"
"laravel/serializable-closure": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.3",
"phpunit/phpunit": "^9.5",
"mnapoli/phpunit-easymock": "^1.3",
"doctrine/annotations": "^1.7",
"ocramius/proxy-manager": "^2.3",
"doctrine/annotations": "~1.10",
"ocramius/proxy-manager": "^2.11.2",
"friendsofphp/php-cs-fixer": "^2.4",
"vimeo/psalm": "^4.6"
},
Expand Down
4 changes: 2 additions & 2 deletions couscous.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ menu:
zf1:
text: Zend Framework 1
url: doc/frameworks/zf1.html
ze:
zend-expressive:
text: Zend Expressive
url: doc/frameworks/zend-expressive.html
silly:
Expand Down Expand Up @@ -143,7 +143,7 @@ menu:
frameworks:
zf1: Zend Framework 1
zf2: Zend Framework 2
ze: Zend Expressive
zend-expressive: Zend Expressive
symfony2: Symfony
silex: Silex
slim: Slim
Expand Down
2 changes: 1 addition & 1 deletion doc/autowiring.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ current_menu: autowiring

In order to achieve that, PHP-DI uses [PHP's reflection](http://php.net/manual/book.reflection.php) to detect what parameters a constructor needs.

Autowiring does not affect performances when [compiling the container](performances.md).
Autowiring does not affect performance when [compiling the container](performances.md).

Let's take this example:

Expand Down
12 changes: 12 additions & 0 deletions doc/definition-overriding.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ When resolved, the array will contain the 2 entries. **If you forget to use `DI\

Note that you can use `DI\add()` even if the array was not declared before.

Adding to arrays in the DI definition does not work recursively, i.e. having an array entry that contains an array.

```php
return [
'array' => [
'subarray' => [DI\get(Entry::class)],
],
];
```

You cannot add elements to `subarray` using `DI\add()`.

### Decorators

You can use `DI\decorate()` to decorate an object:
Expand Down
2 changes: 1 addition & 1 deletion doc/frameworks/zend-expressive.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: documentation
current_menu: ze
current_menu: zend-expressive
---

# PHP-DI in Zend Expressive
Expand Down
1 change: 1 addition & 0 deletions doc/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ For this reason, PHP-DI integrates with some frameworks so that you don't have t
- [Slim](frameworks/slim.md)
- [Silex](frameworks/silex.md)
- [Zend Framework 2](frameworks/zf2.md)
- [Zend Expressive](frameworks/zend-expressive.md)
- [Silly](frameworks/silly.md)

If you want to use PHP-DI with another framework or your own code, try to use `$container->get()` in you root application class or front controller. Have a look at this [**demo application**](https://github.com/PHP-DI/demo) built around PHP-DI for a practical example.
Expand Down
2 changes: 1 addition & 1 deletion doc/lazy-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ $containerPHP->set('foo', \DI\create('MyClass')->lazy());

## When to use

Lazy injection requires to create proxy objects for each object you declare as `lazy`. It is not recommended to use this feature more that a few times in one application.
Lazy injection requires to create proxy objects for each object you declare as `lazy`. It is not recommended to use this feature more than a few times in one application.

While proxies are extremely optimized, they are only worth it if the object you define as lazy has a constructor that takes some time (e.g. connects to a database, writes to a file, etc.).

Expand Down
9 changes: 9 additions & 0 deletions doc/php-definitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,15 @@ return [
];
```

You can use aliases to map interfaces to implementation obtaining the same object when requesting/injecting the class or the interface

```php
return [
// mapping an interface to an implementation (autowire the MyLogger class if not otherwise defined)
'LoggerInterface' => DI\get('MyLogger'),
];
```

### Environment variables

You can get an environment variable's value using the `DI\env()` helper:
Expand Down
6 changes: 6 additions & 0 deletions src/CompiledContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
*/
abstract class CompiledContainer extends Container
{
/**
* This const is overridden in child classes (compiled containers).
* @var array
*/
protected const METHOD_MAPPING = [];

private ?InvokerInterface $factoryInvoker = null;

/**
Expand Down
9 changes: 4 additions & 5 deletions src/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use function dirname;
use function file_put_contents;
use InvalidArgumentException;
use Opis\Closure\SerializableClosure;
use Laravel\SerializableClosure\Support\ReflectionClosure;
use function rename;
use function sprintf;
use function tempnam;
Expand Down Expand Up @@ -224,7 +224,7 @@ private function compileDefinition(string $entryName, Definition $definition) :
$isOptional = $this->compileValue($definition->isOptional());
$defaultValue = $this->compileValue($definition->getDefaultValue());
$code = <<<PHP
\$value = getenv($variableName);
\$value = \$_ENV[$variableName] ?? \$_SERVER[$variableName] ?? getenv($variableName);
if (false !== \$value) return \$value;
if (!$isOptional) {
throw new \DI\Definition\Exception\InvalidDefinition("The environment variable '{$definition->getVariableName()}' has not been defined");
Expand Down Expand Up @@ -338,7 +338,7 @@ public function compileValue(mixed $value) : string

private function createCompilationDirectory(string $directory) : void
{
if (!is_dir($directory) && !@mkdir($directory, 0777, true)) {
if (!is_dir($directory) && !@mkdir($directory, 0777, true) && !is_dir($directory)) {
throw new InvalidArgumentException(sprintf('Compilation directory does not exist and cannot be created: %s.', $directory));
}
if (!is_writable($directory)) {
Expand Down Expand Up @@ -381,8 +381,7 @@ private function isCompilable($value) : string | bool
*/
private function compileClosure(\Closure $closure) : string
{
$wrapper = new SerializableClosure($closure);
$reflector = $wrapper->getReflector();
$reflector = new ReflectionClosure($closure);

if ($reflector->getUseVariables()) {
throw new InvalidDefinition('Cannot compile closures which import variables using the `use` keyword');
Expand Down
19 changes: 12 additions & 7 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ public function __construct(
/**
* Returns an entry of the container by its name.
*
* @param string $id Entry name or a class name.
* @template T
* @param string|class-string<T> $id Entry name or a class name.
*
* @throws DependencyException Error while resolving the entry.
* @throws NotFoundException No entry found for the given name.
* @return mixed|T
*/
public function get(string $id) : mixed
{
Expand Down Expand Up @@ -156,14 +158,16 @@ private function getDefinition(string $name) : ?Definition
*
* This method makes the container behave like a factory.
*
* @param string $name Entry name or a class name.
* @param array $parameters Optional parameters to use to build the entry. Use this to force specific parameters
* to specific values. Parameters not defined in this array will be resolved using
* the container.
* @template T
* @param string|class-string<T> $name Entry name or a class name.
* @param array $parameters Optional parameters to use to build the entry. Use this to force
* specific parameters to specific values. Parameters not defined in this
* array will be resolved using the container.
*
* @throws InvalidArgumentException The name parameter must be of type string.
* @throws DependencyException Error while resolving the entry.
* @throws NotFoundException No entry found for the given name.
* @return mixed|T
*/
public function make(string $name, array $parameters = []) : mixed
{
Expand Down Expand Up @@ -200,10 +204,11 @@ public function has(string $id) : bool
/**
* Inject all dependencies on an existing instance.
*
* @param object $instance Object to perform injection upon
* @template T
* @param object|T $instance Object to perform injection upon
* @throws InvalidArgumentException
* @throws DependencyException Error while injecting dependencies
* @return object $instance Returns the same instance
* @return object|T $instance Returns the same instance
*/
public function injectOn(object $instance) : object
{
Expand Down
3 changes: 2 additions & 1 deletion src/Definition/Exception/InvalidDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
namespace DI\Definition\Exception;

use DI\Definition\Definition;
use Psr\Container\ContainerExceptionInterface;

/**
* Invalid DI definitions.
*
* @author Matthieu Napoli <[email protected]>
*/
class InvalidDefinition extends \Exception
class InvalidDefinition extends \Exception implements ContainerExceptionInterface
{
public static function create(Definition $definition, string $message, \Exception $previous = null) : self
{
Expand Down
2 changes: 1 addition & 1 deletion src/Definition/Resolver/ArrayResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function resolve(Definition $definition, array $parameters = []) : array
$values = $definition->getValues();

// Resolve nested definitions
array_walk_recursive($values, function (&$value, $key) use ($definition) {
array_walk_recursive($values, function (& $value, $key) use ($definition) {
if ($value instanceof Definition) {
$value = $this->resolveDefinition($value, $definition, $key);
}
Expand Down
17 changes: 16 additions & 1 deletion src/Definition/Resolver/EnvironmentVariableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
*/
class EnvironmentVariableResolver implements DefinitionResolver
{
/** @var callable */
private $variableReader;

public function __construct(
private DefinitionResolver $definitionResolver,
private $variableReader = 'getenv',
$variableReader = null
) {
$this->variableReader = $variableReader ?? [$this, 'getEnvVariable'];
}

/**
Expand Down Expand Up @@ -57,4 +61,15 @@ public function isResolvable(Definition $definition, array $parameters = []) : b
{
return true;
}

protected function getEnvVariable(string $variableName)
{
if (isset($_ENV[$variableName])) {
return $_ENV[$variableName];
} elseif (isset($_SERVER[$variableName])) {
return $_SERVER[$variableName];
}

return getenv($variableName);
}
}
1 change: 1 addition & 0 deletions src/Proxy/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ private function proxyManager() : LazyLoadingValueHolderFactory
if ($this->proxyDirectory) {
$config->setProxiesTargetDir($this->proxyDirectory);
$config->setGeneratorStrategy(new FileWriterGeneratorStrategy(new FileLocator($this->proxyDirectory)));
// @phpstan-ignore-next-line
spl_autoload_register($config->getProxyAutoloader());
} else {
$config->setGeneratorStrategy(new EvaluatingGeneratorStrategy());
Expand Down
31 changes: 31 additions & 0 deletions tests/IntegrationTest/ContainerPsrCompatabilityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace DI\Test\IntegrationTest;

use DI\ContainerBuilder;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Container\ContainerExceptionInterface;
use DI\Test\IntegrationTest\Fixtures\Class1;

class ContainerPsrCompatabilityTest extends BaseContainerTest
{
/**
* @dataProvider provideContainer
*/
public function testNotFound(ContainerBuilder $builder)
{
$this->expectException(NotFoundExceptionInterface::class);
$builder->build()->get('key');
}

/**
* @dataProvider provideContainer
*/
public function testUnresolvable(ContainerBuilder $builder)
{
$this->expectException(ContainerExceptionInterface::class);
$builder->build()->get(Class1::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ public function test_closure_with_static_variables_are_supported(ContainerBuilde

public function test_multiple_closures_on_the_same_line_cannot_be_compiled()
{
$this->markTestSkipped('Opis/closure doesn\'t throw on multiple closures on the same line');
$this->markTestSkipped('laravel/serializable-closure doesn\'t throw on multiple closures on the same line');

$this->expectException(InvalidDefinition::class);
$this->expectExceptionMessage('Cannot compile closures when two closures are defined on the same line');
Expand Down
11 changes: 8 additions & 3 deletions website/default.twig
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<link href="{{ baseUrl }}/css/highlight.github.css" rel="stylesheet">
<link href="{{ baseUrl }}/css/all.min.css" rel="stylesheet">
{% endblock %}

<script async defer data-domain="php-di.org" src="https://plausible.io/js/plausible.js"></script>
</head>
<body>

Expand Down Expand Up @@ -75,7 +77,7 @@
{% block footerTop %}
<p>
A question? Unsatisfied with the documentation?
Please <a href="https://github.com/PHP-DI/PHP-DI/issues/new">create an issue</a>
<a href="https://github.com/PHP-DI/PHP-DI/issues/new">Open an issue</a>
or chat on
<a href="https://gitter.im/PHP-DI/PHP-DI" title="PHP-DI chat room">Gitter</a> or
<a href="https://twitter.com/phpdi" title="PHP-DI on Twitter">Twitter</a>.
Expand All @@ -90,8 +92,11 @@
and <a href="https://github.com/PHP-DI/PHP-DI/graphs/contributors" title="PHP-DI contributors">contributors</a>
|
Website generated with
<a href="http://couscous.io" title="Static website generator for software documentation">Couscous</a>,
supported by <a href="https://null.tc/">null</a>.
<a href="http://couscous.io" title="Static website generator for software documentation">Couscous</a>.
</p>
<p>
Want to support my work? Check out
<a href="https://serverless-visually-explained.com/?ref=php-di" title="Serverless course">Serverless Visually Explained</a>.
</p>
</div>
</footer>
Expand Down
6 changes: 5 additions & 1 deletion website/home_sponsors.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
<div>
<a href="https://www.jetbrains.com/" title="JetBrains - Creator of PhpStorm">
<img src="{{ baseUrl }}/img/sponsors/logo-jetbrains.svg" alt="JetBrains" style="height: 80px; margin-left: 30px"></a>
<a class="block self-center mx-6 mb-6" href="https://null.tc/" title="Null - Making serverless simpler">
<img src="{{ baseUrl }}/img/sponsors/logo-null.png" alt="Null" style="width: 70px; margin-left: 50px"></a>
<a class="block self-center mx-6 mb-6" href="https://www.amezmo.com/" title="Amezmo - Cloud hosting and deployment for PHP">
<img src="{{ baseUrl }}/img/sponsors/logo-amezmo.svg" alt="Amezmo" style="width: 90px; margin-top: 5px; margin-left: 50px"></a>
<img src="{{ baseUrl }}/img/sponsors/logo-amezmo.svg" alt="Amezmo" style="width: 90px; margin-top: 8px; margin-left: 50px"></a>
<a class="block self-center mx-6 mb-6" href="https://www.web-id.fr/" title="Web-ID - Laravel web agency">
<img src="{{ baseUrl }}/img/sponsors/logo-web-id.png" alt="Web-ID" style="height: 45px; margin-left: 50px"></a>
<a class="block self-center mx-6 mb-6" href="https://activecollab.com/" title="ActiveCollab">
<img src="{{ baseUrl }}/img/sponsors/logo-activecollab.png" alt="ActiveCollab" style="height: 35px; margin-left: 50px"></a>
</div>

<p class="text-center">
Expand Down
File renamed without changes
Binary file added website/img/sponsors/logo-activecollab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added website/img/sponsors/logo-null.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6cb9fa2

Please sign in to comment.