### Development Setup Commands Source: https://github.com/jbzoo/event/blob/master/CLAUDE.md Provides essential commands for setting up and managing the development environment, including dependency installation and updates using Composer. ```bash make update # Install/update dependencies via Composer ``` -------------------------------- ### PHP: Basic Event Triggering Source: https://github.com/jbzoo/event/blob/master/README.md Demonstrates the fundamental usage of the EventManager for registering a listener and triggering an event. This is the simplest way to get started with the library. ```php use JBZoo\Event\EventManager; $eManager = new EventManager(); // Simple $eManager->on('create', function () { echo "Something action"; }); // Just do it! $eManager->trigger('create'); ``` -------------------------------- ### Update Project Dependencies (Bash) Source: https://github.com/jbzoo/event/blob/master/README.md This command is used to update the project's dependencies. It assumes a `Makefile` is present in the project root and executes the `update` target, which typically handles fetching and installing necessary packages using a package manager like Composer. ```bash make update ``` -------------------------------- ### Reporting Commands Source: https://github.com/jbzoo/event/blob/master/CLAUDE.md Commands for generating comprehensive reports on code coverage and analysis, as well as uploading coverage data to external services. ```bash make report-all # Generate coverage and analysis reports make report-coveralls # Upload coverage to Coveralls ``` -------------------------------- ### Individual QA Tool Commands Source: https://github.com/jbzoo/event/blob/master/CLAUDE.md Allows for the execution of specific static analysis and code style checking tools, enabling targeted quality assurance. ```bash make test-phpstan # Static analysis make test-psalm # Psalm analysis make test-phpcs # Code style check make test-phpcsfixer-fix # Auto-fix code style make test-phpmd # Mess detector ``` -------------------------------- ### Run PHPUnit Tests (Bash) Source: https://github.com/jbzoo/event/blob/master/README.md This command executes the project's test suite using PHPUnit. It's a standard command for verifying the functionality and correctness of the code. Running `make test` typically invokes PHPUnit with a predefined configuration. ```bash make test ``` -------------------------------- ### Listen to Multiple Related Events with Wildcards (PHP) Source: https://github.com/jbzoo/event/blob/master/README.md This snippet demonstrates how to use wildcard characters ('*') to listen to multiple related events simultaneously. It shows how to set up listeners for patterns like 'item.*', '*.init', '*.save', and '*.save.after', and then trigger these events to see the listeners in action. This allows for more flexible event handling without needing to register listeners for each specific event individually. ```php $eManager->on('item.*', function () { // item.init // item.save echo "Any actions with item"; }); $eManager->on('*.init', function () { // tag.init // item.init echo "Init any entity"; }); $eManager->on('*.save', function () { // tag.save // item.save echo "Saving any entity in system"; }); $eManager->on('*.save.after', function () { // tag.save.after // item.save.after echo "Any entity on after save"; }); $eManager->trigger('tag.init'); $eManager->trigger('tag.save.before'); $eManager->trigger('tag.save'); $eManager->trigger('tag.save.after'); $eManager->trigger('item.init'); $eManager->trigger('item.save.before'); $eManager->trigger('item.save'); $eManager->trigger('item.save.after'); ``` -------------------------------- ### Run All Linters (Bash) Source: https://github.com/jbzoo/event/blob/master/README.md This command is used to execute all configured linters for the project. Linting helps identify potential errors, style issues, and enforce coding standards across the codebase. Running this command ensures the code adheres to the project's quality guidelines. ```bash make codestyle ``` -------------------------------- ### Testing Commands for JBZoo Event Source: https://github.com/jbzoo/event/blob/master/CLAUDE.md Includes commands for running various testing suites and quality assurance tools to ensure code integrity and adherence to standards. ```bash make test # Run PHPUnit tests make test-all # Run all tests and code style checks make codestyle # Run linters (PHPStan, Psalm, PHP-CS-Fixer, etc.) ``` -------------------------------- ### Run All Tests and Code Style Checks (Bash) Source: https://github.com/jbzoo/event/blob/master/README.md This command runs the full test suite along with code style checks. It's a comprehensive command designed to ensure both the functional correctness and the adherence to coding standards of the project. It likely combines the execution of PHPUnit tests with static analysis tools. ```bash make test-all ``` -------------------------------- ### PHP: Passing Arguments by Reference for Communication Source: https://github.com/jbzoo/event/blob/master/README.md Explains how to use argument by reference to enable communication between listeners and back to the event emitter. This is useful when listeners need to modify or return data that the original caller can access. ```php use JBZoo\Event\EventManager; $eManager = new EventManager(); $entityId = 5; $warnings = []; $eManager->on('create', function ($entityId, &$warnings) { echo "An entity with id ", $entityId, " just got created.\n"; $warnings[] = "Something bad may or may not have happened.\n"; }); $eManager->trigger('create', [$entityId, &$warnings]); // $warnings will now contain the message added by the listener ``` -------------------------------- ### Set Up One-Time Event Listeners (PHP) Source: https://github.com/jbzoo/event/blob/master/README.md This code illustrates how to create event listeners that execute only once. The `once()` method registers a callback that will be triggered the first time the specified event occurs. After the first execution, the listener is automatically removed, preventing it from running again on subsequent event triggers. This is useful for initialization tasks or actions that should only happen a single time during the application's lifecycle. ```php $eManager->once('app.init', function () { echo "This will only run once, then auto-remove itself"; }); $eManager->trigger('app.init'); // Executes $eManager->trigger('app.init'); // Does nothing - listener was removed ``` -------------------------------- ### PHP EventManager Class Definition Source: https://github.com/jbzoo/event/blob/master/CLAUDE.md Illustrates the core EventManager class in PHP, outlining its capabilities for event subscription, priority management, and namespace-based matching. ```php on('create', function(){ /* ... */ }); // Custom function (closure) $eManager->on('create', 'myFunction'); // Custom function name $eManager->on('create', ['myClass', 'myMethod']); // Static function (assuming myClass exists) $eManager->on('create', [$object, 'Method']); // Method of instance ``` -------------------------------- ### PHP: Event Propagation Control with ExceptionStop Source: https://github.com/jbzoo/event/blob/master/README.md Demonstrates how to stop the propagation of an event chain. By throwing an `ExceptionStop` from a listener, subsequent listeners for the same event will not be executed. The `trigger` method returns the count of executed listeners. ```php use JBZoo\Event\EventManager; use JBZoo\Event\ExceptionStop; $eManager = new EventManager(); $eManager->on('create', function () { throw new ExceptionStop('Some reason'); // Special exception for JBZoo/Event }); // This listener will not be executed if the above one throws ExceptionStop $eManager->on('create', function () { echo "This will not be output."; }); $eManager->trigger('create'); // Returns count of executed listeners (in this case, 1) ``` -------------------------------- ### PHP: Priority-Based Event Handling Source: https://github.com/jbzoo/event/blob/master/README.md Illustrates how to control the execution order of event listeners using different priority levels. Listeners with higher priorities execute earlier. Supports predefined constants like EventManager::HIGH and custom numeric priorities. ```php use JBZoo\Event\EventManager; $eManager = new EventManager(); // Run it first $eManager->on('create', function () { echo "Something high priority action"; }, EventManager::HIGH); // Run it latest $eManager->on('create', function () { echo "Something another action"; }, EventManager::LOW); // Custom index $eManager->on('create', function () { echo "Something action"; }, 42); // Don't care... $eManager->on('create', function () { echo "Something action"; }); ``` -------------------------------- ### PHP: Passing Arguments to Event Listeners Source: https://github.com/jbzoo/event/blob/master/README.md Shows how to pass data to event listeners as function arguments. This allows listeners to receive context or data related to the event. Arguments are passed as an array to the `trigger` method. ```php use JBZoo\Event\EventManager; $eManager = new EventManager(); $eManager->on('create', function ($entityId) { echo "An entity with id ", $entityId, " just got created.\n"; }); $entityId = 5; $eManager->trigger('create', [$entityId]); ``` -------------------------------- ### Advanced Event Manager Usage (PHP) Source: https://github.com/jbzoo/event/blob/master/README.md This snippet covers advanced functionalities of the EventManager, including setting a default global manager, retrieving summary information about registered events, and methods for removing listeners. It demonstrates how to remove specific listeners using a callback reference, remove all listeners for a particular event, and how to clear all listeners from the manager entirely. These features provide fine-grained control over event subscriptions. ```php use JBZoo\Event\EventManager; // Create a global event manager EventManager::setDefault(new EventManager()); $globalManager = EventManager::getDefault(); // Get summary of registered events $summary = $eManager->getSummeryInfo(); // Returns: ['user.create' => 3, 'user.update' => 1, ...] // Remove specific listeners $callback = function() { echo "test"; }; $eManager->on('test', $callback); $eManager->removeListener('test', $callback); // Remove all listeners for an event $eManager->removeListeners('test'); // Remove ALL listeners $eManager->removeListeners(); ``` -------------------------------- ### PHP Exception Classes Source: https://github.com/jbzoo/event/blob/master/CLAUDE.md Defines the custom exception classes used within the JBZoo Event library, including a base exception and a specific exception for halting event propagation. ```php