Skip to content

Commit 3411ce4

Browse files
authored
Merge pull request #73 from GeneaLabs/Laravel-5.5
Integration of PR #72
2 parents 39d5b7a + dc4731a commit 3411ce4

File tree

11 files changed

+290
-68
lines changed

11 files changed

+290
-68
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Change Log
2+
## [0.6.0] - 9 Dec 2017
3+
### Added
4+
- drip timeout check and force page refresh if timeout occurred.
5+
6+
### Changed
7+
- config file setting names to be more explicit.
8+
- middleware is injected only when called from a web page or during testing.

README.md

Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ I chose this approach to keep the integrity of site-security, by avoiding the
2929

3030
## Considerations
3131
### Routes
32-
This package adds the routes under `genealabs/laravel-caffeine`. Please verify
33-
that these don't collide with your existing routes.
32+
This package adds the routes under `genealabs/laravel-caffeine`.
3433

3534
### Dependencies
3635
- Your project must be running one of the following Laravel versions:
@@ -48,7 +47,6 @@ For Laravel 5.2, follow the directions here: https://github.com/GeneaLabs/larave
4847
composer require genealabs/laravel-caffeine
4948
```
5049

51-
5250
2. **This is only required for Laravel 5.4 or below:**
5351
Add the service provider entry in `config/app.php`:
5452
```php
@@ -57,32 +55,93 @@ For Laravel 5.2, follow the directions here: https://github.com/GeneaLabs/larave
5755
// ],
5856
```
5957

60-
3. If you have previously registered the middleware, please remove the following
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
6160
middleware from `app/Http/Kernel.php`:
6261
```php
6362
// protected $middleware = [
6463
GeneaLabs\LaravelCaffeine\Http\Middleware\LaravelCaffeineDripMiddleware::class,
6564
// ];
6665
```
6766

67+
## Upgrad 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+
6873
## Configuration
69-
The following elements are configurable:
70-
- **domain:** (default: `url('/')`) Change to point to a different domain than
71-
your app. This is useful if you are behind a proxy or load-balancer. ___Do not use
72-
the `url()` helper in the config file.___
73-
- **route:** (default: `genealabs/laravel-caffeine/drip`) Change to customize
74-
the drip URL in the browser. This is just cosmetic.
75-
- **dripIntervalInMilliSeconds:** (default: 5 mins) Change to configure the drip
76-
interval.
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+
'dripInterval' => 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+
'outdatedDripCheckInterval' => 2000,
135+
136+
];
137+
```
77138

78139
___Only publish the config file if you need to customize it___:
79140
```sh
80-
php artisan vendor:publish --tag=genealabs-laravel-caffeine
141+
php artisan caffeine:publish --config
81142
```
82143

83-
You can now change the default value in `config/genealabs-laravel-caffeine.php` as desired. Deleting the
84-
`config/genealabs-laravel-caffeine.php` file will revert back to the default settings.
85-
86144
## Usage
87-
That was it! It will apply itself automatically where it finds a form with a `_token` field, or a meta tag named
88-
"csrf-token", while pages are open in browsers.
145+
That was it! It will apply itself automatically where it finds a form with a
146+
`_token` field, or a meta tag named "csrf-token", while pages are open in
147+
browsers.
Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,64 @@
11
<?php
22

