Skip to content

Commit

Permalink
ready for use integration with phraseapp for updating translations
Browse files Browse the repository at this point in the history
  • Loading branch information
nediam committed Sep 15, 2015
0 parents commit d23a68d
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
composer.phar
composer.lock
vendor/

99 changes: 99 additions & 0 deletions Command/PhraseAppUpdateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* @author nediam
* @date 12.09.2015 12:30
*/

namespace nediam\PhraseAppBundle\Command;


use nediam\PhraseAppBundle\Service\PhraseApp;
use RuntimeException;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Translation\Catalogue\DiffOperation;
use Symfony\Component\Translation\MessageCatalogue;
use Symfony\Component\Translation\Writer\TranslationWriter;

class PhraseAppUpdateCommand extends ContainerAwareCommand
{
private $availableLocales;
/** @var TranslationLoader */
private $loader;
/** @var TranslationWriter */
private $writer;
/** @var PhraseApp */
private $phraseApp;
/** @var string */
private $translationsPath;
/** @var string */
private $tmpPath;
/** @var array */
private $validators = [];
/** @var array */
private $locales = [];

protected function configure()
{
$this->setName('phraseapp:update')->addOption('locale', null, InputOption::VALUE_REQUIRED);

$this->addOptionValidator('locale', function ($value) {
if (null === $value) {
return;
}
$this->locales = array_map('trim', explode(',', $value));
});
}

/**
* {@inheritdoc}
*/
protected function initialize(InputInterface $input, OutputInterface $output)
{
foreach ($input->getOptions() as $key => $option) {
if (array_key_exists($key, $this->validators)) {
call_user_func_array($this->validators[$key], [
$input->getOption($key),
$input,
$output
]);
}
}
}

/**
* @param string $name
* @param callback $validator
*
* @throws \Exception
*/
protected function addOptionValidator($name, $validator)
{
if (!is_callable($validator)) {
throw new \Exception('Validator is not callable');
}

$this->validators[$name] = $validator;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$container = $this->getContainer();
$this->phraseApp = $container->get('phrase_app.service');
$this->availableLocales = $this->phraseApp->getLocales();

$unsupportedLocales = array_diff($this->locales, array_keys($this->availableLocales));
if (count($unsupportedLocales)) {
throw new RuntimeException(sprintf('Unsupported locales "%s"', implode(', ', $unsupportedLocales)));
}
if (0 === count($this->locales)) {
$this->locales = array_keys($this->availableLocales);
}

// fetch and save translations
$this->phraseApp->process($this->locales);
}
}
45 changes: 45 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace nediam\PhraseAppBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('phrase_app');

$rootNode
->children()
->scalarNode('token')->isRequired()->end()
->scalarNode('project_id')->isRequired()->end()
->scalarNode('output_format')
->defaultValue('yml')
->end()
->scalarNode('translations_path')->isRequired()->end()
->arrayNode('translations')
->prototype('scalar')->end()
->end()
->arrayNode('locales')
->prototype('scalar')->end()
->end()
;

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.

return $treeBuilder;
}
}
31 changes: 31 additions & 0 deletions DependencyInjection/PhraseAppExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace nediam\PhraseAppBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
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 PhraseAppExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('phrase_app.token', $config['token']);
$container->setParameter('phrase_app.config', $config);

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('phraseapp-services.xml');
}
}
9 changes: 9 additions & 0 deletions PhraseAppBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace nediam\PhraseAppBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class PhraseAppBundle extends Bundle
{
}
20 changes: 20 additions & 0 deletions Resources/config/phraseapp-services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
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">

<services>
<service id="phrase_app.client" class="nediam\PhraseApp\PhraseAppClient" public="false">
<argument>%phrase_app.token%</argument>
</service>

<service id="phrase_app.service" class="nediam\PhraseAppBundle\Service\PhraseApp">
<argument id="phrase_app.client" type="service"/>
<argument id="translation.loader" type="service"/>
<argument id="translation.writer" type="service"/>
<argument>%phrase_app.config%</argument>
</service>
</services>

</container>
Loading

0 comments on commit d23a68d

Please sign in to comment.