-
Notifications
You must be signed in to change notification settings - Fork 4
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
[Feature Request]: Fire an event when building with a generic hook name to be able to target it easily #47
Comments
While I maybe agree with having a "generic" hookName to hook into all Packages, this could also introduce some side effects like hooking into a package which shouldn't be extended. So instead of doing: add_action(
MyPackage\plugin()->hookName(Package::ACTION_INIT),
static function(Package $plugin): void {
$plugin->addModule( ... );
}
); you simply could loop over your packages by doing: $packagesToExtend = [
MyPackage\plugin()
];
foreach($packagesToExtend as $package) {
add_action(
$package->hookName(Package::ACTION_INIT),
static function(Package $plugin): void {
$plugin->addModule( ... );
}
);
} |
The problem here is that you don't know whether |
I think the hook would be good to have. Looping packages, besides using Moreover, one issue of calling To act as soon as the package is ready without side effects is impossible right now, because you need a package instance to add hooks, and obtaining that instance might have side effects. To help avoid possible unwanted effects, we could fire the hook similarly to: do_action(
'inpsyde.modularity.init',
$package->properties()->basename(),
$package
); This way, whoever listens to the event could easily filter on the basename. For example: add_action(
'inpsyde.modularity.init',
static function ($basename, Package $package): void {
if (str_start_with($basename, 'acme-')) {
$package->addModule(/* ...*/);
}
},
10,
2
); This could also be used to build a flexible allow-list that you don't need to update often: const PACKAGES_TO_EXTEND = [
'^(inp)?syde-.+$',
'^my-client-.+$',
'something-else',
];
add_action(
'inpsyde.modularity.init',
static function ($basename, Package $package): void {
foreach (PACKAGES_TO_EXTEND as $package) {
if (preg_match("#$package#", $basename)) {
$package->addModule(/* ...*/);
return;
}
}
},
10,
2
); If a developer decides to do something with a |
@Chrico it seems you agreed on having this hook. Let's agree on :
I was thinking of adding this constant to the class: public const ACTION_MODULARITY_INIT = self::HOOK_PREFIX . self::ACTION_INIT; The constant evaluates to And then do: do_action(
self::ACTION_MODULARITY_INIT,
$this->properties()->baseName(),
$this
); Immediately after the current package-specific "init" hook. That said I was also thinking that we don't have any hook after the package is "built". In 1.7.0 we introduced The first point where it is safe to call If you call public const ACTION_INITIALIZED = 'initialized'; And then, after the status is "initialized" do something like: do_action(
$this->hookName(self::ACTION_INITIALIZED),
$this
); I don't think we need a "generic" hook also there. But having a package-specific hook the first moment it is safe to call What you (and everyone) think about this? If you agree, can we have a single PR that address both these two new hooks? |
I totally agree with the generic
I'm wondering what the use case for accessing the container via this hook at this time could be. Maybe we should revisit the workflow of registration, executing, resolving, extending and when which hook makes sense. v1) We probably could even use the const STATUS_TO_ACTION = [
self::STATUS_READY => self::ACTION_READY,
self::STATUS_INITIALIZED => self::ACTION_INITIALIZED,
];
private function progress(int $status): void
{
$action = self::STATUS_TO_ACTION[$status] ?? null;
if($action !== null && !did_action($action)){
do_action(
$action,
$this->properties()->baseName(),
$this
);
}
$this->status = $status;
} v2) Or we also could implement a similar logic as WordPress has with post_status changes where they have an action which contains "from-to":
v3) Or - probably not that performant - we implement a do_action( self::HOOK_PREFIX . '.progress', $oldStatus, $newStatus, $baseName, $this); |
@Chrico I like the idea of emitting a hook on Statuses help us determine internally what happened and what is happening, while hooks open the class to the extern and we don't need to expose the class externally at any status change. For the extern, we only need to expose:
So we have just 3 hooks but more statuses. I think we could go with:
This could be done in a 100% backward compatible way. So we would have statuses like: public const STATUS_IDLE = 2;
public const STATUS_INIT = 3;
public const STATUS_INITIALIZED = 4;
public const STATUS_BOOTING = 5;
public const STATUS_READY = 7;
public const STATUS_DONE = 8; And a "map" like: private const STATUSES_ACTIONS_MAP = [
self::STATUS_INIT => self::ACTION_INIT,
self::STATUS_INITIALIZED => self::ACTION_INITIALIZED,
self::STATUS_READY => self::ACTION_READY,
]; There's IMO no need for an action for If you agree with the above I can send a PR. |
See #47 **To be merged after #49** - Added the generic, statically-named `ACTION_MODULARITY_INIT` action hook - Refactor hooks triggering, using a map between statuses ans actions, introducing two new statuses: `STATUS_INIT` and `SATUS_DONE`(which replaces the now deprecated `STATUS_BOOTED`) - Deprecate `STATUS_MODULES_ADDED` (`STATUS_BOOTING` was already an alias) - Refactor hook triggering for failed connection, moving it to a separate method. Behavior change: connecting an alreayd connected package still fires a failed action hook but does not throw anymore. - Do not use `PackageProxyContainer` when the package to connect is initialized, considering its container is already available - Allow for multiple consecutive calls to `Package::boot()` and `Package::build()` avoiding throwing unnecessary exceptions - Add extensive inline documentation to explain the different parts of the bootstrapping flow - Add a large amount of tests to cover both existing but untested scenarios as well as the new/updated behaviors - Rework the "Package" and "Application flow" documentation chapters to make them more easily consumable, better account for latest additions (not limited to the changes in this commit).
See #47 - Added the generic, statically-named `ACTION_MODULARITY_INIT` action hook - Refactor hooks triggering, using a map between statuses ans actions, introducing two new statuses: `STATUS_INIT` and `SATUS_DONE`(which replaces the now deprecated `STATUS_BOOTED`) - Deprecate `STATUS_MODULES_ADDED` (`STATUS_BOOTING` was already an alias) - Refactor hook triggering for failed connection, moving it to a separate method. Behavior change: connecting an alreayd connected package still fires a failed action hook but does not throw anymore. - Do not use `PackageProxyContainer` when the package to connect is initialized, considering its container is already available - Allow for multiple consecutive calls to `Package::boot()` and `Package::build()` avoiding throwing unnecessary exceptions - Add extensive inline documentation to explain the different parts of the bootstrapping flow - Add a large amount of tests to cover both existing but untested scenarios as well as the new/updated behaviors - Rework the "Package" and "Application flow" documentation chapters to make them more easily consumable, better account for latest additions (not limited to the changes in this commit).
…ooks (#52) See #47 - Added the generic, statically-named `ACTION_MODULARITY_INIT` action hook - Refactor hooks triggering, using a map between statuses ans actions, introducing two new statuses: `STATUS_INIT` and `SATUS_DONE`(which replaces the now deprecated `STATUS_BOOTED`) - Deprecate `STATUS_MODULES_ADDED` (`STATUS_BOOTING` was already an alias) - Refactor hook triggering for failed connection, moving it to a separate method. Behavior change: connecting an alreayd connected package still fires a failed action hook but does not throw anymore. - Do not use `PackageProxyContainer` when the package to connect is initialized, considering its container is already available - Allow for multiple consecutive calls to `Package::boot()` and `Package::build()` avoiding throwing unnecessary exceptions - Add extensive inline documentation to explain the different parts of the bootstrapping flow - Add a large amount of tests to cover both existing but untested scenarios as well as the new/updated behaviors - Rework the "Package" and "Application flow" documentation chapters to make them more easily consumable, better account for latest additions (not limited to the changes in this commit). * Rename actions and statuses for consistency * Add consistency in failure actions hooks (with back-compat alias for Pacakage::ACTION_FAILED_CONNECTION)
Is your feature request related to a problem?
Currently, the only way to add modules from other places is via adding an action to this hook
The hook name is created dynamically based on the package name.
Since #44 was merged it would be very nice to have a way to add a module with an extension to all packages automatically.
So, in a project with many packages we could do:
Describe the desired solution
Maybe the solution is to add a hook.
Describe the alternatives that you have considered
Option 1:
Adding a generic hook
Option 2:
Using current hookName is also an option but you should hook in another hook like
Additional context
No response
Code of Conduct
The text was updated successfully, but these errors were encountered: