-
Notifications
You must be signed in to change notification settings - Fork 50
Add PluginClientFactory support #202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8f84f28
add PluginClientFactory support
fbourigault 2ab6ef4
deprecate ClientFactory\PluginClientFactory
fbourigault 70119d1
add PluginClientFactory and PluginClientFactorySubscriber class comments
fbourigault 749d889
improve ClientFactory\PluginClientFactory deprecation
fbourigault 5bec5d6
fix typos
fbourigault 08d0321
use client_name option for configured clients
fbourigault 2782c1e
update composer.json
fbourigault 3b58eb5
fix composer.json
fbourigault 35ca277
remove prefer-stable when testing against dev dependencies
fbourigault 729a884
add compatibility with old KernelTestCase::bootKernel signature
fbourigault 47a48ee
require symfony/dependency injection ^2.8.3 || ^3.0.3
fbourigault 65b04ac
update the changelog
fbourigault cef534d
rename PluginClientFactorySubscriber to PluginClientFactoryListener
fbourigault File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Http\HttplugBundle\Collector; | ||
|
||
use Http\Client\Common\Plugin; | ||
use Http\Client\Common\PluginClient; | ||
use Http\Client\HttpAsyncClient; | ||
use Http\Client\HttpClient; | ||
use Symfony\Component\Stopwatch\Stopwatch; | ||
|
||
/** | ||
* This factory is used as a replacement for Http\Client\Common\PluginClientFactory when profiling is enabled. It | ||
* creates PluginClient instances with all profiling decorators and extra plugins. | ||
* | ||
* @author Fabien Bourigault <bourigaultfabien@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
final class PluginClientFactory | ||
{ | ||
/** | ||
* @var Collector | ||
*/ | ||
private $collector; | ||
|
||
/** | ||
* @var Formatter | ||
*/ | ||
private $formatter; | ||
|
||
/** | ||
* @var Stopwatch | ||
*/ | ||
private $stopwatch; | ||
|
||
/** | ||
* @param Collector $collector | ||
* @param Formatter $formatter | ||
* @param Stopwatch $stopwatch | ||
*/ | ||
public function __construct(Collector $collector, Formatter $formatter, Stopwatch $stopwatch) | ||
{ | ||
$this->collector = $collector; | ||
$this->formatter = $formatter; | ||
$this->stopwatch = $stopwatch; | ||
} | ||
|
||
/** | ||
* @param HttpClient|HttpAsyncClient $client | ||
* @param Plugin[] $plugins | ||
* @param array $options { | ||
* | ||
* @var string $client_name to give client a name which may be used when displaying client information like in | ||
* the HTTPlugBundle profiler. | ||
* } | ||
* | ||
* @see PluginClient constructor for PluginClient specific $options. | ||
* | ||
* @return PluginClient | ||
*/ | ||
public function createClient($client, array $plugins = [], array $options = []) | ||
{ | ||
$plugins = array_map(function (Plugin $plugin) { | ||
return new ProfilePlugin($plugin, $this->collector, $this->formatter); | ||
}, $plugins); | ||
|
||
$clientName = isset($options['client_name']) ? $options['client_name'] : 'Default'; | ||
array_unshift($plugins, new StackPlugin($this->collector, $this->formatter, $clientName)); | ||
unset($options['client_name']); | ||
|
||
if (!$client instanceof ProfileClient) { | ||
$client = new ProfileClient($client, $this->collector, $this->formatter, $this->stopwatch); | ||
} | ||
|
||
return new PluginClient($client, $plugins, $options); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Http\HttplugBundle\Collector; | ||
|
||
use Http\Client\Common\PluginClientFactory; | ||
use Http\HttplugBundle\Collector\PluginClientFactory as CollectorPluginClientFactory; | ||
use Symfony\Component\EventDispatcher\Event; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* This subscriber ensures that every PluginClient created when using Http\Client\Common\PluginClientFactory without | ||
* using the Symfony dependency injection container uses the Http\HttplugBundle\Collector\PluginClientFactory factory | ||
* when profiling is enabled. This allows 0 config profiling of third party libraries which use HTTPlug. | ||
* | ||
* @author Fabien Bourigault <bourigaultfabien@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
final class PluginClientFactoryListener implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @var CollectorPluginClientFactory | ||
*/ | ||
private $factory; | ||
|
||
/** | ||
* @param CollectorPluginClientFactory $factory | ||
*/ | ||
public function __construct(CollectorPluginClientFactory $factory) | ||
{ | ||
$this->factory = $factory; | ||
} | ||
|
||
/** | ||
* Make sure to profile clients created using PluginClientFactory. | ||
* | ||
* @param Event $e | ||
*/ | ||
public function onEvent(Event $e) | ||
{ | ||
PluginClientFactory::setFactory([$this->factory, 'createClient']); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
'kernel.request' => ['onEvent', 1024], | ||
'console.command' => ['onEvent', 1024], | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this might be confusing because the base test cases don't do it in older symfony versions. but i think its still a good idea. assigning the bootKernel return value, then accessing static::$kernel and in the end return the variable would be weird.