Skip to content

Commit

Permalink
0.9.007
Browse files Browse the repository at this point in the history
  • Loading branch information
andiwijaya-dev committed Jul 18, 2021
1 parent a151393 commit c4d95e5
Show file tree
Hide file tree
Showing 29 changed files with 1,015 additions and 86 deletions.
16 changes: 16 additions & 0 deletions src/CustomMailServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Andiwijaya\WebApp;

use Illuminate\Mail\MailServiceProvider;
use Illuminate\Support\ServiceProvider;

class CustomMailServiceProvider extends MailServiceProvider
{
protected function registerSwiftTransport()
{
$this->app->singleton('swift.transport', function () {
return new CustomTransportManager($this->app);
});
}
}
32 changes: 32 additions & 0 deletions src/CustomTransportManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Andiwijaya\WebApp;

use Andiwijaya\WebApp\Models\Config;
use Illuminate\Mail\TransportManager;

class CustomTransportManager extends TransportManager{

public function __construct($app)
{
$this->app = $app;

$configs = Config::where('key', 'like', 'smtp%')->get()->groupBy('key');

$this->app['config']['mail'] = [
'driver' => config('mail.driver'),
'host' => $configs['smtp.host'][0]->value ?? config('mail.host'),
'port' => $configs['smtp.port'][0]->value ?? config('mail.port'),
'from' => [
'address' => config('mail.from.address'),
'name' => config('mail.from.name')
],
'encryption' => $configs['smtp.encryption'][0]->value ?? config('mail.encryption'),
'username' => $configs['smtp.username'][0]->value ?? config('mail.username'),
'password' => $configs['smtp.password'][0]->value ?? config('mail.password'),
'sendmail' => config('mail.sendmail'),
'pretend' => config('mail.pretend')
];
}

}
12 changes: 8 additions & 4 deletions src/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
use App\Notifications\LogToSlackNotification;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException;
use Illuminate\Http\Exceptions\PostTooLargeException;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Session;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class Handler extends ExceptionHandler
{
protected $dontReport = [
//
UserException::class,
NotFoundHttpException::class,
];

protected $dontFlash = [
Expand All @@ -27,14 +30,12 @@ class Handler extends ExceptionHandler

public function report(Exception $exception)
{
if($exception instanceof UserException) return;

$message = substr($exception->getMessage(), 0, 255);
$traces = $exception->getTraceAsString();

SysLog::create([
'type'=>SysLog::TYPE_ERROR,
'message'=>$message,
'message'=>strlen($message) <= 0 ? get_class($exception) : $message,
'data'=>[
'console'=>app()->runningInConsole(),
'session_id'=>Session::getId(),
Expand Down Expand Up @@ -96,6 +97,9 @@ public function render($request, Exception $exception)
else if($exception->getMessage() == 'Login required')
return response()->json([ 'script'=>"window.location = '/login';" ]);

else if($exception instanceof MaintenanceModeException)
return response()->json([ 'error'=>0, 'message'=>'Maintenance mode' ]);

else{

$traces = [];
Expand Down
63 changes: 63 additions & 0 deletions src/Exports/GenericExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Andiwijaya\WebApp\Exports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Events\AfterSheet;

class GenericExport implements FromCollection, WithHeadings, ShouldAutoSize, WithEvents
{
protected $columns;
protected $data;

/**
* @return Collection
*/
public function collection()
{
$arr = collect([]);
foreach($this->data as $data){

$obj = [];
foreach($this->columns as $idx=>$column)
$obj[] = $data[$idx] ?? '';

$arr->add($obj);
}

return $arr;
}

public function __construct($columns, $data)
{
$this->columns = $columns;
$this->data = $data;
}

public function headings(): array
{
return $this->columns;
}

public function registerEvents(): array
{
return [
BeforeExport::class => function(BeforeExport $event) {
$event->writer->setCreator(env('APP_NAME'));
},
AfterSheet::class => function(AfterSheet $event) {

$event->sheet->styleCells(
'A1:Z1',
[
'font'=>[ 'bold'=>true ]
]
);
},
];
}
}
31 changes: 27 additions & 4 deletions src/Http/Controllers/ActionableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function index(Request $request){

$action = isset(($actions = explode('|', $request->input('action', 'view')))[0]) ? $actions[0] : '';
$method = action2method($action);
if(method_exists($this, $method))
if(method_exists($this, $method) && $this->validateDocComment($request, $method))
return call_user_func_array([ $this, $method ], func_get_args());
}

Expand All @@ -33,7 +33,7 @@ public function store(Request $request){

$action = isset(($actions = explode('|', $request->input('action', 'save')))[0]) ? $actions[0] : '';
$method = action2method($action);
if(method_exists($this, $method))
if(method_exists($this, $method) && $this->validateDocComment($request, $method))
return call_user_func_array([ $this, $method ], func_get_args());
}

Expand All @@ -45,7 +45,7 @@ public function show(Request $request, $id){

$action = isset(($actions = explode('|', $request->input('action', 'open')))[0]) ? $actions[0] : '';
$method = action2method($action);
if(method_exists($this, $method))
if(method_exists($this, $method) && $this->validateDocComment($request, $method))
return call_user_func_array([ $this, $method ], func_get_args());
}

Expand All @@ -57,7 +57,7 @@ public function patch(Request $request){

$action = isset(($actions = explode('|', $request->input('action', 'patch')))[0]) ? $actions[0] : '';
$method = action2method($action);
if(method_exists($this, $method))
if(method_exists($this, $method) && $this->validateDocComment($request, $method))
return call_user_func_array([ $this, $method ], func_get_args());
}

Expand Down Expand Up @@ -91,4 +91,27 @@ public function validate(Request $request, array $rules, array $messages = [], a
exc(implode("<br />\n", $validator->errors()->all()));
}
}

public function validateDocComment(Request $request, $method)
{
$comment_string = (new \ReflectionClass($this))->getMethod($method)->getDocComment();

$obj = [];
preg_match_all('/@(\w+) (.*)/', $comment_string, $matches);
if(isset($matches[1][0])){
foreach($matches[1] as $idx=>$key){
$obj[$key] = str_replace([ '\'', '"' ], '', $matches[2][$idx]);
}
}

if(isset($obj['method']) && strtolower($obj['method']) != strtolower($request->getMethod()))
return false;

if(isset($obj['ajax'])){
if($obj['ajax'] && !$request->ajax()) return false;
if(!$obj['ajax'] && $request->ajax()) return false;
}

return true;
}
}
Loading

0 comments on commit c4d95e5

Please sign in to comment.