Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.

Commit dcdb3ed

Browse files
vojtechmaresf3l1x
authored andcommitted
added docs, updated tests, psr-4, updated dependencies, changed namespaces (Minetro=>Contributte), php 7.1+, QA + CS (#7)
1 parent 827c645 commit dcdb3ed

30 files changed

+721
-604
lines changed

.docs/README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Events
2+
3+
Simple events for Nette.
4+
5+
## Content
6+
7+
- [Usage - how to register](#usage)
8+
- [Register events](#register-events)
9+
- [Register lazy events](#register-lazy-events)
10+
- [Fire events](#fire-events)
11+
12+
## Usage
13+
14+
### Register extension
15+
16+
Register in your config file (e.q. config.neon).
17+
18+
```neon
19+
extensions:
20+
events: Contributte\DummyEvents\DI\EventsExtension
21+
```
22+
23+
### Register events
24+
25+
On Container compile - **EventsExtension** collect all services which implement **EventsSubscriber** and call their `onEvents($em)` method.
26+
27+
```php
28+
use Contributte\DummyEvents\EventsSubscriber;
29+
use Contributte\DummyEvents\EventsManager;
30+
31+
class TestService implements EventsSubscriber
32+
{
33+
/**
34+
* @param EventsManager $em
35+
*/
36+
public function onEvents(EventsManager $em) {
37+
$em->on('order.update', function($state) {
38+
// Some logic..
39+
});
40+
}
41+
}
42+
```
43+
44+
### Register lazy events
45+
46+
Name tag as event name with prefix **event**.
47+
48+
```neon
49+
services:
50+
{class: TestService, tags: [event.order.update]}
51+
```
52+
53+
Or use tag arrays with key name **events**.
54+
55+
```neon
56+
services:
57+
{class: TestService, tags: [events: [order.update]]}
58+
```
59+
60+
This prevents usage of other tags.
61+
62+
If **EventsSubscriber** register more events and also is lazy registered (by tags in neon). Implemented method
63+
`onEvents(EventsManager $em)` is called **only once**.
64+
65+
```php
66+
use Contributte\DummyEvents\EventsSubscriber;
67+
use Contributte\DummyEvents\EventsManager;
68+
69+
class TestSubscriber implements EventsSubscriber
70+
{
71+
72+
public function onEvents(EventsManager $em) {
73+
$em->on('order.create', function($state) {
74+
// Some logic..
75+
});
76+
77+
$em->on('order.update', function($state) {
78+
// Some logic..
79+
});
80+
81+
$em->on('order.delete', function($state) {
82+
// Some logic..
83+
});
84+
}
85+
}
86+
```
87+
88+
### Fire events
89+
90+
Inject to your class ultra-simple **EventsManager**.
91+
92+
```php
93+
use Contributte\DummyEvents\EventsManager;
94+
95+
/** @var EventsManager @inject **/
96+
public $em;
97+
98+
public function save() {
99+
// Some logic..
100+
101+
// Fire order update events
102+
$this->em->trigger('order.update', $order->state);
103+
}
104+
```
105+

.editorconfig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# Top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
11+
# JS / PHP
12+
[*.{js,php,phpt}]
13+
charset = utf-8
14+
indent_style = tab
15+
indent_size = 4
16+
17+
# NEON
18+
[*.neon]
19+
charset = utf-8
20+
indent_style = tab
21+
indent_size = 4
22+
23+
# Composer, NPM, Travis, BitbucketPipelines
24+
[{composer.json,package.json,.travis.yml,bitbucket-pipelines.yml}]
25+
indent_style = space
26+
indent_size = 2

.gitattributes

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
.gitattributes export-ignore
2-
.gitignore export-ignore
3-
.travis.yml export-ignore
4-
composer.lock export-ignore
5-
tests/ export-ignore
6-
*.md export-ignore
1+
/.gitattributes export-ignore
2+
/.gitignore export-ignore
3+
/.travis.yml export-ignore
4+
/phpstan.neon export-ignore
5+
/tests/ export-ignore
6+
/.editorconfig export-ignore
7+
/composer.lock export-ignore
8+
/README.md export-ignore
9+
/LICENSE export-ignore
10+
/.docs export-ignore

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/composer.lock
55
/tests/*.log
66
/tests/tmp
7+
/.idea

.travis.yml

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,52 @@
11
language: php
22
php:
3-
- 5.4
4-
- 5.5
5-
- 5.6
6-
- 7.0
7-
- hhvm
8-
- hhvm-nightly
3+
- 7.1
4+
- 7.2
95

10-
matrix:
11-
allow_failures:
12-
- php: hhvm
13-
- php: hhvm-nightly
6+
before_install:
7+
# turn off XDebug
8+
- phpenv config-rm xdebug.ini || return 0
149

15-
before_script:
16-
# Update Composer
17-
- composer self-update
10+
install:
11+
# Composer
12+
- travis_retry composer install --no-progress --prefer-dist
1813

19-
# Install Nette Tester
20-
- composer install --no-interaction --prefer-source
14+
script:
15+
# Nette/Tester
16+
- composer run-script tester
2117

22-
# Coveralls
23-
- if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then cat ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini >> ./tests/php-unix.ini; fi
24-
- if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then NTESTER_FLAGS="--coverage ./coverage.xml --coverage-src ./src"; else TESTER_FLAGS=""; fi
18+
jobs:
19+
include:
20+
- stage: Quality Assurance
21+
php: 7.1
22+
script:
23+
- composer run-script qa
2524

26-
script:
27-
- vendor/bin/tester tests/Events -s -p php -c tests/php-unix.ini $NTESTER_FLAGS
25+
- stage: Test Coverage
26+
php: 7.1
27+
script:
28+
- -s -p php --colors 1 -C -d extension=xdebug.so --coverage ./coverage.xml --coverage-src ./src tests/Events
29+
after_script:
30+
- wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar
31+
- php coveralls.phar --verbose --config tests/.coveralls.yml
32+
33+
- stage: Phpstan
34+
php: 7.1
35+
script:
36+
- composer run-script phpstan-install
37+
- composer run-script phpstan
2838

29-
after_script:
30-
# Coveralls
31-
- if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then composer require satooshi/php-coveralls; fi
32-
- if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then php vendor/bin/coveralls -c tests/.coveralls.yml -v; fi
39+
allow_failures:
40+
- stage: Test Coverage
41+
- stage: Phpstan
3342

3443
after_failure:
35-
# Print *.actual content
36-
- 'for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done'
44+
# Print *.actual content
45+
- for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done
46+
47+
sudo: false
48+
49+
cache:
50+
directories:
51+
- $HOME/.composer/cache
52+

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Contributte
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

0 commit comments

Comments
 (0)