Add Event Hook to Plugin.php

VerifiedSafe

Adds a new event hook to src/Plugin.php, registers it in getHooks() and creates the static GenericEvent handler.

Sby Skills Guide Bot
DevelopmentIntermediate
7/22/2026
Claude CodeCursorWindsurfCopilotCodex
#php#event-hooks#plugin-development#symfony-eventdispatcher#unit-testing

Recommended for

Automated summary

Adds a new event hook to src/Plugin.php by registering it in getHooks() and creating the corresponding static GenericEvent handler.

Strengths

  • States the exact signature constraints (public static, single GenericEvent parameter) enforced by existing tests
  • Mandates the category guard and stopPropagation as the final statement in the handler body
  • Documents the self::$module.'.event_name' key convention plus the 'function.requirements' literal-key exception
  • Includes updating tests/PluginTest.php (expected keys and method count) and verifying with composer test

Limitations

  • Highly specific to one PHP plugin using GenericEvent, get_service_define and myadmin_log
  • Explicitly excludes modifying existing handlers and adding inc functions
  • Assumes the GenericEvent import already exists and gives no broader architectural context
When to use it

Use it when you need to wire a new lifecycle event (activate, deactivate, change IP, reactivate) into the plugin.

When not to use it

Do not use it to modify an existing handler or to add a function to the inc files.

Security analysis

Safe
Quality score90/100

The skill provides concrete, narrow instructions for editing a PHP plugin file and its tests. It does not instruct destructive commands, network exfiltration, secret handling, obfuscated payloads, or disabling safety mechanisms.

No concerns found

How this analysis is made

Examples

Log IP change
Add a hook for licenses.change_ip that logs the old and new IP address.
Reactivate reuses activate handler
Add a hook for the reactivate lifecycle event and point it at the existing getActivate handler instead of creating a new method.
Hook calling an inc function
Add a new change_ip hook whose handler calls an inc function, register the requirement in getRequirements(), and update the tests.

name: plugin-hook description: Adds a new event hook to src/Plugin.php — registers in getHooks() and creates the static GenericEvent handler. Use when user says 'add hook', 'handle event', 'new plugin event', or adds a lifecycle action (activate/deactivate/change IP/reactivate). Do NOT use for modifying existing handlers or adding inc functions.

plugin-hook

Critical

  • All handler methods MUST be public static and accept exactly one GenericEvent $event parameter — testHookMethodsArePublicAndStatic and testEventHandlerMethodSignatures will fail otherwise.
  • Always guard the handler body with if ($event['category'] == get_service_define('PARALLELS')) before any logic.
  • Always call $event->stopPropagation() as the last statement inside the category guard.
  • Hook keys use self::$module . '.event_name' (e.g. 'licenses.change_ip') — never hardcode the module string.
  • The one exception is 'function.requirements' which uses a literal key, not the module prefix.
  • Run composer test after every change — a missing method or wrong signature will break testAllHookMethodsExist and testClassMethodCount.

Instructions

  1. Decide the event name and handler name. Event name is snake_case (e.g. change_ip). Handler is PascalCase prefixed with get (e.g. getChangeIp). Verify no existing entry in getHooks() already covers this event.

  2. Register the hook in getHooks() inside src/Plugin.php. Add one line to the returned array:

    self::$module.'.change_ip' => [__CLASS__, 'getChangeIp'],
    

    If the new event should share an existing handler (like reactivate → getActivate), point to the existing method name instead of creating a new one. Verify the array key is unique before proceeding.

  3. Add the handler method to src/Plugin.php after the last existing handler, before the closing }:

    /**
     * @param \Symfony\Component\EventDispatcher\GenericEvent $event
     */
    public static function getChangeIp(GenericEvent $event)
    {
        if ($event['category'] == get_service_define('PARALLELS')) {
            $serviceClass = $event->getSubject();
            myadmin_log(self::$module, 'info', 'Parallels <Action Description>', __LINE__, __FILE__, self::$module, $serviceClass->getId());
            // ... call function_requirements() then the inc function ...
            $event->stopPropagation();
        }
    }
    

    The use Symfony\Component\EventDispatcher\GenericEvent; import is already present at the top — do not add it again.

  4. If the handler calls an inc function, add function_requirements('your_function') before the call and register the function path in getRequirements():

    $loader->add_requirement('your_function', '/../src/parallels.inc.php');
    

    Verify the function exists in src/parallels.inc.php before registering.

  5. Update tests/PluginTest.php — add the new hook key to the $expectedKeys array in testGetHooksContainsExpectedKeys() and bump the count in testClassMethodCount() if a new method was added. Run composer test to confirm all tests pass.

Examples

User says: "Add a hook for licenses.change_ip that logs the old and new IP"

Actions taken:

getHooks() addition in src/Plugin.php:

self::$module.'.change_ip' => [__CLASS__, 'getChangeIp'],

New handler method in src/Plugin.php:

public static function getChangeIp(GenericEvent $event)
{
    if ($event['category'] == get_service_define('PARALLELS')) {
        $serviceClass = $event->getSubject();
        myadmin_log(self::$module, 'info', 'IP Change - (OLD:'.$serviceClass->getIp().") (NEW:{$event['newip']})", __LINE__, __FILE__, self::$module, $serviceClass->getId());
        // perform IP change logic here
        $event['status'] = 'ok';
        $event['status_text'] = 'The IP Address has been changed.';
        $event->stopPropagation();
    }
}

