├── CONTRIBUTING.md
├── README.md
├── UPGRADE.md
├── composer.json
├── spark.md
└── src
├── Mpociot
└── CaptainHook
│ ├── CaptainHookServiceProvider.php
│ ├── Commands
│ ├── AddWebhook.php
│ ├── DeleteWebhook.php
│ └── ListWebhooks.php
│ ├── Http
│ ├── Requests
│ │ ├── CreateWebhookRequest.php
│ │ └── UpdateWebhookRequest.php
│ ├── WebhookController.php
│ └── WebhookEventsController.php
│ ├── Jobs
│ └── TriggerWebhooksJob.php
│ ├── Webhook.php
│ └── WebhookLog.php
├── config
└── config.php
├── database
├── 2015_10_29_000000_captain_hook_setup_table.php
└── 2015_10_29_000001_captain_hook_setup_logs_table.php
├── resources
├── assets
│ └── js
│ │ └── captainhook
│ │ ├── settings
│ │ ├── create-webhook.js
│ │ └── webhooks.js
│ │ └── webhooks.js
└── views
│ ├── webhooks.blade.php
│ └── webhooks
│ ├── create-webhook.blade.php
│ └── webhooks.blade.php
└── routes.php
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing
2 |
3 | Contributions are **welcome** and will be fully **credited**.
4 |
5 | Please read and understand the contribution guide before creating an issue or pull request.
6 |
7 | ## Etiquette
8 |
9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code
10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be
11 | extremely unfair for them to suffer abuse or anger for their hard work.
12 |
13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
14 | world that developers are civilized and selfless people.
15 |
16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.
18 |
19 | ## Viability
20 |
21 | When requesting or submitting new features, first consider whether it might be useful to others. Open
22 | source projects are used by many developers, who may have entirely different needs to your own. Think about
23 | whether or not your feature is likely to be used by other users of the project.
24 |
25 | ## Procedure
26 |
27 | Before filing an issue:
28 |
29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
30 | - Check to make sure your feature suggestion isn't already present within the project.
31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
32 | - Check the pull requests tab to ensure that the feature isn't already in progress.
33 |
34 | Before submitting a pull request:
35 |
36 | - Check the codebase to ensure that your feature doesn't already exist.
37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix.
38 |
39 | ## Requirements
40 |
41 | If the project maintainer has any additional requirements, you will find them listed here.
42 |
43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).
44 |
45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests.
46 |
47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
48 |
49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
50 |
51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
52 |
53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
54 |
55 | **Happy coding**!
56 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | # Captain Hook
3 | ## Add Webhooks to your Laravel app, arrr
4 |
5 | 
6 | 
7 | [](https://codecov.io/github/mpociot/captainhook?branch=master)
8 | [](https://scrutinizer-ci.com/g/mpociot/captainhook/?branch=master)
9 | [](https://travis-ci.org/mpociot/captainhook)
10 | [](https://styleci.io/repos/45216255)
11 |
12 | Implement multiple webhooks into your Laravel app using the Laravel Event system.
13 |
14 | ```
15 | A webhook is a method of altering the behavior of a web application, with custom callbacks.
16 | These callbacks may be managed by third-party users and developers who may not necessarily
17 | be affiliated with the originating application.
18 | ```
19 |
20 | ## Examples
21 |
22 | ```bash
23 | php artisan hook:add http://www.myapp.com/hooks/ '\App\Events\PodcastWasPurchased'
24 | php artisan hook:add http://www.myapp.com/hooks/ 'eloquent.saved: \App\User'
25 | ```
26 |
27 | ```php
28 | Webhook::create([
29 | "url" => Input::get("url"),
30 | "event" => "\\App\\Events\\MyEvent",
31 | "tenant_id" => Auth::id()
32 | ]);
33 | ```
34 |
35 | ## Contents
36 |
37 | - [Installation](#installation)
38 | - [Implementation](#implementation)
39 | - [Usage](#usage)
40 | - [Spark support](#spark)
41 | - [Custom event listeners](#listeners)
42 | - [Add new webhooks](#add)
43 | - [Delete existing webhooks](#delete)
44 | - [List all active webhooks](#list)
45 | - [Receiving a webhook notification](#webhook)
46 | - [Webhook logging](#logging)
47 | - [Using webhooks with multi tenancy](#tenant)
48 | - [License](#license)
49 |
50 |
51 | ## Installation
52 |
53 | In order to add CaptainHook to your project, just add
54 |
55 | "mpociot/captainhook": "~2.0"
56 |
57 | to your `composer.json`'s `require` block. Then run `composer install` or `composer update`.
58 |
59 | Or run `composer require mpociot/captainhook ` if you prefer that.
60 |
61 | Then in your `config/app.php` add
62 |
63 | Mpociot\CaptainHook\CaptainHookServiceProvider::class
64 |
65 | to the `providers` array.
66 |
67 |
68 | Publish and run the migration to create the "webhooks" table that will hold all installed webhooks.
69 |
70 | ```bash
71 | php artisan vendor:publish --provider="Mpociot\CaptainHook\CaptainHookServiceProvider"
72 |
73 | php artisan migrate
74 | ```
75 |
76 |
77 | ## Usage
78 |
79 | The CaptainHook service provider listens for any `eloquent.*` events.
80 |
81 | If the package finds a configured webhook for an event, it will make a `POST` request to the specified URL.
82 |
83 | Webhook data is sent as JSON in the POST request body. The full event object is included and can be used directly, after parsing the JSON body.
84 |
85 | **Example**
86 |
87 | Let's say you want to have a webhook that gets called every time your User model gets updated.
88 |
89 | The event that gets called from Laravel will be:
90 |
91 | `eloquent.updated: \App\User`
92 |
93 | So this will be the event you want to listen for.
94 |
95 |
96 | ### Add new webhooks
97 |
98 | If you know which event you want to listen to, you can add a new webhook by using the `hook:add` artisan command.
99 |
100 | This command takes two arguments:
101 |
102 | - The webhook URL that will receive the POST requests
103 | - The event name. This could either be one of the `eloquent.*` events, or one of your custom events.
104 |
105 | ```bash
106 | php artisan hook:add http://www.myapp.com/hook/ 'eloquent.saved: \App\User'
107 | ```
108 |
109 | You can also add multiple webhooks for the same event, as all configured webhooks will get called asynchronously.
110 |
111 |
112 | ### Delete existing webhooks
113 |
114 | To remove an existing webhook from the system, use the `hook:delete` command. This command takes the webhook ID as an argument.
115 |
116 | ```bash
117 | php artisan hook:delete 2
118 | ```
119 |
120 |
121 | ### List all active webhooks
122 |
123 | To list all existing webhooks, use the `hook:list` command.
124 |
125 | It will output all configured webhooks in a table.
126 |
127 |
128 | ### Spark
129 |
130 | Install this package like stated in the [Installation section](#installation), then follow the [Spark installation instructions](spark.md).
131 |
132 |
133 | ### Custom event listeners
134 |
135 | All listeners are defined in the config file located at `config/captain_hook.php`.
136 |
137 |
138 | ### Receiving a webhook notification
139 |
140 | To receive the event data in your configured webhook, use:
141 |
142 | ```php
143 | // Retrieve the request's body and parse it as JSON
144 | $input = @file_get_contents("php://input");
145 | $event_json = json_decode($input);
146 |
147 | // Do something with $event_json
148 | ```
149 |
150 |
151 | ### Webhook logging
152 |
153 | Starting with version 2.0, this package allows you to log the payload and response of the triggered webhooks.
154 |
155 | > **NOTE:** A non-blocking queue driver (not `sync`) is highly recommended. Otherwise your application will need to wait for the webhook execution.
156 |
157 | You can configure how many logs will be saved **per webhook** (Default 50).
158 |
159 | This value can be modified in the configuration file `config/captain_hook.php`.
160 |
161 |
162 | ### Using webhooks with multi tenancy
163 |
164 | Sometimes you don't want to use system wide webhooks, but rather want them scoped to a specific "tenant".
165 | This could be bound to a user or a team.
166 |
167 | The webhook table has a field `tenant_id` for this purpose.
168 | So if you want your users to be able to add their own webhooks, you won't use the artisan commands to add webhooks to the database,
169 | but add them on your own.
170 |
171 | To add a webhook that is scoped to the current user, you could do for example:
172 |
173 | ```php
174 | Webhook::create([
175 | "url" => Input::get("url"),
176 | "event" => "\\App\\Events\\MyEvent",
177 | "tenant_id" => Auth::id()
178 | ]);
179 | ```
180 |
181 | Now when you fire this event, you want to call the webhook only for the currently logged in user.
182 |
183 | In order to filter the webhooks, modify the `filter` configuration value in the `config/captain_hook.php` file.
184 | This filter is a Laravel collection filter.
185 |
186 | To return only the webhooks for the currently logged in user, it might look like this:
187 |
188 | ```php
189 | 'filter' => function( $webhook ){
190 | return $webhook->tenant_id == Auth::id();
191 | },
192 | ```
193 |
194 |
195 | ## License
196 |
197 | CaptainHook is free software distributed under the terms of the MIT license.
198 |
199 | 'Day 02: Table, Lamp & Treasure Map' image licensed under [Creative Commons 2.0](https://creativecommons.org/licenses/by/2.0/) - Photo from [stevedave](https://www.flickr.com/photos/stevedave/4153323914)
200 |
--------------------------------------------------------------------------------
/UPGRADE.md:
--------------------------------------------------------------------------------
1 | # Upgrade Captain Hook from version 2.* to 3.*
2 |
3 | ## Breaking changes
4 | - The `listeners` property in the configuration file is now an associative array. The key can be used to display a meaningful event name (which will also be used for Spark).
5 |
6 | # Upgrade Captain Hook from version 1.* to 2.*
7 |
8 | ## Breaking changes
9 | - Custom event listeners are no longered declared in an extended Service Provider. Place your custom events in the `config/captain_hook.php` instead.
10 | - There's no longer the need to extend the Service Provider.
11 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "mpociot/captainhook",
3 | "license": "MIT",
4 | "description": "Add webhooks to your Laravel app.",
5 | "keywords": [
6 | "events",
7 | "hook",
8 | "laravel",
9 | "webhook",
10 | "webhooks"
11 | ],
12 | "homepage": "http://github.com/mpociot/captainhook",
13 | "authors": [
14 | {
15 | "name": "Marcel Pociot",
16 | "email": "m.pociot@gmail.com"
17 | }
18 | ],
19 | "support": {
20 | "issues": "https://github.com/mpociot/captainhook/issues",
21 | "source": "https://github.com/mpociot/captainhook"
22 | },
23 | "require": {
24 | "php": ">=5.4.0",
25 | "illuminate/support": "~5.0",
26 | "guzzlehttp/guzzle": "~6.0"
27 | },
28 | "require-dev": {
29 | "illuminate/database": "~5.0",
30 | "illuminate/events": "~5.0",
31 | "mockery/mockery": "~0.9",
32 | "orchestra/testbench": "~3.0",
33 | "phpunit/phpunit": "~4.7 || ~5.0"
34 | },
35 | "autoload": {
36 | "psr-0": {
37 | "Mpociot\\CaptainHook": "src/"
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/spark.md:
--------------------------------------------------------------------------------
1 | # Spark installation process
2 |
3 | This package comes with predefined views and routes to use with your existing Spark installation.
4 |
5 |
6 |
7 | In order to install Captainhook into your Spark application:
8 |
9 | **1. Publish the Spark resources (views, VueJS components):**
10 |
11 | `php artisan vendor:publish --provider="Mpociot\CaptainHook\CaptainHookServiceProvider" --tag="spark-resources"`
12 |
13 | **2. Add the javascript components to your bootstrap.js file**
14 |
15 | Add `require('./captainhook/webhooks.js');` to your `resources/assets/js/components/bootstrap.js` file.
16 |
17 | **3. Compile the Javascript components**
18 |
19 | `gulp`
20 |
21 | **4. Add the HTML snippets**
22 |
23 | File: `vendor/spark/settings.blade.php`
24 |
25 | Place a link to the webhooks settings tab:
26 |
27 | ```html
28 |
29 |
144 | Are you sure you want to delete this webhook? If deleted, the webhook endpoint (@{{ deletingWebhook.url }}) will no
145 | longer receive HTTP requests.
146 |