Installation Add the bundle via Composer in a Symfony/Laravel-compatible project (note: this is a Symfony bundle, but can be adapted for Laravel via Symfony Bridge or standalone usage):
composer require apa1112/pizzon-bundle
Register the bundle in config/bundles.php (Symfony) or manually bootstrap it in Laravel's AppServiceProvider.
Publish Config Publish the default configuration:
php artisan vendor:publish --tag=pizzon-config
This creates config/pizzon.php with default pizza types, toppings, and pricing.
First Use Case: Display a Pizza Menu
Use the PizzonService to fetch available pizzas and render them in a Twig template:
// In a controller
public function showMenu(PizzonService $pizzon)
{
$pizzas = $pizzon->getAllPizzas();
return view('pizzas.menu', compact('pizzas'));
}
In Twig (resources/views/pizzas/menu.blade.php):
{% for pizza in pizzas %}
<div class="pizza">
<h3>{{ pizza.name }}</h3>
<p>Price: ${{ pizza.price }}</p>
<p>Toppings: {{ pizza.toppings|join(', ') }}</p>
</div>
{% endfor %}
Pizza Management
PizzonService methods like:
$pizzon->createPizza(['name' => 'Margherita', 'price' => 10.99, 'toppings' => ['tomato', 'mozzarella']]);
$pizzon->updatePizza(1, ['price' => 11.99]);
$pizzon->deletePizza(1);
PizzonImporter:
$importer = new PizzonImporter();
$importer->importFromYaml(file_get_contents('pizzas.yaml'));
Order Processing
OrderService to handle orders:
$order = $orderService->createOrder([
'pizza_id' => 1,
'quantity' => 2,
'customer_name' => 'John Doe',
'customer_email' => 'john@example.com'
]);
$orderService->updateOrderStatus($order->id, 'delivered');
Customization
Topping entity to add custom logic (e.g., premium toppings):
// In a service
public function addPremiumTopping(Pizza $pizza, string $topping)
{
$pizza->addTopping($topping);
$pizza->price += 2.00; // Add premium surcharge
}
API Integration
Serializer to return JSON responses:
use Symfony\Component\Serializer\SerializerInterface;
public function getPizzas(SerializerInterface $serializer)
{
return $serializer->serialize($this->pizzon->getAllPizzas(), 'json');
}
Symfony Dependency
sensio/framework-extra-bundle, twig-bundle, etc.). For Laravel:
symfony/http-foundation, symfony/routing) or extract core logic into standalone classes.PizzonService to return data for Blade templates.Configuration Overrides
config/pizzon.php:
return [
'pizzas' => [
'default' => [
'name' => 'Custom Pizza',
'price' => 8.99,
'toppings' => ['cheese', 'pepperoni'],
],
],
];
config('pizzon.pizzas') to access dynamic configs.Database Migrations
pizzas table (id, name, price, base_size).toppings table (id, name, price).pizza_toppings pivot table (pizza_id, topping_id).make:migration and define foreign keys explicitly.Twig Dependency
PizzonTwigExtension or extract its logic:
// Example: Move Twig filters to a standalone class
class PizzaFilter
{
public static function formatPrice(float $price): string
{
return '$' . number_format($price, 2);
}
}
Log Service Calls
Add debug logging to PizzonService:
public function getAllPizzas()
{
\Log::debug('Fetching all pizzas', ['pizzas' => $this->pizzaRepository->findAll()]);
return $this->pizzaRepository->findAll();
}
Validate Input
The bundle lacks input validation. Add it using Symfony’s Validator or Laravel’s Validator:
use Symfony\Component\Validator\Constraints as Assert;
$constraints = new Assert\Collection([
'name' => new Assert\NotBlank(),
'price' => new Assert\GreaterThan(0),
'toppings' => new Assert\All([
new Assert\Type('string'),
]),
]);
Test Coverage
PizzonService and OrderService:
public function testCreatePizza()
{
$service = new PizzonService($this->createMock(PizzaRepository::class));
$pizza = $service->createPizza(['name' => 'Test', 'price' => 5.99]);
$this->assertEquals('Test', $pizza->name);
}
Custom Pizza Types
Extend the Pizza entity or use traits:
trait GlutenFreePizza
{
public function isGlutenFree(): bool
{
return true;
}
}
Payment Integration
Hook into the OrderService to add payment logic:
public function createOrder(array $data, PaymentGateway $gateway)
{
$order = $this->orderRepository->create($data);
$gateway->charge($order->total);
return $order;
}
Event Dispatching
Use Symfony’s EventDispatcher (or Laravel’s Events) to trigger actions:
// In PizzonService
$dispatcher->dispatch(new PizzaCreatedEvent($pizza));
Listen for events in a controller or service:
public function onPizzaCreated(PizzaCreatedEvent $event)
{
// Send email, log, etc.
}
Localization Override default strings (e.g., "Pizza", "Order") via translation files:
# config/packages/translation.yaml
frameworks:
translation:
paths: ['%kernel.project_dir%/translations']
Create translations/messages.en.yaml:
pizza:
name: "Delicious Pizza"
How can I help you explore Laravel packages today?