Skip to content

Commit

Permalink
Merge pull request #50 from kiwilan/develop
Browse files Browse the repository at this point in the history
v2.0.11
  • Loading branch information
ewilan-riviere authored May 22, 2024
2 parents 36d4ff7 + beb0acb commit 1417049
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 6 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "kiwilan/php-opds",
"description": "PHP package to create OPDS feed for eBooks.",
"version": "2.0.10",
"version": "2.0.11",
"keywords": [
"php",
"ebook",
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/OpdsJsonEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function feed(): self
];
}

if ($this->opds->getConfig()->getStartUrl() && ! $this->opds->getConfig()->isForceJson()) {
if ($this->opds->getConfig()->getStartUrl() && ! $this->opds->getConfig()->isUseForceJson()) {
$this->contents['links'][] = $this->addJsonLink(
rel: 'alternate',
href: $this->getVersionUrl(OpdsVersionEnum::v1Dot2),
Expand Down
8 changes: 6 additions & 2 deletions src/Opds.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static function make(OpdsConfig $config = new OpdsConfig()): self
$self->url = OpdsEngine::getCurrentUrl();
$self->parseUrl();

if ($config->isForceJson()) {
if ($config->isUseForceJson()) {
$self->version = OpdsVersionEnum::v2Dot0;
}

Expand Down Expand Up @@ -125,7 +125,7 @@ public function get(): self
$this->version = $this->queryVersion;
}

if ($this->config->isForceJson()) {
if ($this->config->isUseForceJson()) {
$this->version = OpdsVersionEnum::v2Dot0;
}

Expand All @@ -146,6 +146,10 @@ public function get(): self
}
$this->response = OpdsResponse::make($this->engine->__toString(), $this->output, 200);

if ($this->config->isUseForceExit()) {
$this->response->forceExit();
}

return $this;
}

Expand Down
16 changes: 15 additions & 1 deletion src/OpdsConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class OpdsConfig
* @param ?DateTime $updated Updated date, for example: `new DateTime()`.
* @param int $maxItemsPerPage Maximum items per page, default is `32`.
* @param bool $forceJson Force OPDS version 2.0 as default, default is `false`.
* @param bool $forceExit Force send response as default, default is `false`.
*/
public function __construct(
protected ?string $name = 'opds',
Expand All @@ -34,6 +35,7 @@ public function __construct(
protected ?DateTime $updated = null,
protected int $maxItemsPerPage = 16,
protected bool $forceJson = false,
protected bool $forceExit = false,
) {
if (! $this->updated) {
$this->updated = new DateTime();
Expand Down Expand Up @@ -90,11 +92,16 @@ public function getMaxItemsPerPage(): int
return $this->maxItemsPerPage;
}

public function isForceJson(): bool
public function isUseForceJson(): bool
{
return $this->forceJson;
}

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

public function setName(string $name): self
{
$this->name = $name;
Expand Down Expand Up @@ -176,6 +183,13 @@ public function forceJson(): self
return $this;
}

public function forceExit(): self
{
$this->forceExit = true;

return $this;
}

/**
* Laravel export
* Generate a URL friendly "slug" from a given string.
Expand Down
23 changes: 23 additions & 0 deletions src/OpdsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ protected function __construct(
protected bool $isXml = false,
protected array $headers = [],
protected ?string $contents = null,
protected ?bool $forceExit = null,
) {
}

Expand Down Expand Up @@ -93,6 +94,14 @@ public function getContents(): string
return $this->contents;
}

/**
* To know if `exit` will be used after sending response.
*/
public function isUseForceExit(): bool
{
return $this->forceExit ?? false;
}

/**
* Get contents as array.
*
Expand Down Expand Up @@ -156,13 +165,27 @@ public function setContents(string $contents): self
return $this;
}

/**
* Force `exit` after sending response.
*/
public function forceExit(): self
{
$this->forceExit = true;

return $this;
}

/**
* Send content to browser with correct header.
*
* @param bool $exit To use `exit` after sending response.
*/
public function send(bool $exit = false): string
{
if ($this->forceExit !== null && $this->forceExit && ! $exit) {
$exit = true;
}

foreach ($this->headers as $type => $value) {
header($type.': '.$value);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/OpdsConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
expect($config->getPaginationQuery())->toBe('pagination');
expect($config->getUpdated())->toBeInstanceOf(DateTime::class);
expect($config->getMaxItemsPerPage())->toBe(10);
expect($config->isForceJson())->toBeTrue();
expect($config->isUseForceJson())->toBeTrue();
});

it('can use slug', function () {
Expand Down
25 changes: 25 additions & 0 deletions tests/OpdsResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,28 @@
expect($response->isJson())->toBeFalse();
expect(fn () => $response->getJson())->toThrow(\Exception::class);
});

it('can use force exit', function () {
$html = '<!DOCTYPE html>';
$config = new OpdsConfig();
$config->forceExit();
$opds = Opds::make($config);

$engine = OpdsXmlEngine::make($opds);
$engine->setContents(['html' => $html]);

expect($engine->getContents())->toBe(['html' => $html]);

$response = $opds->getResponse();
expect($response->isUseForceExit())->toBeTrue();

$opds = Opds::make();

$engine = OpdsXmlEngine::make($opds);
$engine->setContents(['html' => $html]);

expect($engine->getContents())->toBe(['html' => $html]);

$response = $opds->getResponse();
expect($response->isUseForceExit())->toBeFalse();
});

0 comments on commit 1417049

Please sign in to comment.