CRM_Core_CodeGen_Util_Template - simplify beautification param
authorColeman Watts <coleman@civicrm.org>
Thu, 8 Mar 2018 03:21:43 +0000 (22:21 -0500)
committerdeb.monish <monish.deb@jmaconsulting.biz>
Fri, 13 Apr 2018 12:36:14 +0000 (18:06 +0530)
CRM/Core/CodeGen/Reflection.php
CRM/Core/CodeGen/Util/Template.php
CRM/Core/DAO/AllCoreTables.data.php
xml/templates/listAll.tpl

index 515c5b8928d090a886a275abdcb1ee4d781d334f..6459288e5936187fb11de0fbcfc428b2551dd958 100644 (file)
@@ -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());
index 538b832cded03af423e79d9fe640fef54ace0d18..66efbc3867872c1d67d3f7609db30fad2ab413f8 100644 (file)
@@ -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);
   }
 
   /**
index 865e2aeec34c8194069b21f5e69afe8534689ebe..730aadb7b03f510770cab326808b27b40579fd9e 100644 (file)
@@ -1,31 +1,4 @@
 <?php
-
-/*
- +--------------------------------------------------------------------+
- | CiviCRM version 4.7                                                |
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2018                                |
- +--------------------------------------------------------------------+
- | This file is a part of CiviCRM.                                    |
- |                                                                    |
- | CiviCRM is free software; you can copy, modify, and distribute it  |
- | under the terms of the GNU Affero General Public License           |
- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
- |                                                                    |
- | CiviCRM is distributed in the hope that it will be useful, but     |
- | WITHOUT ANY WARRANTY; without even the implied warranty of         |
- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
- | See the GNU Affero General Public License for more details.        |
- |                                                                    |
- | You should have received a copy of the GNU Affero General Public   |
- | License and the CiviCRM Licensing Exception along                  |
- | with this program; if not, contact CiviCRM LLC                     |
- | at info[AT]civicrm[DOT]org. If you have questions about the        |
- | GNU Affero General Public License or the licensing of CiviCRM,     |
- | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
- +--------------------------------------------------------------------+
- */
-
 // (GenCodeChecksum:IGNORE)
 
 return [
@@ -789,4 +762,4 @@ return [
     'class' => 'CRM_Price_DAO_LineItem',
     'table' => 'civicrm_line_item',
   ],
-];
\ No newline at end of file
+];
index 729efdea1a77953336a7011c85209bf4e5029a76..33fa586a0ebfb607ce821dc9a82f7b565fe6a6ea 100644 (file)
@@ -1,39 +1,12 @@
 <?php
-
-/*
- +--------------------------------------------------------------------+
- | CiviCRM version 4.7                                                |
- +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2018                                |
- +--------------------------------------------------------------------+
- | This file is a part of CiviCRM.                                    |
- |                                                                    |
- | CiviCRM is free software; you can copy, modify, and distribute it  |
- | under the terms of the GNU Affero General Public License           |
- | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
- |                                                                    |
- | CiviCRM is distributed in the hope that it will be useful, but     |
- | WITHOUT ANY WARRANTY; without even the implied warranty of         |
- | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
- | See the GNU Affero General Public License for more details.        |
- |                                                                    |
- | You should have received a copy of the GNU Affero General Public   |
- | License and the CiviCRM Licensing Exception along                  |
- | with this program; if not, contact CiviCRM LLC                     |
- | at info[AT]civicrm[DOT]org. If you have questions about the        |
- | GNU Affero General Public License or the licensing of CiviCRM,     |
- | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
- +--------------------------------------------------------------------+
- */
-
 // (GenCodeChecksum:{$genCodeChecksum})
 
-return array(
+return [
 {foreach from=$tables key=tableName item=table}
-  '{$table.className}' => array(
+  '{$table.className}' => [
     'name' => '{$table.objectName}',
     'class' => '{$table.className}',
     'table' => '{$tableName}',
-  ),
+  ],
 {/foreach}
-);
+];