Skip to content

Commit

Permalink
Merge pull request #12 from markstory/4.x
Browse files Browse the repository at this point in the history
Fast approach on a 4.x upgrade
  • Loading branch information
ishanvyas22 authored Nov 5, 2020
2 parents 5a9de31 + 53cd121 commit 3769e7d
Show file tree
Hide file tree
Showing 26 changed files with 101 additions and 87 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ branches:
- master

php:
- 7.1
- 7.2
- 7.3
- 7.4

env:
Expand All @@ -18,9 +16,9 @@ matrix:
fast_finish: true

include:
- php: 7.2
- php: 7.4
env: PHPCS=1 DEFAULT=0
- php: 7.2
- php: 7.4
env: PHPSTAN=1 DEFAULT=0

install:
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"type": "cakephp-plugin",
"license": "MIT",
"require": {
"cakephp/cakephp": ">=3.5.0 <4.0.0",
"ishanvyas22/asset-mix": "^0.6"
"cakephp/cakephp": "^4.1.0",
"ishanvyas22/asset-mix": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^5.7.14|^6.0",
"cakephp/cakephp-codesniffer": "^3.0",
"phpunit/phpunit": "~8.5.0",
"cakephp/cakephp-codesniffer": "^4.2",
"phpstan/phpstan": "^0.12.37"
},
"autoload": {
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
>
<php>
Expand Down
9 changes: 5 additions & 4 deletions src/Controller/InertiaResponseTrait.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?php
declare(strict_types=1);

namespace Inertia\Controller;

use Cake\Event\Event;
use Cake\Event\EventInterface;
use Inertia\Utility\Message;

trait InertiaResponseTrait
{
/**
* @inheritDoc
*/
public function beforeRender(Event $event)
public function beforeRender(EventInterface $event)
{
if ($this->isErrorStatus() || $this->isFailureStatus()) {
return null;
Expand Down Expand Up @@ -83,7 +84,7 @@ private function isFailureStatus()
*/
protected function setFlashData()
{
/** @var \Cake\Http\Session */
/** @var \Cake\Http\Session $session */
$session = $this->getRequest()->getSession();

$this->set('flash', function () use ($session) {
Expand All @@ -108,6 +109,6 @@ protected function setFlashData()
*/
private function setCsrfToken()
{
$this->set('_csrfToken', $this->getRequest()->getParam('_csrfToken'));
$this->set('_csrfToken', $this->getRequest()->getAttribute('csrfToken'));
}
}
33 changes: 18 additions & 15 deletions src/Middleware/InertiaMiddleware.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
<?php
declare(strict_types=1);

namespace Inertia\Middleware;

use Cake\Http\ServerRequest;
use Inertia\Utility\Message;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class InertiaMiddleware
class InertiaMiddleware implements MiddlewareInterface
{
/**
* Invoke method.
*
* @param \Cake\Http\ServerRequest $request The request.
* @param \Cake\Http\Response $response The response.
* @param callable $next Callback to invoke the next middleware.
* @return \Cake\Http\Response A response
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
* @param \Psr\Http\Server\RequestHandlerInterface $handler The request handler.
* @return \Psr\Http\Message\ResponseInterface A response.
*/
public function __invoke($request, $response, $next)
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if (! $request->hasHeader('X-Inertia')) {
return $next($request, $response);
if (!$request->hasHeader('X-Inertia')) {
return $handler->handle($request);
}
if ($request instanceof ServerRequest) {
$this->setupDetectors($request);
}

$this->setupDetectors($request);

$response = $next($request, $response);

$response = $handler->handle($request);
if (
$response instanceof ResponseInterface
&& $response->getStatusCode() === Message::STATUS_FOUND
$response->getStatusCode() === Message::STATUS_FOUND
&& in_array($request->getMethod(), [Message::METHOD_PUT, Message::METHOD_PATCH, Message::METHOD_DELETE])
) {
$response = $response->withStatus(Message::STATUS_SEE_OTHER);
Expand All @@ -44,7 +47,7 @@ public function __invoke($request, $response, $next)
* @param \Cake\Http\ServerRequest $request The request.
* @return void
*/
private function setupDetectors($request)
private function setupDetectors(ServerRequest $request): void
{
$request->addDetector('inertia', function ($request) {
return $request->hasHeader('X-Inertia');
Expand Down
8 changes: 5 additions & 3 deletions src/Plugin.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?php
declare(strict_types=1);

namespace Inertia;

use AssetMix\Plugin as AssetMixPlugin;
use Cake\Core\BasePlugin;
use Cake\Core\PluginApplicationInterface;
use Cake\Http\MiddlewareQueue;
use Inertia\Middleware\InertiaMiddleware;

/**
Expand All @@ -18,7 +20,7 @@ class Plugin extends BasePlugin
* @param \Cake\Http\MiddlewareQueue $middleware The middleware queue to update.
* @return \Cake\Http\MiddlewareQueue
*/
public function middleware($middleware)
public function middleware(MiddlewareQueue $middleware): MiddlewareQueue
{
// Add middleware here.
$middleware->add(new InertiaMiddleware());
Expand All @@ -27,9 +29,9 @@ public function middleware($middleware)
}

/**
* {@inheritdoc}
* @inheritDoc
*/
public function bootstrap(PluginApplicationInterface $app)
public function bootstrap(PluginApplicationInterface $app): void
{
parent::bootstrap($app);

Expand Down
10 changes: 6 additions & 4 deletions src/Utility/Message.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Inertia\Utility;

use Cake\Http\Client\Message as CakeMessage;
Expand All @@ -10,26 +12,26 @@ class Message extends CakeMessage
*
* @var int
*/
const STATUS_NOT_FOUND = 404;
public const STATUS_NOT_FOUND = 404;

/**
* HTTP 409 code
*
* @var int
*/
const STATUS_CONFLICT = 409;
public const STATUS_CONFLICT = 409;

/**
* HTTP 422 code
*
* @var int
*/
const STATUS_UNPROCESSABLE_ENTITY = 422;
public const STATUS_UNPROCESSABLE_ENTITY = 422;

/**
* HTTP 500 code
*
* @var int
*/
const STATUS_INTERNAL_SERVER = 500;
public const STATUS_INTERNAL_SERVER = 500;
}
6 changes: 4 additions & 2 deletions src/View/BaseViewTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Inertia\View;

use Cake\Routing\Router;
Expand Down Expand Up @@ -38,7 +40,7 @@ private function getComponentName()
return sprintf(
'%s/%s',
$this->getRequest()->getParam('controller'),
ucwords($this->getRequest()->getParam('action'))
ucwords((string)$this->getRequest()->getParam('action'))
);
}

Expand All @@ -51,7 +53,7 @@ private function getProps()
{
$props = [];
$only = $this->getPartialData();
$onlyViewVars = (! empty($only)) ? $only : array_keys($this->viewVars);
$onlyViewVars = ! empty($only) ? $only : array_keys($this->viewVars);
$passedViewVars = $this->viewVars;

$this->viewVars = [];
Expand Down
3 changes: 2 additions & 1 deletion src/View/Helper/InertiaHelper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

namespace Inertia\View\Helper;

use Cake\View\Helper;
Expand All @@ -8,7 +10,6 @@
*/
class InertiaHelper extends Helper
{

/**
* Returns inertia div.
*
Expand Down
7 changes: 4 additions & 3 deletions src/View/InertiaJsonView.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php
declare(strict_types=1);

namespace Inertia\View;

use Cake\View\JsonView;
use Inertia\View\BaseViewTrait;

/**
* Returns json response with provided view vars.
Expand All @@ -14,16 +15,16 @@ class InertiaJsonView extends JsonView
/**
* @inheritDoc
*/
public function render($view = null, $layout = null)
public function render(?string $view = null, $layout = null): string
{
$page = [
'component' => $this->getComponentName(),
'url' => $this->getCurrentUri(),
'props' => $this->getProps(),
];

$this->setConfig('serialize', 'page');
$this->set([
'_serialize' => 'page',
'page' => $page,
]);

Expand Down
7 changes: 4 additions & 3 deletions src/View/InertiaWebView.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php
declare(strict_types=1);

namespace Inertia\View;

use Cake\View\View;
use Inertia\View\BaseViewTrait;

/**
* Renders `Inertia./Inertia/app` view with provided view vars.
Expand All @@ -14,7 +15,7 @@ class InertiaWebView extends View
/**
* @inheritDoc
*/
public function initialize()
public function initialize(): void
{
$this->loadHelper('Inertia.Inertia');
$this->loadHelper('AssetMix.AssetMix');
Expand All @@ -23,7 +24,7 @@ public function initialize()
/**
* @inheritDoc
*/
public function render($view = null, $layout = null)
public function render(?string $view = null, $layout = null): string
{
$page = [
'component' => $this->getComponentName(),
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions tests/TestCase/Controller/UsersControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Inertia\Test\TestCase\Controller;

Expand Down
12 changes: 7 additions & 5 deletions tests/TestCase/PluginTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
declare(strict_types=1);

namespace Inertia\Test;

use Cake\Error\Middleware\ErrorHandlerMiddleware;
use Cake\Http\MiddlewareQueue;
use Cake\Http\Middleware\BodyParserMiddleware;
use Cake\Http\MiddlewareQueue;
use Cake\Routing\Middleware\AssetMiddleware;
use Cake\Routing\Middleware\RoutingMiddleware;
use Cake\TestSuite\IntegrationTestTrait;
Expand Down Expand Up @@ -36,9 +37,10 @@ public function testMiddleware()

$middleware = $app->middleware($middleware);

$this->assertInstanceOf(ErrorHandlerMiddleware::class, $middleware->get(0));
$this->assertInstanceOf(AssetMiddleware::class, $middleware->get(1));
$this->assertInstanceOf(BodyParserMiddleware::class, $middleware->get(2));
$this->assertInstanceOf(RoutingMiddleware::class, $middleware->get(3));
$results = iterator_to_array($middleware);
$this->assertInstanceOf(ErrorHandlerMiddleware::class, $results[0]);
$this->assertInstanceOf(AssetMiddleware::class, $results[1]);
$this->assertInstanceOf(BodyParserMiddleware::class, $results[2]);
$this->assertInstanceOf(RoutingMiddleware::class, $results[3]);
}
}
Loading

0 comments on commit 3769e7d

Please sign in to comment.