A professional Symfony Dependency Injection Container integration for WordPress, bringing enterprise-level dependency management and the Symfony Bundle System to WordPress applications.
- ✅ Full Symfony DI Container - Complete container implementation with autowiring and autoconfiguration
- ✅ Bundle System - Symfony-style bundle architecture for modular plugin development
- ✅ Environment-Aware - Automatic environment detection (dev/stage/prod) with separate container caching
- ✅ Cross-Bundle DI - Share services between plugins seamlessly
- ✅ Root Configuration - Centralized configuration in
config/directory - ✅ Translation Support - XLIFF translation resources per bundle
- ✅ WPStarter Compatible - Works out-of-the-box with WPStarter for environment management
- ✅ Enterprise Standards - KISS, DRY, SOLID principles with PSR-4 autoloading
- PHP 8.2 or higher
- WordPress 6.0+
- Composer
- Symfony DI Components 7.0+
composer require brianvarskonst/wp-dependency-injectionCreate wp-content/mu-plugins/application-starter.php:
<?php
declare(strict_types=1);
/**
* Plugin Name: Application Starter
* Description: Integrates Symfony DI Container into WordPress
* Version: 1.0.0
* Requires PHP: 8.2
*/
namespace YourNamespace\AppStarter;
use Brianvarskonst\WordPress\DependencyInjection\Application;
if (!defined('ABSPATH')) {
exit;
}
require __DIR__ . '/../../vendor/autoload.php';
Application::boot();mkdir -p config/packages/{dev,stage,prod}
touch config/bundles.php
touch config/services.yamlIn your .env file (handled by WPStarter):
WORDPRESS_ENV=devCreate config/bundles.php:
<?php
return [
App\CoreBundle::class => ['all' => true],
YourPlugin\PluginBundle::class => ['all' => true],
App\DebugBundle::class => ['dev' => true],
App\CacheBundle::class => ['prod' => true],
];Create config/services.yaml:
services:
_defaults:
autowire: true
autoconfigure: true
public: false
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'<?php
namespace YourPlugin;
use Brianvarskonst\WordPress\DependencyInjection\Bundle\AbstractBundle;
final class PluginBundle extends AbstractBundle
{
public function boot(): void
{
add_action('init', [$this, 'onInit']);
}
public function onInit(): void
{
// Your plugin initialization
}
}// Get container
$container = sf_container();
// Get service
$service = sf_service(YourPlugin\Service\MyService::class);
// Or use in WordPress hooks
add_action('init', function() {
$service = sf_service(YourPlugin\Service\MyService::class);
$service->execute();
});- Installation Guide
- Bundle System
- Configuration
- Service Registration
- Environment Management
- Translation Resources
- Best Practices
- API Reference
- Application - Bootstrap class that initializes the DI container
- Kernel - Builds and caches the Symfony container
- BundleLoader - Discovers and loads bundles with environment awareness
- BundlesConfigLoader - Reads
config/bundles.phpfor bundle registration - TranslationLoader - Manages XLIFF translation resources
wordpress-root/
├── config/
│ ├── bundles.php # Bundle registration
│ ├── services.yaml # Global services
│ └── packages/
│ ├── your_bundle.yaml # Bundle-specific config
│ ├── dev/
│ ├── stage/
│ └── prod/
├── src/
│ └── Integration/
│ └── Bundle/
└── wp-content/
├── plugins/
│ └── your-plugin/
│ ├── YourPluginBundle.php
│ ├── DependencyInjection/
│ ├── Resources/
│ │ ├── config/
│ │ │ └── services.yaml
│ │ └── translations/
│ └── Service/
└── mu-plugins/
└── application-starter.php
The library detects environments in the following order:
$_ENV['WORDPRESS_ENV']or$_SERVER['WORDPRESS_ENV'](WPStarter)WP_ENVIRONMENT_TYPEconstantWP_DEBUGconstant (fallback: dev if true, prod if false)
Supported environments: dev, stage, prod
<?php
return [
// Load in all environments
App\CoreBundle::class => ['all' => true],
// Development only
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true],
// Multiple environments
App\ProfilerBundle::class => ['dev' => true, 'stage' => true],
// Production only
App\CacheBundle::class => ['prod' => true],
];plugins/your-plugin/
├── YourPluginBundle.php # Bundle class
├── DependencyInjection/
│ ├── YourPluginExtension.php # Container extension
│ └── Configuration.php # Config definition
├── Resources/
│ ├── config/
│ │ └── services.yaml # Bundle services
│ └── translations/
│ └── messages.en.xliff
└── Service/
└── YourService.php
services:
_defaults:
autowire: true
autoconfigure: true
public: false
YourPlugin\Service\:
resource: '../../Service/*'
exclude: '../../Service/{Tests}'
YourPlugin\Service\PublicService:
public: true
arguments:
$apiKey: '%your_plugin.api_key%'Create config/packages/your_plugin.yaml:
your_plugin:
api_key: '%env(YOUR_PLUGIN_API_KEY)%'
debug_mode: false
features:
- feature_one
- feature_twoServices from different bundles can depend on each other:
namespace PluginB\Service;
use PluginA\Service\SharedService;
final class NotificationService
{
public function __construct(
private readonly SharedService $sharedService
) {
}
}// Get container instance
$container = sf_container();
// Get service (returns null if not found)
$service = sf_service(MyService::class);
// Check if service exists
if ($container->has(MyService::class)) {
$service = $container->get(MyService::class);
}add_action('symfony_container_ready', function() {
// Container is built and ready
$service = sf_service(MyService::class);
});add_action('init', function() {
$service = sf_service(YourPlugin\Service\EmailService::class);
$service->sendWelcomeEmail();
});
add_shortcode('my_shortcode', function($atts) {
$service = sf_service(YourPlugin\Service\ShortcodeService::class);
return $service->render($atts);
});- Container Caching - Compiled containers cached per environment
- Lazy Loading - Services instantiated only when needed
- Optimized Autoloader - Composer's optimized autoloader enabled
- Production Mode - Debug features disabled in production
Cache location: wp-content/uploads/symfony-cache/container_{environment}.php
Clear cache:
rm -rf wp-content/uploads/symfony-cache/- No browser storage APIs used (localStorage/sessionStorage not supported)
- Early returns pattern for security checks
- Strict type declarations throughout
- Input validation and sanitization
- Protected environment variable handling
This library follows:
- Enterprise-level code quality
- KISS (Keep It Simple, Stupid)
- DRY (Don't Repeat Yourself)
- SOLID principles
- PSR-4 autoloading
- PSR-12 coding style
- SoC (Separation of Concerns)
Contributions are welcome! Please read our Contributing Guide for details.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
composer testThis project is licensed under the MIT License - see the LICENSE file for details.
- Issues: GitHub Issues
- Documentation: Full Documentation
See CHANGELOG.md for version history.
Built with ❤️ by Brianvarskonst
Powered by Symfony Components
This software is only to be used for the specific project for which it was transmitted.! Since it's released under the GIL License you arent allowed to publish or for commercial useage.