Skip to content
This repository has been archived by the owner on Nov 9, 2024. It is now read-only.

Add API key provider #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
56 changes: 51 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ Use 2.2 or lower
## Install

1\. Composer require

```
$ composer require cethyworks/google-place-autocomplete-bundle
```

2\. Register bundles

```php
// AppKernel.php
class AppKernel extends Kernel
{
Expand All @@ -31,19 +32,64 @@ Use 2.2 or lower
new Cethyworks\GooglePlaceAutocompleteBundle\CethyworksGooglePlaceAutocompleteBundle(),
];
// ...

}
}
```

## How to use
1\. Add (optionally) a `config/packages/cethyworks_google_place_autocomplete.yaml` file with :

```yaml
cethyworks_google_place_autocomplete:
google:
api_key: 'your_api_key'

```
2\. Use `Cethyworks\GooglePlaceAutocompleteBundle\Form\SimpleGooglePlaceAutocompleteType` into your forms ;

3\. Done !

## Get API key from custom provider
If you need to get your API key from another way. You can use a custom provider

Example of custom provider with API key stored in database.

```yaml
cethyworks_google_place_autocomplete:
google:
api_key_provider: 'Acme\FooBundle\Provider\BarApiKeyTestProvider'
```

```php
namespace Acme\FooBundle\Provider;

use Doctrine\ORM\EntityManager;
use Cethyworks\GooglePlaceAutocompleteBundle\Provider\ApiKeyProviderInterface;

class BarApiKeyTestProvider implements ApiKeyProviderInterface
{
/** @var EntityManager */
protected $em;

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

/**
* @return string
*/
public function getGoogleApiKey(): string
{
$bar = $this->em->getRepository('FooBundle:Bar')->findOneBy([
'enabled' => true,
]);
return $bar->getApiKey();
}
}
```


## Get more data from the Google Place API
If you need more info from the place API results, you can use the `ComplexGooglePlaceAutocompleteType` in your forms instead.
Expand Down
2 changes: 2 additions & 0 deletions src/CethyworksGooglePlaceAutocompleteBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cethyworks\GooglePlaceAutocompleteBundle;

use Cethyworks\GooglePlaceAutocompleteBundle\DependencyInjection\Compiler\ApiKeyProviderCompilerPass;
use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand All @@ -13,6 +14,7 @@ public function build(ContainerBuilder $container)
parent::build($container);

$this->addRegisterMappingsPass($container);
$container->addCompilerPass(new ApiKeyProviderCompilerPass());
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Command/GooglePlaceAutocompleteLibraryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cethyworks\GooglePlaceAutocompleteBundle\Command;

use Cethyworks\ContentInjectorBundle\Command\TwigCommand;
use Cethyworks\GooglePlaceAutocompleteBundle\Provider\ApiKeyProviderInterface;
use Twig_Environment;

class GooglePlaceAutocompleteLibraryCommand extends TwigCommand
Expand All @@ -13,12 +14,12 @@ class GooglePlaceAutocompleteLibraryCommand extends TwigCommand
* GooglePlaceAutocompleteLibraryCommand constructor.
*
* @param Twig_Environment $twig
* @param string $apiKey
* @param ApiKeyProviderInterface $apiKeyProvider
*/
public function __construct(Twig_Environment $twig, $apiKey)
public function __construct(Twig_Environment $twig, ApiKeyProviderInterface $apiKeyProvider)
{
parent::__construct($twig);
$this->setGoogleApiKey($apiKey);
$this->setGoogleApiKey($apiKeyProvider->getGoogleApiKey());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Cethyworks\GooglePlaceAutocompleteBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* This is the class that loads and manages your bundle configuration.
Expand All @@ -24,6 +24,7 @@ public function load(array $configs, ContainerBuilder $container)

// setup parameters
$container->setParameter('cethyworks.google_place_autocomplete.google_api_key', $config['google']['api_key']);
$container->setParameter('cethyworks.google_place_autocomplete.google_api_key_provider', $config['google']['api_key_provider']);

$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
Expand Down
44 changes: 44 additions & 0 deletions src/DependencyInjection/Compiler/ApiKeyProviderCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Cethyworks\GooglePlaceAutocompleteBundle\DependencyInjection\Compiler;

use Cethyworks\GooglePlaceAutocompleteBundle\Command\GooglePlaceAutocompleteLibraryCommand;
use Cethyworks\GooglePlaceAutocompleteBundle\DependencyInjection\Configuration;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Class ApiKeyProviderCompilerPass
*
* @package Cethyworks\GooglePlaceAutocompleteBundle\DependencyInjection
*/
class ApiKeyProviderCompilerPass implements CompilerPassInterface
{
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
$configs = $container->getExtensionConfig('cethyworks_google_place_autocomplete');
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$commandDefinition = $container->getDefinition(GooglePlaceAutocompleteLibraryCommand::class);
$providerDefinition = $container->getDefinition($config['google']['api_key_provider']);
$commandDefinition->setArgument(1, $providerDefinition);
}

/**
* @param ConfigurationInterface $configuration
* @param array $configs
*
* @return array
*/
protected function processConfiguration(ConfigurationInterface $configuration, array $configs)
{
$processor = new Processor();

return $processor->processConfiguration($configuration, $configs);
}
}
7 changes: 5 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cethyworks\GooglePlaceAutocompleteBundle\DependencyInjection;

