class HandlerStack implements HandlerStackInterface (View source)

SimplePie\HandlerStack is a middleware stack system which is modeled after Guzzle's middleware handler stack system, but is designed specifically for SimplePie's use-cases.

Its primary job is to (a) allow the registration and priority of middleware, and (b) provide the interface for SimplePie NG to trigger middleware.

Traits

Shared code for working with the logger.

Properties

protected LoggerInterface $logger A PSR-3 logger. from LoggerTrait
protected array $stack The middleware stack, grouped by feed type.

Methods

setLogger(LoggerInterface $logger)

Sets the PSR-3 logger.

getLogger()

Retrieves the PSR-3 logger.

__construct()

Constructs a new instance of this class.

append(callable $middleware, string|null $name = null, string|null $overrideType = null)

Appends a new middleware class onto the end of the stack.

appendClosure(string $overrideType, callable $middleware, string|null $name = null)

Appends a new middleware closure onto the end of the stack.

prepend(callable $middleware, string|null $name = null, string|null $overrideType = null)

Prepends a new middleware class onto the beginning of the stack.

prependClosure(string $overrideType, callable $middleware, string|null $name = null)

Prepends a new middleware closure onto the beginning of the stack.

void
invoke(string $feedType, stdClass $feedRoot, string|null $namespaceAlias, DOMXPath $xpath)

Invokes the stack of middleware.

void
registerNamespaces(Ns $ns)

Collects all of the supported namespaces from the registered middleware.

array
debugStack()

Returns information about the HandlerStack that is useful for debugging.

void
validateMiddleware(callable $middleware, string|null $name, string|null $overrideType, callable $fn)

Validates the middleware and applies it to the right stack.

void
logRegistration(callable $middleware, string|null $name = null)

Log that the registration of the middleware occurred.

string
exceptionMessage(callable $middleware, string|null $name = null)

Generate the most appropriate error message based on the parameters that were passed.

Details

LoggerTrait setLogger(LoggerInterface $logger)

Sets the PSR-3 logger.

Parameters

LoggerInterface $logger A PSR-3 compatible logger.

Return Value

LoggerTrait

LoggerInterface getLogger()

Retrieves the PSR-3 logger.

Return Value

LoggerInterface

__construct()

Constructs a new instance of this class.

HandlerStackInterface append(callable $middleware, string|null $name = null, string|null $overrideType = null)

Appends a new middleware class onto the end of the stack.

Parameters

callable $middleware The middleware to add to the stack.
string|null $name A name for the middleware. Can be used with pushBefore() and pushAfter().
string|null $overrideType Override our best guess for which stack to apply the middleware to. By default the appropriate stack will be determined by which SimplePie\Middleware\*\*Interface the middleware extends from. If the middleware is a closure, this parameter is required. If the appropriate stack cannot be determined, a SimplePie\Exception\MiddlewareException exception will be thrown.

Return Value

HandlerStackInterface

Exceptions

MiddlewareException

HandlerStackInterface appendClosure(string $overrideType, callable $middleware, string|null $name = null)

Appends a new middleware closure onto the end of the stack.

Parameters

string $overrideType Override our best guess for which stack to apply the middleware to. By default the appropriate stack will be determined by which SimplePie\Middleware\*\*Interface the middleware extends from. If the middleware is a closure, this parameter is required. If the appropriate stack cannot be determined, a SimplePie\Exception\MiddlewareException exception will be thrown.
callable $middleware The middleware to add to the stack.
string|null $name A name for the middleware. Can be used with pushBefore() and pushAfter().

Return Value

HandlerStackInterface

Exceptions

MiddlewareException

HandlerStackInterface prepend(callable $middleware, string|null $name = null, string|null $overrideType = null)

Prepends a new middleware class onto the beginning of the stack.

Parameters

callable $middleware The middleware to add to the stack.
string|null $name A name for the middleware. Can be used with pushBefore() and pushAfter().
string|null $overrideType Override our best guess for which stack to apply the middleware to. By default the appropriate stack will be determined by which SimplePie\Middleware\*\*Interface the middleware extends from. If the middleware is a closure, this parameter is required. If the appropriate stack cannot be determined, a SimplePie\Exception\MiddlewareException exception will be thrown.

Return Value

HandlerStackInterface

Exceptions

MiddlewareException

HandlerStackInterface prependClosure(string $overrideType, callable $middleware, string|null $name = null)

Prepends a new middleware closure onto the beginning of the stack.

Parameters

string $overrideType Override our best guess for which stack to apply the middleware to. By default the appropriate stack will be determined by which SimplePie\Middleware\*\*Interface the middleware extends from. If the middleware is a closure, this parameter is required. If the appropriate stack cannot be determined, a SimplePie\Exception\MiddlewareException exception will be thrown.
callable $middleware The middleware to add to the stack.
string|null $name A name for the middleware. Can be used with pushBefore() and pushAfter().

Return Value

HandlerStackInterface

Exceptions

MiddlewareException

void invoke(string $feedType, stdClass $feedRoot, string|null $namespaceAlias, DOMXPath $xpath)

Invokes the stack of middleware.

Parameters

string $feedType A valid single feed type from SimplePie\Enum\FeedType. Since FeedType::ALL represents multiple feed types, an exception will be thrown if it is used.
stdClass $feedRoot The root of the feed. This will be written-to when the parsing middleware runs.
string|null $namespaceAlias The preferred namespace alias for a given XML namespace URI. Should be the result of a call to SimplePie\Util\Ns.
DOMXPath $xpath The DOMXPath object with this middleware's namespace alias applied.

Return Value

void

void registerNamespaces(Ns $ns)

Collects all of the supported namespaces from the registered middleware.

NOTE: Only significant for XML-based feed types.

Parameters

Ns $ns The XML namespace handler.

Return Value

void

array debugStack()

Returns information about the HandlerStack that is useful for debugging.

Return Value

array

protected void validateMiddleware(callable $middleware, string|null $name, string|null $overrideType, callable $fn)

Validates the middleware and applies it to the right stack.

Parameters

callable $middleware The middleware to add to the stack.
string|null $name A name for the middleware. Can be used with pushBefore() and pushAfter().
string|null $overrideType Override our best guess for which stack to apply the middleware to. By default the appropriate stack will be determined by which SimplePie\Middleware\*\*Interface the middleware extends from. If the middleware is a closure, this parameter is required. If the appropriate stack cannot be determined, a SimplePie\Exception\MiddlewareException exception will be thrown.
callable $fn A callable which receives the stack by-reference as a parameter, and chooses how to add the middleware to that stack.

Return Value

void

Exceptions

MiddlewareException

protected void logRegistration(callable $middleware, string|null $name = null)

Log that the registration of the middleware occurred.

Parameters

callable $middleware The middleware to add to the stack.
string|null $name A name for the middleware. Can be used with pushBefore() and pushAfter().

Return Value

void

protected string exceptionMessage(callable $middleware, string|null $name = null)

Generate the most appropriate error message based on the parameters that were passed.

Parameters

callable $middleware The middleware to add to the stack.
string|null $name A name for the middleware. Can be used with pushBefore() and pushAfter().

Return Value

string