Skip to content

Commit ecfc8de

Browse files
committed
Add make:cwa-scaffold maker — generate starter AppScaffold fixture class
Closes #185
1 parent fcef2c6 commit ecfc8de

3 files changed

Lines changed: 168 additions & 0 deletions

File tree

src/Maker/MakeCwaScaffold.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}

src/Resources/config/services_maker.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Silverback\ApiComponentsBundle\Resources\config;
1313

1414
use Silverback\ApiComponentsBundle\Maker\MakeApiComponent;
15+
use Silverback\ApiComponentsBundle\Maker\MakeCwaScaffold;
1516
use Silverback\ApiComponentsBundle\Maker\MakePageData;
1617
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1718

@@ -29,4 +30,10 @@
2930
->public();
3031

3132
$services->alias(MakePageData::class, 'silverback.api_components.maker.make_page_data');
33+
34+
$services->set('silverback.api_components.maker.make_cwa_scaffold', MakeCwaScaffold::class)
35+
->tag('maker.command')
36+
->public();
37+
38+
$services->alias(MakeCwaScaffold::class, 'silverback.api_components.maker.make_cwa_scaffold');
3239
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php echo "<?php\n"; ?>
2+
3+
namespace <?php echo $namespace; ?>;
4+
5+
<?php echo $use_statements; ?>
6+
/**
7+
* CWA site scaffold — wire up layouts, pages, and nav links here.
8+
*
9+
* Phase ordering (handled automatically by CwaFixtureBuilder::flush()):
10+
* 1. Entities persisted (layouts, pages, page data)
11+
* 2. Component groups created
12+
* 3. Routes generated (parents before children)
13+
* 4. Component positions and nav links created
14+
*
15+
* Nav links reference routes, so add them AFTER $cwa->page() / $cwa->pageData() calls.
16+
*/
17+
class <?php echo $class_name; ?> extends AbstractCwaScaffold
18+
{
19+
public function build(CwaFixtureBuilder $cwa): void
20+
{
21+
// --- Layout -----------------------------------------------------------
22+
// The nav group is populated below once routes exist.
23+
$navGroup = $cwa->layout('<?php echo $layout_ref; ?>', '<?php echo $layout_component; ?>')
24+
->group('top');
25+
26+
// --- Pages ------------------------------------------------------------
27+
$cwa->page('home', 'PrimaryPageTemplate', layout: '<?php echo $layout_ref; ?>', route: '/', routeName: 'home-page',
28+
configure: fn (PageBuilder $page) => $page
29+
->title('Home')
30+
->group('primary')
31+
);
32+
33+
// Add more pages here, e.g.:
34+
// $cwa->page('about', 'PrimaryPageTemplate', layout: '<?php echo $layout_ref; ?>', route: '/about', routeName: 'about-page',
35+
// configure: fn (PageBuilder $page) => $page->title('About')->group('primary')
36+
// );
37+
38+
// --- Nav links (added after routes exist) -----------------------------
39+
// Replace NavigationLink with your nav component class, e.g.:
40+
// $navGroup->add(
41+
// (new NavigationLink())
42+
// ->setLabel('Home')
43+
// ->setRoute($cwa->getRoute('home-page'))
44+
// ->setPublishedAt(new \DateTime())
45+
// );
46+
}
47+
}

0 commit comments

Comments
 (0)