2016-05-19 15:01:28 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
2017-12-19 16:00:42 +00:00
|
|
|
use App\Models\Note;
|
2019-10-27 16:29:15 +00:00
|
|
|
use App\Observers\NoteObserver;
|
2019-10-27 16:15:14 +00:00
|
|
|
use Codebird\Codebird;
|
2023-01-02 09:39:23 +00:00
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Middleware;
|
2022-11-24 21:44:57 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2017-09-11 15:56:11 +01:00
|
|
|
use Illuminate\Http\Request;
|
2020-02-23 17:12:17 +00:00
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
|
use Illuminate\Support\Collection;
|
2016-05-19 15:01:28 +01:00
|
|
|
use Illuminate\Support\ServiceProvider;
|
2019-10-27 16:29:15 +00:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Laravel\Dusk\DuskServiceProvider;
|
2020-11-28 18:21:29 +00:00
|
|
|
use Lcobucci\JWT\Configuration;
|
|
|
|
use Lcobucci\JWT\Signer\Hmac\Sha256;
|
|
|
|
use Lcobucci\JWT\Signer\Key\InMemory;
|
|
|
|
use Lcobucci\JWT\Validation\Constraint\SignedWith;
|
2022-06-02 09:40:34 +01:00
|
|
|
use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
|
|
|
|
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;
|
2016-05-19 15:01:28 +01:00
|
|
|
|
|
|
|
class AppServiceProvider extends ServiceProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Bootstrap any application services.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function boot()
|
|
|
|
{
|
2017-11-09 11:30:54 +00:00
|
|
|
Note::observe(NoteObserver::class);
|
2017-09-11 15:56:11 +01:00
|
|
|
|
|
|
|
// Request AS macro
|
2017-09-13 17:02:31 +00:00
|
|
|
Request::macro('wantsActivityStream', function () {
|
2019-02-01 18:49:35 +00:00
|
|
|
return Str::contains(mb_strtolower($this->header('Accept')), 'application/activity+json');
|
2017-09-11 15:56:11 +01:00
|
|
|
});
|
2017-09-16 11:39:36 +01:00
|
|
|
|
|
|
|
// configure Intervention/Image
|
|
|
|
$this->app->bind('Intervention\Image\ImageManager', function () {
|
|
|
|
return new \Intervention\Image\ImageManager(['driver' => config('image.driver')]);
|
|
|
|
});
|
2019-10-27 16:15:14 +00:00
|
|
|
|
|
|
|
// Bind the Codebird client
|
2022-05-14 17:44:14 +01:00
|
|
|
// Codebird gets mocked in tests
|
|
|
|
// @codeCoverageIgnoreStart
|
2019-10-27 16:15:14 +00:00
|
|
|
$this->app->bind('Codebird\Codebird', function () {
|
|
|
|
Codebird::setConsumerKey(
|
|
|
|
env('TWITTER_CONSUMER_KEY'),
|
|
|
|
env('TWITTER_CONSUMER_SECRET')
|
|
|
|
);
|
|
|
|
|
|
|
|
$cb = Codebird::getInstance();
|
|
|
|
|
|
|
|
$cb->setToken(
|
|
|
|
env('TWITTER_ACCESS_TOKEN'),
|
|
|
|
env('TWITTER_ACCESS_TOKEN_SECRET')
|
|
|
|
);
|
|
|
|
|
|
|
|
return $cb;
|
|
|
|
});
|
2022-05-14 17:44:14 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
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,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
});
|
2020-11-28 18:21:29 +00:00
|
|
|
|
|
|
|
// Configure JWT builder
|
|
|
|
$this->app->bind('Lcobucci\JWT\Configuration', function () {
|
2022-08-23 20:50:19 +01:00
|
|
|
$key = InMemory::plainText(config('app.key'));
|
2020-11-28 18:21:29 +00:00
|
|
|
|
|
|
|
$config = Configuration::forSymmetricSigner(new Sha256(), $key);
|
|
|
|
|
|
|
|
$config->setValidationConstraints(new SignedWith(new Sha256(), $key));
|
|
|
|
|
|
|
|
return $config;
|
|
|
|
});
|
2022-06-02 09:40:34 +01:00
|
|
|
|
|
|
|
// Configure HtmlSanitizer
|
|
|
|
$this->app->bind(HtmlSanitizer::class, function () {
|
|
|
|
return new HtmlSanitizer(
|
|
|
|
(new HtmlSanitizerConfig())
|
|
|
|
->allowSafeElements()
|
|
|
|
->forceAttribute('a', 'rel', 'noopener nofollow')
|
|
|
|
);
|
|
|
|
});
|
2022-11-24 21:44:57 +00:00
|
|
|
|
2023-01-02 09:39:23 +00:00
|
|
|
// Configure Guzzle
|
|
|
|
$this->app->bind('RetryGuzzle', function () {
|
|
|
|
$handlerStack = \GuzzleHttp\HandlerStack::create();
|
|
|
|
$handlerStack->push(Middleware::retry(
|
|
|
|
function ($retries, $request, $response, $exception) {
|
|
|
|
// Limit the number of retries to 5
|
|
|
|
if ($retries >= 5) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retry connection exceptions
|
|
|
|
if ($exception instanceof \GuzzleHttp\Exception\ConnectException) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retry on server errors
|
|
|
|
if ($response && $response->getStatusCode() >= 500) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally for CloudConvert, retry if status is not final
|
|
|
|
return json_decode($response, false, 512, JSON_THROW_ON_ERROR)->data->status !== 'finished';
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
// Retry after 1 second
|
|
|
|
return 1000;
|
|
|
|
}
|
|
|
|
));
|
2023-02-04 10:30:49 +00:00
|
|
|
|
2023-01-02 09:39:23 +00:00
|
|
|
return new Client(['handler' => $handlerStack]);
|
|
|
|
});
|
|
|
|
|
2022-11-24 21:44:57 +00:00
|
|
|
// Turn on Eloquent strict mode when developing
|
|
|
|
Model::shouldBeStrict(! $this->app->isProduction());
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register any application services.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function register()
|
|
|
|
{
|
2017-02-18 12:27:21 +00:00
|
|
|
if ($this->app->environment('local', 'testing')) {
|
|
|
|
$this->app->register(DuskServiceProvider::class);
|
|
|
|
}
|
2016-05-19 15:01:28 +01:00
|
|
|
}
|
|
|
|
}
|