Result: composer test passes; testGetHooksContainsExpectedKeys and testAllHookMethodsExist both green.

Common Issues

  • testClassMethodCount fails with "Expected 7, got 8": You added a new handler but did not update the count assertion in tests/PluginTest.php. Change the assertCount(7, ...) to the new total.
  • testAllHookMethodsExist fails: The method name string in getHooks() does not exactly match the declared method name. Check spelling and PascalCase — getChangeIp ≠ getChangeIP.
  • testEventHandlerMethodSignatures fails with "parameter should be GenericEvent": The handler is missing the GenericEvent $event type hint, or the use statement is absent (it lives at the top of src/Plugin.php — do not remove it).
  • stopPropagation not called: Event will continue dispatching to other plugins and produce duplicate side-effects. Always place $event->stopPropagation() as the final statement inside the category guard.
  • function_requirements not registered: If activate_parallels or a new inc function is called but not registered in getRequirements(), it will throw a fatal error at runtime. Verify $loader->add_requirement('fn_name', '...') is present.

From the file to a first result

The download contains exactly the content displayed on this page. This copy may differ from the latest version in the source repository.

View the source and supporting files

This download contains SKILL.md only. Any scripts, references, templates and dependencies are not included. Get the required files from the source, preserving their directory structure, before continuing.

1. Download the file

Download SKILL.md

From the root of a test project, save the file as .skill-downloads/add-event-hook-to-plugin-php-mrwakdo8/SKILL.md, or use the command below. This folder is for inspecting the skill before installation.

Command for macOS/Linux or WSL: requires a POSIX shell and curl. On Windows without WSL, use the download button and create the folder manually.

mkdir -p '.skill-downloads/add-event-hook-to-plugin-php-mrwakdo8' && curl --fail --show-error --location 'https://skills-guides.com/api/skills/add-event-hook-to-plugin-php-mrwakdo8/download' --output '.skill-downloads/add-event-hook-to-plugin-php-mrwakdo8/SKILL.md.part' && mv '.skill-downloads/add-event-hook-to-plugin-php-mrwakdo8/SKILL.md.part' '.skill-downloads/add-event-hook-to-plugin-php-mrwakdo8/SKILL.md'

2. Check prerequisites

Read SKILL.md before using it. If it references scripts or external services, inspect them and prepare the required access in your test environment.

No recognized prerequisites section in this file. This does not mean it is self-contained: libraries, tools and access requirements may be mentioned elsewhere in the content or source repository.

The platforms on this page are catalog indications. The paths below follow tool documentation; they do not prove this skill has been run in those tools. No minimum tool version is confirmed for this listing.

3. Load the skill

After inspection, copy the complete folder into your project using one of the commands below, then open a new tool session. The command stops if a folder with the same name already exists: compare the files before replacing it.

Claude Code
test -f '.skill-downloads/add-event-hook-to-plugin-php-mrwakdo8/SKILL.md' && test -s '.skill-downloads/add-event-hook-to-plugin-php-mrwakdo8/SKILL.md' && mkdir -p '.claude/skills' && test ! -e '.claude/skills/plugin-hook' && cp -R '.skill-downloads/add-event-hook-to-plugin-php-mrwakdo8' '.claude/skills/plugin-hook'

Cursor
test -f '.skill-downloads/add-event-hook-to-plugin-php-mrwakdo8/SKILL.md' && test -s '.skill-downloads/add-event-hook-to-plugin-php-mrwakdo8/SKILL.md' && mkdir -p '.cursor/skills' && test ! -e '.cursor/skills/plugin-hook' && cp -R '.skill-downloads/add-event-hook-to-plugin-php-mrwakdo8' '.cursor/skills/plugin-hook'

Codex
test -f '.skill-downloads/add-event-hook-to-plugin-php-mrwakdo8/SKILL.md' && test -s '.skill-downloads/add-event-hook-to-plugin-php-mrwakdo8/SKILL.md' && mkdir -p '.agents/skills' && test ! -e '.agents/skills/plugin-hook' && cp -R '.skill-downloads/add-event-hook-to-plugin-php-mrwakdo8' '.agents/skills/plugin-hook'

OpenSkills

OpenSkills must already be installed. The local install/read procedure is checked with OpenSkills 1.5.0, which requires Node.js ≥ 20.6.0. Catalog slugs are not resolved automatically.

Official OpenSkills installation
openskills install './.skill-downloads/add-event-hook-to-plugin-php-mrwakdo8'

openskills read 'add-event-hook-to-plugin-php-mrwakdo8'

Reading should display the installed file content and its base directory. It does not run the skill or install its dependencies.

For an agent that reads AGENTS.md, then sync the index. This command changes AGENTS.md: review the diff before using it.

openskills sync

Manual method or another tool: attach SKILL.md to a conversation, or open it in an agent that can read files. Include any required supporting resources. No universal folder is recognized by every tool.

4. Try a first task

Use a short, synthetic input: an API endpoint, a small document or an interface brief. In a new conversation, attach the file or give the agent its path, then adapt this request:

Read .skill-downloads/add-event-hook-to-plugin-php-mrwakdo8/SKILL.md and apply its instructions to this example: [describe a test input]. Identify any missing files, dependencies or access. Produce the result requested by the skill without inventing data or running unreviewed scripts.

Expected result: output matching the skill instructions, or a precise list of missing prerequisites. A read confirmation alone does not validate a first use. If an external resource is unavailable, that path remains untested.

Related skills