Skip to content

Commit

Permalink
Rework FOSUserBundle integration to reduce dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd authored and Joseph Bielawski committed Apr 4, 2016
1 parent ed4c22a commit 4357bb6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 56 deletions.
12 changes: 6 additions & 6 deletions .travis.yml
@@ -1,17 +1,16 @@
language: php

php:
- 5.3
- 5.5
- 5.6
- 7.0
- 5.6
- 5.3
- hhvm

sudo: false

cache:
directories:
- $HOME/.composer/cache
- ~/.composer/cache/files

matrix:
fast_finish: true
Expand All @@ -35,12 +34,13 @@ matrix:

before_install:
- if [[ "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then phpenv config-rm xdebug.ini; fi;
- if [[ "$TRAVIS_PHP_VERSION" != "hhvm" ]]; then echo "memory_limit=4096M" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi;

before_script:
- composer self-update
- if [ "$SYMFONY_VERSION" != "" ]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --dev --no-update; fi;
- if [ "$FOSUSERBUNDLE_VERSION" != "" ]; then composer require "friendsofsymfony/user-bundle:${FOSUSERBUNDLE_VERSION}" --dev --no-update; fi;
- if [ "$COMPOSER_FLAGS" != "" ]; then composer update --dev --prefer-dist --no-interaction $COMPOSER_FLAGS; fi;
- composer install --dev --prefer-dist --no-interaction $COMPOSER_FLAGS
- if [ "$COMPOSER_FLAGS" != "" ]; then composer update --prefer-dist --no-interaction --no-scripts $COMPOSER_FLAGS; fi;
- composer install --prefer-dist --no-interaction --no-scripts

script: vendor/bin/phpunit
10 changes: 7 additions & 3 deletions Controller/ConnectController.php
Expand Up @@ -111,9 +111,13 @@ public function registrationAction(Request $request, $key)
->getUserInformation($error->getRawToken())
;

// enable compatibility with FOSUserBundle 1.3.x and 2.x
if (interface_exists('FOS\UserBundle\Form\Factory\FactoryInterface')) {
$form = $this->container->get('hwi_oauth.registration.form.factory')->createForm();
if ($this->container->getParameter('hwi_oauth.fosub_enabled')) {
// enable compatibility with FOSUserBundle 1.3.x and 2.x
if (interface_exists('FOS\UserBundle\Form\Factory\FactoryInterface')) {
$form = $this->container->get('hwi_oauth.registration.form.factory')->createForm();
} else {
$form = $this->container->get('hwi_oauth.registration.form');
}
} else {
$form = $this->container->get('hwi_oauth.registration.form');
}
Expand Down
108 changes: 61 additions & 47 deletions DependencyInjection/HWIOAuthExtension.php
Expand Up @@ -16,13 +16,15 @@
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

/**
* @author Geoffrey Bachelet <geoffrey.bachelet@gmail.com>
* @author Alexander <iam.asm89@gmail.com>
* @author Joseph Bielawski <stloyd@gmail.com>
*/
class HWIOAuthExtension extends Extension
{
Expand Down Expand Up @@ -86,18 +88,64 @@ public function load(array $configs, ContainerBuilder $container)
$oauthUtils->replaceArgument(1, new Reference('security.context'));
}

if (isset($config['fosub'])) {
$container
->setDefinition('hwi_oauth.user.provider.fosub_bridge', new DefinitionDecorator('hwi_oauth.user.provider.fosub_bridge.def'))
->addArgument($config['fosub']['properties'])
;
$this->createConnectIntegration($container, $config);

$container->setParameter('hwi_oauth.templating.engine', $config['templating_engine']);

$container->setAlias('hwi_oauth.user_checker', 'security.user_checker');
}

/**
* Creates a resource owner service.
*
* @param ContainerBuilder $container The container builder
* @param string $name The name of the service
* @param array $options Additional options of the service
*/
public function createResourceOwnerService(ContainerBuilder $container, $name, array $options)
{
// alias services
if (isset($options['service'])) {
// set the appropriate name for aliased services, compiler pass depends on it
$container->setAlias('hwi_oauth.resource_owner.'.$name, $options['service']);
} else {
$type = $options['type'];
unset($options['type']);

$definition = new DefinitionDecorator('hwi_oauth.abstract_resource_owner.'.Configuration::getResourceOwnerType($type));
$definition->setClass("%hwi_oauth.resource_owner.$type.class%");
$definition->replaceArgument(2, $options);
$definition->replaceArgument(3, $name);

$container->setDefinition('hwi_oauth.resource_owner.'.$name, $definition);
}
}

/**
* {@inheritdoc}
*/
public function getAlias()
{
return 'hwi_oauth';
}

// check of the connect controllers etc should be enabled
/**
* Check of the connect controllers etc should be enabled.
*
* @param ContainerBuilder $container
* @param array $config
*/
private function createConnectIntegration(ContainerBuilder $container, array $config)
{
if (isset($config['connect'])) {
$container->setParameter('hwi_oauth.connect', true);

if (isset($config['fosub'])) {
$container->setParameter('hwi_oauth.fosub_enabled', true);

$definition = $container->setDefinition('hwi_oauth.user.provider.fosub_bridge', new DefinitionDecorator('hwi_oauth.user.provider.fosub_bridge.def'));
$definition->addArgument($config['fosub']['properties']);

// setup fosub bridge services
$container->setAlias('hwi_oauth.account.connector', 'hwi_oauth.user.provider.fosub_bridge');

Expand All @@ -115,6 +163,12 @@ public function load(array $configs, ContainerBuilder $container)

$container->setAlias('hwi_oauth.registration.form', 'fos_user.registration.form');
}
} else {
$container->setParameter('hwi_oauth.fosub_enabled', false);

if (!$container->hasDefinition('hwi_oauth.registration.form')) {
throw new RuntimeException('You must create service "hwi_oauth.registration.form" to be able to use "connect" functionality.');
}
}

foreach ($config['connect'] as $key => $serviceId) {
Expand All @@ -126,49 +180,9 @@ public function load(array $configs, ContainerBuilder $container)

$container->setAlias('hwi_oauth.'.str_replace('_', '.', $key), $serviceId);
}

// setup custom services
} else {
$container->setParameter('hwi_oauth.fosub_enabled', false);
$container->setParameter('hwi_oauth.connect', false);
}

$container->setParameter('hwi_oauth.templating.engine', $config['templating_engine']);

$container->setAlias('hwi_oauth.user_checker', 'security.user_checker');
}

/**
* Creates a resource owner service.
*
* @param ContainerBuilder $container The container builder
* @param string $name The name of the service
* @param array $options Additional options of the service
*/
public function createResourceOwnerService(ContainerBuilder $container, $name, array $options)
{
// alias services
if (isset($options['service'])) {
// set the appropriate name for aliased services, compiler pass depends on it
$container->setAlias('hwi_oauth.resource_owner.'.$name, $options['service']);
} else {
$type = $options['type'];
unset($options['type']);

$definition = new DefinitionDecorator('hwi_oauth.abstract_resource_owner.'.Configuration::getResourceOwnerType($type));
$definition->setClass("%hwi_oauth.resource_owner.$type.class%");
$container->setDefinition('hwi_oauth.resource_owner.'.$name, $definition);
$definition
->replaceArgument(2, $options)
->replaceArgument(3, $name)
;
}
}

/**
* {@inheritdoc}
*/
public function getAlias()
{
return 'hwi_oauth';
}
}
2 changes: 2 additions & 0 deletions Tests/Controller/ConnectControllerRegistrationActionTest.php
Expand Up @@ -135,6 +135,8 @@ private function makeRegistrationForm()
->willReturn(new User())
;

$this->container->setParameter('hwi_oauth.fosub_enabled', true);

if (interface_exists('FOS\UserBundle\Form\Factory\FactoryInterface')) {
$registrationFormFactory = $this->getMock('FOS\UserBundle\Form\Factory\FactoryInterface');
$registrationFormFactory->expects($this->any())
Expand Down

0 comments on commit 4357bb6

Please sign in to comment.