master

laravel/framework

Last updated at: 29/12/2023 09:20

LogBroadcaster.php

TLDR

The LogBroadcaster.php file is a class that extends the Broadcaster class. It is responsible for broadcasting events by logging them using a provided logger.

Methods

__construct

This method is used to create a new instance of the LogBroadcaster class. It accepts a LoggerInterface object as a parameter.

auth

This method is inherited from the Broadcaster class and is left empty in this implementation. It is responsible for authenticating the request.

validAuthenticationResponse

This method is inherited from the Broadcaster class and is left empty in this implementation. It is responsible for validating the authentication response.

broadcast

This method is inherited from the Broadcaster class and is overridden in this implementation. It is responsible for broadcasting events to the specified channels. It takes an array of channels, an event name, and an optional payload as parameters. The method logs the event, channels, and payload using the provided logger.

Classes

LogBroadcaster

This class extends the Broadcaster class and handles the logging of broadcasted events. It has a LoggerInterface property that is used for logging.

<?php

namespace Illuminate\Broadcasting\Broadcasters;

use Psr\Log\LoggerInterface;

class LogBroadcaster extends Broadcaster
{
    /**
     * The logger implementation.
     *
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    /**
     * Create a new broadcaster instance.
     *
     * @param  \Psr\Log\LoggerInterface  $logger
     * @return void
     */
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function auth($request)
    {
        //
    }

    /**
     * {@inheritdoc}
     */
    public function validAuthenticationResponse($request, $result)
    {
        //
    }

    /**
     * {@inheritdoc}
     */
    public function broadcast(array $channels, $event, array $payload = [])
    {
        $channels = implode(', ', $this->formatChannels($channels));

        $payload = json_encode($payload, JSON_PRETTY_PRINT);

        $this->logger->info('Broadcasting ['.$event.'] on channels ['.$channels.'] with payload:'.PHP_EOL.$payload);
    }
}