chiqui3d/php-directives-bundle
Installation
composer require chiqui3d/php-directives-bundle
Ensure the bundle is registered in config/bundles.php:
return [
// ...
PHPDirectivesBundle\PHPDirectivesBundle::class => ['all' => true],
];
First Configuration
Create or update config/packages/php_directives.yaml:
php_directives:
php:
date.timezone: "America/New_York" # Replace with your timezone
locale:
LC_ALL: "en_US.UTF-8" # Base locale settings
First Use Case
After configuration, restart your Symfony dev server (symfony serve --no-tls-verify if needed). Verify directives are applied by running:
php -r "echo date_default_timezone_get();"
Should output your configured timezone (e.g., America/New_York).
Environment-Specific Directives
Use Symfony’s environment-aware configuration (e.g., config/packages/dev/php_directives.yaml):
# config/packages/dev/php_directives.yaml
php_directives:
php:
error_reporting: E_ALL
display_errors: 1
Dynamic Directive Loading
Override directives programmatically in a CompilerPass or Kernel event listener:
// src/EventListener/DirectivesListener.php
namespace App\EventListener;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
class DirectivesListener {
public function onKernelController(ControllerEvent $event) {
if ($event->getRequest()->getPathInfo() === '/admin') {
$this->container->get('php_directives')->set('php', ['display_errors' => 0]);
}
}
}
Register the listener in config/services.yaml:
services:
App\EventListener\DirectivesListener:
tags:
- { name: kernel.event_listener, event: kernel.controller }
Locale-Specific Features
Combine with Symfony’s Intl component for locale-aware apps:
# config/packages/php_directives.yaml
php_directives:
locale:
LC_TIME: "fr_FR.UTF-8"
LC_MESSAGES: "fr_FR.UTF-8"
Use in templates:
{{ 'now'|date('l j F Y', 'fr_FR') }} {# Outputs in French #}
Validation Layer
Validate directives in a Command or EventSubscriber:
use PHPDirectivesBundle\PHPDirectives;
$directives = $container->get(PHPDirectives::class);
if (!$directives->has('php', 'date.timezone')) {
throw new \RuntimeException('Timezone directive is missing!');
}
Directive Persistence
ini_set() in bootstrapping code (e.g., bootstrap.php) for critical directives.Locale Conflicts
LC_ALL overrides other locale settings. Avoid mixing it with specific categories (e.g., LC_TIME + LC_ALL).php -r "print_r(locale_get_default());"
Symfony Cache Invalidation
php bin/console cache:clear
cache:warmup in deployment scripts to pre-load directives.Directive Whitelisting
date.timezone: "Invalid/Zone") may silently fail.CompilerPass or use a schema (e.g., Symfony’s Config component).Inspect Applied Directives Dump directives in a controller or command:
use PHPDirectivesBundle\PHPDirectives;
$directives = $container->get(PHPDirectives::class);
dump($directives->getAll());
CLI vs. Web Context
php_directives.yaml apply to both CLI and web requests. For CLI-only directives, use a ConsoleCommand:
$this->getDirectives()->set('php', ['memory_limit' => '256M']);
Custom Directive Sources
Extend the bundle by adding a DirectiveProvider:
namespace App\Directive;
use PHPDirectivesBundle\Provider\DirectiveProviderInterface;
class AppDirectiveProvider implements DirectiveProviderInterface {
public function getDirectives(): array {
return [
'php' => ['upload_max_filesize' => '64M'],
'locale' => ['LC_NUMERIC' => 'C'], // Default to "C" locale
];
}
}
Register it in services.yaml:
services:
App\Directive\AppDirectiveProvider:
tags:
- { name: php_directives.provider }
Dynamic Directive Overrides
Use Symfony’s ParameterBag to override directives at runtime:
# config/services.yaml
parameters:
app.php_directives_override:
php:
max_execution_time: 300
Load overrides in a CompilerPass:
$container->set('php_directives.override', $container->getParameter('app.php_directives_override'));
Integration with Laravel (Hypothetical)
ini_set() in bootstrap/app.php.config/directives.php.// app/Providers/DirectivesServiceProvider.php
public function boot() {
$directives = config('directives.php');
foreach ($directives as $key => $value) {
ini_set($key, $value);
}
}
How can I help you explore Laravel packages today?