-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e97166a
commit 5f3b7ea
Showing
3 changed files
with
75 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ | |
|
||
namespace Lunr\Corona; | ||
|
||
use JsonSchema\Validator; | ||
use Lunr\Core\Configuration; | ||
use stdClass; | ||
use Throwable; | ||
|
@@ -20,16 +21,36 @@ | |
class JsonView extends View | ||
{ | ||
|
||
/** | ||
* Shared instance of the JSON validator | ||
* @var Validator | ||
*/ | ||
protected readonly Validator $json_validator; | ||
|
||
/** | ||
* The list of calls to check and what to check them against | ||
* @var array | ||
*/ | ||
protected array $allowlist = []; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param Request $request Shared instance of the Request class | ||
* @param Response $response Shared instance of the Response class | ||
* @param Configuration $configuration Shared instance of the Configuration class | ||
* @param Request $request Shared instance of the Request class | ||
* @param Response $response Shared instance of the Response class | ||
* @param Configuration $configuration Shared instance of the Configuration class | ||
* @param Validator|null $locator Shared instance of the Validator class | ||
*/ | ||
public function __construct($request, $response, $configuration) | ||
public function __construct($request, $response, $configuration, $validator = NULL) | ||
Check failure on line 44 in src/Lunr/Corona/JsonView.php GitHub Actions / php-tests / phpstan / PHPStan
|
||
{ | ||
parent::__construct($request, $response, $configuration); | ||
|
||
if($validator !== NULL) { | ||
Check failure on line 48 in src/Lunr/Corona/JsonView.php GitHub Actions / php-tests / phpcs / PHPCS
Check failure on line 48 in src/Lunr/Corona/JsonView.php GitHub Actions / php-tests / phpcs / PHPCS
|
||
$this->json_validator = $validator; | ||
|
||
$this->configuration->load_file('validation'); | ||
$this->allowlist = $this->configuration['validation']['schemalist']->toArray(); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -47,7 +68,7 @@ public function __destruct() | |
* | ||
* @return mixed $return Prepared response data | ||
*/ | ||
protected function prepare_data($data) | ||
protected function prepare_data($data): mixed | ||
{ | ||
return $data; | ||
} | ||
|
@@ -57,7 +78,7 @@ protected function prepare_data($data) | |
* | ||
* @return void | ||
*/ | ||
public function print_page() | ||
public function print_page(): void | ||
{ | ||
$identifier = $this->response->get_return_code_identifiers(TRUE); | ||
|
||
|
@@ -81,6 +102,11 @@ public function print_page() | |
header('Content-type: application/json'); | ||
http_response_code($code); | ||
|
||
if(in_array($code, $this->configuration['validation']['http_codes']->toArray())) { | ||
Check failure on line 105 in src/Lunr/Corona/JsonView.php GitHub Actions / php-tests / phpcs / PHPCS
Check failure on line 105 in src/Lunr/Corona/JsonView.php GitHub Actions / php-tests / phpcs / PHPCS
|
||
$this->get_json_validated($json); | ||
} | ||
|
||
|
||
if ($this->request->sapi == 'cli') | ||
{ | ||
echo json_encode($json, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n"; | ||
|
@@ -96,7 +122,7 @@ public function print_page() | |
* | ||
* @return void | ||
*/ | ||
public function print_fatal_error() | ||
public function print_fatal_error(): void | ||
{ | ||
$error = error_get_last(); | ||
|
||
|
@@ -133,7 +159,7 @@ public function print_fatal_error() | |
* | ||
* @return void | ||
*/ | ||
public function print_exception($e) | ||
public function print_exception($e): void | ||
{ | ||
$json = []; | ||
|
||
|
@@ -162,6 +188,37 @@ public function print_exception($e) | |
} | ||
} | ||
|
||
|
||
/** | ||
* Check if the JSON needs to be validated and do so if needed. | ||
* | ||
* @param array $json the response data passed by reference | ||
* | ||
* @return void | ||
*/ | ||
private function get_json_validated(array &$json): void | ||
{ | ||
|
||
$schema_name = $this->request->controller . '/' . $this->request->method; | ||
if(!array_key_exists($schema_name, $this->allowlist)) | ||
{ | ||
return; | ||
} | ||
|
||
$schema_path = $this->request->application_path . str_replace($this->request->base_path, '', $this->statics($this->allowlist[$schema_name])); | ||
if(!is_readable($schema_path)) { | ||
$json['status']['json_message'] = 'JSON schema was not found, not validated'; | ||
return; | ||
} | ||
|
||
$this->json_validator->validate($data, (object)[ '$ref' => 'file://' . realpath($schema_path)]); | ||
|
||
if (!$this->json_validator->isValid()) | ||
{ | ||
$json['status']['json_message'] = 'JSON failed to validate against the schema'; | ||
$json['status']['json_code'] = HttpCode::INTERNAL_SERVER_ERROR; | ||
} | ||
} | ||
} | ||
|
||
?> |