Skip to content

Commit

Permalink
Merge pull request #342 from colemanw/convertXml
Browse files Browse the repository at this point in the history
Add convert-entity command
  • Loading branch information
totten authored Apr 19, 2024
2 parents b912bae + feb5444 commit add8fa0
Show file tree
Hide file tree
Showing 3 changed files with 419 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/CRM/CivixBundle/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function createCommands($context = 'default') {
$commands[] = new Command\ConfigSetCommand();
$commands[] = new Command\InitCommand();
$commands[] = new Command\MixinCommand();
$commands[] = new Command\ConvertEntityCommand();
$commands[] = new Command\PingCommand();
$commands[] = new Command\TestRunCommand();
$commands[] = new Command\UpgradeCommand();
Expand Down
50 changes: 44 additions & 6 deletions src/CRM/CivixBundle/Builder/PhpData.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ class PhpData implements Builder {
*/
private $extensionUtil;

private $literals = [];

private $useCallbacks = [];

public function __construct($path, $header = NULL) {
$this->path = $path;
$this->header = $header;
Expand Down Expand Up @@ -99,6 +103,26 @@ public function useExtensionUtil(string $extensionUtilClass) {
$this->extensionUtil = $extensionUtilClass;
}

/**
* Specify which items should be unwrapped and used literally
*
* @param string $literals
* @return void
*/
public function setLiterals(array $literals) {
$this->literals = $literals;
}

/**
* Specify which items should be wrapped in an anonymous function
*
* @param string $callbacks
* @return void
*/
public function setCallbacks(array $callbacks) {
$this->useCallbacks = $callbacks;
}

/**
* Write the xml document
*/
Expand All @@ -114,13 +138,25 @@ public function save(&$ctx, OutputInterface $output) {
$content .= $this->header;
}
$content .= "\nreturn ";
$data = $this->reduceIndentation(VarExporter::export($this->data));
$data = $this->ucConstants($data);
$content .= $this->varExport($this->data);
$content .= ";\n";
file_put_contents($this->path, $content);
}

private function varExport($values) {
$output = VarExporter::export($values);
$output = $this->reduceIndentation($output);
$output = $this->ucConstants($output);
if ($this->keysToTranslate) {
$data = $this->translateStrings($data, $this->keysToTranslate);
$output = $this->translateStrings($output, $this->keysToTranslate);
}
$content .= "$data;\n";
file_put_contents($this->path, $content);
foreach ($this->useCallbacks as $key) {
$output = str_replace(" '$key' => ", " '$key' => fn() => ", $output);
}
foreach ($this->literals as $key) {
$output = preg_replace("/ '$key' => '(.*)',/", " '$key' => \$1,", $output);
}
return $output;
}

/**
Expand Down Expand Up @@ -151,8 +187,10 @@ private function ucConstants(string $data): string {
* Wrap strings in E::ts()
*/
private function translateStrings(string $data, array $keysToTranslate): string {
$ts = ($this->extensionUtil) ? 'E::ts' : 'ts';

$keys = implode('|', array_unique($keysToTranslate));
$data = preg_replace("/'($keys)' => ('[^']+'),/", "'\$1' => E::ts(\$2),", $data);
$data = preg_replace("/'($keys)' => ('[^']+'),/", "'\$1' => $ts(\$2),", $data);
return $data;
}

Expand Down
Loading

0 comments on commit add8fa0

Please sign in to comment.