Skip to content

Commit

Permalink
Merge pull request #1 from shooka/file-publishing
Browse files Browse the repository at this point in the history
File publishing
  • Loading branch information
shooka committed Sep 20, 2016
2 parents 7cfe7ef + 8be49d4 commit 7f36188
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 2 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,38 @@
[![Coverage Status](https://coveralls.io/repos/shooka/laravel-modelevents/badge.svg?branch=develop)](https://coveralls.io/r/shooka/laravel-modelevents?branch=develop)

Have you ever wanted a place to put your eloquent model events? It is now easier than ever to apply listeners to your models.

## Installation

Add this package to your Laravel project by running:

```bash
composer require shooka/laravel-modelevents 0.2.*
```

To publish the necessary files, add the ServiceProvider to the array of providers in `config/app.php`:
```php
Shooka\ModelEvents\ModelEventServiceProvider::class,
```

Next, publish the files by running:

```bash
php artisan vendor:publish --provider="Shooka\ModelEvents\ModelEventServiceProvider"
```

This publishes two files:
* A `ModelEvents/UserEvent.php` file that contains a dummy class that shows what future `ModelEvent`s are supposed to look like.
* A `Providers/ModelEventServiceProvider.php` with an empty array of listeners that maps actual Eloquent model events to the `ModelEvent` classes.

As the new ServiceProvider has been published, you can add it to the array of providers:

```php
App\Providers\ModelEventServiceProvider::class,
```

And at the same time remove the first one from the array:

```php
Shooka\ModelEvents\ModelEventServiceProvider::class,
```
15 changes: 15 additions & 0 deletions src/ModelEventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ class ModelEventServiceProvider extends ServiceProvider

public function boot()
{
$this->publishes([
$this->simpleProviderPath() => app_path('Providers/ModelEventServiceProvider.php'),
$this->simpleEventPath() => app_path('ModelEvents/UserEvent.php'),
]);

foreach ($this->listeners() as $model => $listeners) {
foreach ((array)$listeners as $listener) {
$model::observe($listener);
Expand All @@ -26,4 +31,14 @@ public function register()
{
//
}

protected function simpleProviderPath()
{
return __DIR__ . '/stubs/provider.stub';
}

protected function simpleEventPath()
{
return __DIR__ . '/stubs/event.stub';
}
}
13 changes: 13 additions & 0 deletions src/stubs/event.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\ModelEvents;

use Shooka\ModelEvents\ModelEvent;

class UserEvent extends ModelEvent
{
public function creating($user)
{
//
}
}
12 changes: 12 additions & 0 deletions src/stubs/provider.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Providers;

use Shooka\ModelEvents\ModelEventServiceProvider as ServiceProvider;

class ModelEventServiceProvider extends ServiceProvider
{
protected $listeners = [
// 'App\User' => 'App\ModelEvents\UserEvent',
];
}
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class TestCase extends \Orchestra\Testbench\TestCase
{
use \Illuminate\Foundation\Testing\DatabaseTransactions;
use \Illuminate\Foundation\Testing\DatabaseMigrations;

public function setUp()
{
Expand All @@ -21,7 +21,7 @@ protected function getEnvironmentSetUp($app)
$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'database' => __DIR__ . '/test.db',
'prefix' => '',
]);
}
Expand Down
Binary file added tests/test.db
Binary file not shown.

0 comments on commit 7f36188

Please sign in to comment.