Skip to content

Merge Profiling back to main extension #100

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 1 commit into from
Jul 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Change Log


## UNRELEASED

### Deprecated

- `auto` value in `toolbar.enabled` config


## 1.2.2 - 2016-07-19

### Fixed
Expand Down
32 changes: 27 additions & 5 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@
*/
class Configuration implements ConfigurationInterface
{
/**
* Whether to use the debug mode.
*
* @see https://github.com/doctrine/DoctrineBundle/blob/v1.5.2/DependencyInjection/Configuration.php#L31-L41
*
* @var bool
*/
private $debug;

/**
* @param bool $debug
*/
public function __construct($debug)
{
$this->debug = (bool) $debug;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -75,12 +92,17 @@ public function getConfigTreeBuilder()
->end()
->arrayNode('toolbar')
->addDefaultsIfNotSet()
->info('Extend the debug profiler with inforation about requests.')
->info('Extend the debug profiler with information about requests.')
->children()
->enumNode('enabled')
->info('If "auto" (default), the toolbar is activated when kernel.debug is true. You can force the toolbar on and off by changing this option.')
->values([true, false, 'auto'])
->defaultValue('auto')
->booleanNode('enabled') // @deprecated value auto in 1.3.0
->beforeNormalization()
->ifString()
->then(function ($v) {
return 'auto' === $v ? $this->debug : $v;
})
->end()
->info('Turn the toolbar on or off. Defaults to kernel debug mode.')
->defaultValue($this->debug)
->end()
->scalarNode('formatter')->defaultNull()->end()
->scalarNode('captured_body_length')
Expand Down
152 changes: 108 additions & 44 deletions DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Http\Discovery\HttpClientDiscovery;
use Http\HttplugBundle\ClientFactory\DummyClient;
use Http\HttplugBundle\ClientFactory\PluginClientFactory;
use Http\HttplugBundle\Collector\DebugPlugin;
use Http\Message\Authentication\BasicAuth;
use Http\Message\Authentication\Bearer;
use Http\Message\Authentication\Wsse;
Expand Down Expand Up @@ -38,6 +39,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('services.xml');
$loader->load('plugins.xml');

// Register default services
foreach ($config['classes'] as $service => $class) {
if (!empty($class)) {
$container->register(sprintf('httplug.%s.default', $service), $class);
Expand All @@ -49,27 +51,37 @@ public function load(array $configs, ContainerBuilder $container)
$container->setAlias(sprintf('httplug.%s', $type), $id);
}

$this->configurePlugins($container, $config['plugins']);
$serviceIds = $this->configureClients($container, $config);
$autoServiceIds = $this->configureAutoDiscoveryClients($container, $config);
// Configure toolbar
if ($config['toolbar']['enabled']) {
$loader->load('data-collector.xml');

if (!empty($config['toolbar']['formatter'])) {
// Add custom formatter
$container
->getDefinition('httplug.collector.debug_collector')
->replaceArgument(0, new Reference($config['toolbar']['formatter']))
;
}

$toolbar = is_bool($config['toolbar']['enabled']) ? $config['toolbar']['enabled'] : $container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug');
if ($toolbar) {
(new ProfilerExtension())->load($config, $container, array_unique(array_merge($serviceIds, $autoServiceIds)));
$container
->getDefinition('httplug.formatter.full_http_message')
->addArgument($config['toolbar']['captured_body_length'])
;
}

$this->configurePlugins($container, $config['plugins']);
$this->configureClients($container, $config);
$this->configureAutoDiscoveryClients($container, $config);
}

/**
* Configure client services.
*
* @param ContainerBuilder $container
* @param array $config
*
* @return array with client service names
*/
private function configureClients(ContainerBuilder $container, array $config)
{
$serviceIds = [];
$first = null;

foreach ($config['clients'] as $name => $arguments) {
Expand All @@ -78,7 +90,7 @@ private function configureClients(ContainerBuilder $container, array $config)
$first = $name;
}

$serviceIds[] = $this->configureClient($container, $name, $arguments);
$this->configureClient($container, $name, $arguments, $config['toolbar']['enabled']);
}

// If we have clients configured
Expand All @@ -89,8 +101,6 @@ private function configureClients(ContainerBuilder $container, array $config)
$container->setAlias('httplug.client.default', 'httplug.client.'.$first);
}
}

return $serviceIds;
}

