Skip to content

Commit

Permalink
Merge branch 'master' into crud
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz authored Feb 24, 2024
2 parents ff1bcd9 + 9ba020a commit c02a72b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"keywords": [
"yii",
"gii",
"code generator"
"code generator",
"dev"
],
"license": "BSD-3-Clause",
"support": {
Expand All @@ -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",
Expand All @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions config/di.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
},
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 10 additions & 7 deletions src/Gii.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, GeneratorInterface> $generators
* @param array<string, GeneratorInterface|LazyGenerator> $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
Expand All @@ -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
);
}
}
6 changes: 5 additions & 1 deletion src/GiiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
21 changes: 16 additions & 5 deletions src/Request/GeneratorRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit c02a72b

Please sign in to comment.