From: Tim Otten Date: Tue, 16 May 2017 05:29:07 +0000 (-0700) Subject: CRM-20600 - Civi\Angular\Manager::getStrings() - Scan raw HTML strings instead of... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=53d0084104da7d8b842e2385a12805e212c1542f;p=civicrm-core.git CRM-20600 - Civi\Angular\Manager::getStrings() - Scan raw HTML strings instead of *.html files Rationale: If the raw HTML can be modified via hook, then the HTML files are not truly representative of the final output. (e.g. a hook might add a new string to some HTML.) Note: This patch means that we no longer benefit from the cache in `CRM_Core_Resources_Strings`. However, the typical user still pulls data from a cache because we're using `AssetBuilder` to manage `angular-modules.json`. Note: `getStrings()` now requires a call to `getPartials()`, which is expected to become more expensive (firing a hook). To avoid building the partials twice, we introduce a private cache. --- diff --git a/Civi/Angular/Manager.php b/Civi/Angular/Manager.php index 8542d32c23..7d46947ef5 100644 --- a/Civi/Angular/Manager.php +++ b/Civi/Angular/Manager.php @@ -30,12 +30,18 @@ class Manager { */ protected $modules = NULL; + /** + * @var \CRM_Utils_Cache_Interface + */ + protected $cache; + /** * @param \CRM_Core_Resources $res * The resource manager. */ - public function __construct($res) { + public function __construct($res, \CRM_Utils_Cache_Interface $cache = NULL) { $this->res = $res; + $this->cache = $cache ? $cache : new \CRM_Utils_Cache_Arraycache(array()); } /** @@ -147,6 +153,11 @@ class Manager { * Invalid partials configuration. */ public function getPartials($name) { + $cacheKey = "angular-partials::$name"; + $cacheValue = $this->cache->get($cacheKey); + if ($cacheValue !== NULL) { + return $cacheValue; + } $module = $this->getModule($name); $result = array(); if (isset($module['partials'])) { @@ -159,6 +170,8 @@ class Manager { } } } + + $this->cache->set($cacheKey, $result); return $result; } @@ -208,19 +221,9 @@ class Manager { $result = array_unique(array_merge($result, $strings)); } } - if (isset($module['partials'])) { - foreach ($module['partials'] as $partialDir) { - $partialDir = $this->res->getPath($module['ext']) . '/' . $partialDir; - $files = \CRM_Utils_File::findFiles($partialDir, '*.html'); - foreach ($files as $file) { - $strings = $this->res->getStrings()->get( - $module['ext'], - $file, - 'text/html' - ); - $result = array_unique(array_merge($result, $strings)); - } - } + $partials = $this->getPartials($name); + foreach ($partials as $partial) { + $result = array_unique(array_merge($result, \CRM_Utils_JS::parseStrings($partial))); } return $result; }