|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Silverback API Components Bundle Project |
| 5 | + * |
| 6 | + * (c) Daniel West <daniel@silverback.is> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Silverback\ApiComponentsBundle\Maker; |
| 13 | + |
| 14 | +use Silverback\ApiComponentsBundle\Fixture\AbstractCwaScaffold; |
| 15 | +use Silverback\ApiComponentsBundle\Fixture\Builder\PageBuilder; |
| 16 | +use Silverback\ApiComponentsBundle\Fixture\CwaFixtureBuilder; |
| 17 | +use Symfony\Bundle\MakerBundle\ConsoleStyle; |
| 18 | +use Symfony\Bundle\MakerBundle\DependencyBuilder; |
| 19 | +use Symfony\Bundle\MakerBundle\Generator; |
| 20 | +use Symfony\Bundle\MakerBundle\InputConfiguration; |
| 21 | +use Symfony\Bundle\MakerBundle\Maker\AbstractMaker; |
| 22 | +use Symfony\Bundle\MakerBundle\Util\UseStatementGenerator; |
| 23 | +use Symfony\Component\Console\Command\Command; |
| 24 | +use Symfony\Component\Console\Input\InputArgument; |
| 25 | +use Symfony\Component\Console\Input\InputInterface; |
| 26 | +use Symfony\Component\Console\Input\InputOption; |
| 27 | + |
| 28 | +final class MakeCwaScaffold extends AbstractMaker |
| 29 | +{ |
| 30 | + public static function getCommandName(): string |
| 31 | + { |
| 32 | + return 'make:cwa-scaffold'; |
| 33 | + } |
| 34 | + |
| 35 | + public static function getCommandDescription(): string |
| 36 | + { |
| 37 | + return 'Generate a starter CWA fixture scaffold (layout + home page)'; |
| 38 | + } |
| 39 | + |
| 40 | + public function configureCommand(Command $command, InputConfiguration $inputConf): void |
| 41 | + { |
| 42 | + $command |
| 43 | + ->addArgument('name', InputArgument::OPTIONAL, 'The class name for your scaffold (e.g. <fg=yellow>AppScaffold</>)') |
| 44 | + ->addOption('layout-ref', null, InputOption::VALUE_REQUIRED, 'Layout reference key used in <comment>$cwa->layout()</comment>', 'main') |
| 45 | + ->addOption('layout-component', null, InputOption::VALUE_REQUIRED, 'Layout UI component name (e.g. <comment>CwaLayoutPrimary</comment>)', 'CwaLayoutPrimary'); |
| 46 | + } |
| 47 | + |
| 48 | + public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void |
| 49 | + { |
| 50 | + if (!$input->getOption('layout-ref') || 'main' === $input->getOption('layout-ref')) { |
| 51 | + $ref = $io->ask('Layout reference key (the string passed to <comment>$cwa->layout()</comment>)', 'main'); |
| 52 | + $input->setOption('layout-ref', $ref); |
| 53 | + } |
| 54 | + |
| 55 | + if (!$input->getOption('layout-component') || 'CwaLayoutPrimary' === $input->getOption('layout-component')) { |
| 56 | + $component = $io->ask('Layout UI component name', 'CwaLayoutPrimary'); |
| 57 | + $input->setOption('layout-component', $component); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void |
| 62 | + { |
| 63 | + $classNameDetails = $generator->createClassNameDetails( |
| 64 | + $input->getArgument('name') ?? 'AppScaffold', |
| 65 | + 'DataFixtures\\' |
| 66 | + ); |
| 67 | + |
| 68 | + $layoutRef = $input->getOption('layout-ref'); |
| 69 | + $layoutComponent = $input->getOption('layout-component'); |
| 70 | + |
| 71 | + $useStatements = new UseStatementGenerator([ |
| 72 | + AbstractCwaScaffold::class, |
| 73 | + CwaFixtureBuilder::class, |
| 74 | + PageBuilder::class, |
| 75 | + ]); |
| 76 | + |
| 77 | + $generator->generateClass( |
| 78 | + $classNameDetails->getFullName(), |
| 79 | + __DIR__ . '/../Resources/skeleton/scaffold/Scaffold.tpl.php', |
| 80 | + [ |
| 81 | + 'use_statements' => $useStatements, |
| 82 | + 'layout_ref' => $layoutRef, |
| 83 | + 'layout_component' => $layoutComponent, |
| 84 | + ] |
| 85 | + ); |
| 86 | + |
| 87 | + $generator->writeChanges(); |
| 88 | + |
| 89 | + $this->writeSuccessMessage($io); |
| 90 | + |
| 91 | + $shortName = $classNameDetails->getShortName(); |
| 92 | + $fullName = $classNameDetails->getFullName(); |
| 93 | + |
| 94 | + $io->text([ |
| 95 | + 'Register your scaffold as a Doctrine fixture service. In <comment>config/services.yaml</comment>:', |
| 96 | + '', |
| 97 | + ' ' . $fullName . ':', |
| 98 | + ' tags: [doctrine.fixture.orm]', |
| 99 | + '', |
| 100 | + 'Or in a PHP config file:', |
| 101 | + '', |
| 102 | + ' $services->set(' . $shortName . '::class)->tag(\'doctrine.fixture.orm\');', |
| 103 | + '', |
| 104 | + 'Then run: <comment>php bin/console doctrine:fixtures:load</comment>', |
| 105 | + '', |
| 106 | + '<fg=yellow>Tip:</> Add your own pages, page data, and components inside <comment>build()</comment>.', |
| 107 | + 'Nav links must be added <fg=yellow>after</> <comment>$cwa->page()</comment> calls so their routes exist.', |
| 108 | + ]); |
| 109 | + } |
| 110 | + |
| 111 | + public function configureDependencies(DependencyBuilder $dependencies): void |
| 112 | + { |
| 113 | + } |
| 114 | +} |
0 commit comments