Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.
Open
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
80 changes: 80 additions & 0 deletions Builder/ClientBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
namespace Ddeboer\GuzzleBundle\Builder;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;

use Ddeboer\GuzzleBundle\Client;
use Guzzle\Service\Description\ServiceDescription;
use Guzzle\Service\Description\ApiCommand;

use Ddeboer\GuzzleBundle\Inspector;

/**
* Client builder factory
*/
class ClientBuilder
{
protected $clientClass;
protected $descriptionClass;

protected $commands;
protected $config;
protected $dispatcher;
protected $inspector;

/**
* Constructor
*
* @param string $clientClass
* @param string $descriptionClass
* @param EventDispatcherInterface $dispatcher
* @param Inspector $inspector
* @param ApiCommand[] $commands
* @param array $config
*/
public function __construct(
$clientClass,
$descriptionClass,
EventDispatcherInterface $dispatcher,
Inspector $inspector,
array $commands,
array $config = array()
) {
$this->clientClass = $clientClass;
$this->descriptionClass = $descriptionClass;
$this->dispatcher = $dispatcher;
$this->inspector = $inspector;
$this->commands = $commands;
$this->config = $config;
}

/**
* Create client instance
*
* @param string $clientId Client id, needed for commands association
* @param string $baseUrl Base url
* @param string $username User name
* @param string $password Password
* @param array $config Other config options, optional
*
* @return Client
*/
public function factory($clientId, $baseUrl, $username, $password, array $config = array())
{
/** @var $client Client */
$client = new $this->clientClass($baseUrl, array_merge($this->config, $config, array(
'username' => $username,
'password' => $password
)));
$client->setEventDispatcher($this->dispatcher);
$client->setInspector($this->inspector);

/** @var $serviceDescription ServiceDescription */
$commands = $this->commands[$clientId];

$serviceDescription = new $this->descriptionClass($commands);
$client->setDescription($serviceDescription);

return $client;
}
}
50 changes: 50 additions & 0 deletions Builder/CommandBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
namespace Ddeboer\GuzzleBundle\Builder;

use Guzzle\Service\Description\ArrayDescriptionBuilder;
use Guzzle\Service\Description\ApiCommand;
use Guzzle\Service\Description\ApiCommandInterface;
use Guzzle\Service\Exception\DescriptionBuilderException;
use Guzzle\Service\Description\ServiceDescription;
class CommandBuilder
{
protected $defaultClass = ServiceDescription::DEFAULT_COMMAND_CLASS;

/**
* @param string $name
* @param ApiCommandInterface[] $extends
*
* @return ApiCommand
*
* @throws \Guzzle\Service\Exception\DescriptionBuilderException
* @throws \InvalidArgumentException
*/
public function build(
$name,
array $command = array(),
array $params = array(),
array $extends = array(),
$class = null
) {
// Extend other commands
if ($extends) {
$resolvedParams = array();

foreach ($extends as $extendedCommand) {
if (!$extendedCommand instanceof ApiCommandInterface) {
throw new DescriptionBuilderException("{$name} extends missing command {$extendedCommand}");
}
$toArray = $extendedCommand->toArray();
$resolvedParams = array_merge($resolvedParams, $toArray['params']);
$command = array_merge($toArray, $command);
}

$params = array_merge($resolvedParams, $params);
}

return new ApiCommand(array_merge($command, array(
'params' => $params,
'class' => $class ? str_replace('.', '\\', $class) : $this->defaultClass,
)));
}
}
15 changes: 15 additions & 0 deletions Builder/TypeBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Ddeboer\GuzzleBundle\Builder;

use Guzzle\Service\Client;
use Guzzle\Service\Description\ServiceDescription;
use Guzzle\Service\Description\ApiCommand;
/**
* Client builder factory
*/
class TypeBuilder
{
public function buildFromArray(array $command)
{
}
}
31 changes: 31 additions & 0 deletions Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Ddeboer\GuzzleBundle;

use Guzzle\Service\Client as BaseClient;
use Guzzle\Service\Command\AbstractCommand;

/**
* Client
*/
class Client extends BaseClient
{
protected $inspector;

public function setInspector(Inspector $inspector)
{
$this->inspector = $inspector;

return $inspector;
}
/**
* {@inheritDoc}
*/
public function getCommand($name, array $args = array())
{
$command = parent::getCommand($name, $args);
/** @var $command AbstractCommand */
$command->setInspector($this->inspector);

return $command;
}
}
14 changes: 14 additions & 0 deletions DdeboerGuzzleBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,21 @@
namespace Ddeboer\GuzzleBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;

