Skip to content

Commit 854c649

Browse files
committed
Initial commit v0.2
0 parents  commit 854c649

23 files changed

Lines changed: 570 additions & 0 deletions

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Composer dependencies
2+
/vendor/
3+
4+
# Composer lock file (optional—commit if you want reproducible builds)
5+
/composer.lock
6+
7+
# PHPStorm settings
8+
.idea/
9+
10+
# Log files
11+
*.log

changelog.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
## [0.2.0] - 2025-03-15
6+
7+
### Added
8+
- Refactored `EventDispatcher` to improve efficiency and align with PSR-14.
9+
- Introduced `ListenerInterface` for structured event handling.
10+
- Added support for prioritized listeners using `SplPriorityQueue`.
11+
- Implemented `StoppableEventInterface` for event propagation control.
12+
- Improved exception handling with `InvalidEventException` and `InvalidListenerException`.
13+

code_of_conduct.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity.
6+
7+
## Our Standards
8+
9+
Examples of behavior that contributes to creating a positive environment include:
10+
- Using welcoming and inclusive language.
11+
- Being respectful of differing viewpoints and experiences.
12+
- Gracefully accepting constructive criticism.
13+
- Focusing on what is best for the community.
14+
15+
Examples of unacceptable behavior by participants include:
16+
- Harassment, intimidation, or discriminatory comments.
17+
- Trolling, insulting, or derogatory comments.
18+
- Unwelcome sexual attention or advances.
19+
- Deliberate intimidation or stalking.
20+
21+
## Reporting and Enforcement
22+
23+
If you experience or witness any behavior that violates this Code of Conduct, please report it by emailing [conduct@commonphp.org](mailto:conduct@commonphp.org). All complaints will be reviewed promptly and confidentially by the project maintainers.
24+
25+
Project maintainers are responsible for enforcing this code throughout the project and community. They have the right to remove, edit, or reject any contributions or comments that do not align with this Code of Conduct.
26+
27+
By participating in this project, you agree to abide by these guidelines and help create a positive environment for everyone.

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "comphp/events",
3+
"description": "A lightweight, modular event dispatcher with PSR-14 compatibility and an object-oriented approach for registering and triggering events.",
4+
"license": "MIT",
5+
"autoload": {
6+
"psr-4": {
7+
"Neuron\\Events\\": "src/"
8+
}
9+
},
10+
"autoload-dev": {
11+
"psr-4": {
12+
"NeuronTests\\Events\\": "test/"
13+
}
14+
},
15+
"require": {
16+
"php": "^8.4",
17+
"psr/event-dispatcher": "^1.0"
18+
},
19+
"require-dev": {
20+
"phpunit/phpunit": "^12.0"
21+
}
22+
}

contributing.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Contributing
2+
3+
Thank you for considering contributing to this project! Your help is essential in making this library better for everyone.
4+
5+
## How to Report Bugs
6+
- Before opening a new issue, please search the issue tracker for similar reports.
7+
- When reporting a bug, include a clear and descriptive title, a detailed explanation, and steps to reproduce the issue.
8+
9+
## How to Request Features
10+
- Check the existing issues to ensure your feature hasn’t already been requested.
11+
- Provide a clear description of the feature, its benefits, and any potential implementation ideas.
12+
13+
## Pull Request Guidelines
14+
- Fork the repository and create your branch from `master`.
15+
- Follow the coding style guidelines (PSR standards).
16+
- Write clear commit messages that explain the rationale behind your changes.
17+
- Ensure all tests pass before submitting your pull request.
18+
- Include relevant documentation updates if necessary.
19+
20+
## Additional Guidelines
21+
- Respect the existing code style and architecture.
22+
- Be open to feedback and constructive criticism.
23+
- Engage in discussions on issues and pull requests respectfully.
24+
25+
Thank you for your contributions!

examples/basic-dispatching.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use Neuron\Events\EventDispatcher;
4+
5+
require '../vendor/autoload.php';
6+
require 'include/user-registered-event.php';
7+
8+
$dispatcher = new EventDispatcher();
9+
$dispatcher->listen(UserRegisteredEvent::class, function (UserRegisteredEvent $event) {
10+
echo "User registered: " . $event->getPayload()['username'] . "\n";
11+
});
12+
$dispatcher->dispatch(new UserRegisteredEvent('Tim')); // Expected output: User registered: Tim
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Neuron\Events\AbstractEvent;
4+
use Psr\EventDispatcher\StoppableEventInterface;
5+
6+
class OrderPlacedEvent extends AbstractEvent implements StoppableEventInterface
7+
{
8+
private bool $stopped = false;
9+
public function isPropagationStopped(): bool { return $this->stopped; }
10+
public function stopPropagation(): void { $this->stopped = true; }
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use Neuron\Events\AbstractEvent;
4+
5+
class UserRegisteredEvent extends AbstractEvent
6+
{
7+
public function __construct(string $username)
8+
{
9+
parent::__construct(['username' => $username]);
10+
}
11+
}
12+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use Neuron\Events\EventInterface;
4+
use Neuron\Events\ListenerInterface;
5+
6+
class WelcomeEmailListener implements ListenerInterface
7+
{
8+
public function handle(EventInterface $event): void
9+
{
10+
echo "Sending welcome email to: " . $event->getPayload()['username'] . "\n";
11+
}
12+
}
13+

examples/listener-objects.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Neuron\Events\EventDispatcher;
4+
5+
require '../vendor/autoload.php';
6+
require 'include/user-registered-event.php';
7+
require 'include/welcome-email-listener.php';
8+
9+
$dispatcher = new EventDispatcher();
10+
$dispatcher->listen(UserRegisteredEvent::class, [new WelcomeEmailListener(), 'handle']);
11+
$dispatcher->dispatch(new UserRegisteredEvent('Alice')); // Expected output: Sending welcome email to: Alice

0 commit comments

Comments
 (0)