Download the PHP package honeystone/context without Composer
On this page you can find all versions of the php package honeystone/context. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download honeystone/context
More information about honeystone/context
Files in honeystone/context
Package context
Short Description Application context manager for Laravel.
License MIT
Homepage https://honeystone.com
Informations about the package context
Application Context Manager for Laravel
We developed honeystone/context
for managing the application context in multi-tenant applications. It provides a
simple, fluent API for intializing, extending and switching contexts using 'context resolvers'. In addition, contexts
are automagically available in queued jobs and can be used to scope Eloquent models.
This package was developed several years ago for our own multi-tenant applications. We've recently decided to release it in the hope it will be useful to the wider Laravel community. We're open to contributions, feedback and constructive criticism.
Getting started
Start by reading the short blog post that demonstrates how to use this package in multi-tenant application.
Support us
We are committed to delivering high-quality open source packages maintained by the team at Honeystone. If you would like to support our efforts, simply use our packages, recommend them and contribute.
If you need any help with your project, or require any custom development, please get in touch.
Installation
Usage
A typical use-case would be to declare and initialize the context in your middleware. You can then access the context data like this:
Defining the context
Contexts need to be defined before they can be resolved:
These context definitions are 'required', so if they cannot be resolved a CouldNotResolveRequiredContextException
will
be thrown. For undefined contexts, a UndefinedContextException
will be thrown.
You can also define 'accepted', but not 'required' contexts:
Or accept / require based on the existence of another context:
Initializing the context
To initialize the context, you'll need to provide a resolver:
Here's an example resolver class:
You can customize serialization logic like this:
You can validate the integrity of a resolved context like this:
You can also reinitialize or deinitialize the current context:
Extending the context
The current context can be extended using another resolver:
Temporarily switching the context
Using a closure:
Using the start and end methods:
Events and receivers
The Honeystone\Context\Events\ContextChanged
event is dispatched whenever the context is changed.
You can also specify receivers to be notified when individual context values are set:
Here's an example receiver:
Scoping models using the context
You can use the current context to scope your Eloquent models.
Here's an example:
Known issues
- Tests could do with improvement
Laravel's "context"
When this solution was created, Laravel context did not exist. The function name collision is unfortunate, but not really a problem. You'll just need to make sure you import the function.
Whilst the name will invite comparison, these packages are solving different problems. Laravel context is a generic global data store. This package is specifically for resolving context objects with more complex logic and using them to scope the application, for example in a multi-tenancy application.
Changelog
A list of changes can be found in the CHANGELOG.md file.
License
Honeystone Consulting Ltd
All versions of context with dependencies
spatie/laravel-package-tools Version ^1.16.4
illuminate/contracts Version ^10.0|^11.0