adunblock/server-tag-symfony
Symfony bundle that fetches script URLs from a remote endpoint and renders them via a Twig server_tag() function. Uses Symfony HttpClient, optional caching with configurable TTL, safe HTML escaping, and graceful error handling. Compatible with Symfony 5–8.
symfony/http-client, symfony/cache, symfony/twig-bridge).HttpClient with Laravel’s HttpClient (or Guzzle under the hood) via a facade/adapter pattern.spatie/laravel-twig) to expose the server_tag() function to Blade templates.symfony/http-client → Laravel’s HttpClient (or Guzzle).symfony/cache → Laravel’s Cache facade.twig/twig → spatie/laravel-twig.spatie/laravel-twig), which may introduce complexity.throw_if, try-catch).Cache::rememberForever).| Symfony Bundle Feature | Laravel Equivalent | Integration Strategy |
|---|---|---|
HttpClient |
Laravel HttpClient / Guzzle |
Create a service provider to bind HttpClient to the bundle’s interface. |
| Twig Integration | Blade + spatie/laravel-twig |
Expose server_tag() via Twig’s global functions or a Blade directive. |
| Caching | Laravel Cache facade |
Replace Symfony’s cache with Laravel’s cache drivers (adjust TTL logic). |
| Dependency Injection | Laravel’s IoC Container | Use Laravel’s service container to resolve dependencies. |
| Error Handling | Laravel Exceptions | Map Symfony’s fallbacks to Laravel’s throw_if or try-catch. |
Phase 1: Dependency Replacement
symfony/http-client with Laravel’s HttpClient via a facade:
// app/Providers/ServerTagServiceProvider.php
public function register()
{
$this->app->bind(
\AdUnblock\ServerTag\Symfony\HttpClientInterface::class,
\Illuminate\Support\Facades\Http::class
);
}
use Illuminate\Support\Facades\Cache;
Cache::remember("server_tag_{$url}", $ttl, fn() => $this->fetchScript($url));
Phase 2: Twig/Blade Integration
spatie/laravel-twig and register the bundle:
composer require spatie/laravel-twig
server_tag() in Twig:
// config/twig.php
'globals' => [
'server_tag' => [\AdUnblock\ServerTag\Twig\ServerTagExtension::class, 'serverTag'],
],
Blade::directive('serverTag', function ($url) {
return "<?php echo app(\AdUnblock\ServerTag\ServerTag::class)->render($url); ?>";
});
Phase 3: Testing & Optimization
Http::fake()).spatie/laravel-circuit-breaker) for failed fetches.?id=123) must be handled via query strings.symfony/* packages for breaking changes (though Laravel equivalents may lag).HttpClient) over Symfony’s to reduce maintenance.tap() or dd() for debugging HTTP responses.Cache::store('file')->get('...').Http::withOptions(['verify' => true])).Cache::tags() for invalidation across instances.fetch-script:handle) for long-running requests.use Illuminate\Support\Facades\Queue;
Queue::push(FetchServerTagJob::class, ['url' => $url]);
scripts table with fetched_at for stale-while-revalidate.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Remote endpoint down | Broken scripts | Fallback to cached version or placeholder. |
| Cache corruption | Stale scripts | Use Cache::rememberForever() with tags. |
| Malicious script injection | XSS vulnerabilities | Sanitize output with htmlspecialchars(). |
| High latency | Slow page load | Pre-fetch scripts via cron or queue. |
| API rate limits | Throttled requests | Implement retries with jitter. |
Http::debug()).How can I help you explore Laravel packages today?