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..1411cd4 100644 --- a/tests/Unit/DripperTest.php +++ b/tests/Unit/DripperTest.php @@ -7,9 +7,28 @@ 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"; + $actualResult = (new Dripper)->url; $this->assertEquals($expectedResult, $actualResult); @@ -18,6 +37,7 @@ public function testUrlAttributeValue() public function testIntervalAttributeValue() { $expectedResult = 300000; + $actualResult = (new Dripper)->interval; $this->assertEquals($expectedResult, $actualResult); @@ -25,9 +45,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); } }