diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 62e8cf2..005266a 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -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(); diff --git a/DependencyInjection/JcidLocoExtension.php b/DependencyInjection/JcidLocoExtension.php index 01424c9..74315df 100644 --- a/DependencyInjection/JcidLocoExtension.php +++ b/DependencyInjection/JcidLocoExtension.php @@ -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"); diff --git a/Loco/Downloader.php b/Loco/Downloader.php index e2e63af..e86ead8 100644 --- a/Loco/Downloader.php +++ b/Loco/Downloader.php @@ -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(); + } } } } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 0c8ade4..d9a33b9 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -14,10 +14,7 @@ - %jcid_loco.configuration.locales% - %jcid_loco.configuration.domains% - %jcid_loco.configuration.target% - %jcid_loco.configuration.key% + %jcid_loco.configurations%