PHP code example of jul6art / push-bundle

1. Go to this page and download the library: Download jul6art/push-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jul6art / push-bundle example snippets


use ApiPlatform\Metadata\ApiResource;

#[ApiResource(mercure: true)]
class SomeTopic
{
}

use Jul6Art\PushBundle\Service\Traits\PusherAwareTrait;

class SomeService
{
    use PusherAwareTrait;

    public function notify(): void
    {
        $this->pusher->push('/some/topic', ['test' => true]);
    }
}

use App\Event\MyClassEvent;
use Doctrine\ORM\Mapping as ORM;
use Jul6Art\PushBundle\Attribute\Asyncable;

#[ORM\Entity(repositoryClass: MyClassRepository::class)]
#[Asyncable(eventClass: MyClassEvent::class)]
class MyClass
{
}



declare(strict_types=1);

namespace App\Event;

use App\Entity\MyClass;
use Jul6Art\CoreBundle\Event\AbstractEvent;

class MyClassEvent extends AbstractEvent
{
    public const string CREATED = 'event.my_class.created';
    public const string DELETED = 'event.my_class.deleted';
    public const string EDITED = 'event.my_class.edited';
    public const string VIEWED = 'event.my_class.viewed';

    public function __construct(private MyClass $myClass)
    {
        parent::__construct();
    }

    public function getMyClass(): MyClass
    {
        return $this->myClass;
    }

    public function setMyClass(MyClass $myClass): static
    {
        $this->myClass = $myClass;

        return $this;
    }
}

#[ORM\Entity(repositoryClass: MyClassRepository::class)]
#[Asyncable(eventClass: MyClassEvent::class, events: ['postLoad', 'postPersist'])]
class MyClass
{
}

use Jul6Art\PushBundle\Attribute\BroadcastableEntity;

#[ORM\Entity]
#[BroadcastableEntity]
class Page { … }

#[BroadcastableEntity(type: 'CmsPage', changedFields: ['title', 'publishedAt'])]
class BlogPost { … }

final class OrganizationFeedTopicResolver implements FeedTopicResolverInterface
{
    public function resolveTopic(object $entity): string
    {
        if ($entity instanceof Organization) {
            return '/organizations/'.$entity->getId().'/feed';
        }

        if (method_exists($entity, 'getOrganization')) {
            $organization = $entity->getOrganization();

            // A tenant-scoped row with no tenant — a super-admin account, an orphan.
            return $organization instanceof Organization
                ? '/organizations/'.$organization->getId().'/feed'
                : '/admin/feed';
        }

        return '/global/feed';
    }
}

#[Route('/organization/mercure-token', methods: ['GET'])]
#[IsGranted('ROLE_USER')]
public function __invoke(SubscriberCookieFactory $factory): Response
{
    $topics = $this->topicsFor($this->getUser());          // ← your authorisation decision
    $token = $factory->createToken($topics);

    $response = new JsonResponse(['subscribed' => $topics, 'token' => $token]);
    $response->headers->setCookie($factory->createCookie($token));

    return $response;
}
shell
bin/console messenger:consume async_priority_high --time-limit 600