Skip to content

Commit

Permalink
read processed twig_component config instead parse the yaml file
Browse files Browse the repository at this point in the history
  • Loading branch information
IndraGunawan committed Dec 5, 2024
1 parent ce60831 commit 195e099
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
1 change: 0 additions & 1 deletion config/makers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

<service id="maker.maker.make_twig_component" class="Symfony\Bundle\MakerBundle\Maker\MakeTwigComponent">
<tag name="maker.command" />
<argument type="service" id="maker.file_manager" />
</service>

<service id="maker.maker.make_controller" class="Symfony\Bundle\MakerBundle\Maker\MakeController">
Expand Down
71 changes: 56 additions & 15 deletions src/Maker/MakeTwigComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
use Symfony\Bundle\MakerBundle\FileManager;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
use Symfony\UX\TwigComponent\DependencyInjection\TwigComponentExtension;

/**
* @author Kevin Bond <[email protected]>
Expand All @@ -32,10 +35,6 @@ final class MakeTwigComponent extends AbstractMaker
{
private string $namespace = 'Twig\\Components';

public function __construct(private FileManager $fileManager)
{
}

public static function getCommandName(): string
{
return 'make:twig-component';
Expand Down Expand Up @@ -103,17 +102,59 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
$input->setOption('live', $io->confirm('Make this a live component?', false));
}

$path = 'config/packages/twig_component.yaml';
$container = $this->compileContainer($command->getApplication());

if (!$this->fileManager->fileExists($path)) {
throw new RuntimeCommandException(message: 'Unable to find twig_component.yaml');
$config = $this->getConfig($container);

if (isset($config['defaults'])) {
$namespace = array_key_first($config['defaults']);
$this->namespace = substr($namespace, \strpos($namespace, '\\') + 1);
}
}

private function compileContainer(Application $application): ContainerBuilder
{
// logic from \Symfony\Bundle\FrameworkBundle\Command\ConfigDebugCommand
$kernel = clone $application->getKernel();

Check failure on line 118 in src/Maker/MakeTwigComponent.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method Symfony\Component\Console\Application::getKernel().
$kernel->boot();

$method = new \ReflectionMethod($kernel, 'buildContainer');
$container = $method->invoke($kernel);
$container->getCompiler()->compile($container);

return $container;
}

try {
$value = Yaml::parse($this->fileManager->getFileContents($path));
$this->namespace = substr(array_key_first($value['twig_component']['defaults']), 4);
} catch (\Throwable $throwable) {
throw new RuntimeCommandException(message: 'Unable to parse twig_component.yaml', previous: $throwable);
private function getConfig(ContainerBuilder $container): mixed
{
return $container->resolveEnvPlaceholders(
$container->getParameterBag()->resolveValue(
$this->getConfigForExtension($container)
), true
);
}

private function getConfigForExtension(ContainerBuilder $container): array

Check failure on line 137 in src/Maker/MakeTwigComponent.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Symfony\Bundle\MakerBundle\Maker\MakeTwigComponent::getConfigForExtension() return type has no value type specified in iterable type array.
{
$extensionAlias = 'twig_component';

$extensionConfig = [];
foreach ($container->getCompilerPassConfig()->getPasses() as $pass) {
if ($pass instanceof ValidateEnvPlaceholdersPass) {
$extensionConfig = $pass->getExtensionConfig();
break;
}
}

if (isset($extensionConfig[$extensionAlias])) {
return $extensionConfig[$extensionAlias];
}

// Fall back to default config if the extension has one
$extension = new TwigComponentExtension();
$configs = $container->getExtensionConfig($extensionAlias);
$configuration = $extension instanceof ConfigurationInterface ? $extension : $extension->getConfiguration($configs, $container);

Check failure on line 156 in src/Maker/MakeTwigComponent.php

View workflow job for this annotation

GitHub Actions / PHPStan

Else branch is unreachable because ternary operator condition is always true.

return (new Processor())->processConfiguration($configuration, $configs);
}
}

0 comments on commit 195e099

Please sign in to comment.