PHP code example of pollora / block-bindings

1. Go to this page and download the library: Download pollora/block-bindings 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/ */

    

pollora / block-bindings example snippets


use Pollora\Attributes\BlockBinding;
use Pollora\Attributes\BlockBinding\BindingField;
use Pollora\BlockBinding\Domain\Models\BindingContext;

#[BlockBinding('acme/event', label: 'Event', postTypes: ['event'])]
final class EventBinding
{
    #[BindingField(label: 'Remaining seats')]
    public function remainingSeats(BindingContext $context, SeatCounter $seats): string
    {
        return $seats->left($context->postId ?? 0).' seats left';
    }

    #[BindingField(type: 'url')]
    public function bookingUrl(BindingContext $context): string
    {
        return 'https://example.test/book/'.$context->postId;
    }
}

use Pollora\BlockBinding\Application\Services\BindingResolver;
use Pollora\BlockBinding\Application\Services\BindingSourceBuilder;
use Pollora\BlockBinding\Application\Services\BindingSourceRegistry;
use Pollora\BlockBinding\Infrastructure\Adapters\WordPressBindingRegistry;
use Pollora\BlockBinding\Infrastructure\Services\BlockBindingDiscovery;
use Pollora\BlockBinding\Infrastructure\Sources\PostMetaSource;
use Pollora\Discovery\Domain\Models\DiscoveryLocation;
use Pollora\Discovery\Infrastructure\Services\DiscoveryEngine;
use Pollora\Hook\Adapter\Out\WordPress\Action;

$builder = new BindingSourceBuilder;
$sources = new BindingSourceRegistry;
$registry = new WordPressBindingRegistry(new Action, new BindingResolver($container, $visibility, $presenter, $metaAccessor));

$shipped = $builder->build(PostMetaSource::class);   // likewise TermMetaSource, AuthorMetaSource, OptionSource
$sources->add($shipped);
$registry->register($shipped);

(new DiscoveryEngine)
    ->addLocation(new DiscoveryLocation('MyPlugin\\', __DIR__.'/src'))
    ->addDiscovery('block_bindings', new BlockBindingDiscovery($builder, $sources, $registry))
    ->run();