/**
Expand Down Expand Up @@ -198,14 +208,29 @@ private function configureAuthentication(ContainerBuilder $container, array $con
* @param ContainerBuilder $container
* @param string $name
* @param array $arguments
*
* @return string The service id of the client.
* @param bool $profiling
*/
private function configureClient(ContainerBuilder $container, $name, array $arguments)
private function configureClient(ContainerBuilder $container, $name, array $arguments, $profiling)
{
$serviceId = 'httplug.client.'.$name;
$definition = $container->register($serviceId, DummyClient::class);
$definition->setFactory([PluginClientFactory::class, 'createPluginClient'])

$pluginClientOptions = [];

if ($profiling) {
// Tell the plugin journal what plugins we used
$container
->getDefinition('httplug.collector.plugin_journal')
->addMethodCall('setPlugins', [$name, $arguments['plugins']])
;

$debugPluginServiceId = $this->registerDebugPlugin($container, $serviceId);

$pluginClientOptions['debug_plugins'] = [new Reference($debugPluginServiceId)];
}

$container
->register($serviceId, DummyClient::class)
->setFactory([PluginClientFactory::class, 'createPluginClient'])
->addArgument(
array_map(
function ($id) {
Expand All @@ -216,31 +241,30 @@ function ($id) {
)
->addArgument(new Reference($arguments['factory']))
->addArgument($arguments['config'])
->addArgument([])
->addArgument($pluginClientOptions)
;

// Tell the plugin journal what plugins we used
$container->getDefinition('httplug.collector.plugin_journal')
->addMethodCall('setPlugins', [$name, $arguments['plugins']]);

/*
* Decorate the client with clients from client-common
*/
if ($arguments['flexible_client']) {
$container->register($serviceId.'.flexible', FlexibleHttpClient::class)
$container
->register($serviceId.'.flexible', FlexibleHttpClient::class)
->addArgument(new Reference($serviceId.'.flexible.inner'))
->setPublic(false)
->setDecoratedService($serviceId);
->setDecoratedService($serviceId)
;
}

if ($arguments['http_methods_client']) {
$container->register($serviceId.'.http_methods', HttpMethodsClient::class)
$container
->register($serviceId.'.http_methods', HttpMethodsClient::class)
->setArguments([new Reference($serviceId.'.http_methods.inner'), new Reference('httplug.message_factory')])
->setPublic(false)
->setDecoratedService($serviceId);
->setDecoratedService($serviceId)
;
}

return $serviceId;
}

/**
Expand All @@ -249,45 +273,44 @@ function ($id) {
*
* @param ContainerBuilder $container
* @param array $config
*
* @return array of service ids.
*/
private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config)
{
$serviceIds = [];

$httpClient = $config['discovery']['client'];

if (!empty($httpClient)) {
if ($httpClient === 'auto') {
$httpClient = $this->registerAutoDiscoverableClient(
$container,
'auto_discovered_client',
[HttpClientDiscovery::class, 'find']
[HttpClientDiscovery::class, 'find'],
$config['toolbar']['enabled']
);
}

$serviceIds[] = $httpClient;
$httpClient = new Reference($httpClient);
}

$asyncHttpClient = $config['discovery']['async_client'];

if (!empty($asyncHttpClient)) {
if ($asyncHttpClient === 'auto') {
$asyncHttpClient = $this->registerAutoDiscoverableClient(
$container,
'auto_discovered_async',
[HttpAsyncClientDiscovery::class, 'find']
[HttpAsyncClientDiscovery::class, 'find'],
$config['toolbar']['enabled']
);
}
$serviceIds[] = $asyncHttpClient;
$asyncHttpClient = new Reference($httpClient);

$asyncHttpClient = new Reference($asyncHttpClient);
}

$container->getDefinition('httplug.strategy')
$container
->getDefinition('httplug.strategy')
->addArgument($httpClient)
->addArgument($asyncHttpClient);

return $serviceIds;
->addArgument($asyncHttpClient)
;
}

/**
Expand All @@ -296,17 +319,58 @@ private function configureAutoDiscoveryClients(ContainerBuilder $container, arra
* @param ContainerBuilder $container
* @param string $name
* @param callable $factory
* @param bool $profiling
*
* @return string service id
*/
private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory)
private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling)
{
$serviceId = 'httplug.auto_discovery.'.$name;
$definition = $container->register($serviceId, DummyClient::class);
$definition

$pluginClientOptions = [];

if ($profiling) {
$debugPluginServiceId = $this->registerDebugPlugin($container, $serviceId);

$pluginClientOptions['debug_plugins'] = [new Reference($debugPluginServiceId)];
}

$container
->register($serviceId, DummyClient::class)
->setFactory([PluginClientFactory::class, 'createPluginClient'])
->setArguments([[], $factory, [], []]);
->setArguments([[], $factory, [], $pluginClientOptions])
;

return $serviceId;
}

/**
* Create a new plugin service for this client.
*
* @param ContainerBuilder $container
* @param string $serviceId
*
* @return string
*/
private function registerDebugPlugin(ContainerBuilder $container, $serviceId)
{
$serviceIdDebugPlugin = $serviceId.'.debug_plugin';

$container
->register($serviceIdDebugPlugin, DebugPlugin::class)
->addArgument(new Reference('httplug.collector.debug_collector'))
->addArgument(substr($serviceId, strrpos($serviceId, '.') + 1))
->setPublic(false)
;

return $serviceIdDebugPlugin;
}

/**
* {@inheritdoc}
*/
public function getConfiguration(array $config, ContainerBuilder $container)
{
return new Configuration($container->getParameter('kernel.debug'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

}
}
Loading