Skip to content

Commit 0e269f5

Browse files
committed
Initial commit
1 parent b7569d6 commit 0e269f5

13 files changed

+485
-0
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; This file is for unifying the coding style for different editors and IDEs.
2+
; More information at http://editorconfig.org
3+
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
indent_size = 4
9+
indent_style = space
10+
end_of_line = lf
11+
insert_final_newline = true
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.gitattributes

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.gitattributes export-ignore
6+
/.gitignore export-ignore
7+
/.travis.yml export-ignore
8+
/phpunit.xml.dist export-ignore
9+
/.scrutinizer.yml export-ignore
10+
/tests export-ignore
11+
/.editorconfig export-ignore
12+
/docs export-ignore

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
build
2+
composer.lock
3+
docs
4+
vendor
5+
.idea
6+
phpcs.xml
7+
phpunit.xml
8+
.phpunit.result.cache

.scrutinizer.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
filter:
2+
excluded_paths: [tests/*, vendor/*]
3+
4+
checks:
5+
php:
6+
remove_extra_empty_lines: true
7+
remove_php_closing_tag: true
8+
remove_trailing_whitespace: true
9+
fix_use_statements:
10+
remove_unused: true
11+
preserve_multiple: false
12+
preserve_blanklines: true
13+
order_alphabetically: true
14+
fix_php_opening_tag: true
15+
fix_linefeed: true
16+
fix_line_ending: true
17+
fix_identation_4spaces: true
18+
fix_doc_comments: true

.styleci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
preset: psr12

.travis.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: php
2+
3+
php:
4+
- 7.2
5+
- 7.3
6+
- 7.4
7+
8+
env:
9+
matrix:
10+
- Laravel=7.x
11+
- Laravel=6.x
12+
- Laravel=5.8.x
13+
14+
install:
15+
- travis_retry composer require --dev "laravel/framework:${Laravel}" --no-interaction
16+
17+
script:
18+
- vendor/bin/phpunit

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) F9WebLtd <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

composer.json

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"name": "f9webltd/laravel-redirect-response-macros",
3+
"description": "Some useful redirect response macros for your Laravel application",
4+
"keywords": [
5+
"laravel",
6+
"laravel redirect",
7+
"redirect response macros",
8+
"laravel macros"
9+
],
10+
"homepage": "https://github.com/f9webltd/laravel-redirect-response-macros",
11+
"license": "MIT",
12+
"authors": [
13+
{
14+
"name": "Rob Allport",
15+
"email": "[email protected]",
16+
"homepage": "https://www.f9web.co.uk",
17+
"role": "Developer"
18+
}
19+
],
20+
"require": {
21+
"php": "^7.2",
22+
"laravel/framework": "5.8.* || ^6.0 || ^7.0"
23+
},
24+
"require-dev": {
25+
"orchestra/testbench": ">=3.8",
26+
"phpunit/phpunit": "^7.0|^8.0"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"F9Web\\LaravelRedirectResponseMacros\\": "src"
31+
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"F9Web\\LaravelRedirectResponseMacros\\Tests\\": "tests"
36+
}
37+
},
38+
"scripts": {
39+
"test": "vendor/bin/phpunit"
40+
},
41+
"config": {
42+
"sort-packages": true
43+
},
44+
"extra": {
45+
"laravel": {
46+
"providers": [
47+
"F9Web\\LaravelRedirectResponseMacros\\LaravelRedirectResponseMacrosServiceProvider"
48+
]
49+
}
50+
},
51+
"minimum-stability": "dev",
52+
"prefer-stable": true
53+
}

phpunit.xml.dist

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="tap" target="build/report.tap"/>
24+
<log type="junit" target="build/report.junit.xml"/>
25+
<log type="coverage-html" target="build/coverage"/>
26+
<log type="coverage-text" target="build/coverage.txt"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
29+
<php>
30+
<server name="APP_NAME" value="Laravel Redirect Response Macros"/>
31+
</php>
32+
</phpunit>
33+

resources/lang/en/messages.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
5+
'created-min' => 'The record was successfully created',
6+
'created' => 'The record was successfully created. <a href=":url" class="alert-link">View inserted record</a>.',
7+
'updated' => 'The record was successfully updated.',
8+
'deleted' => 'The record was successfully deleted.',
9+
'not-found' => 'Sorry, the record could not be found.',
10+
'authorized' => 'Welcome back, you have been securely logged in',
11+
'un-authorized' => 'You do not have permission to perform that action',
12+
13+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace F9Web\LaravelRedirectResponseMacros;
6+
7+
use Exception;
8+
use Illuminate\Http\RedirectResponse;
9+
use Illuminate\Support\ServiceProvider;
10+
11+
use function trans;
12+
13+
class LaravelRedirectResponseMacrosServiceProvider extends ServiceProvider
14+
{
15+
public function boot()
16+
{
17+
$this->loadTranslationsFrom(__DIR__.'/../resources/lang/', 'f9web-laravel-redirect-response-macros');
18+
19+
$this->publishes(
20+
[
21+
__DIR__.'/../resources/lang' => resource_path('lang/vendor/f9web-laravel-redirect-response-macros'),
22+
]
23+
);
24+
25+
$this->registerMacros();
26+
}
27+
28+
private function registerMacros()
29+
{
30+
$this->registerHelper();
31+
32+
foreach (['success', 'info', 'danger', 'warning'] as $type) {
33+
RedirectResponse::macro(
34+
$type,
35+
function ($message) use ($type) {
36+
return $this->with($type, $message);
37+
}
38+
);
39+
}
40+
41+
$key = 'f9web-laravel-redirect-response-macros::messages.';
42+
43+
RedirectResponse::macro(
44+
'created',
45+
function ($route = null) use ($key) {
46+
$message = null === $route
47+
? trans("{$key}created-min")
48+
: trans("{$key}created", ['url' => $route]);
49+
50+
return $this->with('success', $message);
51+
}
52+
);
53+
54+
RedirectResponse::macro(
55+
'updated',
56+
function ($message = null) use ($key) {
57+
return $this->with(
58+
'success',
59+
$message ?? trans("{$key}updated")
60+
);
61+
}
62+
);
63+
64+
RedirectResponse::macro(
65+
'deleted',
66+
function ($message = null) use ($key) {
67+
return $this->with('success', $message ?? trans("{$key}deleted"));
68+
}
69+
);
70+
71+
RedirectResponse::macro(
72+
'error',
73+
function ($message) {
74+
return $this->with(
75+
'error',
76+
$message = $this->determineMessage($message)
77+
);
78+
}
79+
);
80+
81+
RedirectResponse::macro(
82+
'errorNotFound',
83+
function ($message = null) use ($key) {
84+
$message = $this->determineMessage($message);
85+
86+
return $this->with('error', $message ?? trans("{$key}not-found"));
87+
}
88+
);
89+
90+
RedirectResponse::macro(
91+
'authorized',
92+
function ($message = null) use ($key) {
93+
return $this->with('success', $message ?? trans("{$key}authorized"));
94+
}
95+
);
96+
97+
RedirectResponse::macro(
98+
'unAuthorized',
99+
function ($message = null) use ($key) {
100+
$message = $this->determineMessage($message);
101+
102+
return $this->with('error', $message ?? trans("{$key}un-authorized"));
103+
}
104+
);
105+
}
106+
107+
public function register()
108+
{
109+
//
110+
}
111+
112+
private function registerHelper()
113+
{
114+
RedirectResponse::macro(
115+
'determineMessage',
116+
function ($message = null) {
117+
return $message instanceof Exception
118+
? $message->getMessage()
119+
: ($message ?? null);
120+
}
121+
);
122+
}
123+
}

0 commit comments

Comments
 (0)