Installation:
composer require as3/post-process-bundle ^1.0
Add the bundle to AppKernel.php:
new As3\Bundle\PostProcessBundle\As3PostProcessBundle(),
First Use Case: Create a simple task to run after response (e.g., logging, analytics, or async processing):
// src/Task/MyAsyncTask.php
namespace App\Task;
use As3\Bundle\PostProcessBundle\TaskInterface;
class MyAsyncTask implements TaskInterface {
public function run() {
// Example: Send analytics event
file_put_contents('analytics.log', 'Event triggered', FILE_APPEND);
}
}
Register the Task:
# config/services.yaml
services:
App\Task\MyAsyncTask:
tags:
- { name: as3_post_process.task, priority: 1 }
Async Processing: Use tasks for background jobs (e.g., sending emails, updating caches, or triggering webhooks) after the response is sent.
// Dynamically add a task in a controller
$manager = $this->get('as3_post_process.task.manager');
$manager->addTask(new MyAsyncTask(), 1); // Priority 1
Priority Management:
Lower-priority tasks (e.g., analytics) run before higher-priority ones (e.g., critical cleanup). Default priority is 0.
Service Integration: Tag services for automatic registration:
# config/services.yaml
services:
App\Task\CacheCleanupTask:
tags:
- { name: as3_post_process.task, priority: 10 }
Response Modification: Use plugins to inject scripts, headers, or modify content before sending the response.
// src/Plugin/ResponseModifierPlugin.php
use As3\Bundle\PostProcessBundle\PluginInterface;
use Symfony\Component\HttpFoundation\Response;
class ResponseModifierPlugin implements PluginInterface {
public function filterResponse(Response $response) {
$response->headers->set('X-Custom-Header', 'value');
return $response;
}
}
Conditional Logic: Plugins can conditionally alter responses (e.g., inject tracking only for specific routes):
public function filterResponse(Response $response) {
if ($this->isTrackingEnabled()) {
$response->setContent($this->injectTracking($response->getContent()));
}
return $response;
}
Service Registration:
# config/services.yaml
services:
App\Plugin\ResponseModifierPlugin:
tags:
- { name: as3_post_process.plugin }
Symfony 3+ Compatibility:
The bundle supports Symfony 3.x but may require adjustments for newer versions (e.g., AppKernel → Kernel). Test thoroughly.
Task Execution Order: Tasks run in ascending priority order (lower numbers first). Misconfigured priorities can cause delays or race conditions.
Response Modification Side Effects: Plugins modify the response before it’s sent. Avoid heavy operations (e.g., DOM parsing) that could block the HTTP connection.
No Guaranteed Execution:
Tasks/plugins may fail silently if exceptions aren’t caught. Wrap logic in try-catch blocks:
public function run() {
try {
// Risky operations
} catch (\Exception $e) {
error_log('Task failed: ' . $e->getMessage());
}
}
Log Task Execution:
Override TaskInterface::run() to log start/end times:
public function run() {
\Log::info('Task started: ' . __CLASS__);
// Task logic
\Log::info('Task ended: ' . __CLASS__);
}
Check Service Tags:
Verify tags are correctly defined in services.yaml:
tags:
- { name: as3_post_process.task, priority: 5 } # Correct syntax
Disable Tasks Temporarily: Comment out service tags to isolate issues:
# services:
# App\Task\MyAsyncTask: ~
Custom Task Managers:
Extend the bundle’s TaskManager to add features like:
class CustomTaskManager extends \As3\Bundle\PostProcessBundle\Task\TaskManager {
public function addTaskWithRetry(TaskInterface $task, int $priority, int $retries) {
// Custom logic
}
}
Plugin Chaining: Combine multiple plugins for layered response modifications:
services:
app.plugin.chain:
class: App\Plugin\ChainPlugin
arguments: ['@as3_post_process.plugin.manager']
tags:
- { name: as3_post_process.plugin }
Event Integration:
Trigger tasks/plugins via Symfony events (e.g., kernel.terminate):
// src/EventListener/TaskListener.php
use Symfony\Component\HttpKernel\Event\TerminateEvent;
class TaskListener {
public function onTerminate(TerminateEvent $event) {
$manager = $this->container->get('as3_post_process.task.manager');
$manager->addTask(new MyTask(), 1);
}
}
How can I help you explore Laravel packages today?