Skip to content

Commit

Permalink
fixed compability with older Nette 3 DI
Browse files Browse the repository at this point in the history
  • Loading branch information
petrparolek authored and matej21 committed Feb 5, 2020
1 parent cfcf3c7 commit ec2b90f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
parameters:
ignoreErrors:
- '#Call to an undefined method Nette\\DI\\Container::setAppContainer\(\)#'
- '#Parameter \#3 \$getter of static method Nette\\DI\\Resolver::autowireArguments\(\) expects callable, Nette\\DI\\Container given#'
2 changes: 2 additions & 0 deletions src/MangoTesterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nette;
use Nette\DI\CompilerExtension;
use Nette\DI\Container;
use Nette\DI\Definitions\ServiceDefinition;
use Nette\DI\Statement;


Expand Down Expand Up @@ -64,6 +65,7 @@ public function beforeCompile()
{
$builder = $this->getContainerBuilder();
foreach ($builder->findByTag(self::TAG_REQUIRE) as $service => $attrs) {
/** @var ServiceDefinition $def */
$def = $builder->getDefinition($service);
if (is_string($attrs) && strpos($attrs, '\\') === false) {
$def->setFactory(new Statement([$this->prefix('@appContainer'), 'getService'], [$attrs]));
Expand Down
20 changes: 14 additions & 6 deletions src/MethodArgumentsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Nette\DI\Helpers;
use Nette\DI\Resolver;
use Nette\Utils\Strings;
use ReflectionClass;


class MethodArgumentsResolver
Expand All @@ -15,13 +16,20 @@ public function resolve(\ReflectionMethod $method, Container $appContainer, arra
{
$fixedArgs = $this->prepareArguments($method, $appContainer);

$getter = function (string $type, bool $single) use ($appContainer) {
return $single
? $appContainer->getByType($type)
: array_map([$appContainer, 'getService'], $appContainer->findAutowired($type));
};
$ref = new ReflectionClass(Resolver::class);
$params = $ref->getMethod('autowireArguments')->getParameters();

return Resolver::autowireArguments($method, $args + $fixedArgs, $getter);
if ($params[2]->name == 'resolver') {
return Resolver::autowireArguments($method, $args + $fixedArgs, $appContainer);
} elseif ($params[2]->name == 'getter') {
$getter = function (string $type, bool $single) use ($appContainer) {
return $single
? $appContainer->getByType($type)
: array_map([$appContainer, 'getService'], $appContainer->findAutowired($type));
};

return Resolver::autowireArguments($method, $args + $fixedArgs, $getter);
}
}


Expand Down

0 comments on commit ec2b90f

Please sign in to comment.