Skip to content

Commit

Permalink
Merge pull request #1 from gregi99/extended-configuration
Browse files Browse the repository at this point in the history
extended configuration for merge many tags
  • Loading branch information
nediam committed Jan 20, 2016
2 parents 058cbde + d75d512 commit f95a466
Show file tree
Hide file tree
Showing 8 changed files with 370 additions and 40 deletions.
11 changes: 10 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ public function getConfigTreeBuilder()
->end()
->scalarNode('translations_path')->isRequired()->end()
->arrayNode('catalogues')
->prototype('scalar')->end()
->useAttributeAsKey('name')
->prototype('array')
->children()
->arrayNode('tags')
->prototype('scalar')->end()
->end()
->scalarNode('output_format')->defaultValue('yml')->end()
->scalarNode('path')->defaultValue(null)->end()
->end()
->end()
->end()
->arrayNode('locales')
->prototype('scalar')->end()
Expand Down
16 changes: 16 additions & 0 deletions Events/PhraseappEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace nediam\PhraseAppBundle\Events;

final class PhraseappEvents
{
/**
* The 'phraseapp.post_download event is thrown each time when all files are downloaded and wait for final save
*
* The event listener receives an
* nediam\PhraseAppBundle\Events\PostDownloadEvent instance.
*
* @var string
*/
const POST_DOWNLOAD = 'phraseapp.post_download';
}
128 changes: 128 additions & 0 deletions Events/PostDownloadEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
/**
* Author: grzegorz
* Date: 14.01.16 20:39
*/

namespace nediam\PhraseAppBundle\Events;

use Symfony\Component\EventDispatcher\Event;

class PostDownloadEvent extends Event
{
/**
* @var array
*/
private $tempData = [];

/**
* @var null|string
*/
private $finalFilePath = null;

/**
* @var string
*/
private $locale;

/**
* @var string
*/
private $catalogue;

/**
* PostDownloadEvent constructor.
*
* @param array $tempData
* @param string $locale
* @param string $catalogue
*/
public function __construct(array $tempData, $locale, $catalogue)
{
$this->tempData = $tempData;
$this->locale = $locale;
$this->catalogue = $catalogue;
}

/**
* @return array
*/
public function getTempData()
{
return $this->tempData;
}

/**
* @param array $tempFiles
*
* @return $this
*/
public function setTempData(array $tempData)
{
$this->tempData = $tempData;

return $this;
}

/**
* @return null|string
*/
public function getFinalFilePath()
{
return $this->finalFilePath;
}

/**
* @param null|string $finalFilePath
*
* @return $this
*/
public function setFinalFilePath($finalFilePath = null)
{
$this->finalFilePath = $finalFilePath;

return $this;
}

/**
* @return string
*/
public function getLocale()
{
return $this->locale;
}

/**
* @param string $locale
*
* @return $this
*/
public function setLocale($locale)
{
$this->locale = $locale;

return $this;
}

/**
* @return string
*/
public function getCatalogue()
{
return $this->catalogue;
}

/**
* @param string $catalogue
*
* @return $this
*/
public function setCatalogue($catalogue)
{
$this->catalogue = $catalogue;

return $this;
}


}
5 changes: 5 additions & 0 deletions Resources/config/phraseapp-services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<argument id="translation.writer" type="service"/>
<argument>%phrase_app.config%</argument>
<argument id="logger" type="service" on-invalid="null"/>
<argument id="event_dispatcher" type="service"/>
<argument id="phrase_app.file_merger" type="service"/>
</service>

<service id="phrase_app.file_merger" class="nediam\PhraseAppBundle\Service\FileMerger" lazy="true">
</service>
</services>

Expand Down
45 changes: 45 additions & 0 deletions Service/FileMerger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Author: grzegorz
* Date: 15.01.16 11:29
*/

namespace nediam\PhraseAppBundle\Service;

use nediam\PhraseAppBundle\Service\MergeStrategy\MergeInterface;
use nediam\PhraseAppBundle\Service\MergeStrategy\YamlMerger;

class FileMerger
{
/**
* @var MergeInterface[]
*/
private $handlers = [];

/**
* FileMerger constructor.
*
* @param array $adapters
*/
public function __construct()
{
$this->handlers[] = new YamlMerger();
}

/**
* @param $content
* @param $format
*
* @return string
*/
public function merge($content, $format)
{
foreach ($this->handlers as $handler) {
if ($handler->canMerge($format)) {
return $handler->merge($content);
}
}

throw new \InvalidArgumentException('No handler for requested format');
}
}
22 changes: 22 additions & 0 deletions Service/MergeStrategy/MergeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace nediam\PhraseAppBundle\Service\MergeStrategy;

interface MergeInterface
{
/**
* @param $format
*
* @return bool
*/
public function canMerge($format);

/**
* Megres array of jsons
*
* @param array $content
*
* @return string
*/
public function merge($content);
}
40 changes: 40 additions & 0 deletions Service/MergeStrategy/YamlMerger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace nediam\PhraseAppBundle\Service\MergeStrategy;

use Symfony\Component\Yaml\Dumper;

class YamlMerger
{
/**
* @param $format
*
* @return bool
*/
public function canMerge($format)
{
return 'yml' === $format;
}

/**
* @param array $files
*
* @return string
*/
public function merge($content)
{
$yamlTempArray = [];

foreach($content as $input) {
$input = json_decode($input, true);

$yamlTempArray = array_merge($yamlTempArray, $input);
}

ksort($yamlTempArray);

$dumper = new Dumper();

return $dumper->dump($yamlTempArray, 5);
}
}
Loading

0 comments on commit f95a466

Please sign in to comment.