boson-php/globals-provider
Laravel package that exposes PHP superglobals and environment values through a service provider, offering a consistent way to access and share request/runtime globals across your app, with simple configuration and container bindings for easier testing.
Installation Add the package via Composer:
composer require boson-php/globals-provider
Register the service provider in config/app.php:
'providers' => [
// ...
Boson\GlobalsProvider\GlobalsServiceProvider::class,
],
First Use Case
Access global values via the Globals facade:
use Boson\GlobalsProvider\Facades\Globals;
// Set a global value
Globals::set('app_name', 'MyApp');
// Retrieve a global value
$appName = Globals::get('app_name'); // Returns 'MyApp'
Configuration Publish the config file (if needed):
php artisan vendor:publish --provider="Boson\GlobalsProvider\GlobalsServiceProvider" --tag="config"
Default config is minimal; override in config/globals.php:
return [
'prefix' => 'globals_', // Optional prefix for keys
'cache' => [
'enabled' => true,
'driver' => 'file', // Supports 'file', 'redis', 'array'
],
];
Centralized Configuration Use globals for app-wide settings (e.g., feature flags, API endpoints):
// config/app.php
Globals::set('api.base_url', env('API_BASE_URL', 'https://api.example.com'));
// In a controller
$url = Globals::get('api.base_url');
Dynamic Feature Toggles Toggle features without redeploying:
if (Globals::get('features.new_ui', false)) {
return view('new-ui');
}
Environment-Specific Overrides Load environment-specific globals via service provider boot:
public function boot()
{
if (app()->environment('local')) {
Globals::set('debug.toolbar', true);
}
}
Cache Integration
Enable caching in config/globals.php for performance:
'cache' => [
'enabled' => true,
'driver' => env('GLOBALS_CACHE_DRIVER', 'file'),
],
Clear cache manually or via events (e.g., Globals::clear()).
Middleware for Contextual Globals Set request-scoped globals in middleware:
public function handle($request, Closure $next)
{
Globals::set('user.id', auth()->id());
return $next($request);
}
Validation Layer Add a trait to validate globals on retrieval:
trait ValidatesGlobals {
protected function getValidatedGlobal($key, $default = null, $validator = null)
{
$value = Globals::get($key, $default);
return $validator ? $validator($value) : $value;
}
}
Key Collisions
globals_), keys may conflict with Laravel’s built-in storage (e.g., session, cache).config/globals.php:
'prefix' => 'app_globals_',
Cache Invalidation
Globals::clear() is called.Globals::clear('app_globals_*'); // Clear all prefixed globals
Thread Safety
Serialization Issues
Globals::set('user_data', json_encode($user));
$data = json_decode(Globals::get('user_data'));
Inspect All Globals Use Tinker to dump all globals:
php artisan tinker
>>> \Boson\GlobalsProvider\Facades\Globals::all();
Check Cache Driver
If globals aren’t updating, verify the cache driver is configured correctly in .env:
GLOBALS_CACHE_DRIVER=redis
CACHE_DRIVER=redis
Custom Storage Backends
Extend the GlobalsManager to support databases or external APIs:
// app/Providers/GlobalsServiceProvider.php
public function register()
{
$this->app->singleton('globals', function ($app) {
return new \Boson\GlobalsProvider\GlobalsManager(
new \App\Services\CustomGlobalsStore()
);
});
}
Event Hooks Listen for global changes via events (if the package emits them). Example:
// app/Providers/EventServiceProvider.php
protected $listen = [
'Boson\GlobalsProvider\Events\GlobalSet' => [
\App\Listeners\LogGlobalChange::class,
],
];
Laravel Scout Integration Index globals for search:
Globals::search('feature_*')->get(); // Hypothetical; requires custom implementation.
How can I help you explore Laravel packages today?