Skip to content

Commit

Permalink
Merge pull request #6 from JoggApp/v2
Browse files Browse the repository at this point in the history
v2
  • Loading branch information
introwit authored Sep 27, 2018
2 parents 5aa18ed + b61d7be commit 528c4f9
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 24 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
All notable changes to the Laravel Mail Viewer be documented in this file

## 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.
- The config file is now cacheable as well as serializable.
- Directory structure changed.

## v1.0.1 (20-09-2018)
- Updated readme

Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,22 @@ return [
| in the following array. When asked for a mailable, the
| package will search it here for its definition.
|
| Eg: [ new OrderShipped(factory(Order::class)->create()) ]
| Add the mailable definition as shown below in the example.
| The mailable class will be the key and the dependencies
| of the mailable class will be defined in an array as well.
|
| 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 :)
|
| eg: 'mailables' => [
| OrderShipped::class => [
| Order::class,
| 'Personal thank you message',
| ],
| MailWithNoDependency::class => []
| ]
|
*/

Expand Down
17 changes: 16 additions & 1 deletion config/mailviewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,22 @@
| in the following array. When asked for a mailable, the
| package will search it here for its definition.
|
| Eg: [ new OrderShipped(factory(Order::class)->create()) ]
| Add the mailable definition as shown below in the example.
| The mailable class will be the key and the dependencies
| of the mailable class will be defined in an array as well.
|
| 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 :)
|
| eg: 'mailables' => [
| OrderShipped::class => [
| Order::class,
| 'Personal thank you message',
| ],
| MailWithNoDependency::class => []
| ]
|
*/

Expand Down
File renamed without changes.
69 changes: 50 additions & 19 deletions src/MailViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,78 @@

class MailViewer
{
public static function all()
public static function url()
{
$mailables = config('mailviewer.mailables', []);

$mails = [];
return config('mailviewer.url', 'mails');
}

foreach ($mailables as $mailable) {
$reflection = new ReflectionClass($mailable);
public static function middlewares()
{
$middlewares = config('mailviewer.middlewares', []);

$mails[] = $reflection->getShortName();
if (is_array($middlewares)) {
return $middlewares;
}

return $mails;
throw new Exception('The middlewares config value only excepts an array');
}

public static function all()
{
$mailables = config('mailviewer.mailables', []);

return empty($mailables) ? [] : self::prepareMails($mailables);
}

public static function find(string $mail)
{
foreach (config('mailviewer.mailables', []) as $mailable) {
foreach (config('mailviewer.mailables', []) as $mailable => $dependencies) {
$reflection = new ReflectionClass($mailable);

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

foreach ($dependencies as $dep) {
$args[] = class_exists($dep) ? factory($dep)->create() : $dep;
}

return new $mailable(...$args);
}
}

throw new Exception("No mailable called {$mail} is registered in config/mailviewer.php file");
}

public static function url()
public static function prepareMails(array $mailables): array
{
return config('mailviewer.url', 'mails');
}
$mails = [];

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

if (is_array($middlewares)) {
return $middlewares;
$givenParameters = [];

foreach ($dependencies as $dependency) {
$givenParameters[] = class_exists($dependency)
? (new ReflectionClass($dependency))->getName()
: getType($dependency);
}

$constructorParameters = [];

foreach ($reflection->getConstructor()->getParameters() as $parameter) {
$constructorParameters[] = $parameter->getType()->getName();
}

if ($constructorParameters !== $givenParameters) {
throw new Exception(
"The arguments passed for {$mailable} in the config/mailviewer.php file do not match with the constructor params of the {$mailable} class or the constructor params of the {$mailable} aren't typehinted"
);
}

$mails[] = $reflection->getShortName();
}

throw new Exception('The middlewares config value only excepts an array');
return $mails;
}
}
4 changes: 2 additions & 2 deletions src/MailViewerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public function boot()
__DIR__ . '/../config/mailviewer.php' => config_path('mailviewer.php'),
]);

$this->loadRoutesFrom(__DIR__ . '/routes/web.php');
$this->loadRoutesFrom(__DIR__ . '/../routes/web.php');

$this->loadViewsFrom(__DIR__ . '/views', 'mailviewer');
$this->loadViewsFrom(__DIR__ . '/../views', 'mailviewer');
}

public function register()
Expand Down
9 changes: 8 additions & 1 deletion tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use JoggApp\MailViewer\MailViewerServiceProvider;
use JoggApp\MailViewer\Tests\Stubs\Mail\TestEmailForMailViewer;
use JoggApp\MailViewer\Tests\Stubs\Mail\TestEmailWithDependencies;
use Orchestra\Testbench\TestCase;

class BaseTestCase extends TestCase
Expand All @@ -19,7 +20,13 @@ protected function getEnvironmentSetUp($app)

$app['config']->set(
'mailviewer.mailables',
[new TestEmailForMailViewer()]
[
TestEmailForMailViewer::class => [],
TestEmailWithDependencies::class => [
\stdClass::class,
'Some name'
]
]
);

$app['config']->set('mailviewer.url', 'mails');
Expand Down
32 changes: 32 additions & 0 deletions tests/Stubs/Mail/TestEmailWithDependencies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace JoggApp\MailViewer\Tests\Stubs\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class TestEmailWithDependencies extends Mailable
{
use Queueable, SerializesModels;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct(\stdClass $someObject, string $name)
{
//
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('mailviewer::stubs.emailtestview');
}
}
File renamed without changes.
File renamed without changes.

0 comments on commit 528c4f9

Please sign in to comment.