Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed May 29, 2024
1 parent fccb92a commit c8c1840
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 27 deletions.
6 changes: 3 additions & 3 deletions config/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

return [
GiiInterface::class => function (Injector $injector) use ($params): GiiInterface {
$generatorsInstances = [];
$proxies = [];
$generators = $params['yiisoft/yii-gii']['generators'];

foreach ($generators as $generator) {
Expand All @@ -27,9 +27,9 @@
fn() => $injector->make($class, $generator['parameters'] ?? []),
$class,
);
$generatorsInstances[$class::getId()] = $loader;
$proxies[$class::getId()] = $loader;
}
return new Gii($generatorsInstances);
return new Gii($proxies, []);
},
ParametersProvider::class => [
'class' => ParametersProvider::class,
Expand Down
10 changes: 9 additions & 1 deletion src/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Yiisoft\Yii\Gii\Exception\InvalidGeneratorCommandException;
use Yiisoft\Yii\Gii\Generator\CommandHydrator;
use Yiisoft\Yii\Gii\GeneratorCommandInterface;
use Yiisoft\Yii\Gii\GeneratorInterface;
use Yiisoft\Yii\Gii\GeneratorProxy;
use Yiisoft\Yii\Gii\GiiInterface;
use Yiisoft\Yii\Gii\ParametersProvider;
Expand All @@ -39,7 +40,14 @@ public function list(GiiInterface $gii): ResponseInterface
return $this->responseFactory->createResponse([
'generators' => array_map(
$this->serializeGenerator(...),

Check failure on line 42 in src/Controller/DefaultController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

PossiblyInvalidArgument

src/Controller/DefaultController.php:42:17: PossiblyInvalidArgument: Parameter 1 of closure passed to function array_map expects class-string<Yiisoft\Yii\Gii\GeneratorCommandInterface>, but possibly different type class-string<Yiisoft\Yii\Gii\GeneratorInterface> provided (see https://psalm.dev/092)

Check failure on line 42 in src/Controller/DefaultController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

PossiblyInvalidArgument

src/Controller/DefaultController.php:42:17: PossiblyInvalidArgument: Parameter 1 of closure passed to function array_map expects class-string<Yiisoft\Yii\Gii\GeneratorCommandInterface>, but possibly different type class-string<Yiisoft\Yii\Gii\GeneratorInterface> provided (see https://psalm.dev/092)

Check failure on line 42 in src/Controller/DefaultController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

PossiblyInvalidArgument

src/Controller/DefaultController.php:42:17: PossiblyInvalidArgument: Parameter 1 of closure passed to function array_map expects class-string<Yiisoft\Yii\Gii\GeneratorCommandInterface>, but possibly different type class-string<Yiisoft\Yii\Gii\GeneratorInterface> provided (see https://psalm.dev/092)

Check failure on line 42 in src/Controller/DefaultController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

PossiblyInvalidArgument

src/Controller/DefaultController.php:42:17: PossiblyInvalidArgument: Parameter 1 of closure passed to function array_map expects class-string<Yiisoft\Yii\Gii\GeneratorCommandInterface>, but possibly different type class-string<Yiisoft\Yii\Gii\GeneratorInterface> provided (see https://psalm.dev/092)
array_values(array_map(fn(GeneratorProxy $proxy) => $proxy->getClass(), $generators)),
array_values(
array_map(
fn (GeneratorInterface|GeneratorProxy $generator) => $generator instanceof GeneratorProxy
? $generator->getClass()
: $generator::class,
$generators
)
),
),
]);
}
Expand Down
6 changes: 3 additions & 3 deletions src/GeneratorProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@

use Closure;

class GeneratorProxy
final class GeneratorProxy
{
private ?GeneratorInterface $generator = null;

/**
* @psalm-param class-string<GeneratorCommandInterface> $class
* @psalm-param class-string<GeneratorInterface> $class
*/
public function __construct(private readonly Closure $loader, private readonly string $class)
{
}

/**
* @return class-string<GeneratorCommandInterface>
* @return class-string<GeneratorInterface>
*/
public function getClass(): string
{
Expand Down
19 changes: 13 additions & 6 deletions src/Gii.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,39 @@

use Yiisoft\Yii\Gii\Exception\GeneratorNotFoundException;

/**
* @psalm-import-type LazyGenerator from GiiInterface
*/
final class Gii implements GiiInterface
{
/**
* @param array<string, GeneratorInterface|GeneratorProxy> $proxies
* @param array<string, GeneratorInterface> $instances
*/
public function __construct(private array $proxies)
public function __construct(
private readonly array $proxies,
private array $instances,
)
{
}

public function addGenerator(GeneratorInterface $generator): void
{
$this->proxies[$generator::getId()] = new GeneratorProxy(fn () => $generator, $generator::class);
$this->instances[$generator::getId()] = $generator;
}

public function getGenerator(string $id): GeneratorInterface
{
if (isset($this->instances[$id])) {
return $this->instances[$id];
}
return isset($this->proxies[$id])
? $this->proxies[$id]->loadGenerator()
: throw new GeneratorNotFoundException('Generator "' . $id . '" not found');
}

public function getGenerators(): array
{
return $this->proxies;
return [
...$this->instances,
...$this->proxies,
];
}
}
6 changes: 1 addition & 5 deletions src/GiiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@

namespace Yiisoft\Yii\Gii;

use Closure;
use Yiisoft\Yii\Gii\Exception\GeneratorNotFoundException;

/**
* @psalm-type LazyGenerator = Closure(): GeneratorInterface
*/
interface GiiInterface
{
/**
Expand All @@ -23,7 +19,7 @@ public function addGenerator(GeneratorInterface $generator): void;
public function getGenerator(string $id): GeneratorInterface;

/**
* @return GeneratorInterface[]
* @return GeneratorInterface[]|GeneratorProxy[]
*/
public function getGenerators(): array;
}
6 changes: 5 additions & 1 deletion src/Validator/TemplateRuleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Yiisoft\Validator\ValidationContext;
use Yiisoft\Yii\Gii\GeneratorCommandInterface;
use Yiisoft\Yii\Gii\GeneratorInterface;
use Yiisoft\Yii\Gii\GeneratorProxy;
use Yiisoft\Yii\Gii\GiiInterface;
use Yiisoft\Yii\Gii\ParametersProvider;

Expand Down Expand Up @@ -79,9 +80,12 @@ public function validate(mixed $value, object $rule, ValidationContext $context)
private function getGenerator(GeneratorCommandInterface $dataSet): GeneratorInterface
{
foreach ($this->gii->getGenerators() as $generator) {
if ($generator::getCommandClass() === $dataSet::class) {
if ($generator instanceof GeneratorInterface && $generator::getCommandClass() === $dataSet::class) {
return $generator;
}
if ($generator instanceof GeneratorProxy && $generator->getClass()::getCommandClass() === $dataSet::class) {
return $generator->loadGenerator();
}
}
throw new RuntimeException(sprintf('Unknown generator "%s".', $dataSet::class));
}
Expand Down
18 changes: 10 additions & 8 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Yiisoft\Validator\Validator;
use Yiisoft\Validator\ValidatorInterface;
use Yiisoft\Yii\Gii\Generator as Generators;
use Yiisoft\Yii\Gii\GeneratorProxy;
use Yiisoft\Yii\Gii\Gii;
use Yiisoft\Yii\Gii\GiiInterface;

Expand All @@ -54,15 +55,16 @@ protected function getContainer(array $definitions = []): ContainerInterface
$config = ContainerConfig::create()
->withDefinitions([
GiiInterface::class => function (ContainerInterface $container) {
$generators = [
Generators\Controller\Generator::getId() => Generators\Controller\Generator::class,
Generators\ActiveRecord\Generator::getId() => Generators\ActiveRecord\Generator::class,
$proxies = [
Generators\Controller\Generator::getId() => new GeneratorProxy(
fn() => $container->get(Generators\Controller\Generator::class),
Generators\Controller\Generator::class,
),
];
$generatorsInstances = [];
foreach ($generators as $class) {
$generatorsInstances[] = $container->get($class);
}
return new Gii($generatorsInstances);
$instances = [
Generators\ActiveRecord\Generator::getId() => $container->get(Generators\ActiveRecord\Generator::class),
];
return new Gii($proxies, $instances);
},
Aliases::class => new Aliases(
[
Expand Down

0 comments on commit c8c1840

Please sign in to comment.