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 2 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
8 changes: 6 additions & 2 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 @@ -22,8 +23,11 @@
/**
* @var $loader Closure(): GeneratorInterface
*/
$loader = fn() => $injector->make($class, $generator['parameters'] ?? []);
$generatorsInstances[$class] = $loader;
$loader = new GeneratorProxy(
fn() => $injector->make($class, $generator['parameters'] ?? []),
$class,
);
$generatorsInstances[$class::getId()] = $loader;
}
return new Gii($generatorsInstances);
},
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
27 changes: 15 additions & 12 deletions src/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +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;
use Yiisoft\Yii\Gii\Request\GeneratorRequest;
Expand All @@ -37,7 +37,10 @@
$generators = $gii->getGenerators();

return $this->responseFactory->createResponse([
'generators' => array_map($this->serializeGenerator(...), array_values($generators)),
'generators' => array_map(
$this->serializeGenerator(...),
array_values(array_map(fn(GeneratorProxy $proxy) => $proxy->getClass(), $generators)),

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:40: PossiblyInvalidArgument: Parameter 1 of closure passed to function array_map expects Yiisoft\Yii\Gii\GeneratorProxy, but possibly different type 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:40: PossiblyInvalidArgument: Parameter 1 of closure passed to function array_map expects Yiisoft\Yii\Gii\GeneratorProxy, but possibly different type 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:40: PossiblyInvalidArgument: Parameter 1 of closure passed to function array_map expects Yiisoft\Yii\Gii\GeneratorProxy, but possibly different type Yiisoft\Yii\Gii\GeneratorInterface provided (see https://psalm.dev/092)
),
]);
}

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

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

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidArgument

src/Controller/DefaultController.php:52: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 52 in src/Controller/DefaultController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidArgument

src/Controller/DefaultController.php:52: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 52 in src/Controller/DefaultController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidArgument

src/Controller/DefaultController.php:52: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 +95,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 98 in src/Controller/DefaultController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

RiskyTruthyFalsyComparison

src/Controller/DefaultController.php:98: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 98 in src/Controller/DefaultController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

RiskyTruthyFalsyComparison

src/Controller/DefaultController.php:98: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 98 in src/Controller/DefaultController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

RiskyTruthyFalsyComparison

src/Controller/DefaultController.php:98: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 +153,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 161 in src/Controller/DefaultController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedMethod

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

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedMethod

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

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

UndefinedMethod

src/Controller/DefaultController.php:161: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 166 in src/Controller/DefaultController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidStringClass

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

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidStringClass

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

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidStringClass

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

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidStringClass

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

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidStringClass

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

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidStringClass

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

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidStringClass

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

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

InvalidStringClass

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

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

InvalidStringClass

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

$reflection = new ReflectionClass($commandClass);

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

ArgumentTypeCoercion

src/Controller/DefaultController.php:170: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 170 in src/Controller/DefaultController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

ArgumentTypeCoercion

src/Controller/DefaultController.php:170: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 170 in src/Controller/DefaultController.php

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

ArgumentTypeCoercion

src/Controller/DefaultController.php:170: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 +185,12 @@
}

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

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedMethod

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

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.1-ubuntu-latest

UndefinedMethod

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

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

View workflow job for this annotation

GitHub Actions / psalm / PHP 8.2-ubuntu-latest

UndefinedMethod

src/Controller/DefaultController.php:188: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;

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

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

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

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

namespace Yiisoft\Yii\Gii;

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

/**
Expand All @@ -13,31 +12,24 @@
final class Gii implements GiiInterface
{
/**
* @param array<string, GeneratorInterface|LazyGenerator> $generators
* @param array<string, GeneratorInterface|GeneratorProxy> $proxies
*/
public function __construct(private array $generators)
public function __construct(private array $proxies)
{
}

public function addGenerator(GeneratorInterface $generator): void
{
$this->generators[$generator::getId()] = $generator;
$this->proxies[$generator::getId()] = new GeneratorProxy(fn () => $generator, $generator::class);
samdark marked this conversation as resolved.
Show resolved Hide resolved
}

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->proxies[$id]?->loadGenerator() ?? throw new GeneratorNotFoundException('Generator "' . $id . '" not found');
samdark marked this conversation as resolved.
Show resolved Hide resolved
}

public function getGenerators(): array
{
return array_map(
fn (Closure|GeneratorInterface $generator) => $generator instanceof Closure ? $generator() : $generator,
$this->generators
);
return $this->proxies;
}
}
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
Loading