Skip to content

Commit 37d35d5

Browse files
authored
Merge branch 'master' into feature/update-to-laravel-6
2 parents 1e27215 + 5066292 commit 37d35d5

File tree

12 files changed

+54
-266
lines changed

12 files changed

+54
-266
lines changed

.github/FUNDING.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# These are supported funding model platforms
2+
3+
github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4+
patreon: mikebronner
5+
open_collective: # Replace with a single Open Collective username
6+
ko_fi: # Replace with a single Ko-fi username
7+
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8+
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9+
liberapay: # Replace with a single Liberapay username
10+
issuehunt: # Replace with a single IssueHunt username
11+
otechie: # Replace with a single Otechie username
12+
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ addons:
88
php:
99
- 7.1.3
1010
- 7.2
11+
- 7.3
1112

1213
before_script:
1314
- travis_retry composer self-update

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
### Removed
77
- compatibility with older versions of Laravel.
88

9+
## [0.8.3] - 2019-06-30
10+
### Added
11+
- support for Spatie's `laravel-csp` package.
12+
13+
## [0.8.2] - 2019-06-30
14+
### Changed
15+
- method of checking registered middleware groups to use `hasMiddlewareGroup()`.
16+
917
## [0.6.12] - 5 Aug 2018
1018
### Fixed
1119
- middleware response to be a view instead of string. Thanks @dallincoons, #96 #95.

README.md

