sylius-labs/polyfill-symfony-security
PolyfillSymfonySecurity provides compatibility shims for Symfony Security, helping apps and libraries bridge differences across Symfony versions. Useful when supporting multiple Symfony releases without changing your codebase or adding hard dependencies.
security-bundle, security-core) for PHP projects not using Symfony full-stack (e.g., Laravel). It enables integration of Symfony’s authentication/authorization primitives (e.g., UserProvider, Firewall, Voter) without requiring Symfony’s dependency overhead.auth() helper) and Symfony’s components, requiring careful API mapping.security-core and related libraries, which may introduce version conflicts or unused dependencies.UserProviderInterface, AuthenticationManager).Voter, AccessControlList).EventDispatcher is not natively integrated with Laravel’s events. A facade or adapter may be needed.Firewall maps to Laravel middleware, but sequencing (e.g., auth:check) may need adjustment.AuthManager, Request).| Risk Area | Severity | Mitigation |
|---|---|---|
| Version Conflicts | High | Lock Symfony dependencies to compatible versions (e.g., symfony/security-core:^6.0). Use replace in composer.json to avoid pulling Symfony’s full stack. |
| Session Incompatibility | Medium | Implement a custom session handler or use Laravel’s session() with Symfony’s Storage interface. |
| Event Dispatcher Mismatch | Medium | Create a facade to bridge Symfony’s EventDispatcher to Laravel’s Events. |
| Middleware Clashes | Low | Test firewall middleware sequencing early (e.g., ensure auth:check runs after session middleware). |
| Performance Overhead | Low | Profile polyfill’s reflection/adapter layers; optimize if critical paths are hit. |
Why Symfony Security?
auth system?auth:api, sanctum, or passport suffice with less integration effort?Dependency Scope:
http-foundation)? If so, how will you manage conflicts?Long-Term Maintenance:
Alternatives:
spatie/laravel-permission or laravel/breeze for auth needs?security-core:^6.0 for Laravel 9+ (PHP 8.0+).symfony/security-core:^5.4 to avoid deprecations.Illuminate\Auth\AuthManager with Symfony’s AuthenticationManager via a facade.RequestStack alongside Laravel’s Request.Symfony\Component\HttpFoundation\Session\SessionInterface to Laravel’s Session.Phase 1: Authentication
UserProvider with Symfony’s UserProviderInterface.AuthenticationManager:
// app/Facades/SymfonyAuth.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class SymfonyAuth extends Facade {
protected static function getFacadeAccessor() { return 'symfony.auth.manager'; }
}
AuthenticationManager in Laravel’s service container:
// config/app.php
'providers' => [
SymfonyAuthServiceProvider::class,
],
AuthServiceProvider to delegate to Symfony’s AuthenticationProviderManager.Phase 2: Authorization
Gate with Symfony’s Voter:
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
class LaravelGateVoter implements VoterInterface { ... }
AccessControlList or via Laravel’s Gate::before().Phase 3: Firewalls
Firewall to Laravel middleware:
// app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\SymfonyFirewallMiddleware::class,
// ... other middleware
],
];
SymfonyFirewallMiddleware to dispatch Symfony’s AuthenticationEntryPoint.Phase 4: Testing
TokenStorage, UserChecker, and EventDispatcher in tests.HttpTests alongside Symfony’s WebTestCase for integration tests.security-core: Core auth/authorization.security-http: Firewalls, entry points.security-bundle: Avoid (pulls in Symfony’s full stack; use components directly).Firewall can be adapted to PSR-15.ContainerInterface.Order of Operations:
Firewall to ensure session storage is available.VerifyCsrfToken but before route handling.auth:check sparingly; prefer Symfony’s AuthenticationListener.Example Middleware Stack:
1. StartSession (Laravel)
2. SymfonyFirewallMiddleware (Polyfill)
3. VerifyCsrfToken (Laravel)
4. Authenticate (Symfony, via polyfill)
5. Route Handling
Fallbacks:
auth() helper with a feature flag.laravel.log for debugging.6.0.10) to avoid breaking changes.laravel/framework:^9.0).security-core for deprecations (e.g., PHP 8.1+ changes).UserInterface changes).roave/security-advisories to scan for Symfony vulnerabilities.composer script to validate polyfill compatibility:
"scripts": {
"test:polyfill": "php vendor/bin/phpunit --filter=SymfonyAuthTest"
}
AuthenticationException). Map these to Laravel’s error formats.AuthenticationException and rethrow as AuthException:
How can I help you explore Laravel packages today?