Skip to content

Commit

Permalink
CRM_Core_Resources_Common - Add skeleton for bundles, 'coreStyles' an…
Browse files Browse the repository at this point in the history
…d 'coreResources'
  • Loading branch information
totten authored and seamuslee001 committed Sep 3, 2020
1 parent 8d2c99c commit b2d8361
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
68 changes: 68 additions & 0 deletions CRM/Core/Resources/Common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
* Define some common, global lists of resources.
*/
class CRM_Core_Resources_Common {

const REGION = 'html-header';

/**
* The 'bundle.coreStyles' service is a collection of resources used on some
* non-Civi pages (wherein Civi may be mixed-in).
*
* @param string $name
* i.e. 'coreStyles'
* @return \CRM_Core_Resources_Bundle
*/
public static function createStyleBundle($name) {
$bundle = new CRM_Core_Resources_Bundle($name);
// TODO
CRM_Utils_Hook::alterBundle($bundle);
self::useRegion($bundle, self::REGION);
return $bundle;
}

/**
* The 'bundle.coreResources' service is a collection of resources
* shared by Civi pages (ie pages where Civi controls rendering).
*
* @param string $name
* i.e. 'coreResources'
* @return \CRM_Core_Resources_Bundle
*/
public static function createFullBundle($name) {
$bundle = new CRM_Core_Resources_Bundle($name);
// TODO
CRM_Utils_Hook::alterBundle($bundle);
self::useRegion($bundle, self::REGION);
return $bundle;
}

/**
* Ensure that all elements of the bundle are in the same region.
*
* @param CRM_Core_Resources_Bundle $bundle
* @param string $region
* @return CRM_Core_Resources_Bundle
*/
protected static function useRegion($bundle, $region) {
$bundle->filter(function ($s) use ($region) {
if ($s['type'] !== 'settings' && !isset($s['region'])) {
$s['region'] = $region;
}
return $s;
});
return $bundle;
}

}
16 changes: 16 additions & 0 deletions CRM/Utils/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,22 @@ public static function links($op, $objectName, &$objectId, &$links, &$mask = NUL
return self::singleton()->invoke(['op', 'objectName', 'objectId', 'links', 'mask', 'values'], $op, $objectName, $objectId, $links, $mask, $values, 'civicrm_links');
}

/**
* Alter the contents of a resource bundle (ie a collection of JS/CSS/etc).
*
* TIP: $bundle->add*() and $bundle->filter() should be useful for
* adding/removing/updating items.
*
* @param CRM_Core_Resources_Bundle $bundle
* @return null
* @see CRM_Core_Resources_CollectionInterface::add()
* @see CRM_Core_Resources_CollectionInterface::filter()
*/
public static function alterBundle($bundle) {
return self::singleton()
->invoke(['bundle'], $bundle, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, self::$_nullObject, 'civicrm_alterBundle');
}

/**
* This hook is invoked during the CiviCRM form preProcess phase.
*
Expand Down
6 changes: 6 additions & 0 deletions Civi/Core/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ public function createContainer() {
[]
))->setPublic(TRUE);

$container->setDefinition('bundle.coreStyles', new Definition('CRM_Core_Resources_Bundle', ['coreStyles']))
->setFactory('CRM_Core_Resources_Common::createStyleBundle');

$container->setDefinition('bundle.coreResources', new Definition('CRM_Core_Resources_Bundle', ['coreResources']))
->setFactory('CRM_Core_Resources_Common::createFullBundle');

$container->setDefinition('pear_mail', new Definition('Mail'))
->setFactory('CRM_Utils_Mail::createMailer');

Expand Down

0 comments on commit b2d8361

Please sign in to comment.