Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"civicrm/cv-lib": "~0.3.66",
"civicrm/composer-compile-plugin": "~0.18",
"civicrm/composer-downloads-plugin": "~2.1|^3",
"civicrm/php-array-doc": "~1.0.0",
"civicrm/php-array-doc": "~1.0.1 | ~1.1.1",
"symfony/console": "^4|^5",
"symfony/filesystem": "^4|^5",
"symfony/process": "^4|^5",
Expand Down
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 51 additions & 19 deletions src/CRM/CivixBundle/Builder/PhpData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
use CRM\CivixBundle\Builder;
use CRM\CivixBundle\Utils\Files;
use CRM\CivixBundle\Utils\Path;
use PhpArrayDocument\ArrayItemNode;
use PhpArrayDocument\PhpArrayDocument;
use PhpArrayDocument\Printer;
use PhpArrayDocument\ScalarNode;
use Symfony\Component\Console\Output\OutputInterface;

/**
Expand All @@ -34,9 +32,16 @@ class PhpData implements Builder {

/**
* @var string[]
* Ex: ['title', 'description'] means "Translate individual fields named `title` or `description`"
*/
private $keysToTranslate;

/**
* @var string[]
* Ex: ['extra'] means "Skip translation for subtrees named `extra`; e.g. skip `extra.title`".
*/
private $keysToExcludeTs = [];

/**
* @var string
*/
Expand Down Expand Up @@ -96,6 +101,16 @@ public function useTs(array $keysToTranslate) {
$this->keysToTranslate = $keysToTranslate;
}

/**
* Specify fields (and their children) that will not be wrapped in E::ts()
*
* @param array $keysToExclude
* @return void
*/
public function excludeTs(array $keysToExclude) {
$this->keysToExcludeTs = $keysToExclude;
}

/**
* Adds `use Foo_ExtensionUtil as E;` to the top of the file
*
Expand Down Expand Up @@ -147,26 +162,43 @@ public function save(&$ctx, OutputInterface $output) {

$doc->getRoot()->importData($this->data);

foreach ($doc->getRoot()->walkNodes(ArrayItemNode::class) as $arrayItem) {
/**
* @var \PhpArrayDocument\ArrayItemNode $arrayItem
*/
if (in_array($arrayItem->getKey(), $this->keysToTranslate ?: [], true) && $arrayItem->getValue() instanceof ScalarNode) {
// Only use ts if value is not empty
if ($arrayItem->getValue()->getScalar()) {
$arrayItem->getValue()->setFactory($ts);
}
}
if (in_array($arrayItem->getKey(), $this->useCallbacks, true)) {
$arrayItem->getValue()->setDeferred(TRUE);
}
if (in_array($arrayItem->getKey(), $this->literals, true)) {
$arrayItem->getValue()->setFactory('constant');
}
}
$this->applyTranslations($doc->getRoot(), $this->keysToExcludeTs ?: [], $ts);

$content = (new Printer())->print($doc);
file_put_contents($this->path, $content);
}

/**
* Recursively processes nodes and sets factory/deferred flags.
*
* @param \PhpArrayDocument\BaseNode $node
* @param array $excludeKeys
* @param string $ts
* @param bool $isExcluded
* @return void
*/
private function applyTranslations(\PhpArrayDocument\BaseNode $node, array $excludeKeys, string $ts, bool $isExcluded = FALSE) {
if ($node instanceof \PhpArrayDocument\ArrayNode) {
foreach ($node->getItems() as $arrayItem) {
$key = $arrayItem->getKey();
$itemExcluded = $isExcluded || in_array($key, $excludeKeys, TRUE);
$value = $arrayItem->getValue();

if (in_array($key, $this->keysToTranslate ?: [], TRUE) && !$itemExcluded && $value instanceof \PhpArrayDocument\ScalarNode) {
if ($value->getScalar()) {
$value->setFactory($ts);
}
}
if (in_array($key, $this->useCallbacks, TRUE)) {
$value->setDeferred(TRUE);
}
if (in_array($key, $this->literals, TRUE)) {
$value->setFactory('constant');
}

$this->applyTranslations($value, $excludeKeys, $ts, $itemExcluded);
}
}
}

}
133 changes: 133 additions & 0 deletions src/CRM/CivixBundle/Builder/PhpDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace CRM\CivixBundle\Builder;