use Ddeboer\GuzzleBundle\DependencyInjection\Compiler\GrabCommandsPass;


class DdeboerGuzzleBundle extends Bundle
{
/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container)
{
parent::build($container);

$container->addCompilerPass(new GrabCommandsPass());
}
}
54 changes: 54 additions & 0 deletions DependencyInjection/CommandConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Ddeboer\GuzzleBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class CommandConfiguration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$builder = new TreeBuilder();

$builder
->root('guzzle')
->children()
->arrayNode('clients')
->useAttributeAsKey('name')
->prototype('array')
->children()
->scalarNode('name')->end()
->arrayNode('commands')
->useAttributeAsKey('name')
->prototype('array')
->children()
->scalarNode('name')->end()
->scalarNode('method')->defaultValue('GET')->end()
->scalarNode('uri')->end()
->scalarNode('class')->end()
->scalarNode('extends')->end()
->scalarNode('doc')->end()
->arrayNode('params')
->useAttributeAsKey('name')
->prototype('array')
->children()
->scalarNode('doc')->end()
->scalarNode('name')->end()
->scalarNode('type')->end()
->booleanNode('required')->defaultValue(false)->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end();

return $builder;
}
}
53 changes: 53 additions & 0 deletions DependencyInjection/Compiler/GrabCommandsPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace Ddeboer\GuzzleBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\Config\Definition\Processor;

use Millwright\ConfigurationBundle\ContainerUtil as Util;
use Millwright\ConfigurationBundle\PhpUtil;

use Ddeboer\GuzzleBundle\DependencyInjection\CommandConfiguration;
use Guzzle\Service\Inspector;

/**
* FormCompilerPass
*/
class GrabCommandsPass implements CompilerPassInterface
{
/**
* Process
*
* @param ContainerBuilder $container
*
* @return void
*/
public function process(ContainerBuilder $container)
{
$definitions = Util::getDefinitionsByTag('guzzle_type', $container);

$inspector = $container->getDefinition('guzzle.inspector');

foreach ($definitions as $name => $definition) {
$properties = $definition->getProperties();
$inspector->addMethodCall('setConstraint', array(
$name,
$definition,
$properties
));
}

$definitionGroups = Util::getDefinitionsByTag('guzzle_command', $container, true);

$result = array();
foreach ($definitionGroups as $clientId => $definitions) {
foreach ($definitions as $definition) {
$args = $definition->getArguments();
$result[$clientId][$args['name']] = $definition;
}
}

$container->getDefinition('guzzle.client_builder')->replaceArgument(4, $result);
}
}
16 changes: 16 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ public function getConfigTreeBuilder()
->scalarNode('configuration_file')
->defaultValue('%kernel.root_dir%/config/webservices.xml')
->end()
->arrayNode('configuration')
->useAttributeAsKey('name')
->prototype('array')
->children()
->scalarNode('name')->end()
->scalarNode('method')->defaultValue('GET')->end()
->scalarNode('uri')->end()
->scalarNode('class')->end()
->scalarNode('extends')->end()
->arrayNode('params')
->useAttributeAsKey('params')->prototype('scalar')->end()
->end()
->end()
->end()
->end()

->end()
->end()
->booleanNode('logging')->defaultValue($this->debug)->end()
Expand Down
16 changes: 14 additions & 2 deletions DependencyInjection/DdeboerGuzzleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@ public function load(array $configs, ContainerBuilder $container)
new FileLocator(__DIR__.'/../Resources/config')
);
$loader->load('services.xml');
$loader->load('validators.xml');

$processor = new Processor();
$configuration = new Configuration($container->getParameter('kernel.debug'));
$config = $processor->processConfiguration($configuration, $configs);

$container->setParameter('guzzle.service_builder.configuration_file',
$config['service_builder']['configuration_file']);
if (isset($config['service_builder'])) {
$container->setParameter('guzzle.service_builder.configuration',
$config['service_builder']['configuration']);

if (empty($config['service_builder']['configuration'])) {
$container->setParameter('guzzle.service_builder.configuration',
$config['service_builder']['configuration_file']);
}
}

$clients = isset($config['clients']) ? $config['clients'] : array();

$container->setParameter('guzzle.clients', $clients);

if ($config['logging']) {
$container->findDefinition('guzzle.data_collector')
Expand Down
Loading