Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Directives Bundle Laravel Package

chiqui3d/php-directives-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require chiqui3d/php-directives-bundle
    

    Ensure the bundle is registered in config/bundles.php:

    return [
        // ...
        PHPDirectivesBundle\PHPDirectivesBundle::class => ['all' => true],
    ];
    
  2. 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
    
  3. 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).


Implementation Patterns

Workflow Integration

  1. 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
    
  2. 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 }
    
  3. 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 #}
    
  4. 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!');
    }
    

Gotchas and Tips

Pitfalls

  1. Directive Persistence

    • Directives are runtime-only; they won’t persist across PHP processes (e.g., CLI vs. web). Restart your server after changes.
    • Workaround: Use ini_set() in bootstrapping code (e.g., bootstrap.php) for critical directives.
  2. Locale Conflicts

    • LC_ALL overrides other locale settings. Avoid mixing it with specific categories (e.g., LC_TIME + LC_ALL).
    • Debugging: Check applied locales with:
      php -r "print_r(locale_get_default());"
      
  3. Symfony Cache Invalidation

    • If directives change, clear the cache:
      php bin/console cache:clear
      
    • Tip: Use cache:warmup in deployment scripts to pre-load directives.
  4. Directive Whitelisting

    • The bundle doesn’t validate directive names/values. Malformed directives (e.g., date.timezone: "Invalid/Zone") may silently fail.
    • Mitigation: Validate in a CompilerPass or use a schema (e.g., Symfony’s Config component).

Debugging

  1. Inspect Applied Directives Dump directives in a controller or command:

    use PHPDirectivesBundle\PHPDirectives;
    
    $directives = $container->get(PHPDirectives::class);
    dump($directives->getAll());
    
  2. CLI vs. Web Context

    • Directives set in php_directives.yaml apply to both CLI and web requests. For CLI-only directives, use a ConsoleCommand:
      $this->getDirectives()->set('php', ['memory_limit' => '256M']);
      

Extension Points

  1. 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 }
    
  2. 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'));
    
  3. Integration with Laravel (Hypothetical)

    • Not directly applicable (this is a Symfony bundle), but for Laravel, consider:
      • Using ini_set() in bootstrap/app.php.
      • Creating a service provider to load directives from config/directives.php.
      • Example:
        // app/Providers/DirectivesServiceProvider.php
        public function boot() {
            $directives = config('directives.php');
            foreach ($directives as $key => $value) {
                ini_set($key, $value);
            }
        }
        
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager