Skip to content
This repository has been archived by the owner on Jun 4, 2020. It is now read-only.

Commit

Permalink
Loco downloader uitgebreid met mogelijkheid om meerdere type bestande…
Browse files Browse the repository at this point in the history
…n te downloaden
  • Loading branch information
cmodijk committed Dec 26, 2013
1 parent a8d713e commit 70ae67e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 45 deletions.
73 changes: 50 additions & 23 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,60 @@ public function getConfigTreeBuilder()
$rootNode = $treeBuilder->root("jcid_loco");

$treeBuilder->root("jcid_loco")
->children()

// Config the locales to download
->arrayNode("locales")
->isRequired()
->requiresAtLeastOneElement()
->useAttributeAsKey("name")
->prototype("scalar")->end()
->end()

// Config the locales to download
->arrayNode("domains")
->isRequired()
->requiresAtLeastOneElement()
->prototype("scalar")->end()
->end()
// Verwerken opgegeven config
->beforeNormalization()

// Target dir
->scalarNode("target")
->defaultValue("%kernel.root_dir%/Resources/translations")
->end()
// Key is op root niveau ook mogelijk
->ifTrue(function ($v) { return is_array($v) && array_key_exists("key", $v); })
->then(function ($v) {
// Key opslaan
$key = $v["key"];
unset($v["key"]);

// API Key
->scalarNode("key")
->isRequired()
->end()
// Key op array niveau plaatsen
foreach ($v as $code => $config) {
$v[$code]["key"] = $key;
}
return $v;
})

// Mogelijk maken om waardes op hoogste niveau op te geven als dit wenselijk is
->ifTrue(function ($v) { return is_array($v) && array_key_exists("locales", $v); })
->then(function ($v) { return array("default" => $v); })

->end()

// Minimaal een element nodig
->requiresAtLeastOneElement()
->useAttributeAsKey("name")
->prototype("array")
->addDefaultsIfNotSet()
->children()

// Config the locales to download
->arrayNode("locales")
->isRequired()
->requiresAtLeastOneElement()
->useAttributeAsKey("name")
->prototype("scalar")->end()
->end()

// Config the locales to download
->arrayNode("domains")
->isRequired()
->requiresAtLeastOneElement()
->prototype("scalar")->end()
->end()

// Extra params
->scalarNode("key") ->isRequired()->end()
->scalarNode("target") ->defaultValue("%kernel.root_dir%/Resources/translations")->end()
->scalarNode("extension") ->defaultValue("phps")->end()
->scalarNode("format") ->defaultValue("symfony")->end()
->scalarNode("index") ->defaultValue("id")->end()

->end()
->end()
->end();

Expand Down
4 changes: 1 addition & 3 deletions DependencyInjection/JcidLocoExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);

foreach ($config as $key => $value) {
$container->setParameter("jcid_loco.configuration.".$key, $value);
}
$container->setParameter("jcid_loco.configurations", $config);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__."/../Resources/config"));
$loader->load("services.xml");
Expand Down
42 changes: 27 additions & 15 deletions Loco/Downloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,40 @@

class Downloader
{
private $locales;
private $domains;
private $target;
private $client;
private $config;

public function __construct($locales, $domains, $target, $key)
public function __construct($config)
{
$this->locales = $locales;
$this->domains = $domains;
$this->target = $target;

$this->config = $config;
$this->client = new Client("https://localise.biz/api/");
$this->client->setDefaultOption("query/key", $key);
}

public function download()
{
foreach ($this->locales as $localeKey => $localeValue) {
foreach ($this->domains as $domain) {
$response = $this->client->get(sprintf("export/locale/%s.phps?format=symfony&index=id&filter=%s", $localeValue, $domain), array(), array(
"save_to" => sprintf("%s/%s.%s.phps", $this->target, $domain, $localeKey),
))->send();
foreach ($this->config as $name => $config) {
foreach ($config["locales"] as $localeKey => $localeValue) {
foreach ($config["domains"] as $domain) {

// Dir maken als deze niet bestaat
if (!is_dir($config["target"])) {
mkdir($config["target"], 0777, true);
}

// Basis query
$query = array(
"key" => $config["key"],
"format" => $config["format"],
"index" => $config["index"],
"filter" => $domain,
);

// Build url
$url = sprintf("export/locale/%s.%s?%s", $localeValue, $config["extension"], http_build_query($query));
$savePath = sprintf("%s/%s.%s.%s", $config["target"], $domain, $localeKey, $config["extension"]);

// Downloaden
$response = $this->client->get($url, array(), array( "save_to" => $savePath))->send();
}
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
<services>

<service id="jcid_loco.downloader" class="%jcid_loco.downloader.class%">
<argument>%jcid_loco.configuration.locales%</argument>
<argument>%jcid_loco.configuration.domains%</argument>
<argument>%jcid_loco.configuration.target%</argument>
<argument>%jcid_loco.configuration.key%</argument>
<argument>%jcid_loco.configurations%</argument>
</service>

<service id="jcid_loco.translation.phps" class="%jcid_loco.translation.phps.class%">
Expand Down

0 comments on commit 70ae67e

Please sign in to comment.