Skip to content

Commit 7262242

Browse files
authored
Merge pull request #325 from cakephp/docs-update-3x
Update documentation for CakePHP 5 / PHP 8+ compatibility
1 parent 98e6a29 commit 7262242

File tree

6 files changed

+19
-22
lines changed

6 files changed

+19
-22
lines changed

docs/en/component.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ requires use of the Middleware, so make sure it is applied as well. To use the
88
component, first load it::
99

1010
// In your AppController
11-
public function initialize()
11+
public function initialize(): void
1212
{
1313
parent::initialize();
1414
$this->loadComponent('Authorization.Authorization');

docs/en/index.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Getting Started
2222
===============
2323

2424
The Authorization plugin integrates into your application as a middleware layer
25-
and optionally a component to make checking authorization easier. First, lets
25+
and optionally a component to make checking authorization easier. First, let's
2626
apply the middleware. In **src/Application.php** add the following to the class
2727
imports::
2828

@@ -31,7 +31,6 @@ imports::
3131
use Authorization\AuthorizationServiceProviderInterface;
3232
use Authorization\Middleware\AuthorizationMiddleware;
3333
use Authorization\Policy\OrmResolver;
34-
use Psr\Http\Message\ResponseInterface;
3534
use Psr\Http\Message\ServerRequestInterface;
3635

3736
Add the ``AuthorizationServiceProviderInterface`` to the implemented interfaces
@@ -56,7 +55,7 @@ Then make your application's ``middleware()`` method look like::
5655
// and authentication middleware.
5756
->add(new AuthorizationMiddleware($this));
5857

59-
return $middlewareQueue();
58+
return $middlewareQueue;
6059
}
6160

6261
The placement of the ``AuthorizationMiddleware`` is important and must be added
@@ -78,7 +77,7 @@ define the ``AuthorizationService`` it wants to use. Add the following method yo
7877
This configures basic :doc:`/policy-resolvers` that will match
7978
ORM entities with their policy classes.
8079

81-
Next, lets add the ``AuthorizationComponent`` to ``AppController``. In
80+
Next, let's add the ``AuthorizationComponent`` to ``AppController``. In
8281
**src/Controller/AppController.php** add the following to the ``initialize()``
8382
method::
8483

@@ -89,7 +88,7 @@ authorization on a per-action basis more easily. For example, we can do::
8988

9089
public function edit($id = null)
9190
{
92-
$article = $this->Article->get($id);
91+
$article = $this->Articles->get($id);
9392
$this->Authorization->authorize($article, 'update');
9493

9594
// Rest of action

docs/en/middleware.rst

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ A basic example would be::
2424

2525
class Application extends BaseApplication implements AuthorizationServiceProviderInterface
2626
{
27-
public function getAuthorizationService(ServerRequestInterface $request, ResponseInterface $response)
27+
public function getAuthorizationService(ServerRequestInterface $request): AuthorizationServiceInterface
2828
{
2929
$resolver = new OrmResolver();
3030

3131
return new AuthorizationService($resolver);
3232
}
3333

34-
public function middleware($middlewareQueue)
34+
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
3535
{
3636
// other middleware
3737
$middlewareQueue->add(new AuthorizationMiddleware($this));
@@ -114,7 +114,7 @@ implementing the ``Authorization\IdentityInterface`` and using the
114114
/**
115115
* Setter to be used by the middleware.
116116
*/
117-
public function setAuthorization(AuthorizationServiceInterface $service)
117+
public function setAuthorization(AuthorizationServiceInterface $service): static
118118
{
119119
$this->authorization = $service;
120120

@@ -150,10 +150,8 @@ If you also use the Authentication plugin make sure to implement both interfaces
150150

151151
/**
152152
* Authentication\IdentityInterface method
153-
*
154-
* @return string
155153
*/
156-
public function getIdentifier()
154+
public function getIdentifier(): int|string|null
157155
{
158156
return $this->id;
159157
}
@@ -254,7 +252,7 @@ How to create a custom UnauthorizedHandler
254252
#. Create this file ``src/Middleware/UnauthorizedHandler/CustomRedirectHandler.php``::
255253

256254
<?php
257-
declare( strict_types = 1 );
255+
declare(strict_types=1);
258256

259257
namespace App\Middleware\UnauthorizedHandler;
260258

docs/en/policies.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ You can generate empty policy classes for ORM objects using ``bake``:
4646
Writing Policy Methods
4747
======================
4848

49-
The policy class we just created doesn't do much right now. Lets define a method
49+
The policy class we just created doesn't do much right now. Let's define a method
5050
that allows us to check if a user can update an article::
5151

52-
public function canEdit(IdentityInterface $user, Article $article)
52+
public function canEdit(IdentityInterface $user, Article $article): bool
5353
{
54-
return $user->id == $article->user_id;
54+
return $user->id === $article->user_id;
5555
}
5656

5757
Policy methods must return ``true`` or a ``Result`` objects to indicate success.
5858
All other values will be interpreted as failure.
5959

6060
Policy methods will receive ``null`` for the ``$user`` parameter when handling
61-
unauthencticated users. If you want to automatically fail policy methods for
61+
unauthenticated users. If you want to automatically fail policy methods for
6262
anonymous users you can use the ``IdentityInterface`` typehint.
6363

6464
.. _policy-result-objects:
@@ -72,9 +72,9 @@ passed/failed::
7272

7373
use Authorization\Policy\Result;
7474

75-
public function canUpdate(IdentityInterface $user, Article $article)
75+
public function canUpdate(IdentityInterface $user, Article $article): Result
7676
{
77-
if ($user->id == $article->user_id) {
77+
if ($user->id === $article->user_id) {
7878
return new Result(true);
7979
}
8080
// Results let you define a 'reason' for the failure.

docs/en/policy-resolvers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ The OrmResolver supports customization through its constructor::
7676
$overrides = [
7777
'Blog' => 'Cms',
7878
];
79-
$resolver = new OrmResolver($appNamespace, $overrides)
79+
$resolver = new OrmResolver($appNamespace, $overrides);
8080

8181
Using Multiple Resolvers
8282
========================

docs/en/request-authorization-middleware.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ Next, map the request class to the policy inside
5656
use Authorization\Middleware\AuthorizationMiddleware;
5757
use Authorization\Middleware\RequestAuthorizationMiddleware;
5858
use Authorization\Policy\MapResolver;
59-
use Authorization\Policy\OrmResolver;
60-
use Psr\Http\Message\ResponseInterface;
6159
use Cake\Http\ServerRequest;
6260

6361

@@ -77,6 +75,8 @@ AuthorizationMiddleware::
7775
// Add authorization (after authentication if you are using that plugin too).
7876
$middlewareQueue->add(new AuthorizationMiddleware($this));
7977
$middlewareQueue->add(new RequestAuthorizationMiddleware());
78+
79+
return $middlewareQueue;
8080
}
8181

8282
Controller Usage

0 commit comments

Comments
 (0)