Skip to content

Commit

Permalink
use container to instantiate non-eloquent objects
Browse files Browse the repository at this point in the history
  • Loading branch information
introwit committed Sep 27, 2018
1 parent 528c4f9 commit b47b8d7
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
All notable changes to the Laravel Mail Viewer be documented in this file

## v2.0.1 (28-09-2018)
- The package now attempts to instantiate non eloquent objects using the container if no factory exists.

## v2.0.0 (27-09-2018)
- Major changes in how the mailables are registered in the config file.
- Please read the comments in the config file for the 'mailable' key and update yours accordingly.
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ composer require joggapp/laravel-mail-viewer

The package will automatically register itself.

You will have to add the mailables and configure the other settings using the package's config file in order to to use this package. You can publish the config file with:
You will have to add the mailables and configure the other settings using the package's config file in order to to use this package. Please read the comments/description for each config key thoroughly and set their values. You can publish the config file with:

```bash
php artisan vendor:publish --provider="JoggApp\MailViewer\MailViewerServiceProvider"
Expand All @@ -41,8 +41,16 @@ return [
|
| The package will look for the equivalent factory if the
| dependency is an eloquent model. So don't forget to
| create those factories. Also, don't forget to import
| these classes at the top :)
| create those factories. However, things like the factory
| state & times/count feature aren't supported for the factories.
| Eg:
| What the package supports: factory(Order::class)->create();
| What the package doesn't support: factory(Order::class, 5)->state('pending')->create();
|
| The package will try to resolve all other non-eloquent objects
| using the Laravel's service container.
|
| Also, don't forget to import these classes at the top :)
|
| eg: 'mailables' => [
| OrderShipped::class => [
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"require": {
"php": "^7.1",
"illuminate/routing": "5.6.*|5.7.*",
"illuminate/support": "5.6.*|5.7.*"
"illuminate/support": "5.6.*|5.7.*",
"illuminate/database": "5.6.*|5.7.*"
},
"require-dev": {
"mockery/mockery": "^1.1",
Expand Down
12 changes: 10 additions & 2 deletions config/mailviewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@
|
| The package will look for the equivalent factory if the
| dependency is an eloquent model. So don't forget to
| create those factories. Also, don't forget to import
| these classes at the top :)
| create those factories. However, things like the factory
| state & times/count feature aren't supported for the factories.
| Eg:
| What the package supports: factory(Order::class)->create();
| What the package doesn't support: factory(Order::class, 5)->state('pending')->create();
|
| The package will try to resolve all other non-eloquent objects
| using the Laravel's service container.
|
| Also, don't forget to import these classes at the top :)
|
| eg: 'mailables' => [
| OrderShipped::class => [
Expand Down
16 changes: 13 additions & 3 deletions src/MailViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace JoggApp\MailViewer;

use Exception;
use Illuminate\Support\Facades\Config;
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
use ReflectionClass;

class MailViewer
Expand Down Expand Up @@ -33,14 +33,24 @@ public static function all()

public static function find(string $mail)
{
$eloquentFactory = app(EloquentFactory::class);

foreach (config('mailviewer.mailables', []) as $mailable => $dependencies) {
$reflection = new ReflectionClass($mailable);

if ($reflection->getShortName() === $mail) {
$args = [];

foreach ($dependencies as $dep) {
$args[] = class_exists($dep) ? factory($dep)->create() : $dep;
foreach ($dependencies as $dependency) {
if (class_exists($dependency)) {
if (isset($eloquentFactory[$dependency])) {
$args[] = factory($dependency)->create();
} else {
$args[] = app($dependency);
}
} else {
$args[] = $dependency;
}
}

return new $mailable(...$args);
Expand Down
9 changes: 8 additions & 1 deletion tests/MailViewerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@ public function it_lists_all_the_mailables_on_the_url_configured_in_config_file(
}

/** @test */
public function it_renders_the_mailable_on_its_dedicated_route()
public function it_renders_the_mailable_without_dependencies_on_its_dedicated_route()
{
$this->get(route('mv-mailviewer', 'TestEmailForMailViewer'))
->assertSee('The test email view');
}

/** @test */
public function it_renders_the_mailable_with_dependencies_on_its_dedicated_route()
{
$this->get(route('mv-mailviewer', 'TestEmailWithDependencies'))
->assertSee('The test email view');
}
}

0 comments on commit b47b8d7

Please sign in to comment.