Skip to content

Commit 978d70f

Browse files
committed
Update docs, privitize properties in task manager
1 parent 2413aca commit 978d70f

File tree

2 files changed

+102
-5
lines changed

2 files changed

+102
-5
lines changed

README.md

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,101 @@ public function registerBundles()
2525
}
2626
```
2727

28-
## Configuration
28+
## Usage
29+
30+
To use the PostProcessBundle, you must first create a class adhering to the `Task\TaskInterface` or `Plugins\PluginInterface`. A task is a process that will be executed on the Symfony terminate event (after the response is sent,) whereas a Plugin is a process that is run before the response is sent (allowing you to modify it.)
31+
32+
### Tasks
33+
34+
A task can be used to execute logic after the response has been sent to the user, allowing you to trigger long-running processes that need to complete, but the user does not need to wait for them.
35+
36+
Example:
37+
38+
```
39+
use As3\PostProcessBundle\TaskInterface;
40+
41+
class SleepTestTask implements TaskInterface
42+
{
43+
/**
44+
* {@inhericDoc}
45+
*/
46+
public function run()
47+
{
48+
// Some process that takes 5 minutes
49+
sleep(300);
50+
}
51+
}
52+
```
53+
54+
To register your task, call the `addTask` method against the task manager's service (`as3_post_process.task.manager`):
55+
```
56+
$manager = $this->get('as3_post_process.task.manager');
57+
$manager->addTask(new SleepTestTask(), 5);
58+
```
59+
60+
Tasks can have a `priority` set when they are added to the manager -- by default new tasks are added with a priority of `0`. Tasks are executed in ascending order by their priority.
61+
62+
You can also register a service by using the tag `as3_post_process.task` if your task should be run on every request.
63+
64+
```
65+
# src\MyBundle\Resources\services.yml
66+
services:
67+
my_app.my_cool_task:
68+
class: MyCoolTask
69+
tags:
70+
- { name: as3_post_process.task, priority: 5 }
71+
```
72+
73+
### Plugins
74+
75+
A plugin can be used to modify the response before it is returned to the user.
76+
77+
Example:
78+
79+
```
80+
use Symfony\Component\HttpFoundation\Response;
81+
82+
/**
83+
* Integration with New Relic End User Monitoring services
84+
*/
85+
class NewRelicInjector extends PluginInterface
86+
{
87+
/**
88+
* Handles injection of NREUM Javascript
89+
*/
90+
public function filterResponse(Response $response)
91+
{
92+
if (extension_loaded('newrelic')) {
93+
newrelic_disable_autorum();
94+
95+
$content = $response->getContent();
96+
97+
if (false != strpos($content, '</head>')) {
98+
$content = str_replace('</head>', sprintf("\n%s\n</head>", newrelic_get_browser_timing_header()), $content);
99+
}
100+
101+
if (false != strpos($content, '</body>')) {
102+
$content = str_replace('</body>', sprintf("\n%s\n</body>", newrelic_get_browser_timing_footer()), $content);
103+
}
104+
105+
$response->headers->set('X-NREUM', 'Enabled');
106+
107+
// If we modified the content, set it on the response.
108+
if ($content !== $response->getContent()) {
109+
$response->setContent($content);
110+
}
111+
112+
return $response;
113+
}
114+
}
115+
}
116+
```
117+
118+
This plugin will disable automatic injection of NewRelic end user monitoring javascript. To enable this for all requests, add the following service definition:
119+
120+
```
121+
my_app.my_bundle.new_relic_injector:
122+
class: MyApp\MyBundle\NewRelicPlugin
123+
tags:
124+
- { name: as3_post_process.plugin }
125+
```

Task/TaskManager.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,27 @@ class TaskManager
2323
* @see TaskManager::addTask() To add a TaskInterface class
2424
* @var TaskInterface[]
2525
*/
26-
protected $tasks = [];
26+
private $tasks = [];
2727

2828
/**
2929
* All enabled plugins to be executed on kernel.response and kernel.terminate events.
3030
*
3131
* @see TaskManager::addPlugin()
3232
* @var PluginInterface[]
3333
*/
34-
protected $plugins = [];
34+
private $plugins = [];
3535

3636
/**
3737
* @var boolean
3838
*/
39-
protected $masterRequest = true;
39+
private $masterRequest = true;
4040

4141
/**
4242
* Determines whether Tasks should be executed
4343
*
4444
* @var bool
4545
*/
46-
protected $enabled = true;
46+
private $enabled = true;
4747

4848
/**
4949
* Adds a registered task.

0 commit comments

Comments
 (0)