{"id":14987409,"url":"https://github.com/tfhinc/ci-newton","last_synced_at":"2026-01-04T21:40:35.545Z","repository":{"id":57067429,"uuid":"145479878","full_name":"TFHInc/ci-newton","owner":"TFHInc","description":"Simply Observe - Newton allows you to Subscribe and Listen for Event Broadcasts in the Codeigniter framework","archived":false,"fork":false,"pushed_at":"2018-12-05T02:51:10.000Z","size":21,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-26T13:28:50.061Z","etag":null,"topics":["broadcast","codeigniter","codeigniter-library","event-broadcasting","newton"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/TFHInc.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-20T23:16:10.000Z","updated_at":"2019-12-14T13:17:41.000Z","dependencies_parsed_at":"2022-08-24T05:40:06.558Z","dependency_job_id":null,"html_url":"https://github.com/TFHInc/ci-newton","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TFHInc%2Fci-newton","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TFHInc%2Fci-newton/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TFHInc%2Fci-newton/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TFHInc%2Fci-newton/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TFHInc","download_url":"https://codeload.github.com/TFHInc/ci-newton/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244852110,"owners_count":20521151,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["broadcast","codeigniter","codeigniter-library","event-broadcasting","newton"],"created_at":"2024-09-24T14:14:34.465Z","updated_at":"2026-01-04T21:40:35.498Z","avatar_url":"https://github.com/TFHInc.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Newton\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![PHP Version][ico-php-version]][link-packagist]\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n[![Total Downloads][ico-downloads]][link-downloads]\n\nSimply Observe - Newton allows you to Subscribe and Listen for Event Broadcasts in the [Codeigniter](https://codeigniter.com/) framework.\n\n## Installation\n\n```bash\ncomposer require tfhinc/ci-newton\n```\n\nRun the post install `publish-files` command to publish the config, helper and library class files to the appropriate CI directories:\n```bash\ncomposer --working-dir=vendor/tfhinc/ci-newton/ run-script publish-files\n```\n\n## Loading the Library\n\nThere are a few available options for loading the Newton library:\n\n### Using the `newton()` helper function\n\nThe Newton helper function will resolve the Newton class via the CI instance. It will either load the class or return the existing class instance:\n\n``` php\n$this-\u003eload-\u003ehelper('newton');\n```\n\n### Using the Newton Class\n\nThe Newton class can be instantiated via namespace:\n\n``` php\n$newton = new TFHInc/Newton/Newton();\n```\n\n### Using the Newton CI Library\n\nThe Newton class can be loaded like any other CI library:\n\n``` php\n$this-\u003eload-\u003elibrary('Newton');\n```\n\n## Class Structure\n\nThe 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`.\n\n### Event Classes\n\nThe `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.\n\n*application/events/UserCreatedEvent.php*\n``` php\n\u003c?php\ndefined('BASEPATH') OR exit('No direct script access allowed');\n\nnamespace Events;\n\n/**\n * UserCreatedEvent\n *\n * A new User has been created. Yay, new user!\n *\n */\nclass UserCreatedEvent {\n    /**\n     * @var string\n     */\n    public $email;\n\n    /**\n     * @var string\n     */\n    public $first_name;\n\n    /**\n     * @var string\n     */\n    public $last_name;\n\n    /**\n     * Construct the event.\n     *\n     * @param   string   $email\n     * @param   string   $first_name\n     * @param   string   $last_name\n     * @return  UserCreatedEvent\n     */\n    public function __construct(string $email, string $first_name, string $last_name)\n    {\n        $this-\u003eemail = $email;\n        $this-\u003efirst_name = $first_name;\n        $this-\u003elast_name = $last_name;\n    }\n}\n```\n\n### Listener Classes\n\nThe `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:\n\nNote that the `Listener` class must extend the `TFHInc/Newton/NewtonListener` abstract class.\n\n*application/listeners/SendAdminEmailListener.php*\n``` php\n\u003c?php\n\nnamespace Listeners;\n\n/**\n * SendAdminEmailListener\n *\n * Send the admin an email.\n *\n */\nclass SendAdminEmailListener extends TFHInc/Newton/NewtonListener {\n    /**\n     * Run the listener.\n     *\n     * @return  void\n     */\n    public function run($event): void\n    {\n        // Send the Admin an Email.\n        // mailto('admin@example.com', 'New User ' . $event-\u003eemail . ' just signed up!');\n    }\n}\n```\n\n*application/listeners/UpdateUserStatsListener.php*\n```php\n\u003c?php\n\nnamespace Listeners;\n\n/**\n * UpdateUserStatsListener\n *\n * Update the User stats with the new User data.\n *\n */\nclass UpdateUserStatsListener extends TFHInc/Newton/NewtonListener {\n    /**\n     * Run the listener.\n     *\n     * @return  void\n     */\n    public function run($event): void\n    {\n        // Update the User Stats.\n    }\n}\n```\n\n## Subscriptions\n\nYou can subscribe `Listener` classes to the `Event` classes. This means when the `Event` is broadcast the subscribed `Listener` classes wil be invoked via reflection. There are two ways to subscribe `Listeners` to `Events`:\n\n### Via Configuration File\n\n``` php\n$config['subscriptions'] = [\n    'Events\\UserCreatedEvent' =\u003e [\n        'Listeners\\SendAdminEmailListener',\n        'Listeners\\UpdateUserStatsListener'\n    ]\n];\n```\n\n### Via Subscribe Method\n\n```php\nnewton()-\u003esubscribe('Events\\UserCreatedEvent', [\n    'Listeners\\SendAdminEmailListener',\n    'Listeners\\UpdateUserStatsListener'\n]);\n```\n\n## Broadcasting Events\n\nAn `Event` can be broadcast via the `broadcast()` method using the Newton library or `newton()` helper function:\n\n``` php\n// Broadcast an Event to all Subscribed Listeners via Newton helper\nnewton()-\u003ebroadcast('UserCreatedEvent', 'bob@example.com', 'Bob', 'Belcher');\n\n// Broadcast an Event to all Subscribed Listeners via Newton class\n$newton-\u003ebroadcast('UserCreatedEvent', 'bob@example.com', 'Bob', 'Belcher');\n```\n\nIn this example the `UserCreatedEvent` class will be instantiated and in turn,\ninstantiate the subscribed classes `SendAdminEmailListener` and `UpdateUserStatsListener`.\n\n## Contributing\n\nFeel 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.\n\n## Acknowledgements\n\n- [Colin Rafuse][link-author]\n- [All Contributors][link-contributors]\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n[ico-version]: https://img.shields.io/packagist/v/tfhinc/ci-newton.svg?style=flat-square\n[ico-php-version]: https://img.shields.io/packagist/php-v/tfhinc/ci-newton.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/tfhinc/ci-newton.svg?style=flat-square\n\n[link-packagist]: https://packagist.org/packages/tfhinc/ci-newton\n[link-downloads]: https://packagist.org/packages/tfhinc/ci-newton\n[link-author]: https://github.com/crafuse\n[link-contributors]: ../../contributors\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftfhinc%2Fci-newton","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftfhinc%2Fci-newton","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftfhinc%2Fci-newton/lists"}