jonnybarnes.uk/app/Providers/AppServiceProvider.php

82 lines
2.4 KiB
PHP
Raw Normal View History

2016-05-19 15:01:28 +01:00
<?php
namespace App\Providers;
use Illuminate\Database\Eloquent\Model;
2020-02-23 17:12:17 +00:00
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\URL;
2016-05-19 15:01:28 +01:00
use Illuminate\Support\ServiceProvider;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Validation\Constraint\SignedWith;
use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;
2016-05-19 15:01:28 +01:00
class AppServiceProvider extends ServiceProvider
{
2024-03-19 20:13:36 +00:00
/**
* Register any application services.
*/
public function register(): void
{
//
}
2016-05-19 15:01:28 +01:00
/**
* Bootstrap any application services.
*/
2023-02-18 09:34:57 +00:00
public function boot(): void
2016-05-19 15:01:28 +01:00
{
2020-02-23 17:12:17 +00:00
/**
* Paginate a standard Laravel Collection.
*
2022-07-09 10:08:26 +01:00
* @param int $perPage
* @param int $total
* @param int $page
* @param string $pageName
2020-02-23 17:12:17 +00:00
* @return array
*/
Collection::macro('paginate', function ($perPage, $total = null, $page = null, $pageName = 'page') {
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
});
// Configure JWT builder
$this->app->bind('Lcobucci\JWT\Configuration', function () {
$key = InMemory::plainText(config('app.key'));
$config = Configuration::forSymmetricSigner(new Sha256, $key);
$config->setValidationConstraints(new SignedWith(new Sha256, $key));
return $config;
});
// Configure HtmlSanitizer
$this->app->bind(HtmlSanitizer::class, function () {
return new HtmlSanitizer(
(new HtmlSanitizerConfig)
->allowSafeElements()
->forceAttribute('a', 'rel', 'noopener nofollow')
);
});
// Turn on Eloquent strict mode when developing
Model::shouldBeStrict(! $this->app->isProduction());
// Force HTTPS URL generation in production
URL::forceHttps($this->app->isProduction());
2016-05-19 15:01:28 +01:00
}
}