Skip to content

Fix curl/sync_constants.php #9391

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 21, 2022
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: 0 additions & 2 deletions ext/curl/interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,6 @@ PHP_MINFO_FUNCTION(curl)
}
/* }}} */

#define REGISTER_CURL_CONSTANT(__c) REGISTER_LONG_CONSTANT(#__c, __c, CONST_CS | CONST_PERSISTENT)

/* {{{ PHP_MINIT_FUNCTION */
PHP_MINIT_FUNCTION(curl)
{
Expand Down
146 changes: 76 additions & 70 deletions ext/curl/sync-constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,87 @@

const CURL_DOC_FILE = 'https://curl.haxx.se/libcurl/c/symbols-in-versions.html';

const SOURCE_FILE = __DIR__ . '/interface.c';
const SOURCE_FILE = __DIR__ . '/curl_arginfo.h';

const MIN_SUPPORTED_CURL_VERSION = '7.29.0';

const IGNORED_CONSTANTS = [
const IGNORED_CURL_CONSTANTS = [
'CURLOPT_PROGRESSDATA',
'CURLOPT_XFERINFODATA'
'CURLOPT_XFERINFODATA',
];

const IGNORED_PHP_CONSTANTS = [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are custom PHP constants which should be ignored by the sync script

'CURLOPT_BINARYTRANSFER',
'CURLOPT_RETURNTRANSFER',
'CURLOPT_SAFE_UPLOAD',
];

const CONSTANTS_REGEX_PATTERN = '~^CURL(?:OPT|_VERSION)_[A-Z0-9_]+$~';

/**
* A simple helper to create ASCII tables.
* It assumes that the same number of columns is always given to add().
*/
class AsciiTable
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

had to move the declaration to the top, otherwise the class was not found on my system

{
/**
* @var array
*/
private $values = [];

/**
* @var array
*/
private $length = [];

/**
* @var int
*/
private $padding = 4;

/**
* @param string[] $values
*
* @return void
*/
public function add(string ...$values) : void
{
$this->values[] = $values;

foreach ($values as $key => $value) {
$length = strlen($value);

if (isset($this->length[$key])) {
$this->length[$key] = max($this->length[$key], $length);
} else {
$this->length[$key] = $length;
}
}
}

/**
* @return string
*/
public function __toString() : string
{
$result = '';

foreach ($this->values as $values) {
foreach ($values as $key => $value) {
if ($key !== 0) {
$result .= str_repeat(' ', $this->padding);
}

$result .= str_pad($value, $this->length[$key]);
}

$result .= "\n";
}

return $result;
}
}

$curlConstants = getCurlConstants();
$sourceConstants = getSourceConstants();

Expand Down Expand Up @@ -161,7 +231,7 @@ function getCurlConstants() : array
$deprecated = $match[3] ?? null;
$removed = $match[4] ?? null;

if (in_array($name, IGNORED_CONSTANTS, true) || !preg_match(CONSTANTS_REGEX_PATTERN, $name)) {
if (in_array($name, IGNORED_CURL_CONSTANTS, true) || !preg_match(CONSTANTS_REGEX_PATTERN, $name)) {
// not a wanted constant
continue;
}
Expand All @@ -187,7 +257,7 @@ function getSourceConstants() : array
{
$source = file_get_contents(SOURCE_FILE);

preg_match_all('/REGISTER_CURL_CONSTANT\(([A-Za-z0-9_]+)\)/', $source, $matches);
preg_match_all('/REGISTER_LONG_CONSTANT\(\"\w+\", (\w+), .+\)/', $source, $matches);

$constants = [];

Expand All @@ -196,7 +266,7 @@ function getSourceConstants() : array
continue;
}

if (!preg_match(CONSTANTS_REGEX_PATTERN, $name)) {
if (in_array($name, IGNORED_PHP_CONSTANTS, true) || !preg_match(CONSTANTS_REGEX_PATTERN, $name)) {
// not a wanted constant
continue;
}
Expand Down Expand Up @@ -254,67 +324,3 @@ function getHexVersion(string $version) : string

return $hex;
}

/**
* A simple helper to create ASCII tables.
* It assumes that the same number of columns is always given to add().
*/
class AsciiTable
{
/**
* @var array
*/
private $values = [];

/**
* @var array
*/
private $length = [];

/**
* @var int
*/
private $padding = 4;

/**
* @param string[] $values
*
* @return void
*/
public function add(string ...$values) : void
{
$this->values[] = $values;

foreach ($values as $key => $value) {
$length = strlen($value);

if (isset($this->length[$key])) {
$this->length[$key] = max($this->length[$key], $length);
} else {
$this->length[$key] = $length;
}
}
}

/**
* @return string
*/
public function __toString() : string
{
$result = '';

foreach ($this->values as $values) {
foreach ($values as $key => $value) {
if ($key !== 0) {
$result .= str_repeat(' ', $this->padding);
}

$result .= str_pad($value, $this->length[$key]);
}

$result .= "\n";
}

return $result;
}
}