master

laravel/framework

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

UsePusherChannelConventions.php

TLDR

This file is a trait called UsePusherChannelConventions, located in the Illuminate\Broadcasting\Broadcasters namespace. It provides two methods: isGuardedChannel and normalizeChannelName.

Methods

isGuardedChannel

This method takes a channel name as a parameter and returns true if the channel is protected by authentication. It checks if the channel name starts with either "private-" or "presence-" using the Str::startsWith method from the Illuminate\Support\Str class.

normalizeChannelName

This method takes a channel name as a parameter and removes a specific prefix from the channel name. It iterates through an array of prefixes (['private-encrypted-', 'private-', 'presence-']) and uses the Str::startsWith and Str::replaceFirst methods from the Illuminate\Support\Str class to remove the matching prefix from the channel name. The modified channel name is then returned.

<?php

namespace Illuminate\Broadcasting\Broadcasters;

use Illuminate\Support\Str;

trait UsePusherChannelConventions
{
    /**
     * Return true if the channel is protected by authentication.
     *
     * @param  string  $channel
     * @return bool
     */
    public function isGuardedChannel($channel)
    {
        return Str::startsWith($channel, ['private-', 'presence-']);
    }

    /**
     * Remove prefix from channel name.
     *
     * @param  string  $channel
     * @return string
     */
    public function normalizeChannelName($channel)
    {
        foreach (['private-encrypted-', 'private-', 'presence-'] as $prefix) {
            if (Str::startsWith($channel, $prefix)) {
                return Str::replaceFirst($prefix, '', $channel);
            }
        }

        return $channel;
    }
}