Skip to content
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

Fix lazy loading #124

Merged
merged 9 commits into from
Jun 9, 2024
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
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@
"roave/infection-static-analysis-plugin": "^1.23",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.13",
"yiisoft/active-record": "^3.0@dev",
"yiisoft/active-record": "dev-master",
"yiisoft/cache": "^3.0",
"yiisoft/db": "1.2 as dev-master",
"yiisoft/db-sqlite": "^1.0",
"yiisoft/di": "^1.1",
"yiisoft/dummy-provider": "^1.0",
Expand Down Expand Up @@ -98,7 +99,8 @@
"sort-packages": true,
"allow-plugins": {
"infection/extension-installer": true,
"composer/package-versions-deprecated": true
"composer/package-versions-deprecated": true,
"php-http/discovery": false
}
},
"scripts": {
Expand Down
12 changes: 8 additions & 4 deletions config/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Yiisoft\Injector\Injector;
use Yiisoft\Yii\Gii\GeneratorInterface;
use Yiisoft\Yii\Gii\GeneratorProxy;
use Yiisoft\Yii\Gii\Gii;
use Yiisoft\Yii\Gii\GiiInterface;
use Yiisoft\Yii\Gii\ParametersProvider;
Expand All @@ -14,18 +15,21 @@

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

foreach ($generators as $generator) {
$class = $generator['class'];
/**
* @var $loader Closure(): GeneratorInterface
*/
$loader = fn() => $injector->make($class, $generator['parameters'] ?? []);
$generatorsInstances[$class] = $loader;
$loader = new GeneratorProxy(
fn() => $injector->make($class, $generator['parameters'] ?? []),
$class,
);
$proxies[$class::getId()] = $loader;
}
return new Gii($generatorsInstances);
return new Gii($proxies, []);
},
ParametersProvider::class => [
'class' => ParametersProvider::class,
Expand Down
4 changes: 4 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Psr\Http\Message\ResponseFactoryInterface;
use Yiisoft\Csrf\CsrfMiddleware;
use Yiisoft\DataResponse\Middleware\FormatDataResponseAsJson;
use Yiisoft\RequestProvider\RequestCatcherMiddleware;
use Yiisoft\Router\Group;
use Yiisoft\Router\Route;
use Yiisoft\Validator\ValidatorInterface;
Expand Down Expand Up @@ -47,14 +48,17 @@ static function (ResponseFactoryInterface $responseFactory, ValidatorInterface $
->name('generator'),
Route::post('/{generator}/preview')
->middleware(BodyParamsMiddleware::class)
->middleware(RequestCatcherMiddleware::class)
->action([DefaultController::class, 'preview'])
->name('preview'),
Route::post('/{generator}/generate')
->middleware(BodyParamsMiddleware::class)
->middleware(RequestCatcherMiddleware::class)
->action([DefaultController::class, 'generate'])
->name('generate'),
Route::post('/{generator}/diff')
->middleware(BodyParamsMiddleware::class)
->middleware(RequestCatcherMiddleware::class)
->action([DefaultController::class, 'diff'])
->name('diff')
)
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
colors="true"
failOnRisky="true"
failOnWarning="true"
displayDetailsOnTestsThatTriggerWarnings="true"
stopOnFailure="false"
executionOrder="random"
resolveDependencies="true"
Expand Down
33 changes: 22 additions & 11 deletions src/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
use Yiisoft\Yii\Gii\Request\GeneratorRequest;
Expand All @@ -37,7 +38,17 @@
$generators = $gii->getGenerators();

return $this->responseFactory->createResponse([
'generators' => array_map($this->serializeGenerator(...), array_values($generators)),
'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 (GeneratorInterface|GeneratorProxy $generator) => $generator instanceof GeneratorProxy
? $generator->getClass()
: $generator::class,
$generators
)
),
),
]);
}

Expand All @@ -46,7 +57,7 @@
$generator = $request->getGenerator();

return $this->responseFactory->createResponse(
$this->serializeGenerator($generator)
$this->serializeGenerator($generator::class)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidArgument

src/Controller/DefaultController.php:60:39: InvalidArgument: Argument 1 of Yiisoft\Yii\Gii\Controller\DefaultController::serializeGenerator expects class-string<Yiisoft\Yii\Gii\GeneratorCommandInterface>, but class-string<Yiisoft\Yii\Gii\GeneratorInterface> provided (see https://psalm.dev/004)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidArgument

src/Controller/DefaultController.php:60:39: InvalidArgument: Argument 1 of Yiisoft\Yii\Gii\Controller\DefaultController::serializeGenerator expects class-string<Yiisoft\Yii\Gii\GeneratorCommandInterface>, but class-string<Yiisoft\Yii\Gii\GeneratorInterface> provided (see https://psalm.dev/004)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidArgument

src/Controller/DefaultController.php:60:39: InvalidArgument: Argument 1 of Yiisoft\Yii\Gii\Controller\DefaultController::serializeGenerator expects class-string<Yiisoft\Yii\Gii\GeneratorCommandInterface>, but class-string<Yiisoft\Yii\Gii\GeneratorInterface> provided (see https://psalm.dev/004)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidArgument

src/Controller/DefaultController.php:60:39: InvalidArgument: Argument 1 of Yiisoft\Yii\Gii\Controller\DefaultController::serializeGenerator expects class-string<Yiisoft\Yii\Gii\GeneratorCommandInterface>, but class-string<Yiisoft\Yii\Gii\GeneratorInterface> provided (see https://psalm.dev/004)
);
}

Expand Down Expand Up @@ -92,7 +103,7 @@
if ($generatedFile->getId() === $file) {
$content = $generatedFile->preview();
return $this->responseFactory->createResponse(
['content' => $content ?: 'Preview is not available for this file type.']

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

RiskyTruthyFalsyComparison

src/Controller/DefaultController.php:106:35: RiskyTruthyFalsyComparison: Operand of type false|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

RiskyTruthyFalsyComparison

src/Controller/DefaultController.php:106:35: RiskyTruthyFalsyComparison: Operand of type false|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

RiskyTruthyFalsyComparison

src/Controller/DefaultController.php:106:35: RiskyTruthyFalsyComparison: Operand of type false|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

RiskyTruthyFalsyComparison

src/Controller/DefaultController.php:106:35: RiskyTruthyFalsyComparison: Operand of type false|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)
);
}
}
Expand Down Expand Up @@ -150,21 +161,21 @@
];
}

