Skip to content

Commit

Permalink
Merge pull request #6 from jcroll/versionConfig
Browse files Browse the repository at this point in the history
[RFC] Update Bundle Config for New Client API
  • Loading branch information
jcroll authored Sep 7, 2016
2 parents 1c8a1e3 + a8438ba commit bf0f2b8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
6 changes: 6 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public function getConfigTreeBuilder()
->isRequired()
->cannotBeEmpty()
->end()
->scalarNode('version')
->defaultValue(20160901)
->end()
->scalarNode('mode')
->defaultValue('foursquare')
->end()
->end()
;

Expand Down
31 changes: 27 additions & 4 deletions DependencyInjection/JcrollFoursquareApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
;
}

/**
Expand Down Expand Up @@ -98,13 +101,33 @@ 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;
}

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;
}
}
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <your_foursquare_client_id>
client_secret: <your_foursquare_client_secret>
client_secret: <your_foursquare_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
Expand All @@ -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'
Expand All @@ -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
Expand Down
10 changes: 0 additions & 10 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<parameters>
<parameter key="jcroll_foursquare_api.client_id" />
<parameter key="jcroll_foursquare_api.client_secret" />
</parameters>

<services>

<service id="jcroll_foursquare_api.foursquare_client" class="Jcroll\FoursquareApiClient\Client\FoursquareClient">
<factory class="Jcroll\FoursquareApiClient\Client\FoursquareClient" method="factory" />
<argument type="collection">
<argument key="client_id">%jcroll_foursquare_api.client_id%</argument>
<argument key="client_secret">%jcroll_foursquare_api.client_secret%</argument>
</argument>
</service>

<service id="jcroll_foursquare_client" alias="jcroll_foursquare_api.foursquare_client" />
Expand Down

0 comments on commit bf0f2b8

Please sign in to comment.