Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
introwit committed Sep 6, 2018
0 parents commit 2e3fcbf
Show file tree
Hide file tree
Showing 11 changed files with 389 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/vendor
composer.lock
.DS_Store
Thumbs.db
phpunit.xml
/.idea
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
All notable changes to the Laravel Mail Viewer be documented in this file

## v0.1.0 (07-09-2018)
- Initial release
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Jogg Inc <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# View all your mailables at a single place

The Design and content team members often need access to the emails your app will be sending out to the users. This is a fairly simple package that makes it possible and tries to minimize developer dependency. By using this package, you can have a dedicated route to view all your mailables at a single place.

## Installation

You can install this package via composer using this command:

```bash
composer require joggapp/laravel-mail-viewer
```

The package will automatically register itself.

You will have to configure the package settings using the package's config file. You can publish the config file with:

```bash
php artisan vendor:publish --provider="JoggApp\MailViewer\MailViewerServiceProvider" --tag="config"
```

This will create the package's config file called `mailviewer.php` in the `config` directory. These are the contents of the published config file:

```php
return [
/*
|--------------------------------------------------------------------------
| Register the mailables that should be accessible
|--------------------------------------------------------------------------
|
| You have to add the mailables including their dependencies
| in the following array. When asked for a mailable,
| the package will search it here.
|
| Eg: [ new OrderShipped(factory(Order::class)->create()) ]
|
*/

'mailables' => [],

/*
|--------------------------------------------------------------------------
| URL where you want to view the mails
|--------------------------------------------------------------------------
|
| This is the URL where you can view all the mailables
| registered in your application.
|
*/

'url' => 'mails',

/*
|--------------------------------------------------------------------------
| The environments in which the url should be accesible
|--------------------------------------------------------------------------
|
| If you don't want to use this package in production env
| at all, you can restrict that using this option
| rather than by using a middleware.
|
*/

'allowed_environments' => ['local', 'staging'],

/*
|--------------------------------------------------------------------------
| Middlewares that should be applied to the URL
|--------------------------------------------------------------------------
|
| The value should be an array of fully qualified
| class names of the middlware classes.
|
| Eg: [Authenticate::class, CheckForMaintenanceMode::class]
| Don't forget to import the classes at the top!
|
*/

'middlewares' => [],
];
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

## Security

If you discover any security related issues, please email [[email protected]](mailto:[email protected]) instead of using the issue tracker.

## Credits

- [Harish Toshniwal](https://github.com/introwit)
- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.txt) for more information.
33 changes: 33 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "joggapp/laravel-mail-viewer",
"description": "View all your mailables at a single place",
"license": "MIT",
"authors": [
{
"name": "Harish Toshniwal",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"JoggApp\\MailViewer\\": "src"
}
},
"require": {
"php": "^7.1",
"illuminate/routing": "5.6.*|5.7.*",
"illuminate/support": "5.6.*|5.7.*"
},
"extra": {
"laravel": {
"providers": [
"JoggApp\\MailViewer\\MailViewerServiceProvider"
]
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
58 changes: 58 additions & 0 deletions config/mailviewer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Register the mailables that should be accessible
|--------------------------------------------------------------------------
|
| You have to add the mailables including their dependencies
| in the following array. When asked for a mailable,
| the package will search it here.
|
| Eg: [ new OrderShipped(factory(Order::class)->create()) ]
|
*/

'mailables' => [],

/*
|--------------------------------------------------------------------------
| URL where you want to view the mails
|--------------------------------------------------------------------------
|
| This is the URL where you can view all the mailables
| registered in your application.
|
*/

'url' => 'mails',

/*
|--------------------------------------------------------------------------
| The environments in which the url should be accesible
|--------------------------------------------------------------------------
|
| If you don't want to use this package in production env
| at all, you can restrict that using this option
| rather than by using a middleware.
|
*/

'allowed_environments' => ['local', 'staging'],

/*
|--------------------------------------------------------------------------
| Middlewares that should be applied to the URL
|--------------------------------------------------------------------------
|
| The value should be an array of fully qualified
| class names of the middlware classes.
|
| Eg: [Authenticate::class, CheckForMaintenanceMode::class]
| Don't forget to import the classes at the top!
|
*/

'middlewares' => [],
];
58 changes: 58 additions & 0 deletions src/MailViewer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace JoggApp\MailViewer;

use Exception;
use Illuminate\Support\Facades\Config;
use ReflectionClass;

class MailViewer
{
public static function all()
{
$files = scandir(app_path('Mail'));

$mails = [];

foreach ($files as $file) {
$mails[] = str_before($file, '.');
}

return array_slice($mails, 2);
}

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

if ($reflection->getShortName() === $mail) {
return $mailable;
}
}

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

public static function url()
{
$url = config('mailviewer.url', 'mails');

if ((!empty($url)) && is_string($url)) {
return $url;
}

throw new Exception('Please set a valid URL to view the mails');
}

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

if (is_array($middlewares)) {
return $middlewares;
}

throw new Exception('The middlewares config value only excepts an array');
}
}
24 changes: 24 additions & 0 deletions src/MailViewerServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace JoggApp\MailViewer;

use Illuminate\Support\ServiceProvider;

class MailViewerServiceProvider extends ServiceProvider
{
public function boot()
{
$this->publishes([
__DIR__ . '/../config/mailviewer.php' => config_path('mailviewer.php'),
]);

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

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

public function register()
{
//
}
}
30 changes: 30 additions & 0 deletions src/controllers/MailViewerController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace JoggApp\MailViewer\Controllers;

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\App;
use JoggApp\MailViewer\MailViewer;

class MailViewerController extends Controller
{
public function __construct()
{
abort_unless(
App::environment(config('mailviewer.allowed_environments', ['local'])),
403
);
}

public function index()
{
$mails = MailViewer::all();

return view('mailviewer::index', compact('mails'));
}

public function show($mail)
{
return MailViewer::find($mail);
}
}
11 changes: 11 additions & 0 deletions src/routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Illuminate\Support\Facades\Route;
use JoggApp\MailViewer\MailViewer;

Route::get(MailViewer::url(), 'JoggApp\MailViewer\Controllers\MailViewerController@index')
->middleware(MailViewer::middlewares());

Route::get(MailViewer::url() . '/{mail}', 'JoggApp\MailViewer\Controllers\MailViewerController@show')
->middleware(MailViewer::middlewares())
->name('mv-mailviewer');
Loading

0 comments on commit 2e3fcbf

Please sign in to comment.