CodeGen - Add use statement to extensions DAO files
authorColeman Watts <coleman@civicrm.org>
Fri, 7 Aug 2020 03:59:16 +0000 (23:59 -0400)
committerColeman Watts <coleman@civicrm.org>
Fri, 7 Aug 2020 03:59:16 +0000 (23:59 -0400)
CRM/Core/CodeGen/DAO.php
CRM/Core/CodeGen/Util/Template.php
xml/templates/dao.tpl

index 69cc8d0e479ac4d59b5479f9e01fb027ca1f8111..2bd4e810cef0f483ee86486a5172f950a6f55d6e 100644 (file)
@@ -26,6 +26,8 @@ class CRM_Core_CodeGen_DAO extends CRM_Core_CodeGen_BaseTask {
    */
   private $tsFunctionName;
 
+  private $useHelper = '';
+
   /**
    * CRM_Core_CodeGen_DAO constructor.
    *
@@ -37,6 +39,11 @@ class CRM_Core_CodeGen_DAO extends CRM_Core_CodeGen_BaseTask {
     parent::__construct($config);
     $this->name = $name;
     $this->tsFunctionName = $tsFunctionName;
+    // Cleanup helper class with a use statement
+    if (strpos($tsFunctionName, '::ts')) {
+      $this->tsFunctionName = 'E::ts';
+      $this->useHelper = 'use \\' . explode('::', $tsFunctionName)[0] . ' as E;';
+    }
   }
 
   /**
@@ -78,6 +85,7 @@ class CRM_Core_CodeGen_DAO extends CRM_Core_CodeGen_BaseTask {
     }
     $template->assign('genCodeChecksum', $this->getTableChecksum());
     $template->assign('tsFunctionName', $this->tsFunctionName);
+    $template->assign('useHelper', $this->useHelper);
     $template->run('dao.tpl', $this->getAbsFileName());
   }
 
@@ -98,6 +106,7 @@ class CRM_Core_CodeGen_DAO extends CRM_Core_CodeGen_BaseTask {
       }
       $template->assign('genCodeChecksum', 'NEW');
       $template->assign('tsFunctionName', $this->tsFunctionName);
+      $template->assign('useHelper', $this->useHelper);
       $this->raw = $template->fetch('dao.tpl');
     }
     return $this->raw;
index ee0e09bd4baca3eab3ba6b010b9645a9c7abfab3..5febab786d75beff796a82725c290586d3913dd4 100644 (file)
@@ -78,6 +78,7 @@ class CRM_Core_CodeGen_Util_Template {
         '=> true,' => '=> TRUE,',
         '=> false,' => '=> FALSE,',
         'static ::' => 'static::',
+        'use\\' => 'use \\',
       ];
       $contents = str_replace(array_keys($replacements), array_values($replacements), $contents);
       $contents = preg_replace('#(\s*)\\/\\*\\*#', "\n\$1/**", $contents);
index 3a0c51e328a14abcd315670d808e7bdb143ceb06..d1aaad2ee6c4b0166c0511a604b624838118de02 100644 (file)
@@ -7,7 +7,7 @@
  * {$generated}
  * (GenCodeChecksum:{$genCodeChecksum})
  */
-
+{$useHelper}
 /**
  * Database access object for the {$table.entity} entity.
  */