Skip to content

04 Environment Detection

Rumen Damyanov edited this page Jul 29, 2025 · 1 revision

Environment Detection

This example demonstrates how the php-assets package automatically detects different environments and adjusts behavior accordingly.

Automatic Environment Detection

The package automatically detects your environment based on several factors:

<?php

use Rumenx\Assets\Asset;

// The package automatically detects:
// - Production environment
// - Development environment
// - Local development
// - CI/CD environments

// Add assets normally - behavior adapts to environment
Asset::add('css/app.css');
Asset::add('js/app.js');

echo Asset::css();
echo Asset::js();

Environment-Specific Behavior

Development Environment

In development, the package provides:

  • Unminified asset paths
  • Detailed error messages
  • No aggressive caching
// Development output
<link rel="stylesheet" href="css/app.css" />
<script src="js/app.js"></script>

Production Environment

In production, the package provides:

  • Optimized asset handling
  • Cache busting support
  • Minified asset preferences
// Production output (with cache busting)
<link rel="stylesheet" href="css/app.min.css?529e54acf891ccc6592f115afa1cc077" />
<script src="js/app.min.js?a8b2c3d4e5f6789012345678901234567890"></script>

Manual Environment Configuration

You can manually set the environment if needed:

<?php

use Rumenx\Assets\Asset;

// Check current environment
$env = Asset::getEnvironment();
echo "Current environment: " . $env; // 'production', 'development', etc.

// The package respects common environment variables:
// - APP_ENV
// - ENVIRONMENT
// - NODE_ENV
// - CI

Environment-Aware Asset Loading

Conditional Asset Loading

<?php

use Rumenx\Assets\Asset;

// Load different assets based on environment
if (Asset::getEnvironment() === 'development') {
    // Development assets
    Asset::add('css/app.css');
    Asset::add('js/app.js');
    Asset::add('js/debug.js');
} else {
    // Production assets
    Asset::add('css/app.min.css');
    Asset::add('js/app.min.js');
}

// Analytics only in production
if (Asset::getEnvironment() === 'production') {
    Asset::add('js/analytics.js', 'footer');
}

Debug Mode Detection

<?php

use Rumenx\Assets\Asset;

// Add debug assets in development
if (Asset::getEnvironment() !== 'production') {
    Asset::add('css/debug.css');
    Asset::add('js/debug-toolbar.js');
}

// Add error tracking in production
if (Asset::getEnvironment() === 'production') {
    Asset::add('js/error-tracking.js');
}

Framework Integration Examples

Laravel Integration

<?php

use Rumenx\Assets\Asset;

// Laravel automatically sets APP_ENV
// The package will detect this environment

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // Environment-specific asset configuration
        if (app()->environment('production')) {
            Asset::setCachebuster(public_path('build/assets.json'));
        }

        if (app()->environment('local')) {
            // Add development tools
            Asset::add('css/debug.css');
        }
    }
}

Symfony Integration

<?php

use Rumenx\Assets\Asset;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;

class AssetService
{
    private $params;

    public function __construct(ParameterBagInterface $params)
    {
        $this->params = $params;
    }

    public function configureAssets()
    {
        $env = $this->params->get('kernel.environment');

        if ($env === 'prod') {
            Asset::setCachebuster(__DIR__ . '/../public/build/assets.json');
        }

        if ($env === 'dev') {
            Asset::add('css/debug.css');
            Asset::add('js/symfony-profiler.js');
        }
    }
}

Environment Variables

The package recognizes these environment variables:

# Production
APP_ENV=production
ENVIRONMENT=production
NODE_ENV=production

# Development
APP_ENV=development
ENVIRONMENT=development
NODE_ENV=development

# Local development
APP_ENV=local
ENVIRONMENT=local

# CI/CD
CI=true

Practical Example

<?php

use Rumenx\Assets\Asset;

// Base assets for all environments
Asset::add('css/normalize.css');
Asset::add('css/app.css');
Asset::add('js/app.js');

// Environment-specific additions
switch (Asset::getEnvironment()) {
    case 'production':
        Asset::setCachebuster(__DIR__ . '/public/build/assets.json');
        Asset::add('js/analytics.js', 'footer');
        Asset::add('js/error-tracking.js', 'footer');
        break;

    case 'development':
    case 'local':
        Asset::add('css/debug.css');
        Asset::add('js/debug-toolbar.js', 'footer');
        break;

    case 'testing':
        // Minimal assets for testing
        break;
}

?>
<!DOCTYPE html>
<html>
<head>
    <title>Environment-Aware App</title>
    <?= Asset::css() ?>
</head>
<body>
    <h1>Current Environment: <?= Asset::getEnvironment() ?></h1>

    <?= Asset::js() ?>
    <?= Asset::js('footer') ?>
</body>
</html>

Best Practices

  1. Use environment detection: Let the package automatically detect the environment
  2. Conditional assets: Load different assets based on environment needs
  3. Production optimization: Enable cache busting and minified assets in production
  4. Development tools: Include debug assets only in development
  5. Testing simplicity: Use minimal assets in testing environments

Related Examples

Clone this wiki locally