diff --git a/composer.json b/composer.json index d462b375..0cd25c18 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,8 @@ "keywords": [ "yii", "gii", - "code generator" + "code generator", + "dev" ], "license": "BSD-3-Clause", "support": { @@ -26,9 +27,10 @@ "yiisoft/arrays": "^2.1|^3.0", "yiisoft/data-response": "^2.0", "yiisoft/http": "^1.2", + "yiisoft/hydrator": "^1.0", "yiisoft/injector": "^1.1", + "yiisoft/input-http": "dev-master", "yiisoft/json": "^1.0", - "yiisoft/request-model": "dev-master", "yiisoft/router": "^3.0", "yiisoft/strings": "^2.1", "yiisoft/validator": "^1.0", @@ -39,7 +41,7 @@ "jetbrains/phpstorm-attributes": "^1.0", "nyholm/psr7": "^1.5", "phpunit/phpunit": "^10.2", - "rector/rector": "^0.18.4", + "rector/rector": "^1.0.0", "roave/infection-static-analysis-plugin": "^1.23", "spatie/phpunit-watcher": "^1.23", "vimeo/psalm": "^5.13", diff --git a/config/di.php b/config/di.php index 3f8dc62c..fe6fc121 100644 --- a/config/di.php +++ b/config/di.php @@ -20,10 +20,10 @@ foreach ($generators as $generator) { $class = $generator['class']; /** - * @var $generator GeneratorInterface + * @var $loader Closure(): GeneratorInterface */ - $generator = $injector->make($class, $generator['parameters'] ?? []); - $generatorsInstances[] = $generator; + $loader = fn() => $injector->make($class, $generator['parameters'] ?? []); + $generatorsInstances[$class] = $loader; } return new Gii($generatorsInstances); }, diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index a49a99ef..24ff60d0 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -10,7 +10,7 @@ use Yiisoft\DataResponse\DataResponse; use Yiisoft\DataResponse\DataResponseFactoryInterface; use Yiisoft\Http\Status; -use Yiisoft\RequestModel\Attribute\Query; +use Yiisoft\Input\Http\Attribute\Parameter\Query; use Yiisoft\Validator\Helper\RulesDumper; use Yiisoft\Validator\RulesProvider\AttributesRulesProvider; use Yiisoft\Yii\Gii\Component\CodeFile\CodeFile; diff --git a/src/Gii.php b/src/Gii.php index 8c6d1cd0..002b8a98 100644 --- a/src/Gii.php +++ b/src/Gii.php @@ -4,19 +4,19 @@ 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 $generators + * @param array $generators */ public function __construct(private array $generators) { - $this->generators = array_combine( - array_map(fn (GeneratorInterface $generator) => $generator::getId(), $generators), - array_values($this->generators) - ); } public function addGenerator(GeneratorInterface $generator): void @@ -30,11 +30,14 @@ public function getGenerator(string $id): GeneratorInterface throw new GeneratorNotFoundException('Generator "' . $id . '" not found'); } - return $this->generators[$id]; + return $this->generators[$id] instanceof Closure ? $this->generators[$id]() : $this->generators[$id]; } public function getGenerators(): array { - return $this->generators; + return array_map( + fn (Closure|GeneratorInterface $generator) => $generator instanceof Closure ? $generator() : $generator, + $this->generators + ); } } diff --git a/src/GiiInterface.php b/src/GiiInterface.php index 2462000f..614435d3 100644 --- a/src/GiiInterface.php +++ b/src/GiiInterface.php @@ -4,12 +4,16 @@ namespace Yiisoft\Yii\Gii; +use Closure; use Yiisoft\Yii\Gii\Exception\GeneratorNotFoundException; +/** + * @psalm-type LazyGenerator = Closure(): GeneratorInterface + */ interface GiiInterface { /** - * @param GeneratorInterface $generator + * @psalm-param GeneratorInterface $generator */ public function addGenerator(GeneratorInterface $generator): void; diff --git a/src/Request/GeneratorRequest.php b/src/Request/GeneratorRequest.php index 70c9fe25..e63568a0 100644 --- a/src/Request/GeneratorRequest.php +++ b/src/Request/GeneratorRequest.php @@ -4,28 +4,39 @@ namespace Yiisoft\Yii\Gii\Request; -use Yiisoft\RequestModel\RequestModel; +use Yiisoft\Hydrator\Temp\RouteArgument; +use Yiisoft\Input\Http\Attribute\Parameter\Body; +use Yiisoft\Input\Http\RequestInputInterface; use Yiisoft\Yii\Gii\GeneratorInterface; use Yiisoft\Yii\Gii\GiiInterface; -final class GeneratorRequest extends RequestModel +final class GeneratorRequest implements RequestInputInterface { + #[RouteArgument('generator')] + private string $generatorId = ''; + + #[Body('answers')] + private array $answers = []; + + #[Body('parameters')] + private array $parameters = []; + public function __construct(private readonly GiiInterface $gii) { } public function getGenerator(): GeneratorInterface { - return $this->gii->getGenerator($this->getAttributeValue('router.generator')); + return $this->gii->getGenerator($this->generatorId); } public function getAnswers(): array { - return $this->getAttributeValue('body.answers', []); + return $this->answers; } public function getBody(): array { - return $this->getAttributeValue('body.parameters', []); + return $this->parameters; } }