CRM-20600 - Civi\Angular - Generate modules via AssetBuilder
authorTim Otten <totten@civicrm.org>
Fri, 31 Mar 2017 07:36:35 +0000 (00:36 -0700)
committerTim Otten <totten@civicrm.org>
Sat, 17 Jun 2017 02:03:47 +0000 (19:03 -0700)
In the typical use-case, AssetBuilder can avoid an extra CMS+Civi bootstrap
along with any build steps.

Civi/Angular/Page/Main.php
Civi/Angular/Page/Modules.php
Civi/Core/Container.php
js/angular-crmResource/all.js

index 2f945ebae7991b8562dfd726b0ba0af961df0607..81b5536ecb25639696bd23a5b767470fc7b0bef8 100644 (file)
@@ -81,6 +81,7 @@ class Main extends \CRM_Core_Page {
         'angular' => array(
           'modules' => array_merge(array('ngRoute'), array_keys($modules)),
           'cacheCode' => $page->res->getCacheCode(),
+          'bundleUrl' => \Civi::service('asset_builder')->getUrl('angular-modules.json'),
         ),
       ));
     });
@@ -104,11 +105,13 @@ class Main extends \CRM_Core_Page {
     else {
       // Note: addScriptUrl() bypasses the normal string-localization of addScriptFile(),
       // but that's OK because all Angular strings (JS+HTML) will load via crmResource.
-      $aggScriptUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=js&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE);
+      // $aggScriptUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=js&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE);
+      $aggScriptUrl = \Civi::service('asset_builder')->getUrl('angular-modules.js');
       $this->res->addScriptUrl($aggScriptUrl, 120, $this->region);
 
       // FIXME: The following CSS aggregator doesn't currently handle path-adjustments - which can break icons.
       //$aggStyleUrl = \CRM_Utils_System::url('civicrm/ajax/angular-modules', 'format=css&r=' . $page->res->getCacheCode(), FALSE, NULL, FALSE);
+      //$aggStyleUrl = \Civi::service('asset_builder')->getUrl('angular-modules.css');
       //$this->res->addStyleUrl($aggStyleUrl, 120, $this->region);
 
       foreach ($this->angular->getResources(array_keys($modules), 'css', 'cacheUrl') as $url) {
index 2fc3d86368ee3f01ae0a743db81dc84932f0e80f..663a8f6ea149fc0d70e05697189c243f36ad09bc 100644 (file)
@@ -20,7 +20,10 @@ namespace Civi\Angular\Page;
 class Modules extends \CRM_Core_Page {
 
   /**
-   * See class description.
+   * Generate asset content (when accessed via older, custom
+   * "civicrm/ajax/anulgar-modules" route).
+   *
+   * @deprecated
    */
   public function run() {
     /**
@@ -59,6 +62,40 @@ class Modules extends \CRM_Core_Page {
     \CRM_Utils_System::civiExit();
   }
 
+  /**
+   * Generate asset content (when accessed via AssetBuilder).
+   *
+   * @param \Civi\Core\Event\GenericHookEvent $event
+   * @see CRM_Utils_hook::buildAsset()
+   * @see \Civi\Core\AssetBuilder
+   */
+  public static function buildAngularModules($event) {
+    $page = new Modules();
+    $angular = \Civi::service('angular');
+
+    switch ($event->asset) {
+      case 'angular-modules.json':
+        $moduleNames = $page->parseModuleNames(\CRM_Utils_Array::value('modules', $event->params), $angular);
+        $event->mimeType = 'application/json';
+        $event->content = json_encode($page->getMetadata($moduleNames, $angular));
+        break;
+
+      case 'angular-modules.js':
+        $moduleNames = $page->parseModuleNames(\CRM_Utils_Array::value('modules', $event->params), $angular);
+        $event->mimeType = 'application/javascript';
+        $event->content = $page->digestJs($angular->getResources($moduleNames, 'js', 'path'));
+        break;
+
+      case 'angular-modules.css':
+        $moduleNames = $page->parseModuleNames(\CRM_Utils_Array::value('modules', $event->params), $angular);
+        $event->mimeType = 'text/css';
+        $event->content = \CRM_Utils_File::concat($angular->getResources($moduleNames, 'css', 'path'), "\n");
+
+      default:
+        // Not our problem.
+    }
+  }
+
   /**
    * @param array $files
    *   File paths.
index 24bf7e0f8ca38529fc8f280d1bf6d313fb66da62..efe6a656c5d55531385ac462369881bb49afcd8d 100644 (file)
@@ -259,6 +259,7 @@ class Container {
     // TODO We need a better code-convention for metadata about non-hook events.
     $dispatcher->addListener('hook_civicrm_eventDefs', array('\Civi\API\Events', 'hookEventDefs'));
     $dispatcher->addListener('hook_civicrm_eventDefs', array('\Civi\Core\Event\SystemInstallEvent', 'hookEventDefs'));
+    $dispatcher->addListener('hook_civicrm_buildAsset', array('\Civi\Angular\Page\Modules', 'buildAngularModules'));
     $dispatcher->addListener('civi.dao.postInsert', array('\CRM_Core_BAO_RecurringEntity', 'triggerInsert'));
     $dispatcher->addListener('civi.dao.postUpdate', array('\CRM_Core_BAO_RecurringEntity', 'triggerUpdate'));
     $dispatcher->addListener('civi.dao.postDelete', array('\CRM_Core_BAO_RecurringEntity', 'triggerDelete'));
index 7c3632fc2a744184848197d3e98fbe5954c827a1..e8eca721b59a5eab9a25ad4323737d9e7e9fc97d 100644 (file)
@@ -32,7 +32,7 @@
       });
     };
 
-    var moduleUrl = CRM.url('civicrm/ajax/angular-modules', {l: CRM.config.lcMessages, r: CRM.angular.cacheCode});
+    var moduleUrl = CRM.angular.bundleUrl;
     $http.get(moduleUrl)
       .success(function httpSuccess(data) {
         templates = [];