Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Pizzon Bundle Laravel Package

apa1112/pizzon-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. 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.

  2. 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.

  3. 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 %}
    

Implementation Patterns

Core Workflows

  1. Pizza Management

    • CRUD Operations: Use PizzonService methods like:
      $pizzon->createPizza(['name' => 'Margherita', 'price' => 10.99, 'toppings' => ['tomato', 'mozzarella']]);
      $pizzon->updatePizza(1, ['price' => 11.99]);
      $pizzon->deletePizza(1);
      
    • Bulk Actions: Import/export pizzas via YAML/JSON using PizzonImporter:
      $importer = new PizzonImporter();
      $importer->importFromYaml(file_get_contents('pizzas.yaml'));
      
  2. Order Processing

    • Create Orders: Use OrderService to handle orders:
      $order = $orderService->createOrder([
          'pizza_id' => 1,
          'quantity' => 2,
          'customer_name' => 'John Doe',
          'customer_email' => 'john@example.com'
      ]);
      
    • Order Status: Track orders with:
      $orderService->updateOrderStatus($order->id, 'delivered');
      
  3. Customization

    • Dynamic Toppings: Extend the 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
      }
      
  4. API Integration

    • Expose Endpoints: Use Symfony’s Serializer to return JSON responses:
      use Symfony\Component\Serializer\SerializerInterface;
      
      public function getPizzas(SerializerInterface $serializer)
      {
          return $serializer->serialize($this->pizzon->getAllPizzas(), 'json');
      }
      

Gotchas and Tips

Pitfalls

  1. Symfony Dependency

    • The bundle is Symfony-specific (uses sensio/framework-extra-bundle, twig-bundle, etc.). For Laravel:
      • Use Symfony Bridge (symfony/http-foundation, symfony/routing) or extract core logic into standalone classes.
      • Replace Twig with Blade by adapting the PizzonService to return data for Blade templates.
  2. Configuration Overrides

    • Default config is hardcoded in the bundle. Override it in config/pizzon.php:
      return [
          'pizzas' => [
              'default' => [
                  'name' => 'Custom Pizza',
                  'price' => 8.99,
                  'toppings' => ['cheese', 'pepperoni'],
              ],
          ],
      ];
      
    • Tip: Use config('pizzon.pizzas') to access dynamic configs.
  3. Database Migrations

    • The bundle does not include migrations. Manually create them for:
      • pizzas table (id, name, price, base_size).
      • toppings table (id, name, price).
      • pizza_toppings pivot table (pizza_id, topping_id).
    • Tip: Use make:migration and define foreign keys explicitly.
  4. Twig Dependency

    • If not using Twig, mock the 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);
          }
      }
      

Debugging Tips

  1. Log Service Calls Add debug logging to PizzonService:

    public function getAllPizzas()
    {
        \Log::debug('Fetching all pizzas', ['pizzas' => $this->pizzaRepository->findAll()]);
        return $this->pizzaRepository->findAll();
    }
    
  2. 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'),
        ]),
    ]);
    
  3. Test Coverage

    • Write unit tests for 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);
      }
      

Extension Points

  1. Custom Pizza Types Extend the Pizza entity or use traits:

    trait GlutenFreePizza
    {
        public function isGlutenFree(): bool
        {
            return true;
        }
    }
    
  2. 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;
    }
    
  3. 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.
    }
    
  4. 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"
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware