Skip to content

Commit

Permalink
Lazy generators (#116)
Browse files Browse the repository at this point in the history
* Make generators lazy

* Apply fixes from StyleCI

* Revert generators

* Add AR generator

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
xepozz and StyleCIBot authored Oct 8, 2023
1 parent e10310c commit 31f9158
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
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
12 changes: 11 additions & 1 deletion config/params.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Yiisoft\Yii\Gii\Command\ControllerCommand;
use Yiisoft\Yii\Gii\Generator as Generators;

return [
'yiisoft/yii-debug' => [
Expand All @@ -23,7 +24,16 @@
'yiisoft/yii-gii' => [
'enabled' => true,
'allowedIPs' => ['127.0.0.1', '::1'],
'generators' => [],
'generators' => [
[
'class' => Generators\Controller\Generator::class,
'parameters' => [],
],
[
'class' => Generators\ActiveRecord\Generator::class,
'parameters' => [],
],
],
'parameters' => [
'templates' => [],
],
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

0 comments on commit 31f9158

Please sign in to comment.