Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gpanos committed Aug 24, 2021
1 parent 0c6ebfb commit 1569873
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
# PHP 8 attribute to register Laravel model observers.

Instead of defining [observers](https://laravel.com/docs/8.x/eloquent#observers) inside service providers this package offers an alternative way to register model observers for your Laravel applications.
[![tests](https://github.com/gpanos/laravel-observe-attribute/actions/workflows/tests.yml/badge.svg)](https://github.com/gpanos/laravel-observe-attribute/actions/workflows/tests.yml)
[![code style](https://github.com/gpanos/laravel-observe-attribute/actions/workflows/code-style.yml/badge.svg)](https://github.com/gpanos/laravel-observe-attribute/actions/workflows/code-style.yml)

## Installation
Instead of defining [observers](https://laravel.com/docs/8.x/eloquent#observers) inside service providers this package offers an alternative way to register model observers for your Laravel applications.

Under development. For now make sure to configure the repository in your composer.json by running:
Inspired by [spatie/laravel-route-attributes](https://github.com/spatie/laravel-route-attributes)

```bash
composer config repositories.laravel-observe-attribute vcs https://github.com/gpanos/laravel-observe-attribute
```

Then install the package by running:
## Installation

```bash
composer require gpanos/laravel-observe-attribute
Expand Down
5 changes: 5 additions & 0 deletions config/observe-attribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'directory' => app_path('Models'),
];
23 changes: 22 additions & 1 deletion src/ObserveAttributeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,27 @@
class ObserveAttributeServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->registerPublishing();

$this->registerObservers();
}

public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/../config/observe-attribute.php', 'observe-attribute');
}

protected function registerPublishing(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__ . '/../config/observe-attribute.php' => $this->app->configPath('observe-attribute.php'),
], 'observe-attribute-config');
}
}

protected function registerObservers(): void
{
(new ObserverRegistrar())
->useRootNamespace(app()->getNamespace())
Expand All @@ -17,6 +38,6 @@ protected function getDirectory(): string
{
$testClassDirectory = __DIR__ . '/../tests/Stubs';

return app()->runningUnitTests() ? __DIR__ . '/../tests/Stubs' : app_path('Models');
return app()->runningUnitTests() ? __DIR__ . '/../tests/Stubs' : config('observe-attribute.directory');
}
}

0 comments on commit 1569873

Please sign in to comment.