Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(REF) CRM_Core_Resources - Move hook declaration from addCoreResources() to Container.php #14008

Merged
merged 1 commit into from
Apr 9, 2019

Conversation

totten
Copy link
Member

@totten totten commented Apr 9, 2019

Overview

It's easier to declare hook_civicrm_buildAsset listeners at a high-level.

Before

Hook listener declared in-situ (near where it's typically used)

After

Hook listener declared in central way (so it's always defined).

Technical Details

Asset building can use two modes -- production mode writes a static file to disk upfront (when the file is being referenced). Debug mode just generates a URL for a web-service (which in turn dynamically renders the content in a separate page-view).

If the only mode were production mode, then the previous code would be on pretty solid ground. We could even simplify things a lot by changing the AssetBuilder contract to replace the hooks with callbacks, as in:

Civi::service('asset_builder')->getUrl('crm-menu.css', function() {
  return '...the css code...';
});

Why have a hook? Because hooks are generally context-free and always-available. If we use debug-mode (or if we add a feature to warm-up the caches during deployment), then we'll want to fire that hook from a different context (e.g. web-service or CLI), and the hook-listener needs to be available in those other contexts.

It would be nice if we could declare hooks generally without needing to edit the Container.php mega-file (e.g. maybe some kind of annotation). But, for the moment, I think this is the best spot that we have in civicrm-core for ensuring that hook listeners are fully/consistently registered.

…s() to Container.php

tldr: It's easier to declare `hook_civicrm_buildAsset` listeners at a high-level.

Asset building can use two modes -- production mode writes a static file to
disk when it's being reference.  Debug mode just generates a URL for a
web-service (which in turn dynamically renders the content in a separate
page-view).

If the only mode were production mode, then the code would be on pretty
solid ground.  We could even simplify things a lot by changing the
AssetBuilder contract to replace the hooks with callbacks, as in:

```php
Civi::service('asset_builder')->getUrl('crm-menu.css', function() {
  return '...the css code...';
});
```

Why have a hook?  Because hooks are generally context-free and
always-available.  If we use debug-mode (or if we add a feature to warm-up
the caches during deployment), then we'll want to fire that hook from a
different context (e.g.  web-service or CLI), and the hook-listener needs to
be available in those other contexts.

It would be nice if we could declare hooks generally without needing to edit
the `Container.php` mega-file (e.g.  maybe some kind of annotation).  But,
for the moment, I think this is the best spot that we have in `civicrm-core`
for ensuring that hook listeners are fully/consistently registered.
@civibot
Copy link

civibot bot commented Apr 9, 2019

(Standard links)

@civibot civibot bot added the master label Apr 9, 2019
@colemanw
Copy link
Member

colemanw commented Apr 9, 2019

THAT's where it goes! Thanks @totten

@eileenmcnaughton
Copy link
Contributor

unrelated fail

@eileenmcnaughton eileenmcnaughton merged commit 90b5e0c into civicrm:master Apr 9, 2019
@totten totten deleted the master-hook branch April 9, 2019 22:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants