Code Monkey home page Code Monkey logo

psr7-middleware's Introduction

PSR-7 middleware for prooph components

Consume prooph messages (commands, queries and events) with a PSR-7 middleware. Please refer to the service-bus component documentation to see how to configure the different bus types.

Deprecated

Please use prooph/http-middleware instead.

Build Status Coverage Status Gitter

Middleware

For every bus system a middleware exists and one Middleware to rule them all.

  • CommandMiddleware: Dispatches the message data to the command bus system
  • QueryMiddleware: Dispatches the message data to the query bus system
  • EventMiddleware: Dispatches the message data to the event bus system
  • MessageMiddleware: Dispatches the message data to the appropriated bus system depending on message type

Installation

You can install prooph/psr7-middleware via Composer by adding "prooph/psr7-middleware": "^0.1" as requirement to your composer.json.

Documentation

Documentation is in the docs tree, and can be compiled using bookdown.

$ php ./vendor/bin/bookdown docs/bookdown.json
$ php -S 0.0.0.0:8080 -t docs/html/

Then browse to http://localhost:8080/

Support

Contribute

Please feel free to fork and extend existing or add new plugins and send a pull request with your changes! To establish a consistent code quality, please provide unit tests for all your changes and may adapt the documentation.

License

Released under the New BSD License.

psr7-middleware's People

Contributors

basz avatar bgaleotti avatar codeliner avatar prolic avatar sandrokeil avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

psr7-middleware's Issues

a ResponseStrategyFactory

suggested by @oqq

not a bad idea... I now have one strategy that actually handles two kind of responses...

$responseStrategy = $this->responseStrategyFactory->createStrategyFromRequest($request);

return $responseStrategy->fromPromise(
    $this->queryBus->dispatch($query)
);

router config

 [
            'name'            => 'query::status-pong',
            'path'            => '/status/pong',
            'middleware'      => [
                QueryMiddleware::class,
            ],
            'allowed_methods' => ['POST'],
            'options'         => [
                'values' => [
                    QueryMiddleware::NAME_ATTRIBUTE => Status\QueryName::pong,
                    QueryMiddleware::PROMISE_TO_RESPONSE_STRATEGY => A/JsonResponse::class,
                ],
            ],
        ],

Support for queries on standard projections

on chat

I'm thinking bout updating the middleware component to add support for queries on standard projections. Would that be useful?

sounds good

It is possible to query standard projections. One QueryStandardProjection query could do it, but we would loose out on permissions etc.. so... quick and dirty

trait StandardQueryTrait {
 public function streamName(): string {
    return $this->metadata()['stream_name'];
 }

 public function result($state) {
   return $state;
  }
}
abstract class QueryStandardProjection extends Messaging\Query implements Messaging\PayloadConstructable  {
   use Messaging\StandardQueryTrait;
}

class StandardQueryHandler {
    private $projectionManager;

    public function __construct(ProjectionManager $projectionManager)
    {
        $this-> projectionManager = $projectionManager;
    }

    public function onEvent(QueryStandardProjection $query, Deferred $deferred)
    {
        // use projectionManager to retrieve state of $query->streamName()

      $deferred->resolve($query->makeResult($state));
    }
}

class MyQuery extends QueryStandardProjection {
   public function result($state) {
     return ValueObject::fromArray($state);
  }
}

routes

        [
            'name'            => 'query::something',
            'path'            => '/path',
            'middleware'      => [
                StandardQueryMiddleware::class,
            ],
            'allowed_methods' => ['OPTION', 'GET'],
            'options'         => [
                'values' => [
                    StandardQueryMiddleware::NAME_ATTRIBUTE => MyQuery::class,
                    StandardQueryMiddleware::STREAM_NAME => 'my_projection',
                ],
            ],
        ],

Which add a StandardQueryMiddleware.

It's just an rough idea... Looking for input

RFC: Adding arbitrary middlewares to command & query routes

To add additional middleware to prooph routes one currently has to add those manually to each defined route.

eg.

[
  'name'            => 'query::support-doc::list-faqs',
  'path'            => '/support-doc/categories/{category_id}/faqs',
  'middleware'      => [
    JsonPayload::class,
    JsonApiProblem::class,
    ResourceServerMiddleware::class,
    JsonApiRequestMiddleware::class,
    ContentLanguageMiddleware::class,
    QueryMiddleware::class,
  ],
  'allowed_methods' => ['GET'],
  'options'         => ['values' => [QueryMiddleware::NAME_ATTRIBUTE => QueryName::listFaqs]],
],

