Skip to content

Commit 897027e

Browse files
committed
[#.x] - updating readme
1 parent b35db43 commit 897027e

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,107 @@
11
# REST API with Phalcon v6
22

33
A REST API developed with Phalcon v6
4+
5+
The directory structure for this projects follows the recommendations of [pds/skeleton][pds_skeleton]
6+
7+
# Folders
8+
The folders contain:
9+
10+
- `bin`: empty for now, we might use it later on
11+
- `config`: .env configuration files for CI and example
12+
- `docs`: documentation (TODO)
13+
- `public`: entry point of the application where `index.php` lives
14+
- `resources`: stores database migrations and docker files for local development
15+
- `src`: source code of the project
16+
- `storage`: various storage data such as application logs
17+
- `tests`: tests
18+
19+
# Code organization
20+
The application follows the [ADR pattern][adr_pattern] where the application is split into an `Action` layer, the `Domain` layer and a `Responder` layer.
21+
22+
## Action
23+
The folder (under `src/`) contains the handler that is responsible for receiving data from the Domain and injecting it to the Responder so that the response can be generated. It also collects all the Input supplied by the user and injects it into the Domain for further processing.
24+
25+
## Domain
26+
27+
The Domain is organized in folders based on their use. The `Components` folder contains components that are essential to the operation of the application, while the `Services` folder contains classes that map to endpoints of the application
28+
29+
### Container
30+
31+
The application uses the `Phalcon\Di\Di` container with minimal components lazy loaded. Each non "core" component is also registered there (i.e. domain services, responder etc.) and all necessary dependencies are injected based on the service definitions.
32+
33+
Additionally there are two `Providers` that are also registered in the DI container for further functionality. The `ErrorHandlerProvider` which caters for the starting up/shut down of the application and error logging, and the very important `RoutesProvider` which handles registering all the routes that the application serves.
34+
35+
### Enums
36+
37+
There are several enumerations present in the application. Those help with common values for tasks. For example the `FlagsEnum` holds the values for the `co_users.usr_status_flag` field. We could certainly introduce a lookup table in the database for "status" and hold the values there, joining it to the `co_users` table with a lookup table. However, this will introduce an extra join in our query which will inevitable reduce performance. Since the `FlagsEnum` can keep the various statuses, we keep everything in code instead of the database. Thorough tests for enumerations ensure that if a change is made in the future, tests will fail, so that database integrity can be kept.
38+
39+
The `RoutesEnum` holds the various routes of the application. Every route is represented by a specific element in the enumeration and the relevant prefix/suffix are calculated for each endpoint. Also, each endpoint is mapped to a particular service, registered in the DI container, so that the action handler can invoke it when the route is matched.
40+
41+
Finally, the `RoutesEnum` also holds the middleware array, which defines their execution and the "hook" they will execute in (before/after).
42+
43+
### Middleware
44+
45+
There are several middleware registered for this application and they are being executed one after another (order matters) before the action is executed. As a result, the application will stop executing if an error occurs, or if certain validations fail.
46+
47+
The middleware execution order is defined in the `RoutesEnum`. The available middleware is:
48+
49+
- [NotFoundMiddleware.php](src/Domain/Components/Middleware/NotFoundMiddleware.php)
50+
- [HealthMiddleware.php](src/Domain/Components/Middleware/HealthMiddleware.php)
51+
- [ValidateTokenClaimsMiddleware.php](src/Domain/Components/Middleware/ValidateTokenClaimsMiddleware.php)
52+
- [ValidateTokenPresenceMiddleware.php](src/Domain/Components/Middleware/ValidateTokenPresenceMiddleware.php)
53+
- [ValidateTokenRevokedMiddleware.php](src/Domain/Components/Middleware/ValidateTokenRevokedMiddleware.php)
54+
- [ValidateTokenStructureMiddleware.php](src/Domain/Components/Middleware/ValidateTokenStructureMiddleware.php)
55+
- [ValidateTokenUserMiddleware.php](src/Domain/Components/Middleware/ValidateTokenUserMiddleware.php)
56+
57+
58+
59+
**NotFoundMiddleware**
60+
61+
Checks if the route has been matched. If not, it will return a `Resource Not Found` payload
62+
63+
64+
**HealthMiddleware**
65+
66+
Invoked when the `/health` endpoint is called and returns a `OK` payload
67+
68+
**ValidateTokenPresenceMiddleware**
69+
70+
Checks if a JWT token is present in the `Authorization` header. If not, an error is returned
71+
72+
**ValidateTokenStructureMiddleware**
73+
74+
Gets the JWT token and checks if it can be parsed. If not, an error is returned
75+
76+
**ValidateTokenUserMiddleware**
77+
78+
Gets the userId from the JWT token, along with other information, and tries to match it with a user in the database. If the user is not found, an error is returned
79+
80+
**ValidateTokenClaimsMiddleware**
81+
82+
Checks all the claims of the JWT token to ensure that it validates. For instance, this checks the token validity (expired, not before), the claims, etc. If a validation error happens, then an error is returned.
83+
84+
**ValidateTokenRevokedMiddleware**
85+
86+
Checks if the token has been revoked. If it has, an error is returned
87+
88+
89+
## Responder
90+
The responder is responsible for constructing the response with the desired output, and emitting it back to the caller. For the moment we have only implemented a JSON response with a specified array as the payload to be sent back.
91+
92+
The responder receives the outcome of the Domain, by means of a `Payload` object. The object contains all the data necessary to inject in the response.
93+
94+
### Response payload
95+
96+
The application responds always with a specific JSON payload. The payload contains the following nodes:
97+
- `data` - contains any data that are returned back (can be empty)
98+
- `errors` - contains any errors occurred (can be empty)
99+
- `meta` - array of information regarding the payload
100+
- `code` - the application code returned
101+
- `hash` - a `sha1` hash of the `data`, `errors` and timestamp
102+
- `message` - `success` or `error`
103+
- `timestamp` - the time in UTC format
104+
105+
106+
[adr_pattern]: https://github.com/pmjones/adr
107+
[pds_skeleton]: https://github.com/php-pds/skeleton

0 commit comments

Comments
 (0)