X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=CRM%2FCore%2FResources.php;h=77467b61203eeba218c6dc50c47bb17eaaec6e1a;hb=d31fb4e3103752d7a0d72f453644de99f726f8da;hp=0fdc57cf21ded218925ae180b11156b934f0e5e8;hpb=c5609714eeed7825927933f93f8fd4e907a2dd58;p=civicrm-core.git
diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php
index 0fdc57cf21..77467b6120 100644
--- a/CRM/Core/Resources.php
+++ b/CRM/Core/Resources.php
@@ -24,10 +24,12 @@ use Civi\Core\Event\GenericHookEvent;
* @package CRM
* @copyright CiviCRM LLC https://civicrm.org/licensing
*/
-class CRM_Core_Resources {
+class CRM_Core_Resources implements CRM_Core_Resources_CollectionAdderInterface {
const DEFAULT_WEIGHT = 0;
const DEFAULT_REGION = 'page-footer';
+ use CRM_Core_Resources_CollectionAdderTrait;
+
/**
* We don't have a container or dependency-injection, so use singleton instead
*
@@ -46,18 +48,13 @@ class CRM_Core_Resources {
private $strings = NULL;
/**
- * Settings in free-form data tree.
+ * Any bundles that have been added.
*
- * @var array
- */
- protected $settings = [];
-
- /**
- * Setting factories.
+ * Format is ($bundleName => bool).
*
- * @var callable[]
+ * @var array
*/
- protected $settingsFactories = [];
+ protected $addedBundles = [];
/**
* Added core resources.
@@ -68,15 +65,6 @@ class CRM_Core_Resources {
*/
protected $addedCoreResources = [];
- /**
- * Added core styles.
- *
- * Format is ($regionName => bool).
- *
- * @var array
- */
- protected $addedCoreStyles = [];
-
/**
* Added settings.
*
@@ -135,13 +123,13 @@ class CRM_Core_Resources {
*
* @param CRM_Extension_Mapper $extMapper
* Map extension names to their base path or URLs.
- * @param CRM_Utils_Cache_Interface $cache
+ * @param CRM_Core_Resources_Strings $strings
* JS-localization cache.
* @param string|null $cacheCodeKey Random code to append to resource URLs; changing the code forces clients to reload resources
*/
- public function __construct($extMapper, $cache, $cacheCodeKey = NULL) {
+ public function __construct($extMapper, $strings, $cacheCodeKey = NULL) {
$this->extMapper = $extMapper;
- $this->strings = new CRM_Core_Resources_Strings($cache);
+ $this->strings = $strings;
$this->cacheCodeKey = $cacheCodeKey;
if ($cacheCodeKey !== NULL) {
$this->cacheCode = Civi::settings()->get($cacheCodeKey);
@@ -154,329 +142,105 @@ class CRM_Core_Resources {
}
/**
- * Export permission data to the client to enable smarter GUIs.
- *
- * Note: Application security stems from the server's enforcement
- * of the security logic (e.g. in the API permissions). There's no way
- * the client can use this info to make the app more secure; however,
- * it can produce a better-tuned (non-broken) UI.
+ * Add an item to the collection.
*
- * @param array $permNames
- * List of permission names to check/export.
- * @return CRM_Core_Resources
+ * @param array $snippet
+ * @return array
+ * The full/computed snippet (with defaults applied).
+ * @see CRM_Core_Resources_CollectionInterface::add()
*/
- public function addPermissions($permNames) {
- $permNames = (array) $permNames;
- $perms = [];
- foreach ($permNames as $permName) {
- $perms[$permName] = CRM_Core_Permission::check($permName);
+ public function add($snippet) {
+ if (!isset($snippet['region'])) {
+ $snippet['region'] = self::DEFAULT_REGION;
}
- return $this->addSetting([
- 'permissions' => $perms,
- ]);
- }
-
- /**
- * Add a JavaScript file to the current page using \n", $js);
- }
+ $this->addedBundles[$bundle->name] = TRUE;
- /**
- * Add translated string to the js CRM object.
- * It can then be retrived from the client-side ts() function
- * Variable substitutions can happen from client-side
- *
- * Note: this function rarely needs to be called directly and is mostly for internal use.
- * See CRM_Core_Resources::addScriptFile which automatically adds translated strings from js files
- *
- * Simple example:
- * // From php:
- * CRM_Core_Resources::singleton()->addString('Hello');
- * // The string is now available to javascript code i.e.
- * ts('Hello');
- *
- * Example with client-side substitutions:
- * // From php:
- * CRM_Core_Resources::singleton()->addString('Your %1 has been %2');
- * // ts() in javascript works the same as in php, for example:
- * ts('Your %1 has been %2', {1: objectName, 2: actionTaken});
- *
- * NOTE: This function does not work with server-side substitutions
- * (as this might result in collisions and unwanted variable injections)
- * Instead, use code like:
- * CRM_Core_Resources::singleton()->addSetting(array('myNamespace' => array('myString' => ts('Your %1 has been %2', array(subs)))));
- * And from javascript access it at CRM.myNamespace.myString
- *
- * @param string|array $text
- * @param string|null $domain
- * @return CRM_Core_Resources
- */
- public function addString($text, $domain = 'civicrm') {
- foreach ((array) $text as $str) {
- $translated = ts($str, [
- 'domain' => ($domain == 'civicrm') ? NULL : [$domain, NULL],
- 'raw' => TRUE,
- ]);
-
- // We only need to push this string to client if the translation
- // is actually different from the original
- if ($translated != $str) {
- $bucket = $domain == 'civicrm' ? 'strings' : 'strings::' . $domain;
- $this->addSetting([
- $bucket => [$str => $translated],
- ]);
+ // Ensure that every asset has a region.
+ $bundle->filter(function($snippet) {
+ if (empty($snippet['region'])) {
+ $snippet['region'] = isset($snippet['settings'])
+ ? $this->getSettingRegion()->_name
+ : self::DEFAULT_REGION;
}
- }
- return $this;
- }
+ return $snippet;
+ });
- /**
- * Add a CSS file to the current page using .
- *
- * @param string $ext
- * extension name; use 'civicrm' for core.
- * @param string $file
- * file path -- relative to the extension base dir.
- * @param int $weight
- * relative weight within a given region.
- * @param string $region
- * location within the file; 'html-header', 'page-header', 'page-footer'.
- * @return CRM_Core_Resources
- */
- public function addStyleFile($ext, $file, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
- /** @var Civi\Core\Themes $theme */
- $theme = Civi::service('themes');
- foreach ($theme->resolveUrls($theme->getActiveThemeKey(), $ext, $file) as $url) {
- $this->addStyleUrl($url, $weight, $region);
+ $byRegion = CRM_Utils_Array::index(['region', 'name'], $bundle->getAll());
+ foreach ($byRegion as $regionName => $snippets) {
+ CRM_Core_Region::instance($regionName)->merge($snippets);
}
return $this;
}
/**
- * Add a CSS file to the current page using .
- *
- * @param string $url
- * @param int $weight
- * relative weight within a given region.
- * @param string $region
- * location within the file; 'html-header', 'page-header', 'page-footer'.
- * @return CRM_Core_Resources
- */
- public function addStyleUrl($url, $weight = self::DEFAULT_WEIGHT, $region = self::DEFAULT_REGION) {
- CRM_Core_Region::instance($region)->add([
- 'name' => $url,
- 'type' => 'styleUrl',
- 'styleUrl' => $url,
- 'weight' => $weight,
- 'region' => $region,
- ]);
- return $this;
- }
-
- /**
- * Add a CSS content to the current page using