Skip to content

Commit 806896a

Browse files
committed
Refresh samples
1 parent 6f6ee6b commit 806896a

File tree

7 files changed

+229
-86
lines changed

7 files changed

+229
-86
lines changed

samples/server/petstore/php-slim4/.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,10 @@ composer.phar
1515
/.phpunit.result.cache
1616

1717
# Do not commit local PHP_CodeSniffer config
18-
/phpcs.xml
18+
/phpcs.xml
19+
20+
# Application config may contain sensitive data
21+
/config/**/*.*
22+
!/config/.htaccess
23+
!/config/dev/example.inc.php
24+
!/config/prod/example.inc.php

samples/server/petstore/php-slim4/.openapi-generator/FILES

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
.htaccess
33
README.md
44
composer.json
5+
config/.htaccess
6+
config/dev/example.inc.php
7+
config/prod/example.inc.php
58
index.php
69
lib/Api/AbstractPetApi.php
710
lib/Api/AbstractStoreApi.php

samples/server/petstore/php-slim4/README.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ This command downloads the Slim Framework and its third-party dependencies into
2121
$ composer install
2222
```
2323

24+
## Add configs
25+
26+
Application requires at least one config file(`config/dev/config.inc.php` or `config/prod/config.inc.php`). You can use [config/dev/example.inc.php](config/dev/example.inc.php) as starting point.
27+
2428
## Start devserver
2529

2630
Run the following command in terminal to start localhost web server, assuming `./php-slim-server/` is public-accessible directory with `index.php` file:
@@ -82,25 +86,19 @@ $ composer phplint
8286

8387
## Show errors
8488

85-
Switch on option in `./index.php`:
89+
Switch on option in your application config file like:
8690
```diff
87-
/**
88-
* Add Error Handling Middleware
89-
*
90-
* @param bool $displayErrorDetails -> Should be set to false in production
91-
* @param bool $logErrors -> Parameter is passed to the default ErrorHandler
92-
* @param bool $logErrorDetails -> Display error details in error log
93-
* which can be replaced by a callable of your choice.
94-
95-
* Note: This middleware should be added last. It will not handle any exceptions/errors
96-
* for middleware added after it.
97-
*/
98-
--- $app->addErrorMiddleware(false, true, true);
99-
+++ $app->addErrorMiddleware(true, true, true);
91+
return [
92+
'slimSettings' => [
93+
- 'displayErrorDetails' => false,
94+
+ 'displayErrorDetails' => true,
95+
'logErrors' => true,
96+
'logErrorDetails' => true,
97+
],
10098
```
10199

102100
## Mock Server
103-
For a quick start uncomment [mocker middleware config](index.php#L62-L89).
101+
For a quick start uncomment [mocker middleware options](config/dev/example.inc.php#L67-L94) in your application config file.
104102

105103
Used packages:
106104
* [Openapi Data Mocker](https://github.com/ybelenko/openapi-data-mocker) - first implementation of OAS3 fake data generator.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deny from all
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
/**
4+
* OpenAPI Petstore
5+
* PHP version 7.2
6+
*
7+
* @package OpenAPIServer
8+
* @author OpenAPI Generator team
9+
* @link https://github.com/openapitools/openapi-generator
10+
*/
11+
12+
/**
13+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
14+
* The version of the OpenAPI document: 1.0.0
15+
* Generated by: https://github.com/openapitools/openapi-generator.git
16+
*/
17+
18+
/**
19+
* App configuration file example.
20+
*
21+
* Copy file to config/dev/config.inc.php and config/prod/config.inc.php
22+
* App loads dev config only when prod doesn't exist
23+
* in other words if both configs presented - prod config applies
24+
*/
25+
26+
use Psr\Http\Message\ServerRequestInterface;
27+
use Psr\Http\Message\ResponseInterface;
28+
use OpenAPIServer\Mock\OpenApiDataMocker;
29+
30+
$mocker = new OpenApiDataMocker();
31+
$mocker->setModelsNamespace('OpenAPIServer\Model\\');
32+
33+
return [
34+
'slimSettings' => [
35+
'displayErrorDetails' => false,
36+
'logErrors' => true,
37+
'logErrorDetails' => true,
38+
],
39+
40+
'tokenAuthenticationOptions' => [
41+
/**
42+
* Tokens are essentially passwords. You should treat them as such and you should always
43+
* use HTTPS. If the middleware detects insecure usage over HTTP it will return unauthorized
44+
* with a message Required HTTPS for token authentication. This rule is relaxed for requests
45+
* on localhost. To allow insecure usage you must enable it manually by setting secure to
46+
* false.
47+
* Default: true
48+
*/
49+
// 'secure' => true,
50+
51+
/**
52+
* Alternatively you can list your development host to have relaxed security.
53+
* Default: ['localhost', '127.0.0.1']
54+
*/
55+
// 'relaxed' => ['localhost', '127.0.0.1'],
56+
57+
/**
58+
* By default on ocurred a fail on authentication, is sent a response on json format with a
59+
* message (`Invalid Token` or `Not found Token`) and with the token (if found), with status
60+
* `401 Unauthorized`. You can customize it by setting a callable function on error option.
61+
* Default: null
62+
*/
63+
// 'error' => null,
64+
],
65+
66+
'mockerOptions' => [
67+
// 'dataMocker' => $mocker,
68+
69+
// 'getMockStatusCodeCallback' => function (ServerRequestInterface $request, array $responses) {
70+
// // check if client clearly asks for mocked response
71+
// $pingHeader = 'X-OpenAPIServer-Mock';
72+
// $pingHeaderCode = 'X-OpenAPIServer-Mock-Code';
73+
// if (
74+
// $request->hasHeader($pingHeader)
75+
// && $request->getHeader($pingHeader)[0] === 'ping'
76+
// ) {
77+
// $responses = (array) $responses;
78+
// $requestedResponseCode = ($request->hasHeader($pingHeaderCode)) ? $request->getHeader($pingHeaderCode)[0] : 'default';
79+
// if (array_key_exists($requestedResponseCode, $responses)) {
80+
// return $requestedResponseCode;
81+
// }
82+
83+
// // return first response key
84+
// reset($responses);
85+
// return key($responses);
86+
// }
87+
88+
// return false;
89+
// },
90+
91+
// 'afterCallback' => function (ServerRequestInterface $request, ResponseInterface $response) {
92+
// // mark mocked response to distinguish real and fake responses
93+
// return $response->withHeader('X-OpenAPIServer-Mock', 'pong');
94+
// },
95+
],
96+
];
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
/**
4+
* OpenAPI Petstore
5+
* PHP version 7.2
6+
*
7+
* @package OpenAPIServer
8+
* @author OpenAPI Generator team
9+
* @link https://github.com/openapitools/openapi-generator
10+
*/
11+
12+
/**
13+
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
14+
* The version of the OpenAPI document: 1.0.0
15+
* Generated by: https://github.com/openapitools/openapi-generator.git
16+
*/
17+
18+
/**
19+
* App configuration file example.
20+
*
21+
* Copy file to config/dev/config.inc.php and config/prod/config.inc.php
22+
* App loads dev config only when prod doesn't exist
23+
* in other words if both configs presented - prod config applies
24+
*/
25+
26+
use Psr\Http\Message\ServerRequestInterface;
27+
use Psr\Http\Message\ResponseInterface;
28+
use OpenAPIServer\Mock\OpenApiDataMocker;
29+
30+
$mocker = new OpenApiDataMocker();
31+
$mocker->setModelsNamespace('OpenAPIServer\Model\\');
32+
33+
return [
34+
'slimSettings' => [
35+
'displayErrorDetails' => false,
36+
'logErrors' => true,
37+
'logErrorDetails' => true,
38+
],
39+
40+
'tokenAuthenticationOptions' => [
41+
/**
42+
* Tokens are essentially passwords. You should treat them as such and you should always
43+
* use HTTPS. If the middleware detects insecure usage over HTTP it will return unauthorized
44+
* with a message Required HTTPS for token authentication. This rule is relaxed for requests
45+
* on localhost. To allow insecure usage you must enable it manually by setting secure to
46+
* false.
47+
* Default: true
48+
*/
49+
// 'secure' => true,
50+
51+
/**
52+
* Alternatively you can list your development host to have relaxed security.
53+
* Default: ['localhost', '127.0.0.1']
54+
*/
55+
// 'relaxed' => ['localhost', '127.0.0.1'],
56+
57+
/**
58+
* By default on ocurred a fail on authentication, is sent a response on json format with a
59+
* message (`Invalid Token` or `Not found Token`) and with the token (if found), with status
60+
* `401 Unauthorized`. You can customize it by setting a callable function on error option.
61+
* Default: null
62+
*/
63+
// 'error' => null,
64+
],
65+
66+
'mockerOptions' => [
67+
// 'dataMocker' => $mocker,
68+
69+
// 'getMockStatusCodeCallback' => function (ServerRequestInterface $request, array $responses) {
70+
// // check if client clearly asks for mocked response
71+
// $pingHeader = 'X-OpenAPIServer-Mock';
72+
// $pingHeaderCode = 'X-OpenAPIServer-Mock-Code';
73+
// if (
74+
// $request->hasHeader($pingHeader)
75+
// && $request->getHeader($pingHeader)[0] === 'ping'
76+
// ) {
77+
// $responses = (array) $responses;
78+
// $requestedResponseCode = ($request->hasHeader($pingHeaderCode)) ? $request->getHeader($pingHeaderCode)[0] : 'default';
79+
// if (array_key_exists($requestedResponseCode, $responses)) {
80+
// return $requestedResponseCode;
81+
// }
82+
83+
// // return first response key
84+
// reset($responses);
85+
// return key($responses);
86+
// }
87+
88+
// return false;
89+
// },
90+
91+
// 'afterCallback' => function (ServerRequestInterface $request, ResponseInterface $response) {
92+
// // mark mocked response to distinguish real and fake responses
93+
// return $response->withHeader('X-OpenAPIServer-Mock', 'pong');
94+
// },
95+
],
96+
];

samples/server/petstore/php-slim4/index.php

Lines changed: 13 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -27,76 +27,15 @@
2727
use Psr\Http\Message\ResponseInterface;
2828
use OpenAPIServer\Mock\OpenApiDataMocker;
2929

30+
// load config file
3031
$config = [];
31-
32-
/**
33-
* Token Middleware 1.x Options
34-
* Options `header`, `regex`, `parameter`, `cookie`, `attribute`, `path`, `except`, `authenticator`
35-
* are handled by SlimRouter class. These options are ignored by app and they omitted from current
36-
* example.
37-
* Ref: https://github.com/dyorg/slim-token-authentication/tree/1.x
38-
*/
39-
$config['tokenAuthenticationOptions'] = [
40-
/**
41-
* Tokens are essentially passwords. You should treat them as such and you should always
42-
* use HTTPS. If the middleware detects insecure usage over HTTP it will return unathorized
43-
* with a message Required HTTPS for token authentication. This rule is relaxed for requests
44-
* on localhost. To allow insecure usage you must enable it manually by setting secure to
45-
* false.
46-
* Default: true
47-
*/
48-
// 'secure' => true,
49-
50-
/**
51-
* Alternatively you can list your development host to have relaxed security.
52-
* Default: ['localhost', '127.0.0.1']
53-
*/
54-
// 'relaxed' => ['localhost', '127.0.0.1'],
55-
56-
/**
57-
* By default on ocurred a fail on authentication, is sent a response on json format with a
58-
* message (`Invalid Token` or `Not found Token`) and with the token (if found), with status
59-
* `401 Unauthorized`. You can customize it by setting a callable function on error option.
60-
* Default: null
61-
*/
62-
// 'error' => null,
63-
];
64-
65-
/**
66-
* Mocker Middleware options.
67-
*/
68-
$mocker = new OpenApiDataMocker();
69-
$mocker->setModelsNamespace('OpenAPIServer\Model\\');
70-
$config['mockerOptions'] = [
71-
// 'dataMocker' => $mocker,
72-
73-
// 'getMockStatusCodeCallback' => function (ServerRequestInterface $request, $responses) {
74-
// // check if client clearly asks for mocked response
75-
// $pingHeader = 'X-OpenAPIServer-Mock';
76-
// $pingHeaderCode = 'X-OpenAPIServer-Mock-Code';
77-
// if (
78-
// $request->hasHeader($pingHeader)
79-
// && $request->getHeader($pingHeader)[0] === 'ping'
80-
// ) {
81-
// $responses = (array) $responses;
82-
// $requestedResponseCode = ($request->hasHeader($pingHeaderCode)) ? $request->getHeader($pingHeaderCode)[0] : 'default';
83-
// if (array_key_exists($requestedResponseCode, $responses)) {
84-
// return $requestedResponseCode;
85-
// }
86-
87-
// // return first response key
88-
// reset($responses);
89-
// return key($responses);
90-
// }
91-
92-
// return false;
93-
// },
94-
95-
// 'afterCallback' => function ($request, $response) {
96-
// // mark mocked response to distinguish real and fake responses
97-
// return $response->withHeader('X-OpenAPIServer-Mock', 'pong');
98-
// },
99-
];
32+
if (is_array($prodConfig = @include(__DIR__ . '/config/dev/config.inc.php'))) {
33+
$config = $prodConfig;
34+
} elseif (is_array($devConfig = @include(__DIR__ . '/config/prod/config.inc.php'))) {
35+
$config = $devConfig;
36+
} else {
37+
throw new InvalidArgumentException('Config file missed or broken.');
38+
}
10039

10140
$router = new SlimRouter($config);
10241
$app = $router->getSlimApp();
@@ -118,6 +57,10 @@
11857
* Note: This middleware should be added last. It will not handle any exceptions/errors
11958
* for middleware added after it.
12059
*/
121-
$app->addErrorMiddleware(false, true, true);
60+
$app->addErrorMiddleware(
61+
$config['slimSettings']['displayErrorDetails'] ?? false,
62+
$config['slimSettings']['logErrors'] ?? true,
63+
$config['slimSettings']['logErrorDetails'] ?? true
64+
);
12265

12366
$app->run();

0 commit comments

Comments
 (0)