Installation:
composer require amps/lorem-ipsum-bundle
(Note: The package claims Symfony 6.0 support but composer.json requires PHP 8.2 and Symfony 7.3. Verify compatibility before use.)
Enable Bundle (if not using Flex):
Add to config/bundles.php:
return [
// ...
Amps\LoremIpsumBundle\AmpsLoremIpsumBundle::class => ['all' => true],
];
First Use Case: Generate placeholder text in a controller:
use Amps\LoremIpsumBundle\KnpUIpsum;
class DemoController {
public function show(KnpUIpsum $lorem): Response {
$paragraphs = $lorem->getParagraphs(3); // 3 paragraphs
return $this->render('demo.html.twig', ['text' => $paragraphs]);
}
}
KnpUIpsum: Core service for generating text (autowire or fetch via amps_lorem_ipsum.knpu_ipsum).getParagraphs(int $count = 1): Returns $count paragraphs.getWords(int $count = 10): Returns $count random words.getSentences(int $count = 3): Returns $count sentences.Placeholder Content for Development:
{# templates/_demo.html.twig #}
<div class="placeholder-text">
{{ app.service('amps_lorem_ipsum.knpu_ipsum').getParagraphs(5) }}
</div>
Useful for UI mockups or staging environments.
Dynamic Data Seeding:
// src/Command/SeedFakeDataCommand.php
use Amps\LoremIpsumBundle\KnpUIpsum;
class SeedFakeDataCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output, KnpUIpsum $lorem) {
$fakePosts = [];
for ($i = 0; $i < 10; $i++) {
$fakePosts[] = [
'title' => $lorem->getWords(3),
'content' => $lorem->getParagraphs(2),
];
}
// Save to DB...
}
}
Customizing Output:
Combine with Symfony’s Twig for formatted placeholders:
{% for i in 1..3 %}
<article>
<h2>{{ app.service('amps_lorem_ipsum.knpu_ipsum').getWords(2) | join(' ') }}</h2>
<p>{{ app.service('amps_lorem_ipsum.knpu_ipsum').getSentences(5) }}</p>
</article>
{% endfor %}
Testing: Replace real data with lorem ipsum in unit tests:
public function testHomepage(): void {
$this->getContainer()->get('amps_lorem_ipsum.knpu_ipsum')
->setWordList(['test', 'data']); // Override words for predictable output
// ...
}
Environment-Specific Usage:
Use %kernel.environment% to enable/disable lorem ipsum in config/services.yaml:
services:
App\Service\DemoService:
arguments:
$placeholderEnabled: '%env(bool:APP_USE_LOREM)%'
Then conditionally inject KnpUIpsum where needed.
Caching: Cache generated text in a command or service to avoid regenerating on every request:
$cacheKey = 'lorem_paragraphs_' . $count;
$paragraphs = $this->cache->get($cacheKey, function() use ($lorem, $count) {
return $lorem->getParagraphs($count);
});
Symfony Version Mismatch:
composer.json).composer.json if using this bundle:
"require": {
"symfony/*": "7.3.*"
}
WordProviderInterface Not Auto-Discovered:
CustomWordProvider is tagged and registered as a service.config/services.yaml:
services:
App\Service\CustomWordProvider:
tags: ['amps_ipsum_word_provider']
Unpredictable Output in Tests:
$lorem->setWordList(['fixed', 'word', 'list']);
Configuration Overrides:
unicorns_are_real and min_sunshine settings in amps_lorem_ipsum.yaml are ignored (no implementation found in the codebase).Check Service Availability:
If KnpUIpsum is not autowired, verify:
config/bundles.php).amps_lorem_ipsum.knpu_ipsum).Inspect Word Generation: Debug the word list by dumping the service’s internal state:
$reflection = new ReflectionClass($lorem);
$property = $reflection->getProperty('wordList');
$property->setAccessible(true);
dump($property->getValue($lorem));
Custom Word Providers:
WordProviderInterface and tag the service to add domain-specific terms (e.g., medical jargon, tech terms).namespace App\Service;
use Amps\LoremIpsumBundle\WordProviderInterface;
class TechWordProvider implements WordProviderInterface {
public function getWordList(): array {
return ['algorithm', 'database', 'API', 'cache', 'endpoint'];
}
}
Override Core Logic:
KnpUIpsum service by creating a decorator or subclass:
namespace App\Service;
use Amps\LoremIpsumBundle\KnpUIpsum as BaseKnpUIpsum;
class CustomKnpUIpsum extends BaseKnpUIpsum {
public function getCodeSnippets(int $count = 1): string {
$snippets = [];
for ($i = 0; $i < $count; $i++) {
$snippets[] = $this->getWords(5) . '();';
}
return implode("\n", $snippets);
}
}
config/services.yaml:
services:
App\Service\CustomKnpUIpsum:
decorates: 'amps_lorem_ipsum.knpu_ipsum'
arguments: ['@App\Service\CustomKnpUIpsum.inner']
Add HTML/Twig Filters: Create a Twig extension for direct template usage:
namespace App\Twig;
use Amps\LoremIpsumBundle\KnpUIpsum;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
class LoremIpsumExtension extends AbstractExtension {
public function __construct(private KnpUIpsum $lorem) {}
public function getFunctions(): array {
return [
new TwigFunction('lorem_paragraphs', [$this->lorem, 'getParagraphs']),
new TwigFunction('lorem_words', [$this->lorem, 'getWords']),
];
}
}
Usage in Twig:
{{ lorem_paragraphs(2) }}
How can I help you explore Laravel packages today?