Lines changed: 1 addition & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
![Coffee for Laravel](https://github.com/GeneaLabs/laravel-caffeine/blob/master/caffeine.jpg)
2-
31
# Caffeine for Laravel
42
[![Join the chat at https://gitter.im/GeneaLabs/laravel-caffeine](https://badges.gitter.im/GeneaLabs/laravel-caffeine.svg)](https://gitter.im/GeneaLabs/laravel-caffeine?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
53
[![Travis](https://img.shields.io/travis/GeneaLabs/laravel-caffeine.svg)](https://travis-ci.org/GeneaLabs/laravel-caffeine)
@@ -8,222 +6,4 @@
86
[![GitHub (pre-)release](https://img.shields.io/github/release/GeneaLabs/laravel-caffeine/all.svg)](https://github.com/GeneaLabs/laravel-caffeine)
97
[![Packagist](https://img.shields.io/packagist/dt/GeneaLabs/laravel-caffeine.svg)](https://packagist.org/packages/genealabs/laravel-caffeine)
108

11-
## Goal
12-
Prevent forms from timing out when submitting them after leaving them on-screen
13-
for a considerable amount of time. (Laravel defaults to 120 minutes, but that
14-
is configurable and could be different site-by-site.)
15-
16-
## Implementation
17-
To achieve this, we are sending a caffeine-drip (a request at regular intervals)
18-
to keep the session from timing out.
19-
This is only implemented on pages with a `_token` field, so all other pages will
20-
time-out as normal.
21-
22-
## Reasoning
23-
I chose this approach to keep the integrity of site-security, by avoiding the
24-
following:
25-
- exposing the CSRF Token on an unsecured endpoint.
26-
- eliminating CSRF Token validation on specific routes, or even altogether.
27-
- removing session-timeout on all pages.
28-
29-
## Considerations
30-
### Routes
31-
This package adds the routes under `genealabs/laravel-caffeine`.
32-
33-
### Dependencies
34-
- Your project must be running one of the following Laravel versions:
35-
- 5.1 (LTS)
36-
- 5.3
37-
- 5.4
38-
- 5.5 (LTS)
39-
- 5.6
40-
- PHP 7.1.3 or higher.
41-
42-
## Installation
43-
For Laravel 5.2, follow the directions here: https://github.com/GeneaLabs/laravel-caffeine/tree/166e2ca08af7cc62a59360f33e03d1cb8478df6a
44-
45-
1. Install the package:
46-
```sh
47-
composer require genealabs/laravel-caffeine
48-
```
49-
50-
2. **This is only required for Laravel 5.4 or below:**
51-
Add the service provider entry in `config/app.php`:
52-
```php
53-
// 'providers' => [
54-
GeneaLabs\LaravelCaffeine\Providers\Service::class,
55-
// ],
56-
```
57-
58-
3. If you are running 5.5 or above, remove the providers entry from `config/app.php`.
59-
4. If you have previously registered the middleware, please remove the following
60-
middleware from `app/Http/Kernel.php`:
61-
```php
62-
// protected $middleware = [
63-
GeneaLabs\LaravelCaffeine\Http\Middleware\LaravelCaffeineDripMiddleware::class,
64-
// ];
65-
```
66-
67-
## Upgrade Notes
68-
### 0.6.0
69-
This update changes the config file setting names. Please delete the published
70-
config file `config/genealabs-laravel-caffeine.php` if it exists, and follow the
71-
configuration instructions below.
72-
73-
## Configuration
74-
```php
75-
return [
76-
/*
77-
|--------------------------------------------------------------------------
78-
| Drip Interval
79-
|--------------------------------------------------------------------------
80-
|
81-
| Here you may configure the interval with which Caffeine for Laravel
82-
| keeps the session alive. By default this is 5 minutes (expressed
83-
| in milliseconds). This needs to be shorter than your session
84-
| lifetime value configured set in "config/session.php".
85-
|
86-
| Default: 300000 (int)
87-
|
88-
*/
89-
'drip-interval' => 300000,
90-
91-
/*
92-
|--------------------------------------------------------------------------
93-
| Domain
94-
|--------------------------------------------------------------------------
95-
|
96-
| You may optionally configure a separate domain that you are running
97-
| Caffeine for Laravel on. This may be of interest if you have a
98-
| monitoring service that queries other apps. Setting this to
99-
| null will use the domain of the current application.
100-
|
101-
| Default: null (null|string)
102-
|
103-
*/
104-
'domain' => null,
105-
106-
/*
107-
|--------------------------------------------------------------------------
108-
| Drip Endpoint URL
109-
|--------------------------------------------------------------------------
110-
|
111-
| Sometimes you may wish to white-label your app and not expose the AJAX
112-
| request URLs as belonging to this package. To achieve that you can
113-
| rename the URL used for dripping caffeine into your application.
114-
|
115-
| Default: 'genealabs/laravel-caffeine/drip' (string)
116-
|
117-
*/
118-
'route' => 'genealabs/laravel-caffeine/drip', // Customizable end-point URL
119-
120-
/*
121-
|--------------------------------------------------------------------------
122-
| Checking for Lapsed Drips
123-
|--------------------------------------------------------------------------
124-
|
125-
| If the browser is put to sleep on (for example on mobil devices or
126-
| laptops), it will still cause an error when trying to submit the
127-
| form. To avoid this, we force-reload the form 2 minutes prior
128-
| to session time-out or later. Setting this setting to 0
129-
| will disable this check if you don't want to use it.
130-
|
131-
| Default: 2000 (int)
132-
|
133-
*/
134-
'outdated-drip-check-interval' => 2000,
135-
136-
/*
137-
|--------------------------------------------------------------------------
138-
| Use Route Middleware
139-
|--------------------------------------------------------------------------
140-
|
141-
| Drips are enabled via route middleware instead of global middleware.
142-
|
143-
| Default: false (bool)
144-
|
145-
*/
146-
'use-route-middleware' => false,
147-
148-
];
149-
```
150-
151-
___Only publish the config file if you need to customize it___:
152-
```sh
153-
php artisan caffeine:publish --config
154-
```
155-
156-
## Usage
157-
That was it! It will apply itself automatically where it finds a form with a
158-
`_token` field, or a meta tag named "csrf-token", while pages are open in
159-
browsers.
160-
161-
### Prevent Caffeination
162-
There are two methods to prevent Caffeine for Laravel from dripping to keep the
163-
session alive: disabling it in Blade using the meta tag method, or enabling
164-
route-middleware mode, and then only enabling it on routes or route groups.
165-
166-
#### Meta Tag Method
167-
If you would like to prevent a certain page from caffeinating your application,
168-
then add the following meta tag:
169-
```php
170-
<meta name="caffeinated" content="false">
171-
```
172-
173-
#### Route Middleware Method
174-
To enable this mode, you need to publish the configuration file (see the
175-
configuration section above) and then set `use-route-middleware` to `true`. This
176-
will disable the default global middleware mode (which applies it to any page
177-
that has the CSRF token in it across your entire application). Now you need to
178-
selectively enable Caffeine on a given route or route group using route
179-
middleware:
180-
181-
```php
182-
Route::any('test', 'TestController@test')->middleware('caffeinated');
183-
184-
Route::group(['middleware' => ['caffeinated']], function () {
185-
Route::any('test', 'TestController@test');
186-
})
187-
```
188-
189-
You can still use the route middleware method and apply it globally to all
190-
routes by editing `app/Http/Kernel.php` and adding it to the `web` middleware
191-
group. Although you should only use this option if you have a very specific use-
192-
case that prevents you from utilizing the default global middleware option.
193-
194-
__This will only have effect if the page includes a form. If not, the page will
195-
not caffeinate your application anyway.__
196-
197-
# The Fine Print
198-
## Commitment to Quality
199-
During package development I try as best as possible to embrace good design and
200-
development practices to try to ensure that this package is as good as it can
201-
be. My checklist for package development includes:
202-
203-
- ✅ Achieve as close to 100% code coverage as possible using unit tests.
204-
- ✅ Eliminate any issues identified by SensioLabs Insight and Scrutinizer.
205-
- ✅ Be fully PSR1, PSR2, and PSR4 compliant.
206-
- ✅ Include comprehensive documentation in README.md.
207-
- ✅ Provide an up-to-date CHANGELOG.md which adheres to the format outlined
208-
at <http://keepachangelog.com>.
209-
- ✅ Have no PHPMD or PHPCS warnings throughout all code.
210-
211-
## Contributing
212-
Please observe and respect all aspects of the included Code of Conduct
213-
<https://github.com/GeneaLabs/laravel-caffeine/blob/master/CODE_OF_CONDUCT.md>.
214-
215-
### Reporting Issues
216-
When reporting issues, please fill out the included template as completely as
217-
possible. Incomplete issues may be ignored or closed if there is not enough
218-
information included to be actionable.
219-
220-
### Submitting Pull Requests
221-
Please review the Contribution Guidelines <https://github.com/GeneaLabs/laravel-caffeine/blob/master/CONTRIBUTING.md>.
222-
Only PRs that meet all criterium will be accepted.
223-
224-
## ❤️ Open-Source Software - Give ⭐️
225-
We have included the awesome `symfony/thanks` composer package as a dev
226-
dependency. Let your OS package maintainers know you appreciate them by starring
227-
the packages you use. Simply run `composer thanks` after installing this
228-
package. (And not to worry, since it's a dev-dependency it won't be installed in
229-
your live environment.)
9+
Documentation has moved to https://genealabs.com/docs/laravel-caffeine/.

resources/views/script.blade.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
@if (function_exists('csp_nonce'))
2+
<script nonce="{{ csp_nonce() }}">
3+
@else
14
<script>
5+
@endif
26
var lastCheck = new Date();
37
var caffeineSendDrip = function () {
48
var ajax = window.XMLHttpRequest
@@ -14,7 +18,7 @@
1418
ajax.open('GET', '{{ $url }}');
1519
ajax.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
1620
ajax.send();
17-
}
21+
};
1822
1923
setInterval(function () {
2024
caffeineSendDrip();

src/Dripper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ class Dripper extends Model
66
{
77
public function getHtmlAttribute() : string
88
{
9-
$html = (string) view('genealabs-laravel-caffeine::script')
9+
return (string) view('genealabs-laravel-caffeine::script')
1010
->with([
1111
'ageCheckInterval' => $this->ageCheckInterval,
1212
'ageThreshold' => $this->ageThreshold,
1313
'interval' => $this->interval,
1414
'url' => $this->url,
1515
]);
16-
return $html;
1716
}
1817

1918
public function getAgeCheckIntervalAttribute() : int

src/Http/Middleware/LaravelCaffeineDripMiddleware.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ public function handle(Request $request, Closure $next)
4242
}
4343

4444
$dripper = (new Dripper);
45-
$content = str_replace(
46-
'</body>',
47-
"{$dripper->html}</body>",
48-
$content
45+
$content = preg_replace(
46+
'/(<\/body\>?|<\/html\>?|\Z)/',
47+
$dripper->html . '$1',
48+
$content,
49+
1
4950
);
5051
$original = $response->original;
5152
$response->setContent($content);

src/Providers/Service.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Service extends ServiceProvider
1010
{
1111
public function boot()
1212
{
13-
app('router')->group($this->middlewareGroupExists('web')
13+
app('router')->group(app("router")->hasMiddlewareGroup('web')
1414
? ['middleware' => 'web']
1515
: [], function () {
1616
require __DIR__ . '/../../routes/web.php';
@@ -53,29 +53,12 @@ public function boot()
5353
}
5454
}
5555

56-
protected function middlewareGroupExists(string $group) : bool
57-
{
58-
$routes = collect(app('router')->getRoutes()->getRoutes());
59-
60-
return $routes->reduce(function ($carry, Route $route) use ($group) {
61-
$carry = ($carry ?? false) ?: false;
62-
$actions = (array) $route->getAction();
63-
64-
if (array_key_exists('middleware', $actions)
65-
&& in_array($group, (array) $actions['middleware'])
66-
) {
67-
return true;
68-
}
69-
70-
return $carry;
71-
}) ?? false;
72-
}
73-
7456
protected function shouldRegisterGlobalMiddleware() : bool
7557
{
7658
return (! request()->ajax()
7759
&& ! $this->shouldRegisterRouteMiddleware()
7860
&& (php_sapi_name() === 'fpm-fcgi'
61+
|| php_sapi_name() === 'cgi-fcgi'
7962
|| php_sapi_name() === 'apache2handler'
8063
|| config("app.env") === 'internaltesting'));
8164
}

tests/Browser/DripTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<?php namespace GeneaLabs\LaravelCaffeine\Tests\Browser;
22

3-
use GeneaLabs\LaravelCaffeine\Tests\BrowserTestCase;
4-
use Laravel\Dusk\Browser;
3+
// use GeneaLabs\LaravelCaffeine\Tests\BrowserTestCase;
4+
// use Laravel\Dusk\Browser;
55

6-
class DripTest extends BrowserTestCase
7-
{
8-
public function testFormDoesntExpire()
9-
{
10-
$this->browse(function (Browser $browser) {
11-
$response = $browser->visit('tests/form');
6+
// class DripTest extends BrowserTestCase
7+
// {
8+
// public function testFormDoesntExpire()
9+
// {
10+
// $this->browse(function (Browser $browser) {
11+
// $response = $browser->visit('tests/form');
1212

13-
$response->assertSee('Test Form');
14-
});
15-
}
16-
}
13+
// $response->assertSee('Test Form');
14+
// });
15+
// }
16+
// }

tests/BrowserTestCase.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php namespace GeneaLabs\LaravelCaffeine\Tests;
22

3-
use Orchestra\Testbench\Dusk\TestCase as BaseTestCase;
3+
// use Orchestra\Testbench\Dusk\TestCase as BaseTestCase;
44

5-
abstract class BrowserTestCase extends BaseTestCase
6-
{
7-
use CreatesApplication;
8-
}
5+
// abstract class BrowserTestCase extends BaseTestCase
6+
// {
7+
// use CreatesApplication;
8+
// }

0 commit comments

Comments
 (0)