use Cethyworks\GooglePlaceAutocompleteBundle\Provider\DefaultApiKeyProvider;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand All @@ -25,9 +26,11 @@ public function getConfigTreeBuilder()
->arrayNode('google')
->addDefaultsIfNotSet()
->children()
->scalarNode('api_key')
->isRequired()
->scalarNode('api_key_provider')
->defaultValue(DefaultApiKeyProvider::class)
->cannotBeEmpty()
->end()
->scalarNode('api_key')
->defaultValue('')
->end()
->end()
Expand Down
8 changes: 8 additions & 0 deletions src/Provider/ApiKeyProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Cethyworks\GooglePlaceAutocompleteBundle\Provider;

interface ApiKeyProviderInterface
{
public function getGoogleApiKey();
}
27 changes: 27 additions & 0 deletions src/Provider/DefaultApiKeyProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Cethyworks\GooglePlaceAutocompleteBundle\Provider;

class DefaultApiKeyProvider implements ApiKeyProviderInterface
{
/** @var string */
protected $apiKey;

/**
* GoogleApiKeyProvider constructor.
*
* @param string $apiKey
*/
public function __construct(string $apiKey)
{
$this->apiKey = $apiKey;
}

/**
* @return string
*/
public function getGoogleApiKey()
{
return $this->apiKey;
}
}
6 changes: 5 additions & 1 deletion src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ services:
tags:
- { name: twig.extension }

Cethyworks\GooglePlaceAutocompleteBundle\Provider\DefaultApiKeyProvider:
arguments:
- "%cethyworks.google_place_autocomplete.google_api_key%"

Cethyworks\GooglePlaceAutocompleteBundle\Command\GooglePlaceAutocompleteLibraryCommand:
arguments:
- "@twig"
- "%cethyworks.google_place_autocomplete.google_api_key%"
- "@Cethyworks\\GooglePlaceAutocompleteBundle\\Provider\\DefaultApiKeyProvider"

Cethyworks\GooglePlaceAutocompleteBundle\Form\Extension\GooglePlaceAutocompleteInjectorAwareTypeExtension:
arguments:
Expand Down
5 changes: 4 additions & 1 deletion tests/Command/GooglePlaceAutocompleteLibraryCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cethyworks\GooglePlaceAutocompleteBundle\Tests\Command;

use Cethyworks\GooglePlaceAutocompleteBundle\Command\GooglePlaceAutocompleteLibraryCommand;
use Cethyworks\GooglePlaceAutocompleteBundle\Provider\DefaultApiKeyProvider;
use PHPUnit\Framework\TestCase;

class GooglePlaceAutocompleteLibraryCommandTest extends TestCase
Expand All @@ -13,8 +14,10 @@ public function testSetGoogleApiKey()
$twig->expects($this->once())->method('render')->with(
'@CethyworksGooglePlaceAutocompleteBundle/google_place_autocomplete_library.html.twig',
['google_api_key' => 'foobar' ]);
$provider = $this->getMockBuilder(DefaultApiKeyProvider::class)->disableOriginalConstructor()->getMock();
$provider->expects($this->once())->method('getGoogleApiKey')->willReturn('foobar');

$command = new GooglePlaceAutocompleteLibraryCommand($twig, 'foobar');
$command = new GooglePlaceAutocompleteLibraryCommand($twig, $provider);
$command->setGoogleApiKey('foobar');
$command();
}
Expand Down