Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pepa-linha committed Jan 19, 2021
0 parents commit 1aa67a8
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
composer.lock
/vendor
19 changes: 19 additions & 0 deletions README.md
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.
20 changes: 20 additions & 0 deletions composer.json
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]"
}
]
}
94 changes: 94 additions & 0 deletions src/InertiaPresenter.php
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;
}
38 changes: 38 additions & 0 deletions src/PageObject.php
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,
];
}
}

0 comments on commit 1aa67a8

Please sign in to comment.