From 4655780e671473373c18fd03f904476c3b1fbb23 Mon Sep 17 00:00:00 2001 From: Andreas Schempp Date: Thu, 30 Jul 2015 16:45:47 +0200 Subject: [PATCH 1/3] Use ConfigurableExtension class provided by Symfony see http://symfony.com/doc/current/cookbook/bundles/configuration.html --- DependencyInjection/HappyRGoogleApiExtension.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/DependencyInjection/HappyRGoogleApiExtension.php b/DependencyInjection/HappyRGoogleApiExtension.php index ec78141..4132c8b 100644 --- a/DependencyInjection/HappyRGoogleApiExtension.php +++ b/DependencyInjection/HappyRGoogleApiExtension.php @@ -4,7 +4,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; -use Symfony\Component\HttpKernel\DependencyInjection\Extension; +use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; use Symfony\Component\DependencyInjection\Loader; /** @@ -12,16 +12,13 @@ * * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} */ -class HappyRGoogleApiExtension extends Extension +class HappyRGoogleApiExtension extends ConfigurableExtension { /** * {@inheritDoc} */ - public function load(array $configs, ContainerBuilder $container) + public function loadInternal(array $config, ContainerBuilder $container) { - $configuration = new Configuration(); - $config = $this->processConfiguration($configuration, $configs); - $container->setParameter('happy_r_google_api', $config); $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); From b496c627de89d545c64ec8d196a4b054a09e49ba Mon Sep 17 00:00:00 2001 From: Andreas Schempp Date: Thu, 30 Jul 2015 18:21:51 +0200 Subject: [PATCH 2/3] Allow to define multiple account configurations --- DependencyInjection/Configuration.php | 80 +++++++++++---- .../HappyRGoogleApiExtension.php | 97 +++++++++++++++++-- Resources/config/services.yml | 26 ----- 3 files changed, 151 insertions(+), 52 deletions(-) delete mode 100644 Resources/config/services.yml diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index ab82de3..dd00d0b 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -20,22 +20,7 @@ public function getConfigTreeBuilder() $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('happy_r_google_api'); - $rootNode - ->children() - ->scalarNode('application_name')->isRequired()->cannotBeEmpty()->end() - ->scalarNode('oauth2_client_id')->isRequired()->cannotBeEmpty()->end() - ->scalarNode('oauth2_client_secret')->isRequired()->cannotBeEmpty()->end() - ->scalarNode('oauth2_redirect_uri')->isRequired()->cannotBeEmpty()->end() - ->scalarNode('developer_key')->isRequired()->cannotBeEmpty()->end() - ->scalarNode('site_name')->isRequired()->cannotBeEmpty()->end() - - ->scalarNode('authClass')->end() - ->scalarNode('ioClass')->end() - ->scalarNode('cacheClass')->end() - ->scalarNode('basePath')->end() - ->scalarNode('ioFileCache_directory')->end() - //end rootnode children - ->end(); + $this->configureAccountNode($rootNode); //let use the api defaults //$this->addServicesSection($rootNode); @@ -43,7 +28,6 @@ public function getConfigTreeBuilder() return $treeBuilder; } - /** * Add the service section * @@ -144,4 +128,66 @@ private function addServicesSection(ArrayNodeDefinition $rootNode) ; } + + /** + * Add properties and validation for account configuration. + * + * @param ArrayNodeDefinition $node + */ + private function configureAccountNode(ArrayNodeDefinition $node) + { + $node + ->beforeNormalization() + ->ifTrue(function ($config) { + return is_array($config) && !array_key_exists('accounts', $config) && !array_key_exists('account', $config); + }) + ->then(function ($config) { + // Key that should not be rewritten to the accounts config + $excludedKeys = array('default_account' => true); + $accounts = array(); + + foreach ($config as $key => $value) { + if (isset($excludedKeys[$key])) { + continue; + } + + $accounts[$key] = $config[$key]; + unset($config[$key]); + } + + $config['default_account'] = isset($config['default_account']) ? (string) $config['default_account'] : 'default'; + $config['accounts'] = array($config['default_account'] => $accounts); + + return $config; + }) + ->end() + ->children() + ->scalarNode('default_account')->end() + ->end() + ->fixXmlConfig('account') + ->children() + ->arrayNode('accounts') + ->isRequired() + ->requiresAtLeastOneElement() + ->useAttributeAsKey('name') + ->prototype('array') + ->children() + ->scalarNode('application_name')->isRequired()->cannotBeEmpty()->end() + ->scalarNode('oauth2_client_id')->isRequired()->cannotBeEmpty()->end() + ->scalarNode('oauth2_client_secret')->isRequired()->cannotBeEmpty()->end() + ->scalarNode('oauth2_redirect_uri')->isRequired()->cannotBeEmpty()->end() + ->scalarNode('developer_key')->isRequired()->cannotBeEmpty()->end() + ->scalarNode('site_name')->isRequired()->cannotBeEmpty()->end() + + ->scalarNode('authClass')->end() + ->scalarNode('ioClass')->end() + ->scalarNode('cacheClass')->end() + ->scalarNode('basePath')->end() + ->scalarNode('ioFileCache_directory')->end() + ->end() + ->end() + ->end() + ->end() + ; + } } diff --git a/DependencyInjection/HappyRGoogleApiExtension.php b/DependencyInjection/HappyRGoogleApiExtension.php index 4132c8b..d8a8775 100644 --- a/DependencyInjection/HappyRGoogleApiExtension.php +++ b/DependencyInjection/HappyRGoogleApiExtension.php @@ -3,15 +3,12 @@ namespace HappyR\Google\ApiBundle\DependencyInjection; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\DependencyInjection\Definition; +use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; use Symfony\Component\DependencyInjection\Loader; -/** - * This is the class that loads and manages your bundle configuration - * - * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} - */ class HappyRGoogleApiExtension extends ConfigurableExtension { /** @@ -19,9 +16,91 @@ class HappyRGoogleApiExtension extends ConfigurableExtension */ public function loadInternal(array $config, ContainerBuilder $container) { - $container->setParameter('happy_r_google_api', $config); + foreach ($config['accounts'] as $name => $account) { + $this->loadAccount($name, $account, $container); + } - $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); - $loader->load('services.yml'); + // Backwards compatibility + $default = $config['default_account']; + $container->setParameter('happy_r_google_api', array_merge($config['accounts'][$default], $config)); + $container->setAlias('happyr.google.api.client', sprintf('happyr.google.api.%s_client', $default)); + $container->setAlias('happyr.google.api.analytics', sprintf('happyr.google.api.%s_analytics', $default)); + $container->setAlias('happyr.google.api.youtube', sprintf('happyr.google.api.%s_youtube', $default)); + $container->setAlias('happyr.google.api.groups_migration', sprintf('happyr.google.api.%s_groups_migration', $default)); + $container->setAlias('happyr.google.api.slides', sprintf('happyr.google.api.%s_slides', $default)); + $container->setAlias('happyr.google.api.drive', sprintf('happyr.google.api.%s_drive', $default)); + } + + /** + * Define Google Client and add services for each account configuration. + * + * @param string $name The account name + * @param array $config The account configuration + * @param ContainerBuilder $container The container builder + */ + private function loadAccount($name, array $config, ContainerBuilder $container) + { + $clientId = sprintf('happyr.google.api.%s_client', $name); + + $client = new Definition( + 'HappyR\Google\ApiBundle\Services\GoogleClient', + array($config, new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)) + ); + + $client->addTag('monolog.logger', array('channel' => 'google_client')); + + $container->setDefinition($clientId, $client); + + $this->createServices($name, $clientId, $container); + } + + /** + * Create Google services for an account. + * + * @param $name + * @param $clientId + * @param ContainerBuilder $container + */ + private function createServices($name, $clientId, ContainerBuilder $container) + { + $container->setDefinition( + sprintf('happyr.google.api.%s_analytics', $name), + new Definition( + 'HappyR\Google\ApiBundle\Services\AnalyticsService', + array(new Reference($clientId)) + ) + ); + + $container->setDefinition( + sprintf('happyr.google.api.%s_youtube', $name), + new Definition( + 'HappyR\Google\ApiBundle\Services\YoutubeService', + array(new Reference($clientId)) + ) + ); + + $container->setDefinition( + sprintf('happyr.google.api.%s_groups_migration', $name), + new Definition( + 'HappyR\Google\ApiBundle\Services\GroupsMigrationService', + array(new Reference($clientId)) + ) + ); + + $container->setDefinition( + sprintf('happyr.google.api.%s_slides', $name), + new Definition( + 'HappyR\Google\ApiBundle\Services\SlidesService', + array(new Reference($clientId)) + ) + ); + + $container->setDefinition( + sprintf('happyr.google.api.%s_drive', $name), + new Definition( + 'HappyR\Google\ApiBundle\Services\DriveService', + array(new Reference($clientId)) + ) + ); } } diff --git a/Resources/config/services.yml b/Resources/config/services.yml deleted file mode 100644 index 26ff4dd..0000000 --- a/Resources/config/services.yml +++ /dev/null @@ -1,26 +0,0 @@ -services: - happyr.google.api.client: - class: HappyR\Google\ApiBundle\Services\GoogleClient - arguments: ["%happy_r_google_api%", "@?logger"] - tags: - - { name: monolog.logger, channel: google_client } - - happyr.google.api.analytics: - class: HappyR\Google\ApiBundle\Services\AnalyticsService - arguments: ["@happyr.google.api.client"] - - happyr.google.api.youtube: - class: HappyR\Google\ApiBundle\Services\YoutubeService - arguments: ["@happyr.google.api.client"] - - happyr.google.api.groups_migration: - class: HappyR\Google\ApiBundle\Services\GroupsMigrationService - arguments: ["@happyr.google.api.client"] - - happyr.google.api.slides: - class: HappyR\Google\ApiBundle\Services\SlidesService - arguments: ["@happyr.google.api.client"] - - happyr.google.api.drive: - class: HappyR\Google\ApiBundle\Services\DriveService - arguments: ["@happyr.google.api.client"] From 1ee1f37dc38211fbbc5f8e480e03086f76c718b1 Mon Sep 17 00:00:00 2001 From: Andreas Schempp Date: Thu, 30 Jul 2015 18:32:46 +0200 Subject: [PATCH 3/3] Updated README for the new configuration --- README.md | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0b32181..e2eae43 100644 --- a/README.md +++ b/README.md @@ -52,12 +52,14 @@ $bundles = array( # app/config/config.yml # you will get these parameters form https://code.google.com/apis/console/" happy_r_google_api: - application_name: MySite - oauth2_client_id: - oauth2_client_secret: - oauth2_redirect_uri: - developer_key: - site_name: mysite.com + accounts: + default: + application_name: MySite + oauth2_client_id: + oauth2_client_secret: + oauth2_redirect_uri: + developer_key: + site_name: mysite.com ```