-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Introduce mixed mode for headless
Patch introduces new way for checking if we are in headless mode, you can set not enabled, mixed mode (fluid & headless at the same time), or full headless mode. Mixed mode depends on `Accept` header sent when requesting the site. To configure headless mode, please use site configuration flag, for example: ` 'headless': 0|1|2 ` Legacy flag (true|false) is respected, but please migrate to int notation Where: 0 (old: false) = headless disabled 1 (old: true) = headless fully enabled 2 = headless in mixed mode In mixed mode also you need to load special "Headless - Mixed mode JSON response" typoscript in order to work properly, default one overrides fluid one.
- Loading branch information
1 parent
6cec289
commit 413abb4
Showing
39 changed files
with
707 additions
and
260 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "headless" Extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.md file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendsOfTYPO3\Headless\Middleware; | ||
|
||
use FriendsOfTYPO3\Headless\Utility\Headless; | ||
use FriendsOfTYPO3\Headless\Utility\HeadlessMode; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\MiddlewareInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use TYPO3\CMS\Core\Site\Entity\Site; | ||
|
||
class HeadlessModeSetter implements MiddlewareInterface | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | ||
{ | ||
$mode = HeadlessMode::NONE; | ||
|
||
/** | ||
* @var Site $site | ||
*/ | ||
$site = $request->getAttribute('site'); | ||
if ($site) { | ||
$mode = (int)($site->getConfiguration()['headless'] ?? HeadlessMode::NONE); | ||
} | ||
|
||
$request = $request->withAttribute('headless', new Headless($mode)); | ||
|
||
$GLOBALS['TYPO3_REQUEST'] = $request; | ||
return $handler->handle($request); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "headless" Extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.md file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendsOfTYPO3\Headless\Utility; | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
final class Headless | ||
{ | ||
private int $mode; | ||
|
||
public function __construct(int $mode = HeadlessMode::NONE) | ||
{ | ||
$this->mode = $mode; | ||
} | ||
|
||
public function getMode(): int | ||
{ | ||
return $this->mode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the "headless" Extension for TYPO3 CMS. | ||
* | ||
* For the full copyright and license information, please read the | ||
* LICENSE.md file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FriendsOfTYPO3\Headless\Utility; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
final class HeadlessMode | ||
{ | ||
public const NONE = 0; | ||
public const FULL = 1; | ||
public const MIXED = 2; | ||
|
||
private ?ServerRequestInterface $request = null; | ||
|
||
public function withRequest(ServerRequestInterface $request): self | ||
{ | ||
$this->request = $request; | ||
return $this; | ||
} | ||
public function isEnabled(): bool | ||
{ | ||
if ($this->request === null) { | ||
return false; | ||
} | ||
|
||
$headless = $this->request->getAttribute('headless') ?? new Headless(); | ||
|
||
if ($headless->getMode() === self::NONE) { | ||
return false; | ||
} | ||
|
||
return $headless->getMode() === self::FULL || | ||
($headless->getMode() === self::MIXED && ($this->request->getHeader('Accept')[0] ?? '') === 'application/json'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.