From 8fe9ce40b5ae1e5a1ecbbeeea9fd0d070d004473 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 7 Mar 2018 22:21:43 -0500 Subject: [PATCH] CRM_Core_CodeGen_Util_Template - simplify beautification param --- CRM/Core/CodeGen/Reflection.php | 2 +- CRM/Core/CodeGen/Util/Template.php | 60 +++++++++++------------------ CRM/Core/DAO/AllCoreTables.data.php | 29 +------------- xml/templates/listAll.tpl | 35 ++--------------- 4 files changed, 29 insertions(+), 97 deletions(-) diff --git a/CRM/Core/CodeGen/Reflection.php b/CRM/Core/CodeGen/Reflection.php index 515c5b8928..6459288e59 100644 --- a/CRM/Core/CodeGen/Reflection.php +++ b/CRM/Core/CodeGen/Reflection.php @@ -20,7 +20,7 @@ class CRM_Core_CodeGen_Reflection extends CRM_Core_CodeGen_BaseTask { */ public function run() { echo "Generating table list\n"; - $template = new CRM_Core_CodeGen_Util_Template('php', ['beautifier' => FALSE]); + $template = new CRM_Core_CodeGen_Util_Template('php', FALSE); $template->assign('tables', $this->tables); $template->assign('genCodeChecksum', 'IGNORE'); $template->run('listAll.tpl', $this->getAbsFileName()); diff --git a/CRM/Core/CodeGen/Util/Template.php b/CRM/Core/CodeGen/Util/Template.php index 538b832cde..66efbc3867 100644 --- a/CRM/Core/CodeGen/Util/Template.php +++ b/CRM/Core/CodeGen/Util/Template.php @@ -10,28 +10,22 @@ class CRM_Core_CodeGen_Util_Template { protected $beautifier; /** - * @var array - * 'beautifier' => bool - * 'fudging' => bool - * 'arraySyntax' => bool + * @var bool */ - protected $filters; + protected $beautify; /** * @param string $filetype - * @praam array $filters - * 'beautifier' => bool - * 'fudging' => bool - * 'arraySyntax' => bool + * @param bool $beautify */ - public function __construct($filetype, $filters = array()) { + public function __construct($filetype, $beautify = TRUE) { $this->filetype = $filetype; $this->smarty = CRM_Core_CodeGen_Util_Smarty::singleton()->createSmarty(); $this->assign('generated', "DO NOT EDIT. Generated by CRM_Core_CodeGen"); - if ($this->filetype === 'php') { + if ($this->filetype === 'php' && $beautify) { require_once 'PHP/Beautifier.php'; // create an instance $this->beautifier = new PHP_Beautifier(); @@ -40,12 +34,9 @@ class CRM_Core_CodeGen_Util_Template { $this->beautifier->setIndentChar(' '); $this->beautifier->setIndentNumber(2); $this->beautifier->setNewLine("\n"); - - $phpDefaultFilters = ['beautifier' => TRUE, 'fudging' => TRUE, 'arraySyntax' => TRUE]; - $filters = array_merge($phpDefaultFilters, $filters); } - $this->filters = $filters; + $this->beautify = $beautify; } /** @@ -75,34 +66,29 @@ class CRM_Core_CodeGen_Util_Template { public function run($infile, $outpath) { $contents = $this->smarty->fetch($infile); - if ($this->filetype === 'php') { - if ($this->filters['beautifier']) { - $this->beautifier->setInputString($contents); - $this->beautifier->process(); - $contents = $this->beautifier->get(); - } + if ($this->filetype === 'php' && $this->beautify) { + $this->beautifier->setInputString($contents); + $this->beautifier->process(); + $contents = $this->beautifier->get(); // The beautifier isn't as beautiful as one would hope. Here's some extra string fudging. - if ($this->filters['fudging']) { - $replacements = [ - ') ,' => '),', - "\n }\n}\n" => "\n }\n\n}\n", - '=> true,' => '=> TRUE,', - '=> false,' => '=> FALSE,', - ]; - $contents = str_replace(array_keys($replacements), array_values($replacements), $contents); - $contents = preg_replace('#(\s*)\\/\\*\\*#', "\n\$1/**", $contents); - } + $replacements = [ + ') ,' => '),', + "\n }\n}\n" => "\n }\n\n}\n", + '=> true,' => '=> TRUE,', + '=> false,' => '=> FALSE,', + ]; + $contents = str_replace(array_keys($replacements), array_values($replacements), $contents); + $contents = preg_replace('#(\s*)\\/\\*\\*#', "\n\$1/**", $contents); // Convert old array syntax to new square brackets - if ($this->filters['arraySyntax']) { - $contents = CRM_Core_CodeGen_Util_ArraySyntaxConverter::convert($contents); - } - file_put_contents($outpath, $contents); + $contents = CRM_Core_CodeGen_Util_ArraySyntaxConverter::convert($contents); } - else { - file_put_contents($outpath, $contents); + // Ensure file ends with a newline + if (substr($contents, -1) !== "\n") { + $contents .= "\n"; } + file_put_contents($outpath, $contents); } /** diff --git a/CRM/Core/DAO/AllCoreTables.data.php b/CRM/Core/DAO/AllCoreTables.data.php index 865e2aeec3..730aadb7b0 100644 --- a/CRM/Core/DAO/AllCoreTables.data.php +++ b/CRM/Core/DAO/AllCoreTables.data.php @@ -1,31 +1,4 @@ 'CRM_Price_DAO_LineItem', 'table' => 'civicrm_line_item', ], -]; \ No newline at end of file +]; diff --git a/xml/templates/listAll.tpl b/xml/templates/listAll.tpl index 729efdea1a..33fa586a0e 100644 --- a/xml/templates/listAll.tpl +++ b/xml/templates/listAll.tpl @@ -1,39 +1,12 @@ array( + '{$table.className}' => [ 'name' => '{$table.objectName}', 'class' => '{$table.className}', 'table' => '{$tableName}', - ), + ], {/foreach} -); +]; -- 2.25.1