-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1aa67a8
Showing
5 changed files
with
174 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.idea | ||
composer.lock | ||
/vendor |
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,19 @@ | ||
# Nette Inertia | ||
|
||
The [Nette](https://nette.org) adapter for [Inertia.js](https://inertiajs.com/). | ||
|
||
## Installation | ||
|
||
``` | ||
composer require pepa-linha/nette-inertia | ||
``` | ||
|
||
## Usage | ||
|
||
Extends your presenter | ||
|
||
``` | ||
class BasePresenter extends InertiaPresenter | ||
``` | ||
|
||
and then use `$this->inertia(...)` method to send page object. |
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,20 @@ | ||
{ | ||
"name": "pepa-linha/nette-inertia", | ||
"type": "library", | ||
"description": "The Nette adapter for Inertia.js.", | ||
"keywords": ["nette", "inertia", "javascript"], | ||
"license": "MIT", | ||
"require": { | ||
"php": ">=7.4", | ||
"nette/application": "^3.0" | ||
}, | ||
"autoload": { | ||
"classmap": ["src/"] | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Pepa Linha", | ||
"email": "[email protected]" | ||
} | ||
] | ||
} |
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,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PL\NetteInertia; | ||
|
||
use Nette\Application\Responses\VoidResponse; | ||
use Nette\Application\UI\Presenter; | ||
use Nette\Http\IRequest; | ||
use Nette\Http\IResponse; | ||
use Nette\Utils\Json; | ||
|
||
abstract class InertiaPresenter extends Presenter | ||
{ | ||
private ?PageObject $pageObject = null; | ||
|
||
protected function startup() | ||
{ | ||
parent::startup(); | ||
|
||
if ($this->isInertiaRequest()) { | ||
if ($this->getInertiaAssetVersion() !== $this->getAssetVersion() | ||
&& $this->getHttpRequest()->isMethod(IRequest::GET)) | ||
{ | ||
$httpResponse = $this->getHttpResponse(); | ||
$httpResponse->addHeader('X-Inertia-Location', $this->link('this')); | ||
$httpResponse->setCode(IResponse::S409_CONFLICT); | ||
$response = new VoidResponse(); | ||
$response->send($this->getHttpRequest(), $httpResponse); | ||
$this->sendResponse($response); | ||
} | ||
|
||
$this->addInertiaHeaders(); | ||
} | ||
} | ||
|
||
protected function afterRender() | ||
{ | ||
parent::afterRender(); | ||
$this->template->inertiaPageObject = Json::encode($this->pageObject->getData()); | ||
} | ||
|
||
public function inertia(array $props = [], ?string $component = null, ?string $link = null) | ||
{ | ||
$pageObject = new PageObject( | ||
$component ?? $this->getInertiaComponentName(), | ||
$props, | ||
$link ?? $this->link('this'), | ||
$this->getAssetVersion() | ||
); | ||
|
||
if ($this->isInertiaRequest()) { | ||
$this->sendJson($pageObject->getData()); | ||
} | ||
|
||
$this->pageObject = $pageObject; | ||
} | ||
|
||
public function isInertiaRequest(): bool | ||
{ | ||
return $this->getHttpRequest()->getHeader('X-Inertia') === 'true'; | ||
} | ||
|
||
public function isInertiaPartialReload(): bool | ||
{ | ||
return $this->getHttpRequest()->getHeader('X-Inertia-Partial-Component') !== null | ||
&& $this->getHttpRequest()->getHeader('X-Inertia-Partial-Data') !== null; | ||
} | ||
|
||
public function getInertiaPartialDesiredProps(): array | ||
{ | ||
$data = $this->getHttpRequest()->getHeader('X-Inertia-Partial-Data'); | ||
return $data !== null ? explode(',', $data) : []; | ||
} | ||
|
||
protected function getInertiaComponentName(): string | ||
{ | ||
return substr($this->getName(), strpos($this->getName(), ':') + 1); | ||
} | ||
|
||
private function addInertiaHeaders(): void | ||
{ | ||
$response = $this->getHttpResponse(); | ||
$response->addHeader('Vary', 'Accept'); | ||
$response->addHeader('X-Inertia', 'true'); | ||
} | ||
|
||
private function getInertiaAssetVersion(): ?string | ||
{ | ||
return $this->getHttpRequest()->getHeader('X-Inertia-Version'); | ||
} | ||
|
||
abstract protected function getAssetVersion(): string; | ||
} |
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,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PL\NetteInertia; | ||
|
||
class PageObject | ||
{ | ||
private string $component; | ||
|
||
private array $props; | ||
|
||
private string $url; | ||
|
||
private string $version; | ||
|
||
public function __construct( | ||
string $component, | ||
array $props, | ||
string $url, | ||
string $version | ||
) { | ||
$this->component = $component; | ||
$this->props = $props; | ||
$this->url = $url; | ||
$this->version = $version; | ||
} | ||
|
||
public function getData(): array | ||
{ | ||
return [ | ||
'component' => $this->component, | ||
'props' => $this->props, | ||
'url' => $this->url, | ||
'version' => $this->version, | ||
]; | ||
} | ||
} |