netglue / Expressive-Prismic

Zend Expressive library for working with prismic.io's content management api
MIT License
1 stars 0 forks source link

Provide example with middleWare and route #4

Open Mondane opened 6 years ago

Mondane commented 6 years ago

Could you provide an example which middleWare to instantiate in the route config?

    /**
     * @var \Zend\Expressive\Application $app
     * @var \Zend\Stratigility\MiddlewarePipeInterface $middlewarePipe
     */
    $app->route('/', [$middlewarePipe], ['GET'], 'home')
        ->setOptions([
            'defaults' => [
                'template' => 'page::default',
                'prismic-bookmark' => 'home',
            ],
        ]);

After some searching, I came up with

    /**
     * @var \Zend\Expressive\Application $app
     * @var \Zend\Stratigility\MiddlewarePipeInterface $middlewarePipe
     */
    $app->route('/', [\ExpressivePrismic\Middleware\WebhookPipe::class], ['GET'], 'home')
        ->setOptions([
            'defaults' => [
                'template' => 'page::default',
                'prismic-bookmark' => 'home',
            ],
        ]);

But I'm not sure if this is correct.

Mondane commented 6 years ago

After some digging around, I came up with this:

A factory to create a 'FoundPipe':

<?php
declare(strict_types = 1);

namespace App\Container\Middleware;

use Psr\Container\ContainerInterface;
use Zend\Stratigility\MiddlewarePipe;
use Zend\Expressive\MiddlewareFactory;
use ExpressivePrismic\Middleware;

class FoundPipeFactory
{

    public function __invoke(ContainerInterface $container) : MiddlewarePipe
    {
        $factory = $container->get(MiddlewareFactory::class);

        $pipeline = new MiddlewarePipe;
        $pipeline->pipe($factory->prepare(Middleware\InjectPreviewScript::class));
        $pipeline->pipe($factory->prepare(Middleware\ExperimentInitiator::class));
        $pipeline->pipe($factory->prepare(Middleware\DocumentResolver::class));
        $pipeline->pipe($factory->prepare(Middleware\PrismicTemplate::class));

        return $pipeline;
    }
}

The 'FoundPipe':

<?php
declare(strict_types=1);

namespace App\Middleware;

use Zend\Stratigility\MiddlewarePipeInterface;

interface FoundPipe extends MiddlewarePipeInterface
{

}

A pipe to provide in this library?

gsteel commented 6 years ago

Hi @Mondane, your example of a 'Found Pipeline' is exactly what I do. Most content driven routes will normally use a very similar pipeline, so I'll set up a interface to target and create a factory for it. The reason the library doesn't include a default pipeline for any given project is because there are limitless ways in which you might want to process a request before finally rendering an HTML response - it made more sense to me to leave it out. It's probably a good idea to put this in the docs perhaps with an example.

Sorry it took a while to get back to you ;)