We could introduce a way so QueryMiddleware and CommandMiddleware are capable of adding arbitrary middlewares so it can be easily configured.

I'm thinking of this configuration bit and some factory modifications;

    'prooph' => [
        'middleware' => [
            'query'   => [
                'response_strategy' => \HF\Api\ResponseStrategy\JsonApiResponseStrategy::class,
                'message_factory'   => \HF\Api\Messaging\QueryMessageFactory::class,
                'metadata_gatherer' => \HF\Api\Middleware\RequestMetadataGatherer::class,
                'middlewares'       =>
                    [
                        JsonPayload::class,
                        JsonApiProblem::class,
                        JsonApiRequestMiddleware::class,
                        ContentLanguageMiddleware::class,
                    ],
            ],
            'command' => [
                'response_strategy' => \HF\Api\ResponseStrategy\JsonApiResponseStrategy::class,
                'message_factory'   => \HF\Api\Messaging\CommandMessageFactory::class,
                'metadata_gatherer' => \HF\Api\Middleware\RequestMetadataGatherer::class,
                'middlewares'       =>
                    [
                        JsonPayload::class,
                        JsonApiProblem::class,
                        JsonApiRequestMiddleware::class,
                        ContentLanguageMiddleware::class,
                    ],
            ],
            ...
        ],
    ],

These 'middlewares' would need to be fetched from the container by Command and Query Factories and passed to the Command and QueryMiddlewares whom should process them in order and before the MetadataGatherer runs.

Shouldn't be too difficult, thoughts?

Service Prooph\Psr7Middleware\CommandMiddleware did not to resolve to a Psr\Http\Server\MiddlewareInterface

Hey guys,

I've just been following the prooph video tutorial over on youtube. Inside he's using the Psr7Middleware.

While using the most current Expressive default installation I'm running into the issue that the CommandMiddleware cannot be instantiated properly as it does not comform with the most current standard that is Psr\Http\Server\MiddlewareInterface.

Upon further inspection I assume this requires an update of the underlying package webimpress/http-middleware-compatibility as I do not see him handling the case inside the package.

Locally this can be solved by class_alias the CommandMiddleware to have the proper interface and structure but I suppose an update either here or on the side of webimpress should be done. If I am overlooking something please let me know, I'll gladly provide more info or PRs where required :)

Add possibility to get metadata from request

It could be useful to get specific metadata from the psr7 request and create a command/event/query using the metadata. One example is an identity value object which was added by an authentication middleware to the request as an attribute.

One option would be to inject a "MetadataGatherer" (maybe optional dependency or a noop default) to each message handling middleware. The creation of a message could then look something like this:

$query = $this->queryFactory->createMessageFromArray($queryName, [
  'payload' => $payload,
  'metadata' => $this->metadataGatherer->getFromRequest($request),
]);

ResponseStrategy should be context aware

Hi!

Currently i see no clean way to provide some context metadata to response strategy.
As example it would be handy to use the request or the query that was dispatched to the query bus, to get the provided metadata.

final class HtmlResponseStrategy implements ResponseStrategy
{
    /** @var TemplateRendererInterface */
    private $templateRenderer;

    /**
     * @param TemplateRendererInterface $templateRenderer
     */
    public function __construct(TemplateRendererInterface $templateRenderer)
    {
        $this->templateRenderer = $templateRenderer;
    }

    /**
     * @inheritDoc
     */
    public function fromPromise(Promise $promise, array $context)
    {
        /** @var ServerRequestInterface $request */
        $request = $context['request'];
        $template = $request->getAttribute('template');

        $data = null;

        $promise->done(function($result) use (&$data) {
            $data = $result;
        });

        return new HtmlResponse(
            $this->templateRenderer->render($template),
            ['data' => $data]
        );
    }
}

Without $context i have create a abstract factory to inject the template name in the strategy, which would ends in a config nighmare.

Or am i missing a already existing solution for that issue?
Some hints would to be welcome.

Thanks!

Rename Package to psr15-middlewares

Since there would be a new PSR for middlewares with identifier PSR-15 sone, we should rename this repo before a stable release.

Also this changes here: #34
uses the http-interop vendor. This should be changed to the final psr/http-server-middleware once it is available.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.