Navigation Menu

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

Commit

Permalink
add a test for basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlindhe committed Nov 9, 2015
1 parent 8e20fda commit 16d840d
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 46 deletions.
20 changes: 20 additions & 0 deletions phpunit.xml
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
syntaxCheck="true"
verbose="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
50 changes: 4 additions & 46 deletions src/Commands/GenerateInclude.php
@@ -1,9 +1,9 @@
<?php namespace MartinLindhe\VueInternationalizationGenerator\Commands;

use DirectoryIterator;
use Exception;
use Illuminate\Console\Command;

use MartinLindhe\VueInternationalizationGenerator\Generator;

class GenerateInclude extends Command
{
/**
Expand All @@ -20,63 +20,21 @@ class GenerateInclude extends Command
*/
protected $description = "Generates a vue-i18n compatible js array out of project translations";


/**
* Execute the console command.
* @return mixed
* @throws Exception
*/
public function handle()
{
$root = base_path() . '/resources/lang';
if (!is_dir($root)) {
throw new Exception('Path not found: '.$root);
}

$projectLocales = [];
$dir = new DirectoryIterator($root);
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()
&& $fileinfo->isDir()
&& !in_array($fileinfo->getFilename(), ['vendor'])
) {
$projectLocales[$fileinfo->getFilename()] =
$this->allocateLocaleArray($root . '/' . $fileinfo->getFilename());
}
}

$data = 'export default '
. json_encode($projectLocales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
$data = (new Generator)
->generateFromPath($root);

$jsFile = base_path() . '/resources/assets/js/vue-i18n-locales.generated.js';

file_put_contents($jsFile, $data);

echo "Written to " . $jsFile . PHP_EOL;
}

private function allocateLocaleArray($path)
{
$data = [];

$dir = new DirectoryIterator($path);
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
$noExt = $this->removeExtension($fileinfo->getFilename());
$data[$noExt] = include($path . '/' . $fileinfo->getFilename());
}
}

return $data;
}

private function removeExtension($filename)
{
$pos = mb_strrpos($filename, '.');
if ($pos === false) {
return $filename;
}

return mb_substr($filename, 0, $pos);
}
}
67 changes: 67 additions & 0 deletions src/Generator.php
@@ -0,0 +1,67 @@
<?php namespace MartinLindhe\VueInternationalizationGenerator;

use DirectoryIterator;
use Exception;

class Generator
{
/**
* @param string $path
* @return string
* @throws Exception
*/
public function generateFromPath($path)
{
if (!is_dir($path)) {
throw new Exception('Directory not found: '.$path);
}

$locales = [];
$dir = new DirectoryIterator($path);
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()
&& $fileinfo->isDir()
&& !in_array($fileinfo->getFilename(), ['vendor'])
) {
$locales[$fileinfo->getFilename()] =
$this->allocateLocaleArray($path . '/' . $fileinfo->getFilename());
}
}

return 'export default '
. json_encode($locales, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . PHP_EOL;
}

/**
* @param string $path
* @return array
*/
private function allocateLocaleArray($path)
{
$data = [];

$dir = new DirectoryIterator($path);
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
$noExt = $this->removeExtension($fileinfo->getFilename());
$data[$noExt] = include($path . '/' . $fileinfo->getFilename());
}
}

return $data;
}

/**
* @param string $filename
* @return string
*/
private function removeExtension($filename)
{
$pos = mb_strrpos($filename, '.');
if ($pos === false) {
return $filename;
}

return mb_substr($filename, 0, $pos);
}
}
90 changes: 90 additions & 0 deletions tests/GenerateTest.php
@@ -0,0 +1,90 @@
<?php

use MartinLindhe\VueInternationalizationGenerator\Generator;

class GenerateTest extends \PHPUnit_Framework_TestCase
{
private function generateLocaleFilesFrom(array $arr)
{
$root = sys_get_temp_dir() . '/' . sha1(microtime(true) . mt_rand());

if (!is_dir($root)) {
mkdir($root, 0777, true);
}

foreach ($arr as $key => $val) {

if (!is_dir($root . '/' . $key)) {
mkdir($root . '/' . $key);
}

foreach ($val as $group => $content) {
$outFile = $root . '/'. $key . '/' . $group . '.php';
file_put_contents($outFile, '<?php return ' . var_export($content, true) . ';');
}
}

return $root;
}

private function destroyLocaleFilesFrom(array $arr, $root)
{
foreach ($arr as $key => $val) {

foreach ($val as $group => $content) {
$outFile = $root . '/'. $key . '/' . $group . '.php';
if (file_exists($outFile)) {
unlink($outFile);
}
}

if (is_dir($root . '/' . $key)) {
rmdir($root . '/' . $key);
}

}

if (is_dir($root)) {
rmdir($root);
}
}

function testBasic()
{
$arr = [
'en' => [
'help' => [
'yes' => 'yes',
'no' => 'no',
]
],
'sv' => [
'help' => [
'yes' => 'ja',
'no' => 'nej',
]
]
];

$root = $this->generateLocaleFilesFrom($arr);

$this->assertEquals(
'export default {' . PHP_EOL
. ' "en": {' . PHP_EOL
. ' "help": {' . PHP_EOL
. ' "yes": "yes",' . PHP_EOL
. ' "no": "no"' . PHP_EOL
. ' }' . PHP_EOL
. ' },' . PHP_EOL
. ' "sv": {' . PHP_EOL
. ' "help": {' . PHP_EOL
. ' "yes": "ja",' . PHP_EOL
. ' "no": "nej"' . PHP_EOL
. ' }' . PHP_EOL
. ' }' . PHP_EOL
. '}' . PHP_EOL,
(new Generator)->generateFromPath($root));

$this->destroyLocaleFilesFrom($arr, $root);
}
}

0 comments on commit 16d840d

Please sign in to comment.