diff --git a/README.md b/README.md index dd7860b..51b616f 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,28 @@ Another option is to extend the `App\Http\Middleware\TrustProxies` class to `Mon ... ``` +## Custom proxies callback + +You can define your own proxies callback by calling the `LaravelCloudflare::getProxiesUsing()` to change the behavior of the `LaravelCloudflare::getProxies()` method. +This method should typically be called in the `boot` method of your `AppServiceProvider` class: + +```php +use Monicahq\Cloudflare\LaravelCloudflare; +use Monicahq\Cloudflare\Facades\CloudflareProxies; + +/** + * Bootstrap any application services. + * + * @return void + */ +public function boot() +{ + LaravelCloudflare::getProxiesUsing(function() { + return CloudflareProxies::load(); + }); +} +``` + # How it works diff --git a/src/Commands/Reload.php b/src/Commands/Reload.php index d50c1d7..2ca7ef7 100644 --- a/src/Commands/Reload.php +++ b/src/Commands/Reload.php @@ -5,7 +5,7 @@ use Illuminate\Console\Command; use Illuminate\Contracts\Cache\Factory; use Illuminate\Contracts\Config\Repository; -use Monicahq\Cloudflare\CloudflareProxies; +use Monicahq\Cloudflare\LaravelCloudflare; class Reload extends Command { @@ -32,10 +32,9 @@ class Reload extends Command */ public function handle(Factory $cache, Repository $config) { - /** @var CloudflareProxies */ - $loader = $this->laravel->make(CloudflareProxies::class); + $proxies = LaravelCloudflare::getProxies(); - $cache->store()->forever($config->get('laravelcloudflare.cache'), $loader->load()); + $cache->store()->forever($config->get('laravelcloudflare.cache'), $proxies); $this->info('Cloudflare\'s IP blocks have been reloaded.'); } diff --git a/src/Facades/CloudflareProxies.php b/src/Facades/CloudflareProxies.php new file mode 100644 index 0000000..4f71a22 --- /dev/null +++ b/src/Facades/CloudflareProxies.php @@ -0,0 +1,23 @@ +load(); + return LaravelCloudflare::getProxies(); }); if (is_array($cachedProxies) && count($cachedProxies) > 0) { diff --git a/src/LaravelCloudflare.php b/src/LaravelCloudflare.php new file mode 100644 index 0000000..4604046 --- /dev/null +++ b/src/LaravelCloudflare.php @@ -0,0 +1,40 @@ +mergeConfigFrom( __DIR__.'/../config/laravelcloudflare.php', 'laravelcloudflare' ); + $this->app->singleton(\Monicahq\Cloudflare\Facades\CloudflareProxies::class, \Monicahq\Cloudflare\CloudflareProxies::class); if ($this->app->runningInConsole()) { $this->commands([ diff --git a/tests/Unit/Commands/ReloadTest.php b/tests/Unit/Commands/ReloadTest.php index 073b8b4..b29507d 100644 --- a/tests/Unit/Commands/ReloadTest.php +++ b/tests/Unit/Commands/ReloadTest.php @@ -4,8 +4,7 @@ use Illuminate\Http\Client\Factory as HttpClient; use Illuminate\Support\Facades\Http; -use Mockery\MockInterface; -use Monicahq\Cloudflare\CloudflareProxies; +use Monicahq\Cloudflare\Facades\CloudflareProxies; use Monicahq\Cloudflare\Tests\FeatureTestCase; class ReloadTest extends FeatureTestCase @@ -13,10 +12,9 @@ class ReloadTest extends FeatureTestCase /** @test */ public function it_loads_proxies() { - $this->mock(CloudflareProxies::class, function (MockInterface $mock) { - $mock->shouldReceive('load') - ->andReturn(['expect']); - }); + CloudflareProxies::shouldReceive('load') + ->once() + ->andReturn(['expect']); $this->artisan('cloudflare:reload') ->expectsOutput('Cloudflare\'s IP blocks have been reloaded.') diff --git a/tests/Unit/Http/Middleware/TrustProxiesTest.php b/tests/Unit/Http/Middleware/TrustProxiesTest.php index 96af64a..275cd7a 100644 --- a/tests/Unit/Http/Middleware/TrustProxiesTest.php +++ b/tests/Unit/Http/Middleware/TrustProxiesTest.php @@ -4,8 +4,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; -use Mockery\MockInterface; -use Monicahq\Cloudflare\CloudflareProxies; +use Monicahq\Cloudflare\Facades\CloudflareProxies; use Monicahq\Cloudflare\Http\Middleware\TrustProxies; use Monicahq\Cloudflare\Tests\FeatureTestCase; @@ -48,10 +47,9 @@ public function it_does_not_sets_trusted_proxies() /** @test */ public function it_load_trustproxies() { - $this->mock(CloudflareProxies::class, function (MockInterface $mock) { - $mock->shouldReceive('load') - ->andReturn(['expect']); - }); + CloudflareProxies::shouldReceive('load') + ->once() + ->andReturn(['expect']); $request = new Request(); diff --git a/tests/Unit/LaravelCloudflareTest.php b/tests/Unit/LaravelCloudflareTest.php new file mode 100644 index 0000000..243d429 --- /dev/null +++ b/tests/Unit/LaravelCloudflareTest.php @@ -0,0 +1,35 @@ +app->make(TrustProxies::class)->handle($request, function () { + }); + + $proxies = $request->getTrustedProxies(); + + $this->assertTrue(static::$run); + $this->assertEquals(['expect'], $proxies); + } +}