From a8438ba953d0f605ada0ec4e98accf80409c20d8 Mon Sep 17 00:00:00 2001 From: Jarrett Croll Date: Tue, 6 Sep 2016 15:57:56 +0000 Subject: [PATCH] update bundle config for new client api --- DependencyInjection/Configuration.php | 6 ++++ .../JcrollFoursquareApiExtension.php | 31 ++++++++++++++++--- README.md | 19 ++++++++---- Resources/config/services.xml | 10 ------ 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 0f4e5e9..5abd627 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -25,6 +25,12 @@ public function getConfigTreeBuilder() ->isRequired() ->cannotBeEmpty() ->end() + ->scalarNode('version') + ->defaultValue(20160901) + ->end() + ->scalarNode('mode') + ->defaultValue('foursquare') + ->end() ->end() ; diff --git a/DependencyInjection/JcrollFoursquareApiExtension.php b/DependencyInjection/JcrollFoursquareApiExtension.php index d865b01..adfd5f6 100644 --- a/DependencyInjection/JcrollFoursquareApiExtension.php +++ b/DependencyInjection/JcrollFoursquareApiExtension.php @@ -2,6 +2,7 @@ namespace Jcroll\FoursquareApiBundle\DependencyInjection; +use Symfony\Component\Config\Definition\ConfigurationInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; @@ -26,8 +27,10 @@ public function load(array $configs, ContainerBuilder $container) $loader = $this->getLoader($container); $loader->load('services.xml'); - $container->setParameter('jcroll_foursquare_api.client_id', $config['client_id']); - $container->setParameter('jcroll_foursquare_api.client_secret', $config['client_secret']); + $container + ->getDefinition('jcroll_foursquare_api.foursquare_client') + ->addArgument($config) + ; } /** @@ -98,8 +101,9 @@ private function getLoader(ContainerBuilder $container) */ private function getExtensionConfig($extension, ContainerBuilder $container, array $required) { - $configs = $container->getExtensionConfig($extension); - $config = reset($configs); + $configs = $container->getExtensionConfig($extension); + $configuration = $container->getExtension($extension)->getConfiguration($configs, $container); + $config = $this->normalizeConfiguration($configuration, $configs); if (count(array_intersect_key($config, array_flip($required))) !== count($required)) { return null; @@ -107,4 +111,23 @@ private function getExtensionConfig($extension, ContainerBuilder $container, arr return $config; } + + /** + * @param ConfigurationInterface $configuration + * @param array $configs + * + * @return array + */ + private function normalizeConfiguration(ConfigurationInterface $configuration, array $configs) + { + $configTree = $configuration->getConfigTreeBuilder()->buildTree(); + $currentConfig = array(); + + foreach ($configs as $config) { + $config = $configTree->normalize($config); + $currentConfig = $configTree->merge($currentConfig, $config); + } + + return $currentConfig; + } } diff --git a/README.md b/README.md index 04b47f0..624c075 100644 --- a/README.md +++ b/README.md @@ -49,18 +49,20 @@ Add the JcrollFoursquareApiBundle to your AppKernel.php ## Basic configuration 1. If you're not using [HWIOAuthBundle](https://github.com/hwi/HWIOAuthBundle) add your application id and secret - parameters: + parameters (other parameters are optional): ```yaml # app/config/config.yml jcroll_foursquare_api: client_id: - client_secret: + client_secret: + version: 20140806 # optional + mode: foursquare # optional ``` 2. If you are using [HWIOAuthBundle](https://github.com/hwi/HWIOAuthBundle) configure a `foursquare` resource owner - and the client's credentials will automatically be configured (that's right you do not need to define this bundle in - `config.yml`). + and the client's credentials will automatically be configured (unless you wish to specify custom values for + `version` or `mode`). ```yaml # app/config/config.yml @@ -77,7 +79,11 @@ Add the JcrollFoursquareApiBundle to your AppKernel.php ```php $client = $this->container->get('jcroll_foursquare_client'); -$client->addToken($oauthToken); // optional for user specific requests + +$client->setToken($oauthToken); // optional for user specific requests + +$client->setMode('swarm'); // switch from mode 'foursquare' to 'swarm' + $command = $client->getCommand('venues/search', [ 'near' => 'Chicago, IL', 'query' => 'sushi' @@ -93,7 +99,8 @@ but basically they should be the same as the [api endpoints listed in the docs]( If you are using [HWIOAuthBundle](https://github.com/hwi/HWIOAuthBundle) this bundle will automatically look for a `resource_owner` of type `foursquare` in that bundle's configuration and inject the `client_id` and `client_secret` -into the `jcroll_foursquare_client` service (no need to configure this bundle). +into the `jcroll_foursquare_client` service (no need to configure this bundle unless you want to define custom values +for `version` or `mode`). Additionally a listener will be configured and if the authenticated user possesses an oauth token belonging to foursquare the token will be automatically injected into the `jcroll_foursquare_client` service for signed requests (no need to call diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 847fcd3..5c67b68 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -4,19 +4,9 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - - - - - - - %jcroll_foursquare_api.client_id% - %jcroll_foursquare_api.client_secret% -