diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 8b149bd1..df6837b4 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -131,6 +131,14 @@ private function configsNode(ArrayNodeDefinition $root) ->arrayNode('whitelist_domains') ->prototype('scalar')->end() ->end() + ->arrayNode('local_storage') + ->info('Where translations are stored locally.') + ->prototype('enum')->values($this->getStorages())->defaultValue('filesystem')->end() + ->end() + ->arrayNode('remote_storage') + ->info('Any remote storage for the translation, could be a third party translation service.') + ->prototype('enum')->values($this->getStorages())->defaultValue('blackhole')->end() + ->end() ->scalarNode('output_dir')->isRequired()->cannotBeEmpty()->end() ->scalarNode('project_root')->info("The root dir of your project. By default this will be kernel_root's parent. ")->end() ->end() @@ -156,11 +164,35 @@ private function addTranslationServiceNode(ArrayNodeDefinition $root) { $root ->children() - ->enumNode('storage') - ->info('Where translations are stored.') - ->values(['blackhole', 'filesystem', 'loco']) - ->defaultValue('filesystem') + ->arrayNode('platforms') + ->children() + ->arrayNode('loco') + ->addDefaultsIfNotSet() + ->children() + ->arrayNode('projects') + ->useAttributeAsKey('name') + ->prototype('array') + ->children() + ->scalarNode('api_key')->info('The API key for this project.')->isRequired()->end() + ->end() + ->end() + ->end() + ->end() ->end() ->end(); } + + /** + * + * @return array + */ + private function getStorages() + { + return [ + 'blackhole', + 'filesystem', + 'loco' + ]; + } + } diff --git a/DependencyInjection/TranslationExtension.php b/DependencyInjection/TranslationExtension.php index 353c1d9e..0b2c7e28 100644 --- a/DependencyInjection/TranslationExtension.php +++ b/DependencyInjection/TranslationExtension.php @@ -11,6 +11,7 @@ namespace Translation\Bundle\DependencyInjection; +use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\DefinitionDecorator; @@ -18,6 +19,7 @@ use Symfony\Component\HttpKernel\DependencyInjection\Extension; use Symfony\Component\DependencyInjection\Loader; use Translation\Bundle\Service\StorageService; +use Translation\PlatformAdapter\Adapter\Loco; /** * This is the class that loads and manages your bundle configuration. @@ -55,6 +57,8 @@ public function load(array $configs, ContainerBuilder $container) $this->enableFallbackAutoTranslator($container, $config); } + $this->handlePlatformAdapters($loader, $container, $config['platforms']); + $first = null; foreach ($config['configs'] as $name => &$c) { if ($first === null || $name === 'default') { @@ -87,6 +91,41 @@ private function enableWebUi(ContainerBuilder $container, $config) { } + /** + * @param LoaderInterface $loader + * @param ContainerBuilder $container + * @param array $config + */ + private function handlePlatformAdapters(LoaderInterface $loader, ContainerBuilder $container, array $config) + { + foreach ($config as $name => $config) { + switch ($name) { + case 'loco': + $this->verifyClassExists(Loco::class); + // TODO load + break; + default: + throw new \LogicException(sprintf('Platform named "%s" was not found.', $name)); + } + } + } + + /** + * @param string $class + * @param string|null $message + * + * @throws \LogicException if class does not exist. + */ + private function verifyClassExists($class, $message = null) + { + if (null === $message) { + $message = sprintf('Could not find class "%s" that is needed for your current Translation configration.', $class); + } + if (!class_exists($class)) { + throw new \LogicException($message); + } + } + private function enableSymfonyProfiler(ContainerBuilder $container, $config) { $container->setParameter('php_translation.toolbar.allow_edit', $config['symfony_profiler']['allow_edit']);