-
-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
read processed twig_component config instead parse the yaml file
- Loading branch information
1 parent
ce60831
commit 195e099
Showing
2 changed files
with
56 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
|
@@ -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'; | ||
|
@@ -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(); | ||
$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 | ||
{ | ||
$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); | ||
|
||
return (new Processor())->processConfiguration($configuration, $configs); | ||
} | ||
} |