Skip to content

Commit

Permalink
PHP 7.4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
janbarasek committed Oct 28, 2020
1 parent b1791c8 commit 18969ba
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 102 deletions.
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
}
],
"require": {
"php": ">=7.1.0",
"ext-curl": "*",
"nette/caching": "^3.0"
"php": ">=7.4.0",
"nette/caching": "^3.0",
"ext-curl": "*"
},
"require-dev": {
"phpstan/phpstan": "^0.12.18",
"tracy/tracy": "^2.7",
"phpstan/phpstan-nette": "^0.12.6",
"nette/security": "^3.0"
"nette/security": "^3.0",
"symplify/easy-coding-standard": "^7.2"
},
"autoload": {
"classmap": [
Expand Down
12 changes: 3 additions & 9 deletions src/Entity/ApiData.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ class ApiData extends \stdClass implements \ArrayAccess, \Countable, \IteratorAg

/**
* @param mixed[] $arr
* @param bool $recursive
* @return ApiData
*/
public static function from(array $arr, bool $recursive = true): self
{
$obj = new self;

foreach ($arr as $key => $value) {
if ($recursive && is_array($value)) {
$obj->$key = static::from($value, true);
Expand All @@ -31,18 +29,14 @@ public static function from(array $arr, bool $recursive = true): self
}


/**
* Returns an iterator over all items.
*/
/** Returns an iterator over all items. */
public function getIterator(): \RecursiveArrayIterator
{
return new \RecursiveArrayIterator((array) $this);
}


/**
* Returns items count.
*/
/** Returns items count. */
public function count(): int
{
return count((array) $this);
Expand Down Expand Up @@ -98,4 +92,4 @@ public function offsetUnset($key): void
{
unset($this->$key);
}
}
}
49 changes: 7 additions & 42 deletions src/GitLabApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,18 @@

final class GitLabApi
{
private string $token;

/** @var string */
private $token;
private bool $validateToken = false;

/** @var bool */
private $validateToken = false;
private ?Cache $cache;

/** @var Cache|null */
private $cache;

/** @var string */
private $baseUrl = 'https://gitlab.com/api/v4/';
private string $baseUrl = 'https://gitlab.com/api/v4/';


/**
* For valid service you must set $token or Nette user profile.
* If user profile is not available API will use default $token.
*
* @param string $token
* @param User|null $user
*/
public function __construct(string $token, ?User $user = null)
{
Expand All @@ -47,31 +39,21 @@ public function __construct(string $token, ?User $user = null)
}


/**
* @param string $baseUrl
*/
public function setBaseUrl(string $baseUrl): void
{
$this->baseUrl = rtrim($baseUrl, '/') . '/';
}


/**
* Use Nette Cache for storage API responses.
*
* @param IStorage $IStorage
*/
/** Use Nette Cache for storage API responses. */
public function setCache(IStorage $IStorage): void
{
$this->cache = new Cache($IStorage, 'gitlab-api');
}


/**
* @param string $url
* @param string[]|null $data
* @param string $cache
* @param string|null $token
* @return ApiData|ApiData[]
* @throws GitLabApiException
*/
Expand All @@ -91,13 +73,12 @@ public function request(string $url, ?array $data = null, string $cache = '12 ho
$hash = md5(json_encode([$url, $data, $token]));
$body = $this->cache === null ? null : $this->cache->load($hash);

if (!isset($_SERVER['REMOTE_ADDR'])) {
if (PHP_SAPI === 'cli') {
echo "\e[0;32m" . '[GitLab | URL: ' . "\e[0m\e[0;33m" . $url . "\e[0m\e[0;32m"
. ($data !== null ? ', DATA: ' . json_encode($data) : '')
. ' | TOKEN: ' . json_encode($token)
. ']' . "\e[0m\n";
}

if ($body !== null) {
GitLabApiPanel::addData($this->baseUrl, [
'duration' => Helper::timer($requestHash) * 1000,
Expand All @@ -120,7 +101,6 @@ public function request(string $url, ?array $data = null, string $cache = '12 ho
CURLOPT_HEADER => 1,
CURLINFO_HEADER_OUT => true,
]);

curl_setopt($curl, CURLOPT_HTTPHEADER, [
'PRIVATE-TOKEN: ' . $token,
]);
Expand All @@ -130,10 +110,8 @@ public function request(string $url, ?array $data = null, string $cache = '12 ho
$rawData = substr($resp, $headerSize);

$body = $this->mapToApiData(Helper::decode($rawData));

if (isset($body['error'])) {
$errorMessages = [];

foreach ($body as $key => $value) {
$errorMessages[] = trim($key) . ': ' . json_encode($value);
}
Expand Down Expand Up @@ -162,10 +140,7 @@ public function request(string $url, ?array $data = null, string $cache = '12 ho


/**
* @param string $url
* @param string[]|null $data
* @param string $method
* @param string|null $token
* @return ApiData|ApiData[]
* @throws GitLabApiException
*/
Expand All @@ -182,15 +157,14 @@ public function changeRequest(string $url, ?array $data = null, string $method =
$requestHash = 'url' . md5($url);
Helper::timer($requestHash);

if (!isset($_SERVER['REMOTE_ADDR'])) {
if (PHP_SAPI === 'cli') {
echo "\e[0;32m" . '[GitLab | URL: ' . "\e[0m\e[0;33m" . $url . "\e[0m\e[0;32m"
. ($data !== null ? ', DATA: ' . json_encode($data) : '')
. ' | TOKEN: ' . json_encode($token)
. ']' . "\e[0m\n";
}

$fullUrl = rtrim($this->baseUrl, '/') . '/' . ltrim($url, '/');

$configRequest = [
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_RETURNTRANSFER => 1,
Expand All @@ -210,7 +184,6 @@ public function changeRequest(string $url, ?array $data = null, string $method =
]);

$resp = curl_exec($curl);

if ($resp === false) {
GitLabApiPanel::addData($this->baseUrl, [
'duration' => Helper::timer($requestHash) * 1000,
Expand All @@ -225,12 +198,9 @@ public function changeRequest(string $url, ?array $data = null, string $method =

$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$rawData = substr((string) $resp, $headerSize);

$body = $this->mapToApiData(Helper::decode($rawData));

if (isset($body['error'])) {
$errorMessages = [];

foreach ($body as $key => $value) {
$errorMessages[] = trim($key) . ': ' . json_encode($value);
}
Expand Down Expand Up @@ -261,8 +231,6 @@ public function changeRequest(string $url, ?array $data = null, string $method =


/**
* @param string $token
* @return bool
* @throws GitLabApiException
*/
public function validateToken(string $token): bool
Expand All @@ -281,17 +249,14 @@ private function mapToApiData($haystack)
{
if (\is_array($haystack)) {
$return = [];

foreach ($haystack as $key => $value) {
$return[$key] = $this->mapToApiData($value);
}

return $return;
}

if ($haystack instanceof \stdClass) {
$return = new ApiData;

foreach ((array) $haystack as $key => $value) {
$return->{$key} = $value;
}
Expand Down
25 changes: 3 additions & 22 deletions src/GitLabApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ class GitLabApiException extends \Exception
*
* @var string[]
*/
private $errorConfigs;
private array $errorConfigs;


/**
* @param string $message
* @param string[] $errorConfigs
* @throws GitLabApiException
*/
Expand All @@ -39,14 +38,11 @@ public function __construct(string $message = '', array $errorConfigs = [])


/**
* @param string $token
* @throws GitLabApiException
*/
public static function tokenIsInvalid(string $token): void
{
throw new self(
'GitLab token "' . $token . '" is invalid.'
);
throw new self('GitLab token "' . $token . '" is invalid.');
}


Expand All @@ -59,9 +55,6 @@ public function getErrorConfigs(): array
}


/**
* @return string
*/
public function getErrorType(): string
{
if ($this->isKey('error')) {
Expand All @@ -72,30 +65,18 @@ public function getErrorType(): string
}


/**
* @return bool
*/
public function isDefaultError(): bool
{
return $this->getErrorType() === self::ERROR_ERROR;
}


/**
* @param string $key
* @return bool
*/
public function isKey(string $key): bool
{
return isset($this->errorConfigs[$key]);
}


/**
* @param string $key
* @param string|null $default
* @return string|null
*/
public function getKey(string $key, ?string $default = null): ?string
{
if ($this->isKey($key)) {
Expand All @@ -104,4 +85,4 @@ public function getKey(string $key, ?string $default = null): ?string

return $default;
}
}
}
8 changes: 3 additions & 5 deletions src/GitLabApiPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ class GitLabApiPanel implements IBarPanel
{
private const ICON = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAIAAABMXPacAAAABnRSTlMA/wD/AP83WBt9AAAN1UlEQVR4AexcZXPjSBTcXxOTvMy7xxfGZWaGaJmZmZmZmZmZmdnMzB7JNwv1qs6VOJY0tuWUp/rz5PW0q0f99JQakcxK6eItQGZlBMgIkFkZATICZFZGgIwAmZURICMAshitiybrexXblk5DNnOk2i3G6bCvmYcJWuaMCevVohPAsWGx6h/Zd/wrd2xbWf0EcB3YqsqmfnK0LZseYZCIBEBWE/5p4Mp+wtCvJWO3Vquf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wVwgAP5krX/LV8zbcAFCisjiRnxpI9wrkhX3qAlxCsibnYD+1YAAQUJkQ/dozL8ZEBzIf28eTYaHJtGa7mWwAEFPalNtdNDo89bphIfwBdzLWhBlnzLQD+JwoH+7/qVvFlpwqpPT34mm8B8M/n15+PLf90cGHRpxf4RwvAHt8DsMcCsADssQAsAHssAAvAni8AV5380akCdgAAAABJRU5ErkJggg==';

/** @var string */
private static $baseUrl;
private static string $baseUrl;

/** @var mixed[][] */
private static $calls = [];
private static array $calls = [];


/**
* @param string $baseUrl
* @param mixed[] $data
*/
public static function addData(string $baseUrl, array $data): void
Expand Down Expand Up @@ -69,4 +67,4 @@ public function getPanel(): string
return '<h1>GitLab requests' . (self::$baseUrl ? ' [' . self::$baseUrl . ']' : '') . '</h1>'
. '<div class="tracy-inner">' . $table . '</div>';
}
}
}
9 changes: 2 additions & 7 deletions src/GitLabUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

interface GitLabUser
{

/**
* Return current user GitLab token.
*
* @return string|null
*/
/** Return current user GitLab token. */
public function getGitLabToken(): ?string;
}
}
14 changes: 1 addition & 13 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,13 @@ final class Helper
private const FORCE_ARRAY = 0b0001;


/**
* @throws \Error
*/
/** @throws \Error */
public function __construct()
{
throw new \Error('Class ' . get_class($this) . ' is static and cannot be instantiated.');
}


/**
* Migrated from Nette/Tracy.
*
* @param string|null $name
* @return float
*/
public static function timer(?string $name = null): float
{
static $time = [];
Expand All @@ -40,8 +32,6 @@ public static function timer(?string $name = null): float
* Migrated from Nette/Utils.
* Decodes a JSON string. Accepts flag Json::FORCE_ARRAY.
*
* @param string $json
* @param int $flags
* @return mixed
* @throws GitLabApiException
*/
Expand All @@ -68,8 +58,6 @@ public static function decode(string $json, int $flags = 0)
* if (!@rename($src, $dst)) {
* throw new Exception('Unable to move directory: ' . Helper::getLastErrorMessage());
* }
*
* @return string|null
*/
public static function getLastErrorMessage(): ?string
{
Expand Down

0 comments on commit 18969ba

Please sign in to comment.