Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tfhinc/ci-newton

Simply Observe - Newton allows you to Subscribe and Listen for Event Broadcasts in the Codeigniter framework
https://github.com/tfhinc/ci-newton

broadcast codeigniter codeigniter-library event-broadcasting newton

Last synced: 3 days ago
JSON representation

Simply Observe - Newton allows you to Subscribe and Listen for Event Broadcasts in the Codeigniter framework

Awesome Lists containing this project

README

        

# Newton

[![Latest Version on Packagist][ico-version]][link-packagist]
[![PHP Version][ico-php-version]][link-packagist]
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Total Downloads][ico-downloads]][link-downloads]

Simply Observe - Newton allows you to Subscribe and Listen for Event Broadcasts in the [Codeigniter](https://codeigniter.com/) framework.

## Installation

```bash
composer require tfhinc/ci-newton
```

Run the post install `publish-files` command to publish the config, helper and library class files to the appropriate CI directories:
```bash
composer --working-dir=vendor/tfhinc/ci-newton/ run-script publish-files
```

## Loading the Library

There are a few available options for loading the Newton library:

### Using the `newton()` helper function

The Newton helper function will resolve the Newton class via the CI instance. It will either load the class or return the existing class instance:

``` php
$this->load->helper('newton');
```

### Using the Newton Class

The Newton class can be instantiated via namespace:

``` php
$newton = new TFHInc/Newton/Newton();
```

### Using the Newton CI Library

The Newton class can be loaded like any other CI library:

``` php
$this->load->library('Newton');
```

## Class Structure

The Newton library allows for `Listener` classes to subscribe to `Event` class broadcasts. This helps decouple your business logic into single purpose `Listener` classes that can be invoked en masse by the broadcast of an `Event`.

### Event Classes

The `Event` class defines the properties that a given event will require and receive where there is a broadcast. The `Event` class does not contain any business logic - think of it as a blueprint of required data for a given event.

*application/events/UserCreatedEvent.php*
``` php
email = $email;
$this->first_name = $first_name;
$this->last_name = $last_name;
}
}
```

### Listener Classes

The `Listener` class contains the business logic that will be performed when a subscribed `Event` is broadcast. The `Listener` class will receive an instance of the `Event` class, which includes the event properties for usage in your business logic:

Note that the `Listener` class must extend the `TFHInc/Newton/NewtonListener` abstract class.

*application/listeners/SendAdminEmailListener.php*
``` php
email . ' just signed up!');
}
}
```

*application/listeners/UpdateUserStatsListener.php*
```php
[
'Listeners\SendAdminEmailListener',
'Listeners\UpdateUserStatsListener'
]
];
```

### Via Subscribe Method

```php
newton()->subscribe('Events\UserCreatedEvent', [
'Listeners\SendAdminEmailListener',
'Listeners\UpdateUserStatsListener'
]);
```

## Broadcasting Events

An `Event` can be broadcast via the `broadcast()` method using the Newton library or `newton()` helper function:

``` php
// Broadcast an Event to all Subscribed Listeners via Newton helper
newton()->broadcast('UserCreatedEvent', '[email protected]', 'Bob', 'Belcher');

// Broadcast an Event to all Subscribed Listeners via Newton class
$newton->broadcast('UserCreatedEvent', '[email protected]', 'Bob', 'Belcher');
```

In this example the `UserCreatedEvent` class will be instantiated and in turn,
instantiate the subscribed classes `SendAdminEmailListener` and `UpdateUserStatsListener`.

## Contributing

Feel free to create a GitHub issue or send a pull request with any bug fixes. Please see the GutHub issue tracker for isses that require help.

## Acknowledgements

- [Colin Rafuse][link-author]
- [All Contributors][link-contributors]

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

[ico-version]: https://img.shields.io/packagist/v/tfhinc/ci-newton.svg?style=flat-square
[ico-php-version]: https://img.shields.io/packagist/php-v/tfhinc/ci-newton.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/tfhinc/ci-newton.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/tfhinc/ci-newton
[link-downloads]: https://packagist.org/packages/tfhinc/ci-newton
[link-author]: https://github.com/crafuse
[link-contributors]: ../../contributors