|
13 | 13 |
|
14 | 14 | use Symfony\Bundle\MakerBundle\ConsoleStyle; |
15 | 15 | use Symfony\Bundle\MakerBundle\DependencyBuilder; |
| 16 | +use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; |
| 17 | +use Symfony\Bundle\MakerBundle\FileManager; |
16 | 18 | use Symfony\Bundle\MakerBundle\Generator; |
17 | 19 | use Symfony\Bundle\MakerBundle\InputConfiguration; |
18 | 20 | use Symfony\Component\Console\Command\Command; |
19 | 21 | use Symfony\Component\Console\Input\InputArgument; |
20 | 22 | use Symfony\Component\Console\Input\InputInterface; |
21 | 23 | use Symfony\Component\Console\Input\InputOption; |
| 24 | +use Symfony\Component\Yaml\Yaml; |
22 | 25 | use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; |
23 | 26 | use Symfony\UX\TwigComponent\Attribute\AsTwigComponent; |
24 | 27 |
|
|
27 | 30 | */ |
28 | 31 | final class MakeTwigComponent extends AbstractMaker |
29 | 32 | { |
| 33 | + private string $namespace = 'Twig\\Components'; |
| 34 | + |
| 35 | + public function __construct(private FileManager $fileManager) |
| 36 | + { |
| 37 | + } |
| 38 | + |
30 | 39 | public static function getCommandName(): string |
31 | 40 | { |
32 | 41 | return 'make:twig-component'; |
@@ -62,7 +71,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen |
62 | 71 |
|
63 | 72 | $factory = $generator->createClassNameDetails( |
64 | 73 | $name, |
65 | | - 'Twig\\Components', |
| 74 | + $this->namespace, |
66 | 75 | ); |
67 | 76 |
|
68 | 77 | $templatePath = str_replace('\\', '/', $factory->getRelativeNameWithoutSuffix()); |
@@ -93,5 +102,18 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma |
93 | 102 | if (!$input->getOption('live')) { |
94 | 103 | $input->setOption('live', $io->confirm('Make this a live component?', false)); |
95 | 104 | } |
| 105 | + |
| 106 | + $path = 'config/packages/twig_component.yaml'; |
| 107 | + |
| 108 | + if (!$this->fileManager->fileExists($path)) { |
| 109 | + throw new RuntimeCommandException(message: 'Unable to find twig_components.yaml'); |
| 110 | + } |
| 111 | + |
| 112 | + try { |
| 113 | + $value = Yaml::parse($this->fileManager->getFileContents($path)); |
| 114 | + $this->namespace = substr(array_key_first($value['twig_component']['defaults']), 4); |
| 115 | + } catch (\Throwable $throwable) { |
| 116 | + throw new RuntimeCommandException(message: 'Unable to parse twig_components.yaml', previous: $throwable); |
| 117 | + } |
96 | 118 | } |
97 | 119 | } |
0 commit comments