From 60d16d0fc8db35a0dd0911e9a86c8778c461c1bc Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 9 Dec 2017 09:22:20 -0800 Subject: [PATCH 1/5] Squashed commit of the following: commit 39d5b7a37124884f673c2e0bc1d9d8c8b4969185 Merge: 986d780 fe3c184 Author: Mike Bronner Date: Sat Dec 9 09:19:17 2017 -0800 Merge pull request #71 from ItaloBC/ItaloBC-ReloadOnOldDrip Reload the Page if we miss the Drip commit fe3c1848b060fe781bbb5f5a5f69312fc52d8dca Author: ItaloBC Date: Sat Dec 9 12:35:33 2017 -0300 Fixed the calc between old/missed drips Now it works. commit 37426aa3530f981f1e5f2038978296bb7d6f5c1c Author: ItaloBC Date: Sat Dec 9 12:18:26 2017 -0300 Wtf was I doing? threshold is fine by itself commit 32fd4f6d2cdcecc6d643883614307c829147c12b Author: ItaloBC Date: Sat Dec 9 12:15:03 2017 -0300 Forgot to add the `interval` to the `threshold` commit bdba85f0cc10b6c7537c5485b86f55bd14b6f0c6 Author: ItaloBC Date: Sat Dec 9 12:11:55 2017 -0300 Changed variable to something more criptic commit bb8eac739ec8e362550004ea6dce986cedb3f568 Author: ItaloBC Date: Sat Dec 9 12:09:18 2017 -0300 Fixed the setInterval for dripping commit 94aeedf2864c767a4e9d4307471bd60387c82201 Author: ItaloBC Date: Sat Dec 9 11:48:15 2017 -0300 Threshold default now matches the config default commit 154d9b95a6515baa622783ca423c22eacc1c7782 Author: ItaloBC Date: Sat Dec 9 11:47:40 2017 -0300 Changed the old drip to a sensible 10 seconds commit caf1ad655223d84f804172d1a93c83dc71941559 Author: ItaloBC Date: Sat Dec 9 11:40:43 2017 -0300 Corrected functions to get the new settings. Also, changed the JavaScript to check the old drip, send the drip and check oldness at different intervals. commit a9d4fd621e8c169af5ebba49ed79f38c87981538 Author: ItaloBC Date: Sat Dec 9 11:36:34 2017 -0300 Added two new settings for checking old drips commit e624a519c2ee4957db59dff41e3d427e8f359ffe Author: ItaloBC Date: Sat Dec 9 11:29:52 2017 -0300 Update Dripper.php --- config/genealabs-laravel-caffeine.php | 6 ++-- src/Dripper.php | 42 +++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/config/genealabs-laravel-caffeine.php b/config/genealabs-laravel-caffeine.php index d70b04f..5adc174 100644 --- a/config/genealabs-laravel-caffeine.php +++ b/config/genealabs-laravel-caffeine.php @@ -1,7 +1,9 @@ 300000, // every 5 minutes + 'dripIntervalInMilliSeconds' => 300000, // Drip every 5 minutes 'domain' => null, // defaults to url('/') - 'route' => 'genealabs/laravel-caffeine/drip', // can be customized + 'route' => 'genealabs/laravel-caffeine/drip', // Can be customized + 'thresholdDifference' => 10000, // When the drip will be considered old to reload the page + 'checkLastDripInterval' => 2000 // How often we will check if the drip is old ]; diff --git a/src/Dripper.php b/src/Dripper.php index 6b1e5b1..e9e2b0d 100644 --- a/src/Dripper.php +++ b/src/Dripper.php @@ -11,11 +11,27 @@ class Dripper extends Model { public function getHtmlAttribute() : string { - return '"; + + return '"; } public function getIntervalAttribute() : string @@ -25,6 +41,22 @@ public function getIntervalAttribute() : string 300000 ); } + + public function getThresholdAttribute() : int + { + return config( + 'genealabs-laravel-caffeine.thresholdDifference', + 10000 + ); + } + + public function getCheckIntervalAttribute() : int + { + return config( + 'genealabs-laravel-caffeine.checkLastDripInterval', + 2000 + ); + } public function getUrlAttribute() : string { From 48f20d9ed76d0dd5ac977f016980ba4487d3f3fc Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 9 Dec 2017 13:10:14 -0800 Subject: [PATCH 2/5] Add page reload functionality Integration of PR #72 Thanks @ItaloBC! --- CHANGELOG.md | 8 ++ README.md | 95 +++++++++++++++++---- config/genealabs-laravel-caffeine.php | 65 ++++++++++++-- resources/views/script.blade.php | 30 +++++++ src/Dripper.php | 57 ++++--------- src/Providers/Service.php | 13 ++- tests/Feature/CaffeineTest.php | 2 +- tests/Fixtures/expired_script.txt | 30 +++++++ tests/Fixtures/unexpired_script.txt | 30 +++++++ tests/Unit/Console/Commands/PublishTest.php | 3 +- tests/Unit/DripperTest.php | 7 +- 11 files changed, 272 insertions(+), 68 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 resources/views/script.blade.php create mode 100644 tests/Fixtures/expired_script.txt create mode 100644 tests/Fixtures/unexpired_script.txt diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..392375e --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,8 @@ +# Change Log +## [0.6.0] - 9 Dec 2017 +### Added +- drip timeout check and force page refresh if timeout occurred. + +### Changed +- config file setting names to be more explicit. +- middleware is injected only when called from a web page or during testing. diff --git a/README.md b/README.md index 5730579..c6a9149 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,7 @@ I chose this approach to keep the integrity of site-security, by avoiding the ## Considerations ### Routes -This package adds the routes under `genealabs/laravel-caffeine`. Please verify - that these don't collide with your existing routes. +This package adds the routes under `genealabs/laravel-caffeine`. ### Dependencies - 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 composer require genealabs/laravel-caffeine ``` - 2. **This is only required for Laravel 5.4 or below:** Add the service provider entry in `config/app.php`: ```php @@ -57,7 +55,8 @@ For Laravel 5.2, follow the directions here: https://github.com/GeneaLabs/larave // ], ``` -3. If you have previously registered the middleware, please remove the following +3. If you are running 5.5 or above, remove the providers entry from `config/app.php`. +4. If you have previously registered the middleware, please remove the following middleware from `app/Http/Kernel.php`: ```php // protected $middleware = [ @@ -65,24 +64,84 @@ For Laravel 5.2, follow the directions here: https://github.com/GeneaLabs/larave // ]; ``` +## Upgrad Notes +### 0.6.0 +This update changes the config file setting names. Please delete the published +config file `config/genealabs-laravel-caffeine.php` if it exists, and follow the +configuration instructions below. + ## Configuration -The following elements are configurable: -- **domain:** (default: `url('/')`) Change to point to a different domain than - your app. This is useful if you are behind a proxy or load-balancer. ___Do not use - the `url()` helper in the config file.___ -- **route:** (default: `genealabs/laravel-caffeine/drip`) Change to customize - the drip URL in the browser. This is just cosmetic. -- **dripIntervalInMilliSeconds:** (default: 5 mins) Change to configure the drip - interval. +```php +return [ + /* + |-------------------------------------------------------------------------- + | Drip Interval + |-------------------------------------------------------------------------- + | + | Here you may configure the interval with which Caffeine for Laravel + | keeps the session alive. By default this is 5 minutes (expressed + | in milliseconds). This needs to be shorter than your session + | lifetime value configured set in "config/session.php". + | + | Default: 300000 (int) + | + */ + 'dripInterval' => 300000, + + /* + |-------------------------------------------------------------------------- + | Domain + |-------------------------------------------------------------------------- + | + | You may optionally configure a separate domain that you are running + | Caffeine for Laravel on. This may be of interest if you have a + | monitoring service that queries other apps. Setting this to + | null will use the domain of the current application. + | + | Default: null (null|string) + | + */ + 'domain' => null, + + /* + |-------------------------------------------------------------------------- + | Drip Endpoint URL + |-------------------------------------------------------------------------- + | + | Sometimes you may wish to white-label your app and not expose the AJAX + | request URLs as belonging to this package. To achieve that you can + | rename the URL used for dripping caffeine into your application. + | + | Default: 'genealabs/laravel-caffeine/drip' (string) + | + */ + 'route' => 'genealabs/laravel-caffeine/drip', // Customizable end-point URL + + /* + |-------------------------------------------------------------------------- + | Checking for Lapsed Drips + |-------------------------------------------------------------------------- + | + | If the browser is put to sleep on (for example on mobil devices or + | laptops), it will still cause an error when trying to submit the + | form. To avoid this, we force-reload the form 2 minutes prior + | to session time-out or later. Setting this setting to 0 + | will disable this check if you don't want to use it. + | + | Default: 2000 (int) + | + */ + 'outdatedDripCheckInterval' => 2000, + +]; +``` ___Only publish the config file if you need to customize it___: ```sh -php artisan vendor:publish --tag=genealabs-laravel-caffeine +php artisan caffeine:publish --config ``` -You can now change the default value in `config/genealabs-laravel-caffeine.php` as desired. Deleting the -`config/genealabs-laravel-caffeine.php` file will revert back to the default settings. - ## Usage -That was it! It will apply itself automatically where it finds a form with a `_token` field, or a meta tag named - "csrf-token", while pages are open in browsers. +That was it! It will apply itself automatically where it finds a form with a +`_token` field, or a meta tag named "csrf-token", while pages are open in +browsers. diff --git a/config/genealabs-laravel-caffeine.php b/config/genealabs-laravel-caffeine.php index 5adc174..11b510f 100644 --- a/config/genealabs-laravel-caffeine.php +++ b/config/genealabs-laravel-caffeine.php @@ -1,9 +1,64 @@ 300000, // Drip every 5 minutes - 'domain' => null, // defaults to url('/') - 'route' => 'genealabs/laravel-caffeine/drip', // Can be customized - 'thresholdDifference' => 10000, // When the drip will be considered old to reload the page - 'checkLastDripInterval' => 2000 // How often we will check if the drip is old + /* + |-------------------------------------------------------------------------- + | Drip Interval + |-------------------------------------------------------------------------- + | + | Here you may configure the interval with which Caffeine for Laravel + | keeps the session alive. By default this is 5 minutes (expressed + | in milliseconds). This needs to be shorter than your session + | lifetime value configured set in "config/session.php". + | + | Default: 300000 (int) + | + */ + 'dripInterval' => 300000, + + /* + |-------------------------------------------------------------------------- + | Domain + |-------------------------------------------------------------------------- + | + | You may optionally configure a separate domain that you are running + | Caffeine for Laravel on. This may be of interest if you have a + | monitoring service that queries other apps. Setting this to + | null will use the domain of the current application. + | + | Default: null (null|string) + | + */ + 'domain' => null, + + /* + |-------------------------------------------------------------------------- + | Drip Endpoint URL + |-------------------------------------------------------------------------- + | + | Sometimes you may wish to white-label your app and not expose the AJAX + | request URLs as belonging to this package. To achieve that you can + | rename the URL used for dripping caffeine into your application. + | + | Default: 'genealabs/laravel-caffeine/drip' (string) + | + */ + 'route' => 'genealabs/laravel-caffeine/drip', // Customizable end-point URL + + /* + |-------------------------------------------------------------------------- + | Checking for Lapsed Drips + |-------------------------------------------------------------------------- + | + | If the browser is put to sleep on (for example on mobil devices or + | laptops), it will still cause an error when trying to submit the + | form. To avoid this, we force-reload the form 2 minutes prior + | to session time-out or later. Setting this setting to 0 + | will disable this check if you don't want to use it. + | + | Default: 2000 (int) + | + */ + 'outdatedDripCheckInterval' => 2000, + ]; diff --git a/resources/views/script.blade.php b/resources/views/script.blade.php new file mode 100644 index 0000000..d0ade64 --- /dev/null +++ b/resources/views/script.blade.php @@ -0,0 +1,30 @@ + diff --git a/src/Dripper.php b/src/Dripper.php index e9e2b0d..82cbebf 100644 --- a/src/Dripper.php +++ b/src/Dripper.php @@ -2,59 +2,38 @@ use Jenssegers\Model\Model; -/** - * @property string $html - * @property string $interval - * @property string $url - */ class Dripper extends Model { public function getHtmlAttribute() : string { - - return '"; + $html = (string) view('genealabs-laravel-caffeine::script') + ->with([ + 'ageCheckInterval' => $this->ageCheckInterval, + 'ageThreshold' => $this->ageThreshold, + 'interval' => $this->interval, + 'url' => $this->url, + ]); + return $html; } - public function getIntervalAttribute() : string + public function getAgeCheckIntervalAttribute() : int { return config( - 'genealabs-laravel-caffeine.dripIntervalInMilliSeconds', - 300000 + 'genealabs-laravel-caffeine.outdatedDripCheckInterval', + 2000 ); } - - public function getThresholdAttribute() : int + + public function getAgeThresholdAttribute() : int { - return config( - 'genealabs-laravel-caffeine.thresholdDifference', - 10000 - ); + return (config('session.lifetime', 32) - 2) * 60000; } - - public function getCheckIntervalAttribute() : int + + public function getIntervalAttribute() : string { return config( - 'genealabs-laravel-caffeine.checkLastDripInterval', - 2000 + 'genealabs-laravel-caffeine.dripInterval', + 300000 ); } diff --git a/src/Providers/Service.php b/src/Providers/Service.php index 0cada71..dd800db 100644 --- a/src/Providers/Service.php +++ b/src/Providers/Service.php @@ -2,8 +2,9 @@ use GeneaLabs\LaravelCaffeine\Console\Commands\Publish; use GeneaLabs\LaravelCaffeine\Http\Middleware\LaravelCaffeineDripMiddleware; -use Illuminate\Support\ServiceProvider; +use Illuminate\Contracts\Http\Kernel; use Illuminate\Routing\Route; +use Illuminate\Support\ServiceProvider; class Service extends ServiceProvider { @@ -33,7 +34,9 @@ public function register() $this->commands(Publish::class); $this->mergeConfigFrom(__DIR__ . '/../../config/genealabs-laravel-caffeine.php', 'genealabs-laravel-caffeine'); - app('Illuminate\Contracts\Http\Kernel')->pushMiddleware('\GeneaLabs\LaravelCaffeine\Http\Middleware\LaravelCaffeineDripMiddleware'); + if ($this->shouldRegisterMiddleware()) { + app(Kernel::class)->pushMiddleware('\\' . LaravelCaffeineDripMiddleware::class); + } } public function provides() : array @@ -58,4 +61,10 @@ private function middlewareGroupExists(string $group) : bool return $carry; }) ?? false; } + + private function shouldRegisterMiddleware() : bool + { + return (! request()->ajax() + && (php_sapi_name() === 'fpm-fcgi' || app('env') === 'testing')); + } } diff --git a/tests/Feature/CaffeineTest.php b/tests/Feature/CaffeineTest.php index 6b36a3c..27d27b1 100644 --- a/tests/Feature/CaffeineTest.php +++ b/tests/Feature/CaffeineTest.php @@ -14,7 +14,7 @@ public function testBootstrap3TestPageCanLoad() public function testMiddlewareInjectsDripScript() { - $expectedResult = ""; + $expectedResult = file_get_contents(__DIR__ . '/../Fixtures/expired_script.txt'); $response = $this->get(route('genealabs-laravel-caffeine.tests.form')); diff --git a/tests/Fixtures/expired_script.txt b/tests/Fixtures/expired_script.txt new file mode 100644 index 0000000..ad2ab9d --- /dev/null +++ b/tests/Fixtures/expired_script.txt @@ -0,0 +1,30 @@ + diff --git a/tests/Fixtures/unexpired_script.txt b/tests/Fixtures/unexpired_script.txt new file mode 100644 index 0000000..89b411f --- /dev/null +++ b/tests/Fixtures/unexpired_script.txt @@ -0,0 +1,30 @@ + diff --git a/tests/Unit/Console/Commands/PublishTest.php b/tests/Unit/Console/Commands/PublishTest.php index 588478c..0ff74df 100644 --- a/tests/Unit/Console/Commands/PublishTest.php +++ b/tests/Unit/Console/Commands/PublishTest.php @@ -15,8 +15,9 @@ public function testConfigFileContainsCorrectSettings() { $settings = file_get_contents(base_path('config/genealabs-laravel-caffeine.php')); - $this->assertContains("'dripIntervalInMilliSeconds' => 300000,", $settings); + $this->assertContains("'dripInterval' => 300000,", $settings); $this->assertContains("'domain' => null,", $settings); $this->assertContains("'route' => 'genealabs/laravel-caffeine/drip',", $settings); + $this->assertContains("'outdatedDripCheckInterval' => 2000,", $settings); } } diff --git a/tests/Unit/DripperTest.php b/tests/Unit/DripperTest.php index eecaf6d..c13df0d 100644 --- a/tests/Unit/DripperTest.php +++ b/tests/Unit/DripperTest.php @@ -10,6 +10,7 @@ class DripperTest extends TestCase public function testUrlAttributeValue() { $expectedResult = "/genealabs/laravel-caffeine/drip"; + $actualResult = (new Dripper)->url; $this->assertEquals($expectedResult, $actualResult); @@ -18,6 +19,7 @@ public function testUrlAttributeValue() public function testIntervalAttributeValue() { $expectedResult = 300000; + $actualResult = (new Dripper)->interval; $this->assertEquals($expectedResult, $actualResult); @@ -25,9 +27,10 @@ public function testIntervalAttributeValue() public function testHtmlAttributeValue() { - $expectedResult = ""; + $expectedResult = file_get_contents(__DIR__ . '/../Fixtures/unexpired_script.txt'); + $actualResult = (new Dripper)->html; - $this->assertEquals($expectedResult, $actualResult); + $this->assertEquals($actualResult, $expectedResult); } } From 61eb2fdbbf4984621b062f56c4f89e769ea57a22 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 9 Dec 2017 13:35:17 -0800 Subject: [PATCH 3/5] Add missing unit tests --- tests/Unit/DripperTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Unit/DripperTest.php b/tests/Unit/DripperTest.php index c13df0d..1411cd4 100644 --- a/tests/Unit/DripperTest.php +++ b/tests/Unit/DripperTest.php @@ -7,6 +7,24 @@ class DripperTest extends TestCase { + public function testAgeCheckIntervalAttributeValue() + { + $expectedResult = 2000; + + $actualResult = (new Dripper)->ageCheckInterval; + + $this->assertEquals($expectedResult, $actualResult); + } + + public function testAgeThresholdAttributeValue() + { + $expectedResult = 7080000; + + $actualResult = (new Dripper)->ageThreshold; + + $this->assertEquals($expectedResult, $actualResult); + } + public function testUrlAttributeValue() { $expectedResult = "/genealabs/laravel-caffeine/drip"; From 0989e5a0a4c9421e32cca883e9aee8281215aee5 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 9 Dec 2017 13:46:05 -0800 Subject: [PATCH 4/5] . --- src/Dripper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Dripper.php b/src/Dripper.php index ad3e75b..6a94589 100644 --- a/src/Dripper.php +++ b/src/Dripper.php @@ -36,7 +36,7 @@ public function getIntervalAttribute() : string 300000 ); } - + public function getThresholdAttribute() : int { return config( @@ -44,7 +44,7 @@ public function getThresholdAttribute() : int 10000 ); } - + public function getCheckIntervalAttribute() : int { return config( From dc4731ac9fb53b52c35f4964b7d556d2676eb410 Mon Sep 17 00:00:00 2001 From: Mike Bronner Date: Sat, 9 Dec 2017 13:58:02 -0800 Subject: [PATCH 5/5] Remove dead code --- src/Dripper.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/Dripper.php b/src/Dripper.php index 6a94589..82cbebf 100644 --- a/src/Dripper.php +++ b/src/Dripper.php @@ -37,22 +37,6 @@ public function getIntervalAttribute() : string ); } - public function getThresholdAttribute() : int - { - return config( - 'genealabs-laravel-caffeine.thresholdDifference', - 10000 - ); - } - - public function getCheckIntervalAttribute() : int - { - return config( - 'genealabs-laravel-caffeine.checkLastDripInterval', - 2000 - ); - } - public function getUrlAttribute() : string { return trim(config('genealabs-laravel-caffeine.domain', url('/')), '/')