PHP code example of chamber-orchestra / view-bundle

1. Go to this page and download the library: Download chamber-orchestra/view-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/ */

    

chamber-orchestra / view-bundle example snippets


return [
    // ...
    ChamberOrchestra\ViewBundle\ChamberOrchestraViewBundle::class => ['all' => true],
];

use ChamberOrchestra\ViewBundle\Attribute\BindsFrom;
use ChamberOrchestra\ViewBundle\Attribute\Type;
use ChamberOrchestra\ViewBundle\View\BindView;
use ChamberOrchestra\ViewBundle\View\IterableView;

#[BindsFrom(User::class)]
final class UserView extends BindView
{
    public string $id;
    public string $name;

    #[Type(ImageView::class)]
    public IterableView $images;

    public function __construct(User $user)
    {
        parent::__construct($user);
    }
}

final class ImageView extends BindView
{
    public string $path;
}

#[Route('/user/me', methods: ['GET'])]
final class GetMeAction
{
    public function __invoke(): UserView
    {
        return new UserView($this->getUser());
    }
}

public static function createCacheSignature(object $source): string
{
    \assert($source instanceof User);

    return sprintf('user_%d_%d', $source->getId(), $source->getUpdatedAt()->getTimestamp());
}

final class UserView extends BindView implements CacheableViewInterface
{
    public ?int $id = null;
    public ?string $name = null;

    public function __construct(private readonly User $user)
    {
        parent::__construct($user);
    }

    public function getCacheSignature(): string
    {
        return sprintf('user_%d_%d', $this->user->getId(), $this->user->getUpdatedAt()->getTimestamp());
    }
}

final class UserView extends BindView implements CacheableViewInterface
{
    use AutoCacheSignatureTrait;

    public ?int $id = null;
    public ?string $name = null;
}

final class UserView extends CachedBindView
{
    public ?int $id = null;
    public ?string $name = null;

    public static function createCacheSignature(object $source): string
    {
        \assert($source instanceof User);

        return sprintf('user_%d_%d', $source->getId(), $source->getUpdatedAt()->getTimestamp());
    }
}

// controller — unchanged BindView ergonomics
return new UserView($user);

public function show(User $user): CachedView
{
    return new CachedView($user, UserView::class);          // UserView implements SourceCacheSignatureInterface
    // or with an explicit factory / TTL:
    return new CachedView($user, UserView::class, fn (): ViewInterface => new UserView($user), ttl: 3600);
}

final class ArticleView extends View implements SourceCacheSignatureInterface
{
    use LocalisationAwareTrait;
    use SecurityAwareTrait;

    public int $id;
    public string $title;
    public bool $canEdit;

    public function __construct(Article $article)
    {
        $this->id = $article->getId();
        $this->title = $article->getTitle(self::getLocale() ?? 'en');
        $this->canEdit = self::getUserIdentifier() === $article->getAuthorIdentifier();
    }

    public static function createCacheSignature(object $source): string { /* id + updated-at */ }
}

// controller
public function show(Article $article): PrivateCachedView
{
    return new PrivateCachedView($article, ArticleView::class);
}

final class UserListView extends BindView
{
    #[Type(UserView::class, cached: true)]  // UserView implements SourceCacheSignatureInterface
    public ?IterableView $users = null;
}

new IterableView($articles, fn (object $a): PrivateCachedView => new PrivateCachedView($a, ArticleView::class));
bash
vendor/bin/phpbench run benchmark/CachedViewRedisProfile.php --report=aggregate