/**
* @group unit
*/
class PhpDataTest extends \PHPUnit\Framework\TestCase {

private $tempDir;

public function setUp(): void {
parent::setUp();
$this->tempDir = sys_get_temp_dir() . '/civix_test_' . uniqid();
mkdir($this->tempDir);
}

public function tearDown(): void {
parent::tearDown();
if (is_dir($this->tempDir)) {
array_map('unlink', glob("$this->tempDir/*"));
rmdir($this->tempDir);
}
}

public function testExcludeTsWithTopLevelKey() {
$testFile = $this->tempDir . '/test_exclude.php';
$ctx = [];

$builder = new PhpData($testFile);
$builder->useExtensionUtil('TestExt_ExtensionUtil');
$builder->useTs(['title', 'label', 'description']);
$builder->excludeTs(['metadata']);

$builder->set([
'title' => 'Main Title',
'label' => 'Main Label',
'metadata' => [
'title' => 'Metadata Title',
'label' => 'Metadata Label',
'description' => 'Metadata Description',
],
]);

$output = new \Symfony\Component\Console\Output\BufferedOutput();
$builder->save($ctx, $output);

$content = file_get_contents($testFile);

// Main title and label should use E::ts
$this->assertStringContainsString("E::ts('Main Title')", $content);
$this->assertStringContainsString("E::ts('Main Label')", $content);

// Metadata fields should NOT use E::ts
$this->assertStringNotContainsString("E::ts('Metadata Title')", $content);
$this->assertStringNotContainsString("E::ts('Metadata Label')", $content);
$this->assertStringNotContainsString("E::ts('Metadata Description')", $content);

// Metadata fields should be plain strings
$this->assertStringContainsString("'Metadata Title'", $content);
$this->assertStringContainsString("'Metadata Label'", $content);
$this->assertStringContainsString("'Metadata Description'", $content);
}

public function testExcludeTsWithNestedStructure() {
$testFile = $this->tempDir . '/test_nested.php';
$ctx = [];

$builder = new PhpData($testFile);
$builder->useExtensionUtil('TestExt_ExtensionUtil');
$builder->useTs(['title', 'label']);
$builder->excludeTs(['config']);

$builder->set([
'title' => 'Root Title',
'config' => [
'title' => 'Config Title',
'nested' => [
'title' => 'Deep Config Title',
'label' => 'Deep Config Label',
],
],
]);

$output = new \Symfony\Component\Console\Output\BufferedOutput();
$builder->save($ctx, $output);

$content = file_get_contents($testFile);

// Root title should use E::ts
$this->assertStringContainsString("E::ts('Root Title')", $content);

// All config fields (even nested) should NOT use E::ts
$this->assertStringNotContainsString("E::ts('Config Title')", $content);
$this->assertStringNotContainsString("E::ts('Deep Config Title')", $content);
$this->assertStringNotContainsString("E::ts('Deep Config Label')", $content);
}

public function testExcludeTsWithMultipleExcludedKeys() {
$testFile = $this->tempDir . '/test_multiple.php';
$ctx = [];

$builder = new PhpData($testFile);
$builder->useExtensionUtil('TestExt_ExtensionUtil');
$builder->useTs(['title', 'description']);
$builder->excludeTs(['metadata', 'raw_data']);

$builder->set([
'title' => 'Main Title',
'metadata' => [
'title' => 'Meta Title',
],
'raw_data' => [
'description' => 'Raw Description',
],
'description' => 'Main Description',
]);

$output = new \Symfony\Component\Console\Output\BufferedOutput();
$builder->save($ctx, $output);

$content = file_get_contents($testFile);

// Main fields should use E::ts
$this->assertStringContainsString("E::ts('Main Title')", $content);
$this->assertStringContainsString("E::ts('Main Description')", $content);

// Excluded sections should NOT use E::ts
$this->assertStringNotContainsString("E::ts('Meta Title')", $content);
$this->assertStringNotContainsString("E::ts('Raw Description')", $content);
}

}
Loading