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.
Turning false for