danielbackes/symfony-casts-lorem-ipsum-bundle
Symfony bundle that generates joyful lorem ipsum. Install via Composer and autowire the KnpUIpsum service to create fake paragraphs. Configure unicorn/sunshine options and extend the word list by adding WordProviderInterface services.
Installation:
composer require danielbackes/symfony-casts-lorem-ipsum-bundle --dev
(Note: The package name in the README is incorrect; use danielbackes/symfony-casts-lorem-ipsum-bundle as per the description.)
Enable the Bundle (if not using Symfony Flex):
Add to config/bundles.php:
return [
// ...
KnpU\LoremIpsumBundle\KnpULoremIpsumBundle::class => ['all' => true],
];
First Use Case:
Inject the KnpUIpsum service into a controller or service to generate placeholder text:
use KnpU\LoremIpsumBundle\KnpUIpsum;
class SomeController {
public function index(KnpUIpsum $loremIpsum) {
$paragraphs = $loremIpsum->getParagraphs(3); // 3 paragraphs
return $this->render('page.html.twig', ['content' => $paragraphs]);
}
}
KnpUIpsum for available methods like:
getParagraphs(int $count)getWords(int $count)getSentences(int $count)config/packages/knpu_lorem_ipsum.yaml (default values provided).knpu_ipsum) for templates:
{{ knpu_ipsum.paragraphs(2) }}
Placeholder Content for Development: Use in controllers, services, or factories to generate mock data:
$fakeUser = [
'name' => $loremIpsum->getWords(2), // "Lorem ipsum"
'bio' => $loremIpsum->getParagraphs(1),
];
Testing: Seed tests with realistic-looking data:
public function testUserProfile() {
$user = new User();
$user->bio = $this->loremIpsum->getParagraphs(2);
$this->assertStringContainsString('Lorem', $user->bio);
}
Twig Templates: Replace hardcoded placeholder text with dynamic generation:
<div class="placeholder-content">
{{ knpu_ipsum.words(10) }}
</div>
Custom Word Providers: Extend functionality by adding domain-specific terms (e.g., for an e-commerce app):
// src/Service/EcommerceWordProvider.php
class EcommerceWordProvider implements WordProviderInterface {
public function getWordList(): array {
return ['purchase', 'cart', 'checkout', 'discount'];
}
}
(Automatically picked up if tagged knpu_ipsum_word_provider.)
Dependency Injection:
Prefer constructor injection for KnpUIpsum in services:
class DataSeeder {
public function __construct(private KnpUIpsum $loremIpsum) {}
}
Configuration Overrides:
Customize output globally via config/packages/knpu_lorem_ipsum.yaml:
knpu_lorem_ipsum:
unicorns_are_real: false # Disables "unicorn-themed" words
min_sunshine: 5 # Adjusts "sunshine" word frequency
Caching: Cache generated text in long-running processes (e.g., CLI commands) to avoid redundant calls:
$cachedText = Cache::remember('lorem_ipsum_paragraphs', 3600, function() use ($loremIpsum) {
return $loremIpsum->getParagraphs(5);
});
Package Name Mismatch:
The README references symfony-casts/lorem-ipsum-bundle, but the actual package is danielbackes/symfony-casts-lorem-ipsum-bundle. Use the correct name in composer.json and bundles.php.
Symfony Flex Compatibility:
If using Symfony Flex, ensure the bundle is auto-loaded. Manually enabling it in bundles.php may cause conflicts.
Word Provider Registration:
Custom WordProviderInterface implementations must be tagged knpu_ipsum_word_provider if not using auto-configuration:
# config/services.yaml
services:
App\Service\CustomWordProvider:
tags: ['knpu_ipsum_word_provider']
Unicode/Encoding Issues: The generated text may include non-ASCII characters (e.g., "unicorn" words). Ensure your database/templates handle UTF-8.
Deterministic Output: The bundle uses randomness for word/sentence generation. For reproducible tests, seed the random generator:
$loremIpsum = $this->container->get('knpu_lorem_ipsum.knpu_ipsum');
$loremIpsum->setRandomEngine(new \Random\Engine\Mt19937(['seed' => 123]));
Check Service Availability: Verify the service is registered:
php bin/console debug:container knpu_lorem_ipsum.knpu_ipsum
(Should return KnpUIpsum as the service ID.)
Inspect Word Lists: Debug the active word providers:
$reflection = new \ReflectionClass($loremIpsum);
$property = $reflection->getProperty('wordProviders');
$property->setAccessible(true);
var_dump($property->getValue($loremIpsum));
Configuration Overrides:
Clear cache after modifying knpu_lorem_ipsum.yaml:
php bin/console cache:clear
Custom Word Lists:
Override the default word lists by implementing WordProviderInterface and prioritizing your provider via tags:
tags:
- { name: knpu_ipsum_word_provider, priority: 100 } # Higher priority = used first
Sentence/Paragraph Templates:
Extend the bundle by creating a decorator for KnpUIpsum to modify sentence structure:
class CustomLoremIpsumDecorator implements KnpUIpsumInterface {
public function __construct(private KnpUIpsum $decorated) {}
public function getParagraphs(int $count) {
return $this->decorated->getParagraphs($count) . " [Custom suffix]";
}
}
Register as a service with the same ID (knpu_lorem_ipsum.knpu_ipsum) to override.
Language Support: Add multi-language support by creating locale-specific word providers and switching them via configuration.
CLI Usage: Generate bulk placeholder data in commands:
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class GenerateFakeDataCommand extends Command {
protected static $defaultName = 'app:generate-fake-data';
public function __construct(private KnpUIpsum $loremIpsum) {
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int {
$output->writeln($this->loremIpsum->getParagraphs(10));
return Command::SUCCESS;
}
}
API Responses: Use for mock API responses in tests or staging:
$response = [
'data' => [
'title' => $loremIpsum->getWords(3),
'description' => $loremIpsum->getParagraphs(2),
],
];
Performance: For high-frequency calls (e.g., in loops), generate text once and reuse:
$batchText = $loremIpsum->getParagraphs(100); // Generate all at once
foreach (range(1, 100) as $i) {
$item = "Item $i: " . substr($batchText, $i * 100, 100);
}
How can I help you explore Laravel packages today?