33
return [
4-
'dripIntervalInMilliSeconds' => 300000, // Drip every 5 minutes
5-
'domain' => null, // defaults to url('/')
6-
'route' => 'genealabs/laravel-caffeine/drip', // Can be customized
7-
'thresholdDifference' => 10000, // When the drip will be considered old to reload the page
8-
'checkLastDripInterval' => 2000 // How often we will check if the drip is old
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Drip Interval
7+
|--------------------------------------------------------------------------
8+
|
9+
| Here you may configure the interval with which Caffeine for Laravel
10+
| keeps the session alive. By default this is 5 minutes (expressed
11+
| in milliseconds). This needs to be shorter than your session
12+
| lifetime value configured set in "config/session.php".
13+
|
14+
| Default: 300000 (int)
15+
|
16+
*/
17+
'dripInterval' => 300000,
18+
19+
/*
20+
|--------------------------------------------------------------------------
21+
| Domain
22+
|--------------------------------------------------------------------------
23+
|
24+
| You may optionally configure a separate domain that you are running
25+
| Caffeine for Laravel on. This may be of interest if you have a
26+
| monitoring service that queries other apps. Setting this to
27+
| null will use the domain of the current application.
28+
|
29+
| Default: null (null|string)
30+
|
31+
*/
32+
'domain' => null,
33+
34+
/*
35+
|--------------------------------------------------------------------------
36+
| Drip Endpoint URL
37+
|--------------------------------------------------------------------------
38+
|
39+
| Sometimes you may wish to white-label your app and not expose the AJAX
40+
| request URLs as belonging to this package. To achieve that you can
41+
| rename the URL used for dripping caffeine into your application.
42+
|
43+
| Default: 'genealabs/laravel-caffeine/drip' (string)
44+
|
45+
*/
46+
'route' => 'genealabs/laravel-caffeine/drip', // Customizable end-point URL
47+
48+
/*
49+
|--------------------------------------------------------------------------
50+
| Checking for Lapsed Drips
51+
|--------------------------------------------------------------------------
52+
|
53+
| If the browser is put to sleep on (for example on mobil devices or
54+
| laptops), it will still cause an error when trying to submit the
55+
| form. To avoid this, we force-reload the form 2 minutes prior
56+
| to session time-out or later. Setting this setting to 0
57+
| will disable this check if you don't want to use it.
58+
|
59+
| Default: 2000 (int)
60+
|
61+
*/
62+
'outdatedDripCheckInterval' => 2000,
63+
964
];

resources/views/script.blade.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<script>
2+
let lastCheck = new Date();
3+
const caffeineSendDrip = function () {
4+
const ajax = window.XMLHttpRequest
5+
? new XMLHttpRequest
6+
: new ActiveXObject('Microsoft.XMLHTTP');
7+
8+
ajax.onreadystatechange = function () {
9+
if (ajax.readyState === 4 && ajax.status === 204) {
10+
lastCheck = new Date();
11+
}
12+
};
13+
14+
ajax.open('GET', '{{ $url }}');
15+
ajax.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
16+
ajax.send();
17+
}
18+
19+
setInterval(function () {
20+
caffeineSendDrip();
21+
}, {{ $interval }});
22+
23+
if ({{ $ageCheckInterval }} > 0) {
24+
setInterval(function () {
25+
if (new Date() - lastCheck >= {{ $ageCheckInterval + $ageThreshold }}) {
26+
location.reload(true);
27+
}
28+
}, {{ $ageCheckInterval }});
29+
}
30+
</script>

src/Dripper.php

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,38 @@
22

33
use Jenssegers\Model\Model;
44

