Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template warmup #11

Open
wants to merge 2 commits into
base: 9.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions config/remote-view.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php declare(strict_types=1);

use Schnoop\RemoteTemplate\Support\DefaultBladeFilename;
use Schnoop\RemoteTemplate\Support\DefaultUrlModifier;

return [
'guzzle-config' => [
Expand All @@ -10,7 +9,7 @@
'connect_timeout' => 5,
],

'url_modifier' => DefaultUrlModifier::class,
'url_modifier' => [],

'blade_modifier' => DefaultBladeFilename::class,

Expand All @@ -25,12 +24,15 @@
'hosts' => [
'default' => [
'cache' => false,
'host' => 'https://www.your-first-content-domain.tld',
'host' => 'https://www.sportschau.de',
'request_options' => [
'auth_user' => '',
'auth_password' => '',
],
'mapping' => [],
'mapping' => [
'403' => '/fussball/fifa-wm-2022/wm-katar-auslosung-deutschland-gegner-100.html',
'404' => '/fussball/fifa-wm-2022/audio-dfb-team-so-lief-die-qualifikation-zur-wm-in-katar-100.html',
],
],

'specific' => [
Expand Down
46 changes: 46 additions & 0 deletions src/Commands/WarmupTemplates.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types=1);

namespace Schnoop\RemoteTemplate\Commands;

use Exception;
use Illuminate\Console\Command;

class WarmupTemplates extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'remote-template:warmup';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Warmup all configured templates.';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle(): int
{
$fileViewFinder = app('view.finder');

foreach (config('remote-view.hosts') as $host => $config) {
foreach ($config['mapping'] as $name => $url) {
try {
$result = $fileViewFinder->find('remote:'.$host.'::'.$name);
$this->comment($result);
} catch (Exception) {
$this->error('Das war nix.');
}
}
}

return 0;
}
}
9 changes: 9 additions & 0 deletions src/Exceptions/InvalidUrlModifierException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace Schnoop\RemoteTemplate\Exceptions;

use Exception;

class InvalidUrlModifierException extends Exception
{
}
2 changes: 2 additions & 0 deletions src/RemoteTemplateServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\View\Engines\EngineResolver;
use Illuminate\View\ViewFinderInterface;
use Illuminate\View\ViewServiceProvider;
use Schnoop\RemoteTemplate\Commands\WarmupTemplates;
use Schnoop\RemoteTemplate\View\Factory;
use Schnoop\RemoteTemplate\View\FileViewFinder;
use Schnoop\RemoteTemplate\View\RemoteTemplateFinder;
Expand Down Expand Up @@ -34,6 +35,7 @@ public function register(): void
$this->registerFactory();
$this->registerBladeCompiler();
$this->registerEngineResolver();
$this->commands(WarmupTemplates::class);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/Support/AbstractUrlModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Schnoop\RemoteTemplate\Support;

abstract class AbstractUrlModifier
{
protected bool $breakTheCycle = false;

abstract public function applicable(): bool;

public function breakTheCycle(): bool
{
return $this->breakTheCycle;
}

abstract public function getQueryString(): bool;
}
11 changes: 0 additions & 11 deletions src/Support/DefaultResponseHandler.php

This file was deleted.

11 changes: 0 additions & 11 deletions src/Support/DefaultUrlModifier.php

This file was deleted.

22 changes: 19 additions & 3 deletions src/View/RemoteTemplateFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use Schnoop\RemoteTemplate\Exceptions\IgnoredUrlSuffixException;
use Schnoop\RemoteTemplate\Exceptions\InvalidUrlModifierException;
use Schnoop\RemoteTemplate\Exceptions\RemoteHostNotConfiguredException;
use Schnoop\RemoteTemplate\Exceptions\RemoteTemplateNotFoundException;
use Schnoop\RemoteTemplate\Exceptions\UrlIsForbiddenException;
use Schnoop\RemoteTemplate\Support\AbstractUrlModifier;
use Schnoop\RemoteTemplate\Support\DefaultBladeFilename;
use Schnoop\RemoteTemplate\Support\DefaultUrlModifier;

class RemoteTemplateFinder
{
Expand Down Expand Up @@ -59,7 +60,7 @@ public function hasRemoteInformation(string $name): bool
* @throws RemoteTemplateNotFoundException
* @throws RemoteHostNotConfiguredException
* @throws UrlIsForbiddenException
* @throws GuzzleException
* @throws GuzzleException|InvalidUrlModifierException
*/
public function findRemotePathView(string $name): string
{
Expand All @@ -86,7 +87,22 @@ public function findRemotePathView(string $name): string
}

$url = $this->getTemplateUrlForIdentifier($name, $remoteHost);
$url = app(config('remote-view.url_modifier', DefaultUrlModifier::class))->determine($url);

foreach (config('remote-view.url_modifier', []) as $urlModifierClass) {
$modifierInstance = app($urlModifierClass);

if (! $modifierInstance instanceof AbstractUrlModifier) {
throw new InvalidUrlModifierException;
}

if ($modifierInstance->applicable()) {
$url .= $modifierInstance->getQueryString();
}

if ($modifierInstance->breakTheCycle()) {
break;
}
}

$path = $this->getViewFolder($namespace);
$path .= app(config('remote-view.blade_modifier', DefaultBladeFilename::class))->determine($url);
Expand Down