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 3 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 @@ public function list(GiiInterface $gii): ResponseInterface
$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)),
),
]);
}

Expand All @@ -46,7 +49,7 @@ public function get(GeneratorRequest $request): ResponseInterface
$generator = $request->getGenerator();

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

Expand Down Expand Up @@ -150,12 +153,12 @@ private function serializeCodeFile(CodeFile $file): array
];
}

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();

$dataset = new AttributesRulesProvider($commandClass);
$rules = $dataset->getRules();
Expand All @@ -182,12 +185,12 @@ private function serializeGenerator(GeneratorInterface $generator): array
}

return [
'id' => $generator::getId(),
'name' => $generator::getName(),
'description' => $generator::getDescription(),
'id' => $generatorClass::getId(),
'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