diff --git a/CHANGELOG.md b/CHANGELOG.md index 86f8529..3b9dc56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 71dbacb..dfeaf22 100644 --- a/README.md +++ b/README.md @@ -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 => [] + | ] | */ diff --git a/config/mailviewer.php b/config/mailviewer.php index eca5f43..eca4ba8 100644 --- a/config/mailviewer.php +++ b/config/mailviewer.php @@ -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 => [] + | ] | */ diff --git a/src/routes/web.php b/routes/web.php similarity index 100% rename from src/routes/web.php rename to routes/web.php diff --git a/src/MailViewer.php b/src/MailViewer.php index 65bf4db..87dc69d 100644 --- a/src/MailViewer.php +++ b/src/MailViewer.php @@ -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; } } diff --git a/src/MailViewerServiceProvider.php b/src/MailViewerServiceProvider.php index fb95ada..fcf69f3 100644 --- a/src/MailViewerServiceProvider.php +++ b/src/MailViewerServiceProvider.php @@ -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() diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index cc3697a..ddf1912 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -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 @@ -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'); diff --git a/tests/Stubs/Mail/TestEmailWithDependencies.php b/tests/Stubs/Mail/TestEmailWithDependencies.php new file mode 100644 index 0000000..f1ef21d --- /dev/null +++ b/tests/Stubs/Mail/TestEmailWithDependencies.php @@ -0,0 +1,32 @@ +view('mailviewer::stubs.emailtestview'); + } +} diff --git a/src/views/index.blade.php b/views/index.blade.php similarity index 100% rename from src/views/index.blade.php rename to views/index.blade.php diff --git a/src/views/stubs/emailtestview.blade.php b/views/stubs/emailtestview.blade.php similarity index 100% rename from src/views/stubs/emailtestview.blade.php rename to views/stubs/emailtestview.blade.php