private function serializeGenerator(GeneratorInterface $generator): array
/**
* @psalm-param class-string<GeneratorCommandInterface> $generatorClass
*/
private function serializeGenerator(string $generatorClass): array
{
/**
* @psalm-var class-string<GeneratorCommandInterface> $commandClass
*/
$commandClass = $generator::getCommandClass();
$commandClass = $generatorClass::getCommandClass();

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedMethod

src/Controller/DefaultController.php:169:25: UndefinedMethod: Method Yiisoft\Yii\Gii\GeneratorCommandInterface::getcommandclass does not exist (see https://psalm.dev/022)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

UndefinedMethod

src/Controller/DefaultController.php:169:25: UndefinedMethod: Method Yiisoft\Yii\Gii\GeneratorCommandInterface::getcommandclass does not exist (see https://psalm.dev/022)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedMethod

src/Controller/DefaultController.php:169:25: UndefinedMethod: Method Yiisoft\Yii\Gii\GeneratorCommandInterface::getcommandclass does not exist (see https://psalm.dev/022)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

UndefinedMethod

src/Controller/DefaultController.php:169:25: UndefinedMethod: Method Yiisoft\Yii\Gii\GeneratorCommandInterface::getcommandclass does not exist (see https://psalm.dev/022)

$dataset = new AttributesRulesProvider($commandClass);
$rules = $dataset->getRules();
$dumpedRules = (new RulesDumper())->asArray($rules);
$attributes = $commandClass::getAttributes();

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidStringClass

src/Controller/DefaultController.php:174:23: InvalidStringClass: String cannot be used as a class (see https://psalm.dev/160)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidStringClass

src/Controller/DefaultController.php:174:23: InvalidStringClass: String cannot be used as a class (see https://psalm.dev/160)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidStringClass

src/Controller/DefaultController.php:174:23: InvalidStringClass: String cannot be used as a class (see https://psalm.dev/160)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidStringClass

src/Controller/DefaultController.php:174:23: InvalidStringClass: String cannot be used as a class (see https://psalm.dev/160)
$hints = $commandClass::getHints();

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidStringClass

src/Controller/DefaultController.php:175:18: InvalidStringClass: String cannot be used as a class (see https://psalm.dev/160)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidStringClass

src/Controller/DefaultController.php:175:18: InvalidStringClass: String cannot be used as a class (see https://psalm.dev/160)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidStringClass

src/Controller/DefaultController.php:175:18: InvalidStringClass: String cannot be used as a class (see https://psalm.dev/160)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidStringClass

src/Controller/DefaultController.php:175:18: InvalidStringClass: String cannot be used as a class (see https://psalm.dev/160)
$labels = $commandClass::getAttributeLabels();

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidStringClass

src/Controller/DefaultController.php:176:19: InvalidStringClass: String cannot be used as a class (see https://psalm.dev/160)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidStringClass

src/Controller/DefaultController.php:176:19: InvalidStringClass: String cannot be used as a class (see https://psalm.dev/160)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidStringClass

src/Controller/DefaultController.php:176:19: InvalidStringClass: String cannot be used as a class (see https://psalm.dev/160)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidStringClass

src/Controller/DefaultController.php:176:19: InvalidStringClass: String cannot be used as a class (see https://psalm.dev/160)

$reflection = new ReflectionClass($commandClass);

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

ArgumentTypeCoercion

src/Controller/DefaultController.php:178:43: ArgumentTypeCoercion: Argument 1 of ReflectionClass::__construct expects class-string|object|trait-string, but parent type object|string provided (see https://psalm.dev/193)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

ArgumentTypeCoercion

src/Controller/DefaultController.php:178:43: ArgumentTypeCoercion: Argument 1 of ReflectionClass::__construct expects class-string|object|trait-string, but parent type object|string provided (see https://psalm.dev/193)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

ArgumentTypeCoercion

src/Controller/DefaultController.php:178:43: ArgumentTypeCoercion: Argument 1 of ReflectionClass::__construct expects class-string|object|trait-string, but parent type object|string provided (see https://psalm.dev/193)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

ArgumentTypeCoercion

src/Controller/DefaultController.php:178:43: ArgumentTypeCoercion: Argument 1 of ReflectionClass::__construct expects class-string|object|trait-string, but parent type object|string provided (see https://psalm.dev/193)
$constructorParameters = $reflection->getConstructor()?->getParameters() ?? [];

$attributesResult = [];
Expand All @@ -182,12 +193,12 @@
}

return [
'id' => $generator::getId(),
'name' => $generator::getName(),
'description' => $generator::getDescription(),
'id' => $generatorClass::getId(),

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedMethod

src/Controller/DefaultController.php:196:21: UndefinedMethod: Method Yiisoft\Yii\Gii\GeneratorCommandInterface::getid does not exist (see https://psalm.dev/022)

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedMethod

src/Controller/DefaultController.php:196:21: UndefinedMethod: Method Yiisoft\Yii\Gii\GeneratorCommandInterface::getid does not exist (see https://psalm.dev/022)
'name' => $generatorClass::getName(),
'description' => $generatorClass::getDescription(),
'commandClass' => $commandClass,
'attributes' => $attributesResult,
'templates' => $this->parametersProvider->getTemplates($generator::getId()),
'templates' => $this->parametersProvider->getTemplates($generatorClass::getId()),
];
}

Expand Down
32 changes: 32 additions & 0 deletions src/GeneratorProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\Gii;

use Closure;

final class GeneratorProxy
{
private ?GeneratorInterface $generator = null;
vjik marked this conversation as resolved.
Show resolved Hide resolved

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

/**
* @return class-string<GeneratorInterface>
*/
public function getClass(): string
{
return $this->class;
}

public function loadGenerator(): GeneratorInterface
{
return $this->generator ??= ($this->loader)();
}
}
31 changes: 14 additions & 17 deletions src/Gii.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,37 @@

namespace Yiisoft\Yii\Gii;

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

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

public function addGenerator(GeneratorInterface $generator): void
{
$this->generators[$generator::getId()] = $generator;
$this->instances[$generator::getId()] = $generator;
}

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

return $this->generators[$id] instanceof Closure ? $this->generators[$id]() : $this->generators[$id];
return $this->instances[$id] ?? (isset($this->proxies[$id])
? $this->proxies[$id]->loadGenerator()
: throw new GeneratorNotFoundException('Generator "' . $id . '" not found'));
}

public function getGenerators(): array
{
return array_map(
fn (Closure|GeneratorInterface $generator) => $generator instanceof Closure ? $generator() : $generator,
$this->generators
);
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;
}
2 changes: 1 addition & 1 deletion src/Request/GeneratorRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Yiisoft\Yii\Gii\Request;

use Yiisoft\Hydrator\Temp\RouteArgument;
use Yiisoft\Input\Http\Attribute\Parameter\Body;
use Yiisoft\Input\Http\RequestInputInterface;
use Yiisoft\Router\HydratorAttribute\RouteArgument;
use Yiisoft\Yii\Gii\GeneratorInterface;
use Yiisoft\Yii\Gii\GiiInterface;

Expand Down
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
Loading