acseo/fast-show-generator-bundle
Symfony bundle to quickly generate “show” view data for entities using annotations or YAML. Define labels, visibility, and groups per property, then fetch showable data in your controller and render it easily in Twig. Compatible with Symfony 5/6.
Installation
composer require acseo/fast-show-generator-bundle
Ensure your config/bundles.php includes:
return [
// ...
ACSEO\FastShowGeneratorBundle\ACSEOFastShowGeneratorBundle::class => ['all' => true],
];
Choose Configuration Method
use ACSEO\FastShowGeneratorBundle\Annotations\Show;
src/ACSEO/Bundle/MyBundle/Resources/config/fastshowgenerator/MyEntity.default.fastshowgenerator.yml
First Use Case
Generate a show action for an entity in a controller:
use ACSEO\FastShowGeneratorBundle\Driver\AnnotationDriver;
public function showAction(MyEntity $entity, EntityManagerInterface $em): Response
{
$fastShow = $this->container->get(AnnotationDriver::class);
$fastShow->setEntity($entity);
$fastShow->setGroup('default');
$fastShow->setClassMetadata($em->getClassMetadata(get_class($entity)));
$data = $fastShow->getShowableData();
return $this->render('my_template.html.twig', ['data' => $data]);
}
Dynamic Group Handling
Use groups to manage different data views (e.g., admin, public):
// In YAML
groups: {admin, public}
// In controller
$fastShow->setGroup('admin'); // or 'public'
Twig Integration Pass generated data directly to Twig:
{% for property, value in data %}
<div>{{ property.label }}: {{ value }}</div>
{% endfor %}
Service-Based Generation Extend the bundle by creating a service that wraps the driver:
# config/services.yaml
services:
App\Service\ShowGenerator:
arguments:
$annotationDriver: '@acseo_fast_show_generator.driver.annotation'
$yamlDriver: '@acseo_fast_show_generator.driver.yaml'
Event Listeners Hook into entity lifecycle events to auto-generate show data:
$fastShow->setEntity($entity);
$fastShow->setGroup('default');
$showData = $fastShow->getShowableData();
// Use $showData in prePersist/preUpdate listeners
Hybrid Configuration Combine annotations and YAML for flexibility:
{{ form_label(form.field, data[field.name].label) }}
getOperationArgs to inject show data:
public function getOperationArgs(Operation $operation, array $uriVariables, array $context = [])
{
$showData = $this->showGenerator->generate($operation->getClass(), 'default');
return array_merge($context, ['show_data' => $showData]);
}
postLoad:
public function postLoad(LifecycleEventArgs $args): void
{
$entity = $args->getObject();
$entity->setShowData($this->showGenerator->generate($entity, 'default'));
}
Caching Issues
php bin/console cache:clear
php bin/console doctrine:cache:clear-metadata
Group Mismatches
// Throws exception if group doesn't exist
$fastShow->setGroup('nonexistent');
default group if unsure.Circular References
show: false:
# YAML
user:
show: false
@ACSEOFastShowGenerator\Show(show=false).EntityManager Dependency
ClassMetadata via $em->getClassMetadata():
$fastShow->setClassMetadata($em->getClassMetadata(get_class($entity)));
Namespace Conflicts
use statement is correct:
// Wrong (throws error)
use ACSEO\FastShowGeneratorBundle\Annotations as Show;
// Correct
use ACSEO\FastShowGeneratorBundle\Annotations\Show;
Validate YAML Syntax Use Symfony’s validator:
php bin/console debug:config acseo_fast_show_generator
Check for missing files or invalid YAML.
Annotation Errors Enable annotation debugging:
$fastShow->setDebug(true); // Logs missing annotations
Data Dumping Dump raw show data for inspection:
$data = $fastShow->getShowableData();
dump($data); // Check structure in CLI
Custom Drivers
Implement ACSEO\FastShowGeneratorBundle\Driver\DriverInterface:
class CustomDriver implements DriverInterface
{
public function getShowableData(): array
{
// Custom logic (e.g., database queries)
return [];
}
}
Register in services.yaml:
services:
App\Driver\CustomDriver:
tags: ['acseo_fast_show_generator.driver']
Override Default Labels Use a compiler pass to modify labels dynamically:
public function process(ContainerBuilder $container)
{
$definition = $container->findDefinition('acseo_fast_show_generator.driver.annotation');
$definition->addMethodCall('setLabelOverride', ['property' => 'customLabel']);
}
Add Custom Properties
Extend the Show annotation:
/**
* @Annotation
* @Target({"PROPERTY"})
*/
class Show extends \Doctrine\Common\Annotations\Annotation
{
public $label;
public $show;
public $groups;
public $customProperty; // Add new field
}
Update the driver to handle the new property.
Event Dispatching Dispatch events before/after data generation:
$dispatcher->dispatch(new ShowDataEvent($entity, $data), 'fast_show_generator.generate');
How can I help you explore Laravel packages today?