diff --git a/README.md b/README.md index 1bc3aff..cc85c8f 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ be affiliated with the originating application. ```bash php artisan hook:add http://www.myapp.com/hooks/ '\App\Events\PodcastWasPurchased' -php artisan hook:add http://www.myapp.com/hooks/ 'eloquent.saved \App\User' +php artisan hook:add http://www.myapp.com/hooks/ 'eloquent.saved: \App\User' ``` ```php @@ -88,7 +88,7 @@ Let's say you want to have a webhook that gets called every time your User model The event that gets called from Laravel will be: -`eloquent.updated \App\User` +`eloquent.updated: \App\User` So this will be the event you want to listen for. @@ -103,7 +103,7 @@ This command takes two arguments: - The event name. This could either be one of the `eloquent.*` events, or one of your custom events. ```bash -php artisan hook:add http://www.myapp.com/hook/ 'eloquent.saved \App\User' +php artisan hook:add http://www.myapp.com/hook/ 'eloquent.saved: \App\User' ``` You can also add multiple webhooks for the same event, as all configured webhooks will get called asynchronously. diff --git a/src/Mpociot/CaptainHook/CaptainHookServiceProvider.php b/src/Mpociot/CaptainHook/CaptainHookServiceProvider.php index f979181..f94f611 100644 --- a/src/Mpociot/CaptainHook/CaptainHookServiceProvider.php +++ b/src/Mpociot/CaptainHook/CaptainHookServiceProvider.php @@ -5,6 +5,7 @@ use GuzzleHttp\Client; use GuzzleHttp\ClientInterface; use Illuminate\Support\Facades\Event; +use Illuminate\Support\Facades\Schema; use Illuminate\Support\ServiceProvider; use Mpociot\CaptainHook\Commands\AddWebhook; use Illuminate\Foundation\Bus\DispatchesJobs; @@ -81,10 +82,13 @@ public function register() protected function publishMigration() { $migrations = [ - __DIR__.'/../../database/2015_10_29_000000_captain_hook_setup_table.php' => database_path('/migrations/'.date('Y_m_d').'_000000_captain_hook_setup_table.php'), - __DIR__.'/../../database/2015_10_29_000001_captain_hook_setup_logs_table.php' => database_path('/migrations/'.date('Y_m_d').'_000001_captain_hook_setup_logs_table.php'), + __DIR__.'/../../database/2015_10_29_000000_captain_hook_setup_table.php' => + database_path('/migrations/2015_10_29_000000_captain_hook_setup_table.php'), + __DIR__.'/../../database/2015_10_29_000001_captain_hook_setup_logs_table.php' => + database_path('/migrations/2015_10_29_000001_captain_hook_setup_logs_table.php'), ]; + // To be backwards compatible foreach ($migrations as $migration => $toPath) { preg_match('/_captain_hook_.*\.php/', $migration, $match); $published_migration = glob(database_path('/migrations/*'.$match[0])); @@ -152,13 +156,14 @@ public function setWebhooks($webhooks) */ public function getWebhooks() { - if (! $this->getCache()->has(Webhook::CACHE_KEY)) { - $this->getCache()->rememberForever(Webhook::CACHE_KEY, function () { + // Check if migration ran + if (Schema::hasTable((new Webhook)->getTable())) { + return collect($this->getCache()->rememberForever(Webhook::CACHE_KEY, function () { return Webhook::all(); - }); + })); } - return collect($this->getCache()->get(Webhook::CACHE_KEY)); + return collect(); } /** @@ -214,22 +219,10 @@ public function handleEvent($eventData) */ protected function registerCommands() { - $this->app['hook.list'] = $this->app->share(function () { - return new ListWebhooks(); - }); - - $this->app['hook.add'] = $this->app->share(function () { - return new AddWebhook(); - }); - - $this->app['hook.delete'] = $this->app->share(function () { - return new DeleteWebhook(); - }); - $this->commands( - 'hook.list', - 'hook.add', - 'hook.delete' + ListWebhooks::class, + AddWebhook::class, + DeleteWebhook::class ); } diff --git a/src/Mpociot/CaptainHook/Webhook.php b/src/Mpociot/CaptainHook/Webhook.php index 8a33bf6..cf80eb2 100644 --- a/src/Mpociot/CaptainHook/Webhook.php +++ b/src/Mpociot/CaptainHook/Webhook.php @@ -8,6 +8,10 @@ /** * This file is part of CaptainHook arrrrr. * + * @property integer id + * @property integer tenant_id + * @property string event + * @property string url * @license MIT */ class Webhook extends Eloquent diff --git a/src/database/2015_10_29_000000_captain_hook_setup_table.php b/src/database/2015_10_29_000000_captain_hook_setup_table.php index da35c9d..05edcbf 100644 --- a/src/database/2015_10_29_000000_captain_hook_setup_table.php +++ b/src/database/2015_10_29_000000_captain_hook_setup_table.php @@ -14,7 +14,7 @@ public function up() { Schema::create('webhooks', function (Blueprint $table) { $table->increments('id'); - $table->integer('tenant_id')->nullable(); + $table->integer('tenant_id')->unsigned()->nullable(); $table->string('url'); $table->string('event'); $table->timestamps(); diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index 5a27376..aaee99c 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -1,6 +1,9 @@ shouldReceive('error') ->once() @@ -36,7 +39,7 @@ public function testCannotAddWebhookWithoutName() public function testCanAddWebhook() { - $cmd = m::mock('\\Mpociot\\CaptainHook\\Commands\\AddWebhook[argument,info]'); + $cmd = m::mock(AddWebhook::class . '[argument,info]'); $cmd->shouldReceive('argument') ->with('url') @@ -59,11 +62,11 @@ public function testCanAddWebhook() public function testCannotDeleteWebhookWithWrongID() { - $webhook = \Mpociot\CaptainHook\Webhook::create([ + Webhook::create([ 'url' => 'http://foo.baz', 'event' => 'DeleteWebhook', ]); - $cmd = m::mock('\\Mpociot\\CaptainHook\\Commands\\DeleteWebhook[argument,error]'); + $cmd = m::mock(DeleteWebhook::class . '[argument,error]'); $cmd->shouldReceive('argument') ->with('id') @@ -82,11 +85,11 @@ public function testCannotDeleteWebhookWithWrongID() public function testCanDeleteWebhook() { - $webhook = \Mpociot\CaptainHook\Webhook::create([ + $webhook = Webhook::create([ 'url' => 'http://foo.baz', 'event' => 'DeleteWebhook', ]); - $cmd = m::mock('\\Mpociot\\CaptainHook\\Commands\\DeleteWebhook[argument,info]'); + $cmd = m::mock(DeleteWebhook::class . '[argument,info]'); $cmd->shouldReceive('argument') ->with('id')