5-
/**
6-
* @property string $html
7-
* @property string $interval
8-
* @property string $url
9-
*/
105
class Dripper extends Model
116
{
127
public function getHtmlAttribute() : string
138
{
14-
15-
return '<script>'
16-
. "let ld = new Date();"
17-
. "function caffeineSendDrip () {"
18-
. " let e = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject('Microsoft.XMLHTTP');"
19-
. " e.onreadystatechange = function () {"
20-
. " if (e.readyState === 4 && e.status === 204) {"
21-
. " ld = new Date();"
22-
. " }"
23-
. " };"
24-
. " e.open('GET', '{$this->url}', !0);"
25-
. " e.setRequestHeader('X-Requested-With', 'XMLHttpRequest');"
26-
. " e.send();"
27-
. "}"
28-
. "setInterval(function () { caffeineSendDrip(); }, $this->interval);"
29-
. "setInterval(function () {"
30-
. " if (new Date() - ld >= $this->interval + $this->threshold) {"
31-
. " location.reload(true);"
32-
. " }"
33-
. "}, $this->checkInterval);"
34-
. "</script>";
9+
$html = (string) view('genealabs-laravel-caffeine::script')
10+
->with([
11+
'ageCheckInterval' => $this->ageCheckInterval,
12+
'ageThreshold' => $this->ageThreshold,
13+
'interval' => $this->interval,
14+
'url' => $this->url,
15+
]);
16+
return $html;
3517
}
3618

37-
public function getIntervalAttribute() : string
19+
public function getAgeCheckIntervalAttribute() : int
3820
{
3921
return config(
40-
'genealabs-laravel-caffeine.dripIntervalInMilliSeconds',
41-
300000
22+
'genealabs-laravel-caffeine.outdatedDripCheckInterval',
23+
2000
4224
);
4325
}
44-
45-
public function getThresholdAttribute() : int
26+
27+
public function getAgeThresholdAttribute() : int
4628
{
47-
return config(
48-
'genealabs-laravel-caffeine.thresholdDifference',
49-
10000
50-
);
29+
return (config('session.lifetime', 32) - 2) * 60000;
5130
}
52-
53-
public function getCheckIntervalAttribute() : int
31+
32+
public function getIntervalAttribute() : string
5433
{
5534
return config(
56-
'genealabs-laravel-caffeine.checkLastDripInterval',
57-
2000
35+
'genealabs-laravel-caffeine.dripInterval',
36+
300000
5837
);
5938
}
6039

src/Providers/Service.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
use GeneaLabs\LaravelCaffeine\Console\Commands\Publish;
44
use GeneaLabs\LaravelCaffeine\Http\Middleware\LaravelCaffeineDripMiddleware;
5-
use Illuminate\Support\ServiceProvider;
5+
use Illuminate\Contracts\Http\Kernel;
66
use Illuminate\Routing\Route;
7+
use Illuminate\Support\ServiceProvider;
78

89
class Service extends ServiceProvider
910
{
@@ -33,7 +34,9 @@ public function register()
3334
$this->commands(Publish::class);
3435
$this->mergeConfigFrom(__DIR__ . '/../../config/genealabs-laravel-caffeine.php', 'genealabs-laravel-caffeine');
3536

36-
app('Illuminate\Contracts\Http\Kernel')->pushMiddleware('\GeneaLabs\LaravelCaffeine\Http\Middleware\LaravelCaffeineDripMiddleware');
37+
if ($this->shouldRegisterMiddleware()) {
38+
app(Kernel::class)->pushMiddleware('\\' . LaravelCaffeineDripMiddleware::class);
39+
}
3740
}
3841

3942
public function provides() : array
@@ -58,4 +61,10 @@ private function middlewareGroupExists(string $group) : bool
5861
return $carry;
5962
}) ?? false;
6063
}
64+
65+
private function shouldRegisterMiddleware() : bool
66+
{
67+
return (! request()->ajax()
68+
&& (php_sapi_name() === 'fpm-fcgi' || app('env') === 'testing'));
69+
}
6170
}

tests/Feature/CaffeineTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function testBootstrap3TestPageCanLoad()
1414

1515
public function testMiddlewareInjectsDripScript()
1616
{
17-
$expectedResult = "<script>setInterval(function(){var e=window.XMLHttpRequest?new XMLHttpRequest:new ActiveXObject('Microsoft.XMLHTTP');e.open('GET','/genealabs/laravel-caffeine/drip',!0);e.setRequestHeader('X-Requested-With','XMLHttpRequest');e.send();}, 50000);</script>";
17+
$expectedResult = file_get_contents(__DIR__ . '/../Fixtures/expired_script.txt');
1818

1919
$response = $this->get(route('genealabs-laravel-caffeine.tests.form'));
2020

tests/Fixtures/expired_script.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<script>
2+
let lastCheck = new Date();
3+
const caffeineSendDrip = function () {
4+
const ajax = window.XMLHttpRequest
5+
? new XMLHttpRequest
6+
: new ActiveXObject('Microsoft.XMLHTTP');
7+
8+
ajax.onreadystatechange = function () {
9+
if (ajax.readyState === 4 && ajax.status === 204) {
10+
lastCheck = new Date();
11+
}
12+
};
13+
14+
ajax.open('GET', '/genealabs/laravel-caffeine/drip');
15+
ajax.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
16+
ajax.send();
17+
}
18+
19+
setInterval(function () {
20+
caffeineSendDrip();
21+
}, 300000);
22+
23+
if (2000 > 0) {
24+
setInterval(function () {
25+
if (new Date() - lastCheck >= -58000) {
26+
location.reload(true);
27+
}
28+
}, 2000);
29+
}
30+
</script>

0 commit comments

Comments
 (0)