-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PoC][FEATURE] Introduce mixed mode for headless (#578)
* [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. 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 integer notation Possible options: 0 (old: false) = headless disabled for site in TYPO3 instance 1 (old: true) = headless fully enabled site in TYPO3 instance 2 = headless in mixed mode (fluid & json API in one site in TYPO3 instance) Values 0 (old: false) or 1 (old: true) inform extension to fully disable/enable headless for particular site. To enable mixed mode for chosen site in TYPO3 you need to: - In typoscript template for site, you have to load "Headless - Mixed mode JSON response" setup file instead of default headless one. - set `headless` flag to value of `2` site configuration's file or configure flag via editor in Site's management backend Mixed mode flag (value of `2`) inform EXT:headless extension to additionally check for `Accept` header with value `application/json` when processing request to the particular site in TYPO3 instance. In case of request without `Accept` header or `Accept` with different value than `application/json` TYPO3 will respond with HTML content (standard TYPO3's response) In case of request with header `Accept` match value of `application/json`, TYPO3 will respond with JSON response. resolves #94 #578 #348
- Loading branch information
1 parent
15e981e
commit b6a1620
Showing
35 changed files
with
584 additions
and
226 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,43 @@ | ||
<?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.