Skip to content

Commit

Permalink
Merge pull request #8 from christophrumpel/feature/makePackageFindAll
Browse files Browse the repository at this point in the history
Automatically load notifications and notifiables within app namespace
  • Loading branch information
Christoph Rumpel authored Oct 4, 2018
2 parents ad9680e + 22b2018 commit c990e5f
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 45 deletions.
17 changes: 0 additions & 17 deletions config/nova-notifications.php

This file was deleted.

40 changes: 37 additions & 3 deletions src/ClassFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

namespace Christophrumpel\NovaNotifications;

use ReflectionClass;
use Illuminate\Support\Collection;

class ClassFinder
{
/**
* @return mixed
*/
public function getNamespace()
{
return $this->getAppNamespace();
}

/**
* @param string $nameSpace
* @return Collection
Expand All @@ -14,9 +23,34 @@ public function find(string $nameSpace): Collection
{
$composer = require base_path('vendor/autoload.php');

return collect($composer->getClassMap())->filter(function ($value, $key) use ($nameSpace) {
return starts_with($key, $nameSpace);
});
}

/**
* Find classes which are extending a specific class.
*
* @param string $className
* @return Collection
*/
public function findByExtending(string $className): Collection
{
$composer = require base_path('vendor/autoload.php');

return collect($composer->getClassMap())
->filter(function ($value, $key) use ($nameSpace) {
return starts_with($key, $nameSpace);
});
->filter(function ($value, $key) {
return starts_with($key, app()->getNamespace());
})
->filter(function ($value, $key) use ($className) {
try {
$classInfo = new ReflectionClass($key);
} catch (\ReflectionException $e) {
return false;
}

return $classInfo->isSubclassOf($className);
})
->keys();
}
}
11 changes: 5 additions & 6 deletions src/Http/Controllers/NotifiableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@ public function __construct(ClassFinder $classFinder)

public function index()
{
$modelClasses = $this->classFinder->find(config('nova-notifications.modelNamespace'))
->filter(function ($path, $className) {
$modelClasses = $this->classFinder->findByExtending('Illuminate\Database\Eloquent\Model')
->filter(function ($className) {
$classInfo = new ReflectionClass($className);

return $classInfo->isSubclassOf('Illuminate\Database\Eloquent\Model') && in_array('Illuminate\Notifications\Notifiable',
$classInfo->getTraitNames());
return in_array('Illuminate\Notifications\Notifiable', $classInfo->getTraitNames());
})
->map(function ($path, $className) {
->map(function ($className) {
return [
'name' => str_replace('\\', '.', $className),
'options' => $className::all(),
Expand All @@ -46,7 +45,7 @@ public function index()
->toArray();

return [
'data' => $modelClasses,
'data' => $modelClasses->values(),
'filter' => [
'name' => __('Notifiables'),
'options' => $options,
Expand Down
5 changes: 1 addition & 4 deletions src/Http/Controllers/NotificationClassesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ public function __construct(ClassFinder $classFinder)

public function index()
{
return $this->classFinder->find(config('nova-notifications.notificationNamespace'))
->map(function ($path, $className) {
return $className;
})
return $this->classFinder->findByExtending('Illuminate\Notifications\Notification')
->map(function ($className) {
$classInfo = new ReflectionMethod($className, '__construct');
$notificationClassInfo = new stdClass();
Expand Down
4 changes: 0 additions & 4 deletions src/ToolServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ class ToolServiceProvider extends ServiceProvider
*/
public function boot()
{
$this->publishes([
__DIR__.'/../config/nova-notifications.php' => config_path('nova-notifications.php'),
], 'config');

$this->loadViewsFrom(__DIR__.'/../resources/views', 'nova-notifications');

$this->publishes([
Expand Down
16 changes: 8 additions & 8 deletions tests/Controllers/NotifiableControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public function it_returns_no_results()
$classFinder = Mockery::mock(ClassFinder::class);
$this->app->instance(ClassFinder::class, $classFinder);

$classFinder->shouldReceive('find')
->withArgs([config('nova-notifications.modelNamespace')])
$classFinder->shouldReceive('findByExtending')
->withArgs(['Illuminate\Database\Eloquent\Model'])
->andReturn(collect([]));

$response = $this->get('nova-vendor/nova-notifications/notifiables')
Expand All @@ -36,23 +36,23 @@ public function it_returns_given_notifiable()
$classFinder = Mockery::mock(ClassFinder::class);
$this->app->instance(ClassFinder::class, $classFinder);

$classFinder->shouldReceive('find')
->withArgs([config('nova-notifications.modelNamespace')])
->andReturn(collect([$testModelClassName => 'path/to/file']));
$classFinder->shouldReceive('findByExtending')
->withArgs(['Illuminate\Database\Eloquent\Model'])
->andReturn(collect([$testModelClassName]));

$response = $this->get('nova-vendor/nova-notifications/notifiables')
$this->get('nova-vendor/nova-notifications/notifiables')
->assertSuccessful()
->assertJson([
'data' => [
$testModelClassName => [
[
'name' => $testModelClassNameDots,
'options' => [],
],
],
'filter' => [
'name' => 'Notifiables',
'options' => [
$testModelClassName => [
[
'name' => $testModelClassNameDots,
],
],
Expand Down
6 changes: 3 additions & 3 deletions tests/Controllers/NotificationClassesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public function setUp()
**/
public function it_returns_given_notification_classes()
{
$this->classFinder->shouldReceive('find')
->withArgs(['Christophrumpel\NovaNotifications\Tests\Notifications'])
->andReturn(collect([$this->testNotificationClassName => 'path/to/file']));
$this->classFinder->shouldReceive('findByExtending')
->withArgs(['Illuminate\Notifications\Notification'])
->andReturn(collect([$this->testNotificationClassName]));

$this->get('nova-vendor/nova-notifications/notifications/classes')
->assertSuccessful()
Expand Down

0 comments on commit c990e5f

Please sign in to comment.