Skip to content

Instantly share code, notes, and snippets.

@harikt
Last active March 10, 2017 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harikt/84c3b6f4a0c0b39aa0368aaad80c0d3f to your computer and use it in GitHub Desktop.
Save harikt/84c3b6f4a0c0b39aa0368aaad80c0d3f to your computer and use it in GitHub Desktop.

I am following Creating and enabling a module

What I need is get the instance of Hkt\Psr7Asset\AssetLocator from container and do some specific things on the object. This need to be done for all routes.

Is it possible to register a pipe in ConfigProvider class below ?.

<?php
namespace Hkt\Psr7AssetExample;
use Hkt\Psr7AssetExample\Middleware\Welcome;
use Hkt\Psr7AssetExample\Container\WelcomeFactory;
use Hkt\Psr7AssetExample\Middleware\Asset;
use Hkt\Psr7AssetExample\Container\AssetFactory;
class ConfigProvider
{
    public function __invoke()
    {
        return [
            'dependencies' => $this->getDependencies(),
            'templates' => [
                'paths' => [
                    'hkt-psr7-asset-example' => [
                        dirname(__DIR__) . '/templates'
                    ],
                ],
            ],
            'middleware_pipeline' => [
                [
                    'middleware' => Asset::class,
                ],
            ]
        ];
    }
    
    public function getDependencies()
    {
        return [
            'factories' => [
                Welcome::class => WelcomeFactory::class,
                Asset::class => AssetFactory::class,
            ],
        ];
    }
}

Currently this is what I did for the module

<?php
namespace Hkt\Psr7AssetExample\Container;
use Hkt\Psr7AssetExample\Middleware\Asset;
class AssetFactory
{
    public function __invoke($container)
    {
        return new Asset($container->get('Hkt\Psr7Asset\AssetLocator'));
    }
}
<?php
namespace Hkt\Psr7AssetExample\Middleware;
use Interop\Http\ServerMiddleware\MiddlewareInterface;
use Interop\Http\ServerMiddleware\DelegateInterface;
use Psr\Http\Message\ServerRequestInterface;
use Hkt\Psr7Asset\AssetLocator;
class Asset implements MiddlewareInterface
{
    protected $locator;
    public function __construct(AssetLocator $locator)
    {
        $this->locator = $locator;
    }
    public function process(ServerRequestInterface $request, DelegateInterface $delegate)
    {
        $this->locator->set('hkt/psr7-asset-example', dirname(dirname(__DIR__)) . '/public');
        return $delegate->process($request);
    }
}

and in the application level I need to register

$app->pipe(Hkt\Psr7AssetExample\Middleware\Asset::class);

What I am looking is how to get rid of creating a factory or middleware just to get the instance from DI container.

UPDATE : Need to turn off programmatic_pipeline to false for the config routes or pipeline to be called and executed.

@harikt
Copy link
Author

harikt commented Mar 10, 2017

Turning false for

// /config/autoload/zend-expressive.global.php

use Zend\ConfigAggregator\ConfigAggregator;

return [
    // ... more code
    'zend-expressive' => [
        // Enable programmatic pipeline: Any `middleware_pipeline` or `routes`
        // configuration will be ignored when creating the `Application` instance.
        'programmatic_pipeline' => false,
    ],
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment