Download the PHP package brain/context without Composer
On this page you can find all versions of the php package brain/context. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download brain/context
More information about brain/context
Files in brain/context
Informations about the package context
Context
Context is package that aims to collect "context" to pass to templates based on a query object.
Best paired with a template engine. And maybe with Hierarchy.
Quick start
Let's assume a couple of classed designed to provide context for the homepage and the singular view, respectively:
Now we can make use of the Context
class to generate the context for our templates:
Context
class emit the action `"brain.context.providers" that can be used to add providers from
different places:
Examples using Hierarchy
Here's an example of using context in combination with Brain Hierarchy to render mustache templates passing them context.
Above is all the necessary code to render *.mustache
templates from a /templates
subfolder
in current theme (or parent theme, if any), according to WP template hierarchy, passing to templates
context data that can be extended via ad-hoc "view context" classes which will implement
Context\ProviderFactory
interface.
Providers
Composite providers
The "Quick start" section above uses Context\Provider\ArrayMerge
class to "merge" several
providers.
Besides that class, there's also a Context\Provider\ArrayMergeRecursive
"composite" provider.
Atomic providers
The "composite" providers merge multiple "atomic" providers that can be either custom (anything
implementing Context\Provider
) or one of the shipped provider classes:
ByArray
- which provides a given array as-isByCallback
- which provides an array returned by a given callbackComments
- which provides an array of comments using given comment query argumentsPosts
- which provides an array of posts using given post query argumentsSubquery
- which provides aWP_Query
instance using given query argumentsTerms
- which provides an array of comments using given taxonomy terms query argumentsUsers
- which provides an array of comments using given user query arguments
Custom providers
The Context\Provider
interface has a single method:
Which can be implemented to build custom providers. In the case the provider should not be used
based on conditions, it can return null
.
The given PSR-3 logger interface can be used to log errors and distinguish a provider that returns
null due to errors form another that returns null
because, for example, not targeting the current
query.
Logger
All providers support a PSR-3 logger. Context
class implements PSR-3 LoggerAwareInterface
, so
it is possible to call setLogger
when instantiating it.
There's also a "brain.context.logger"
action that passes a callback that can be used to set the
logger:
Requirements
Context requires PHP 7.1+ and Composer to be installed.
Installation
Best served by Composer, available on Packagist with name brain/context
.
License
Context is released under MIT.