├── .editorconfig ├── .github └── workflows │ └── quality-assurance.yaml ├── LICENSE ├── README.md ├── composer.json └── src ├── AggregateProvider.php ├── DelegatingProvider.php ├── ParameterDeriverTrait.php └── TaggedProviderTrait.php /.editorconfig: -------------------------------------------------------------------------------- 1 | ; This file is for unifying the coding style for different editors and IDEs. 2 | ; More information at http://editorconfig.org 3 | 4 | root = true 5 | 6 | [*] 7 | charset = utf-8 8 | indent_size = 4 9 | indent_style = space 10 | end_of_line = lf 11 | insert_final_newline = true 12 | trim_trailing_whitespace = true 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.github/workflows/quality-assurance.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | name: Quality Assurance 3 | on: 4 | push: ~ 5 | pull_request: ~ 6 | 7 | jobs: 8 | phpunit: 9 | name: PHPUnit tests on ${{ matrix.php }} ${{ matrix.composer-flags }} 10 | runs-on: ubuntu-latest 11 | strategy: 12 | matrix: 13 | php: [ '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3' ] 14 | composer-flags: [ '' ] 15 | phpunit-flags: [ '--coverage-text' ] 16 | steps: 17 | - uses: actions/checkout@v2 18 | - uses: shivammathur/setup-php@v2 19 | with: 20 | php-version: ${{ matrix.php }} 21 | coverage: xdebug 22 | tools: composer:v2 23 | - run: composer update --no-progress ${{ matrix.composer-flags }} 24 | - run: vendor/bin/phpunit ${{ matrix.phpunit-flags }} 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 PHP-FIG 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. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PSR-14 Event Dispatcher Utility Library 2 | 3 | [![Latest Version on Packagist][ico-version]][link-packagist] 4 | [![Software License][ico-license]](LICENSE.md) 5 | [![Build Status][ico-travis]][link-travis] 6 | [![Coverage Status][ico-scrutinizer]][link-scrutinizer] 7 | [![Quality Score][ico-code-quality]][link-code-quality] 8 | [![Total Downloads][ico-downloads]][link-downloads] 9 | 10 | 11 | This package contains a series of traits and base classes to cover the common, boilerplate portions of implementing a PSR-14-compliant event dispatcher library. 12 | 13 | 14 | ## Install 15 | 16 | Via Composer 17 | 18 | ``` bash 19 | $ composer require fig/event-dispatcher-util 20 | ``` 21 | 22 | ## Usage 23 | 24 | Coming Soon! 25 | 26 | ## Testing 27 | 28 | ``` bash 29 | $ composer test 30 | ``` 31 | 32 | ## Contributing 33 | 34 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 35 | 36 | ## Security 37 | 38 | If you discover any security related issues, please email one of the current PHP-FIG Secretaries instead of using the issue tracker. 39 | 40 | ## License 41 | 42 | This package is released under the MIT license. Please see [License File](LICENSE.md) for more information. 43 | 44 | [ico-version]: https://img.shields.io/packagist/v/Crell/Tukio.svg?style=flat-square 45 | [ico-license]: https://img.shields.io/badge/License-MIT-green.svg?style=flat-square 46 | [ico-travis]: https://img.shields.io/travis/Crell/Tukio/master.svg?style=flat-square 47 | [ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/Crell/Tukio.svg?style=flat-square 48 | [ico-code-quality]: https://img.shields.io/scrutinizer/g/Crell/Tukio.svg?style=flat-square 49 | [ico-downloads]: https://img.shields.io/packagist/dt/Crell/Tukio.svg?style=flat-square 50 | 51 | [link-packagist]: https://packagist.org/packages/fig/event-dispatcher-util 52 | [link-travis]: https://travis-ci.org/fig/event-dispatcher-util 53 | [link-scrutinizer]: https://scrutinizer-ci.com/g/fig/event-dispatcher-util/code-structure 54 | [link-code-quality]: https://scrutinizer-ci.com/g/fig/event-dispatcher-util 55 | [link-downloads]: https://packagist.org/packages/fig/event-dispatcher-util 56 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fig/event-dispatcher-util", 3 | "type": "library", 4 | "description": "Useful utility classes and traits for implementing the PSR events standard", 5 | "keywords": [ 6 | "PSR-14", 7 | "Events" 8 | ], 9 | "repositories": [ 10 | { 11 | "type": "vcs", 12 | "url": "https://github.com/php-fig/event-dispatcher" 13 | } 14 | ], 15 | "homepage": "https://github.com/php-fig/event-dispatcher-util", 16 | "license": "MIT", 17 | "authors": [ 18 | { 19 | "name": "PHP-FIG", 20 | "homepage": "https://www.php-fig.org/" 21 | }, 22 | { 23 | "name": "Larry Garfield", 24 | "email": "larry@garfieldtech.com", 25 | "homepage": "https://www.garfieldtech.com/", 26 | "role": "Developer" 27 | } 28 | ], 29 | "require": { 30 | "php": ">=7.2", 31 | "psr/event-dispatcher": "^1.0" 32 | }, 33 | "autoload": { 34 | "psr-4": { 35 | "Fig\\EventDispatcher\\": "src" 36 | } 37 | }, 38 | "autoload-dev": { 39 | "psr-4": { 40 | "Fig\\EventDispatcher\\": "tests" 41 | } 42 | }, 43 | "require-dev": { 44 | "phpunit/phpunit": "^8.5" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/AggregateProvider.php: -------------------------------------------------------------------------------- 1 | providers as $provider) { 27 | yield from $provider->getListenersForEvent($event); 28 | } 29 | } 30 | 31 | /** 32 | * Enqueues a listener provider to this set. 33 | * 34 | * @param ListenerProviderInterface $provider 35 | * The provider to add. 36 | * @return AggregateProvider 37 | * The called object. 38 | */ 39 | public function addProvider(ListenerProviderInterface $provider): self 40 | { 41 | $this->providers[] = $provider; 42 | return $this; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/DelegatingProvider.php: -------------------------------------------------------------------------------- 1 | defaultProvider = $defaultProvider; 42 | } 43 | } 44 | 45 | /** 46 | * Adds a provider that will be deferred to for the specified Event types. 47 | * 48 | * @param ListenerProviderInterface $provider 49 | * The provider to which to defer. 50 | * @param array $types 51 | * An array of class types. Any events matching these types will be delegated 52 | * to the specified provider(s). 53 | * @return DelegatingProvider 54 | */ 55 | public function addProvider(ListenerProviderInterface $provider, array $types): self 56 | { 57 | foreach ($types as $type) { 58 | $this->providers[$type][] = $provider; 59 | } 60 | 61 | return $this; 62 | } 63 | 64 | /** 65 | * Sets the provider that will be deferred to for un-listed Event types. 66 | * 67 | * @param ListenerProviderInterface $provider 68 | * The provider that should be called unless preempted by a dedicated provider. 69 | * @return self 70 | * The called object 71 | */ 72 | public function setDefaultProvider(ListenerProviderInterface $provider): self 73 | { 74 | $this->defaultProvider = $provider; 75 | 76 | return $this; 77 | } 78 | 79 | public function getListenersForEvent(object $event): iterable 80 | { 81 | foreach ($this->providers as $type => $providers) { 82 | if ($event instanceof $type) { 83 | /** @var ListenerProviderInterface $provider */ 84 | foreach ($providers as $provider) { 85 | yield from $provider->getListenersForEvent($event); 86 | } 87 | return; 88 | } 89 | } 90 | 91 | yield from $this->defaultProvider->getListenersForEvent($event); 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/ParameterDeriverTrait.php: -------------------------------------------------------------------------------- 1 | isClassCallable($callable)) { 29 | $reflect = new \ReflectionClass($callable[0]); 30 | $params = $reflect->getMethod($callable[1])->getParameters(); 31 | } else { 32 | $reflect = new \ReflectionFunction(\Closure::fromCallable($callable)); 33 | $params = $reflect->getParameters(); 34 | } 35 | 36 | $rType = $params[0]->getType(); 37 | if ($rType === null) { 38 | throw new \InvalidArgumentException('Listeners must declare an object type they can accept.'); 39 | } 40 | $type = $rType->getName(); 41 | } 42 | catch (\ReflectionException $e) { 43 | throw new \RuntimeException('Type error registering listener.', 0, $e); 44 | } 45 | 46 | return $type; 47 | } 48 | 49 | /** 50 | * Determines if a callable represents a function. 51 | * 52 | * Or at least a reasonable approximation, since a function name may not be defined yet. 53 | * 54 | * @deprecated No longer necessary so will be removed at some point in the future. 55 | * 56 | * @param callable $callable 57 | * @return bool 58 | * True if the callable represents a function, false otherwise. 59 | */ 60 | protected function isFunctionCallable(callable $callable): bool 61 | { 62 | // We can't check for function_exists() because it may be included later by the time it matters. 63 | return is_string($callable); 64 | } 65 | 66 | /** 67 | * Determines if a callable represents a closure/anonymous function. 68 | * 69 | * @deprecated No longer necessary so will be removed at some point in the future. 70 | * 71 | * @param callable $callable 72 | * @return bool 73 | * True if the callable represents a closure object, false otherwise. 74 | */ 75 | protected function isClosureCallable(callable $callable): bool 76 | { 77 | return $callable instanceof \Closure; 78 | } 79 | 80 | /** 81 | * Determines if a callable represents a method on an object. 82 | * 83 | * @deprecated No longer necessary so will be removed at some point in the future. 84 | * 85 | * @param callable $callable 86 | * @return bool 87 | * True if the callable represents a method object, false otherwise. 88 | */ 89 | protected function isObjectCallable(callable $callable): bool 90 | { 91 | return is_array($callable) && is_object($callable[0]); 92 | } 93 | 94 | /** 95 | * Determines if a callable represents a static class method. 96 | * 97 | * The parameter here is untyped so that this method may be called with an 98 | * array that represents a class name and a non-static method. The routine 99 | * to determine the parameter type is identical to a static method, but such 100 | * an array is still not technically callable. Omitting the parameter type here 101 | * allows us to use this method to handle both cases. 102 | * 103 | * This method must therefore be called first above, as the array is not actually 104 | * an `is_callable()` and will fail `Closure::fromCallable()`. Because PHP. 105 | * 106 | * @param callable $callable 107 | * @return bool 108 | * True if the callable represents a static method, false otherwise. 109 | */ 110 | protected function isClassCallable($callable): bool 111 | { 112 | return is_array($callable) && is_string($callable[0]) && class_exists($callable[0]); 113 | } 114 | 115 | /** 116 | * Determines if a callable is a class that has __invoke() method 117 | * 118 | * @deprecated No longer necessary so will be removed at some point in the future. 119 | * 120 | * @param callable $callable 121 | * @return bool 122 | * True if the callable represents an invokable object, false otherwise. 123 | */ 124 | private function isInvokable(callable $callable): bool 125 | { 126 | return is_object($callable); 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /src/TaggedProviderTrait.php: -------------------------------------------------------------------------------- 1 | eventType(); 89 | if (!$event instanceof $eventType) { 90 | return []; 91 | } 92 | 93 | $tag = $event->{$this->tagMethod()}(); 94 | 95 | yield from $this->filterListenersForEvent($event, $this->getListenersForTag($tag)); 96 | yield from $this->filterListenersForEvent($event, $this->getListenersForAllTags()); 97 | } 98 | 99 | /** 100 | * @param object $event 101 | * The Event to match against. 102 | * @param iterable $listenerSet 103 | * An iterable in the format returned by getListenersForTag()/getListenersForAllTags(). 104 | * @return iterable 105 | * An iterable of listeners to be called. 106 | */ 107 | protected function filterListenersForEvent(object $event, iterable $listenerSet): iterable 108 | { 109 | foreach ($listenerSet as $type => $listeners) { 110 | if ($event instanceof $type) { 111 | yield from $listeners; 112 | } 113 | } 114 | } 115 | } 116 | --------------------------------------------------------------------------------