Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

T15608 json last error #16381

Merged
merged 7 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- Added `Phalcon\Storage\Adapter\Weak` implemented with WeakReference has a cache/retrieval solution for objects not yet collected by the Garbage Collection. [#16372](https://github.com/phalcon/cphalcon/issues/16372)
- Extended `Phalcon\Di\Injectable` from `stdClass` to remove the deprecation warning (dynamic properties) for PHP 8.2 [#16308](https://github.com/phalcon/cphalcon/issues/16308)
- Corrected the return type of `Phalcon\Mvc\View::getVar()` so that stubs can be accurate. [#16276](https://github.com/phalcon/cphalcon/issues/16276)
- Changed all the `encode`/`decode` methods for JSON to use the `Phalcon\Support\Helper\Json\*` classes. [#15608](https://github.com/phalcon/cphalcon/issues/15608)
- Changed the `Phalcon\Support\Helper\Json\*` classes to clear up `json_last_error()` before doing any conversions. [#15608](https://github.com/phalcon/cphalcon/issues/15608)


## [5.2.2](https://github.com/phalcon/cphalcon/releases/tag/v5.2.2) (2023-06-18)
Expand Down
27 changes: 2 additions & 25 deletions phalcon/Config/Adapter/Json.zep
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

namespace Phalcon\Config\Adapter;

use InvalidArgumentException; // @todo this will also be removed when traits are available
use Phalcon\Config\Config;
use Phalcon\Support\Helper\Json\Decode;

/**
* Reads JSON files and converts them to Phalcon\Config\Config objects.
Expand Down Expand Up @@ -41,33 +41,10 @@ class Json extends Config
public function __construct(string! filePath)
{
parent::__construct(
this->decode(
(new Decode())->__invoke(
file_get_contents(filePath),
true
)
);
}

/**
* @todo This will be removed when traits are introduced
*/
private function decode(
string! data,
bool associative = false,
int depth = 512,
int options = 0
) -> var
{
var decoded;

let decoded = json_decode(data, associative, depth, options);

if unlikely JSON_ERROR_NONE !== json_last_error() {
throw new InvalidArgumentException(
"json_decode error: " . json_last_error_msg()
);
}

return decoded;
}
}
34 changes: 9 additions & 25 deletions phalcon/DataMapper/Pdo/Profiler/Profiler.zep
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@

namespace Phalcon\DataMapper\Pdo\Profiler;

use InvalidArgumentException; // @todo this will also be removed when traits are available
use Phalcon\DataMapper\Pdo\Exception\Exception;
use Phalcon\Logger\Enum;
use Phalcon\Logger\LoggerInterface;
use Phalcon\Support\Helper\Json\Encode;

/**
* Sends query profiles to a logger.
Expand Down Expand Up @@ -50,6 +50,11 @@ class Profiler implements ProfilerInterface
*/
protected logger;

/**
* @var Encode
*/
private encode;

/**
* Constructor.
*
Expand All @@ -63,7 +68,8 @@ class Profiler implements ProfilerInterface

let this->logFormat = "{method} ({duration}s): {statement} {backtrace}",
this->logLevel = Enum::DEBUG,
this->logger = logger;
this->logger = logger,
this->encode = new Encode();
}

/**
Expand All @@ -85,7 +91,7 @@ class Profiler implements ProfilerInterface
this->context["duration"] = finish - this->context["start"],
this->context["finish"] = finish,
this->context["statement"] = statement,
this->context["values"] = empty(values) ? "" : this->encode(values);
this->context["values"] = empty(values) ? "" : this->encode->__invoke(values);

this->logger->log(this->logLevel, this->logFormat, this->context);

Expand Down Expand Up @@ -189,26 +195,4 @@ class Profiler implements ProfilerInterface
];
}
}

/**
* @todo This will be removed when traits are introduced
*/
private function encode(
var data,
int options = 0,
int depth = 512
) -> string
{
var encoded;

let encoded = json_encode(data, options, depth);

if unlikely JSON_ERROR_NONE !== json_last_error() {
throw new InvalidArgumentException(
"json_encode error: " . json_last_error_msg()
);
}

return encoded;
}
}
40 changes: 12 additions & 28 deletions phalcon/Encryption/Security/JWT/Builder.zep
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@

namespace Phalcon\Encryption\Security\JWT;

use InvalidArgumentException; // @todo this will also be removed when traits are available
use Phalcon\Support\Collection;
use Phalcon\Support\Collection\CollectionInterface;
use Phalcon\Encryption\Security\JWT\Exceptions\ValidatorException;
use Phalcon\Encryption\Security\JWT\Signer\SignerInterface;
use Phalcon\Encryption\Security\JWT\Token\Enum;
use Phalcon\Encryption\Security\JWT\Token\Item;
use Phalcon\Encryption\Security\JWT\Token\Signature;
use Phalcon\Encryption\Security\JWT\Token\Token;
use Phalcon\Support\Collection;
use Phalcon\Support\Collection\CollectionInterface;
use Phalcon\Support\Helper\Json\Encode;

/**
* JWT Builder
Expand All @@ -32,6 +32,11 @@ class Builder
*/
private claims;

/**
* @var Encode
*/
private encode;

/**
* @var CollectionInterface
*/
Expand All @@ -57,7 +62,8 @@ class Builder
) {
this->init();

let this->signer = signer;
let this->signer = signer,
this->encode = new Encode();

this->jose->set(
Enum::ALGO,
Expand Down Expand Up @@ -192,9 +198,9 @@ class Builder
);
}

let encodedClaims = this->encodeUrl(this->encode(this->getClaims())),
let encodedClaims = this->encodeUrl(this->encode->__invoke(this->getClaims())),
claims = new Item(this->getClaims(), encodedClaims),
encodedHeaders = this->encodeUrl(this->encode(this->getHeaders())),
encodedHeaders = this->encodeUrl(this->encode->__invoke(this->getHeaders())),
headers = new Item(this->getHeaders(), encodedHeaders),
signatureHash = this->signer->sign(
encodedHeaders . "." . encodedClaims,
Expand Down Expand Up @@ -426,26 +432,4 @@ class Builder
{
return str_replace("=", "", strtr(base64_encode(input), "+/", "-_"));
}

/**
* @todo This will be removed when traits are introduced
*/
private function encode(
var data,
int options = 0,
int depth = 512
) -> string
{
var encoded;

let encoded = json_encode(data, options, depth);

if unlikely JSON_ERROR_NONE !== json_last_error() {
throw new InvalidArgumentException(
"json_encode error: " . json_last_error_msg()
);
}

return encoded;
}
}
45 changes: 20 additions & 25 deletions phalcon/Encryption/Security/JWT/Token/Parser.zep
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
namespace Phalcon\Encryption\Security\JWT\Token;

use InvalidArgumentException;
use Phalcon\Support\Helper\Json\Decode;

/**
* Token Parser class.
Expand All @@ -21,6 +22,23 @@ use InvalidArgumentException;
*/
class Parser
{
/**
* @var Decode
*/
private decode;

public function __construct(<Decode> decode = null)
{
var service;

let service = decode;
if (null === service) {
let service = new Decode();
}

let this->decode = service;
}

/**
* Parse a token and return it
*
Expand Down Expand Up @@ -55,7 +73,7 @@ class Parser
{
var decoded;

let decoded = this->decode(this->decodeUrl(claims), true);
let decoded = this->decode->__invoke(this->decodeUrl(claims), true);

if typeof decoded !== "array" {
throw new InvalidArgumentException(
Expand Down Expand Up @@ -84,7 +102,7 @@ class Parser
{
var decoded;

let decoded = this->decode(this->decodeUrl(headers), true);
let decoded = this->decode->__invoke(this->decodeUrl(headers), true);

if typeof decoded !== "array" {
throw new InvalidArgumentException(
Expand Down Expand Up @@ -147,29 +165,6 @@ class Parser
return parts;
}

/**
* @todo This will be removed when traits are introduced
*/
private function decode(
string! data,
bool associative = false,
int depth = 512,
int options = 0
) -> var
{
var decoded;

let decoded = json_decode(data, associative, depth, options);

if unlikely JSON_ERROR_NONE !== json_last_error() {
throw new InvalidArgumentException(
"json_decode error: " . json_last_error_msg()
);
}

return decoded;
}

/**
* @todo This will be removed when traits are introduced
*/
Expand Down
42 changes: 13 additions & 29 deletions phalcon/Http/Response.zep
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ namespace Phalcon\Http;

use DateTime;
use DateTimeZone;
use InvalidArgumentException; // @todo this will also be removed when traits are available
use Phalcon\Di\Di;
use Phalcon\Di\DiInterface;
use Phalcon\Di\InjectionAwareInterface;
use Phalcon\Events\EventsAwareInterface;
use Phalcon\Events\ManagerInterface;
use Phalcon\Http\Message\ResponseStatusCodeInterface;
use Phalcon\Http\Response\CookiesInterface;
use Phalcon\Http\Response\Exception;
use Phalcon\Http\Response\HeadersInterface;
use Phalcon\Http\Response\CookiesInterface;
use Phalcon\Mvc\Url\UrlInterface;
use Phalcon\Mvc\ViewInterface;
use Phalcon\Http\Response\Headers;
use Phalcon\Di\InjectionAwareInterface;
use Phalcon\Events\EventsAwareInterface;
use Phalcon\Events\ManagerInterface;
use Phalcon\Support\Helper\Json\Encode;

/**
* Part of the HTTP cycle is return responses to the clients.
Expand Down Expand Up @@ -82,6 +82,11 @@ class Response implements ResponseInterface, InjectionAwareInterface, EventsAwar
*/
protected statusCodes = [];

/**
* @var Encode
*/
private encode;

/**
* Phalcon\Http\Response constructor
*/
Expand All @@ -91,7 +96,8 @@ class Response implements ResponseInterface, InjectionAwareInterface, EventsAwar

// A Phalcon\Http\Response\Headers bag is temporary used to manage
// the headers before sent them to the client
let this->headers = new Headers();
let this->headers = new Headers(),
this->encode = new Encode();

if content !== null {
this->setContent(content);
Expand Down Expand Up @@ -638,7 +644,7 @@ class Response implements ResponseInterface, InjectionAwareInterface, EventsAwar
{
this->setContentType("application/json");

this->setContent(this->encode(content, jsonOptions, depth));
this->setContent(this->encode->__invoke(content, jsonOptions, depth));

return this;
}
Expand Down Expand Up @@ -865,26 +871,4 @@ class Response implements ResponseInterface, InjectionAwareInterface, EventsAwar

return filename;
}

/**
* @todo This will be removed when traits are introduced
*/
private function encode(
var data,
int options = 0,
int depth = 512
) -> string
{
var encoded;

let encoded = json_encode(data, options, depth);

if unlikely JSON_ERROR_NONE !== json_last_error() {
throw new InvalidArgumentException(
"json_encode error: " . json_last_error_msg()
);
}

return encoded;
}
}
Loading