mirror of
https://codeberg.org/demostf/api.git
synced 2026-06-03 18:04:08 +02:00
fmt, typings, typos
This commit is contained in:
parent
eb997b5ba2
commit
7de4c35ab4
34 changed files with 644 additions and 1048 deletions
|
|
@ -4,8 +4,9 @@ $finder = PhpCsFixer\Finder::create()
|
||||||
->exclude('vendor')
|
->exclude('vendor')
|
||||||
->in(__DIR__);
|
->in(__DIR__);
|
||||||
|
|
||||||
return PhpCsFixer\Config::create()
|
|
||||||
->setRules([
|
$config = new PhpCsFixer\Config();
|
||||||
|
return $config->setRules([
|
||||||
'@PSR2' => true,
|
'@PSR2' => true,
|
||||||
'@Symfony' => true,
|
'@Symfony' => true,
|
||||||
'@Symfony:risky' => true,
|
'@Symfony:risky' => true,
|
||||||
|
|
@ -1,12 +1,11 @@
|
||||||
{
|
{
|
||||||
"require": {
|
"require": {
|
||||||
"mikecao/flight": "1.3.8",
|
"mikecao/flight": "1.3.8",
|
||||||
"vlucas/phpdotenv": "^1.1",
|
"vlucas/phpdotenv": "^v5.4.1",
|
||||||
"ircmaxell/random-lib": "^1.1",
|
"ircmaxell/random-lib": "^v1.2.0",
|
||||||
"ehesp/steam-login": "^1.2",
|
"ehesp/steam-login": "^1.2",
|
||||||
"koraktor/steam-condenser": "dev-master#92dde9e3b462e93cc498fe795a712c61eec7051e",
|
"koraktor/steam-condenser": "dev-master#92dde9e3b462e93cc498fe795a712c61eec7051e",
|
||||||
"guzzlehttp/guzzle": "^6.5",
|
"doctrine/dbal": "^3.3.6",
|
||||||
"doctrine/dbal": "^3.0",
|
|
||||||
"ext-pdo": "*",
|
"ext-pdo": "*",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"ext-apcu": "*"
|
"ext-apcu": "*"
|
||||||
|
|
@ -24,9 +23,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "^9",
|
"phpunit/phpunit": "^9.5.20",
|
||||||
"friendsofphp/php-cs-fixer": "^2.16",
|
"friendsofphp/php-cs-fixer": "^v3.3.0",
|
||||||
"phpstan/phpstan": "^0.12.57"
|
"phpstan/phpstan": "^1.6.8"
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
"allow-plugins": {
|
"allow-plugins": {
|
||||||
|
|
|
||||||
1607
composer.lock
generated
1607
composer.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -34,13 +34,8 @@ class BaseController {
|
||||||
return $this->request->data[$name] ?? $default;
|
return $this->request->data[$name] ?? $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $data
|
|
||||||
*
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
protected function json(
|
protected function json(
|
||||||
$data,
|
mixed $data,
|
||||||
int $code = 200,
|
int $code = 200,
|
||||||
bool $encode = true,
|
bool $encode = true,
|
||||||
string $charset = 'utf-8',
|
string $charset = 'utf-8',
|
||||||
|
|
|
||||||
|
|
@ -40,8 +40,8 @@ class DemoController extends BaseController {
|
||||||
|
|
||||||
public function get(string $id): void {
|
public function get(string $id): void {
|
||||||
$demo = $this->demoProvider->get(\intval($id, 10));
|
$demo = $this->demoProvider->get(\intval($id, 10));
|
||||||
if ($demo === null) {
|
if (null === $demo) {
|
||||||
throw new NotFoundException("requested demo not found");
|
throw new NotFoundException('requested demo not found');
|
||||||
} else {
|
} else {
|
||||||
$this->json($demo);
|
$this->json($demo);
|
||||||
}
|
}
|
||||||
|
|
@ -146,7 +146,7 @@ class DemoController extends BaseController {
|
||||||
|
|
||||||
$demo = $this->demoProvider->get((int) $id);
|
$demo = $this->demoProvider->get((int) $id);
|
||||||
if (!$demo) {
|
if (!$demo) {
|
||||||
throw new NotFoundException("Demo not found");
|
throw new NotFoundException('Demo not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
$existingHash = $demo->getHash();
|
$existingHash = $demo->getHash();
|
||||||
|
|
|
||||||
|
|
@ -87,7 +87,6 @@ class DemoPlayer implements JsonSerializable {
|
||||||
/**
|
/**
|
||||||
* @return array{'id': int, 'user_id': int, 'name': string, 'team': string, 'class': string, 'steamid': string, 'kills': int, 'assists': int, 'deaths': int}
|
* @return array{'id': int, 'user_id': int, 'name': string, 'team': string, 'class': string, 'steamid': string, 'kills': int, 'assists': int, 'deaths': int}
|
||||||
*/
|
*/
|
||||||
#[\ReturnTypeWillChange]
|
|
||||||
public function jsonSerialize(): array {
|
public function jsonSerialize(): array {
|
||||||
return [
|
return [
|
||||||
'id' => $this->getId(),
|
'id' => $this->getId(),
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ class SteamUser implements JsonSerializable {
|
||||||
public function getName(): string {
|
public function getName(): string {
|
||||||
return $this->name;
|
return $this->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array{'id': int, 'name': string, 'steamid': string}
|
* @return array{'id': int, 'name': string, 'steamid': string}
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -34,11 +34,11 @@ class User implements JsonSerializable {
|
||||||
public function getToken(): string {
|
public function getToken(): string {
|
||||||
return $this->token;
|
return $this->token;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array{'id': int, 'name': string, 'steamid': string}
|
* @return array{'id': int, 'name': string, 'steamid': string}
|
||||||
*/
|
*/
|
||||||
#[\ReturnTypeWillChange]
|
public function jsonSerialize(): array {
|
||||||
public function jsonSerialize() {
|
|
||||||
return [
|
return [
|
||||||
'id' => $this->getId(),
|
'id' => $this->getId(),
|
||||||
'steamid' => $this->getSteamId(),
|
'steamid' => $this->getSteamId(),
|
||||||
|
|
|
||||||
|
|
@ -35,8 +35,7 @@ class ChatMessage implements JsonSerializable {
|
||||||
/**
|
/**
|
||||||
* @return array{'user': string, 'time': int, 'message': string}
|
* @return array{'user': string, 'time': int, 'message': string}
|
||||||
*/
|
*/
|
||||||
#[\ReturnTypeWillChange]
|
public function jsonSerialize(): array {
|
||||||
public function jsonSerialize() {
|
|
||||||
return [
|
return [
|
||||||
'user' => $this->user,
|
'user' => $this->user,
|
||||||
'time' => $this->time,
|
'time' => $this->time,
|
||||||
|
|
@ -44,8 +43,11 @@ class ChatMessage implements JsonSerializable {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromRow(array $row): ChatMessage {
|
/**
|
||||||
return new ChatMessage(
|
* @param array{'from': string, 'time': string, 'text': string} $row
|
||||||
|
*/
|
||||||
|
public static function fromRow(array $row): self {
|
||||||
|
return new self(
|
||||||
$row['from'],
|
$row['from'],
|
||||||
(int) $row['time'],
|
(int) $row['time'],
|
||||||
$row['text']
|
$row['text']
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,25 @@ class Demo implements JsonSerializable {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed[] $row
|
* @param array{
|
||||||
|
* 'id': string,
|
||||||
|
* 'url': string,
|
||||||
|
* 'name': string,
|
||||||
|
* 'server': string,
|
||||||
|
* 'duration': string,
|
||||||
|
* 'nick': string,
|
||||||
|
* 'map': string,
|
||||||
|
* 'created_at': string,
|
||||||
|
* 'red': string,
|
||||||
|
* 'blu': string,
|
||||||
|
* 'scoreRed': string,
|
||||||
|
* 'scoreBlue': string,
|
||||||
|
* 'playerCount': string,
|
||||||
|
* 'uploader': string,
|
||||||
|
* 'hash': string,
|
||||||
|
* 'backend': string,
|
||||||
|
* 'path': string,
|
||||||
|
* } $row
|
||||||
*
|
*
|
||||||
* @return Demo
|
* @return Demo
|
||||||
*/
|
*/
|
||||||
|
|
@ -190,15 +208,27 @@ class Demo implements JsonSerializable {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array{
|
* @return array{
|
||||||
* 'id': int, 'url': string, 'name': string,
|
* 'id': int,
|
||||||
* 'server': string, 'duration': float, 'nick': string,
|
* 'url': string,
|
||||||
* 'map': string, 'time': int, 'red': string, 'blue': string,
|
* 'name': string,
|
||||||
* 'redScore': int, 'blueScore': int, 'playerCount': int,
|
* 'server': string,
|
||||||
* 'uploader': array, 'hash': string, 'backend': string, 'path': string
|
* 'duration': float,
|
||||||
|
* 'nick': string,
|
||||||
|
* 'map': string,
|
||||||
|
* 'time': int,
|
||||||
|
* 'red': string,
|
||||||
|
* 'blue': string,
|
||||||
|
* 'redScore': int,
|
||||||
|
* 'blueScore': int,
|
||||||
|
* 'playerCount': int,
|
||||||
|
* 'uploader': User|int,
|
||||||
|
* 'hash': string,
|
||||||
|
* 'backend': string,
|
||||||
|
* 'path': string,
|
||||||
|
* 'players': ?DemoPlayer
|
||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
#[\ReturnTypeWillChange]
|
public function jsonSerialize(): array {
|
||||||
public function jsonSerialize() {
|
|
||||||
$data = [
|
$data = [
|
||||||
'id' => $this->getId(),
|
'id' => $this->getId(),
|
||||||
'url' => $this->getUrl(),
|
'url' => $this->getUrl(),
|
||||||
|
|
@ -218,7 +248,7 @@ class Demo implements JsonSerializable {
|
||||||
'backend' => $this->getBackend(),
|
'backend' => $this->getBackend(),
|
||||||
'path' => $this->getPath(),
|
'path' => $this->getPath(),
|
||||||
];
|
];
|
||||||
if (is_array($this->players)) {
|
if (\is_array($this->players)) {
|
||||||
$data['players'] = $this->getPlayers();
|
$data['players'] = $this->getPlayers();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -71,13 +71,13 @@ class DemoSaver {
|
||||||
|
|
||||||
foreach ($demo->getKills() as $kill) {
|
foreach ($demo->getKills() as $kill) {
|
||||||
if ($kill->getAttackerDemoId()) {
|
if ($kill->getAttackerDemoId()) {
|
||||||
$kills[$kill->getAttackerDemoId()]++;
|
++$kills[$kill->getAttackerDemoId()];
|
||||||
}
|
}
|
||||||
if ($kill->getAssisterDemoId()) {
|
if ($kill->getAssisterDemoId()) {
|
||||||
$assists[$kill->getAssisterDemoId()]++;
|
++$assists[$kill->getAssisterDemoId()];
|
||||||
}
|
}
|
||||||
if ($kill->getVictimDemoId()) {
|
if ($kill->getVictimDemoId()) {
|
||||||
$deaths[$kill->getVictimDemoId()]++;
|
++$deaths[$kill->getVictimDemoId()];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ class Header {
|
||||||
protected float $duration;
|
protected float $duration;
|
||||||
protected int $ticks;
|
protected int $ticks;
|
||||||
protected int $frames;
|
protected int $frames;
|
||||||
protected int $sigon;
|
protected int $singOn;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
string $type,
|
string $type,
|
||||||
|
|
@ -43,7 +43,7 @@ class Header {
|
||||||
$this->duration = $duration;
|
$this->duration = $duration;
|
||||||
$this->ticks = $ticks;
|
$this->ticks = $ticks;
|
||||||
$this->frames = $frames;
|
$this->frames = $frames;
|
||||||
$this->sigon = $sigon;
|
$this->singOn = $sigon;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDuration(): float {
|
public function getDuration(): float {
|
||||||
|
|
@ -74,8 +74,8 @@ class Header {
|
||||||
return $this->server;
|
return $this->server;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSigon(): int {
|
public function getSingOn(): int {
|
||||||
return $this->sigon;
|
return $this->singOn;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getTicks(): int {
|
public function getTicks(): int {
|
||||||
|
|
@ -91,7 +91,19 @@ class Header {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed[] $info
|
* @param array{
|
||||||
|
* 'type': string,
|
||||||
|
* 'version': int,
|
||||||
|
* 'protocol': int,
|
||||||
|
* 'server': string,
|
||||||
|
* 'nick': string,
|
||||||
|
* 'map': string,
|
||||||
|
* 'game': string,
|
||||||
|
* 'duration': float,
|
||||||
|
* 'ticks': int,
|
||||||
|
* 'frames': int,
|
||||||
|
* 'singon': int,
|
||||||
|
* } $info
|
||||||
*
|
*
|
||||||
* @return Header
|
* @return Header
|
||||||
*/
|
*/
|
||||||
|
|
@ -107,7 +119,7 @@ class Header {
|
||||||
$info['duration'],
|
$info['duration'],
|
||||||
$info['ticks'],
|
$info['ticks'],
|
||||||
$info['frames'],
|
$info['frames'],
|
||||||
$info['sigon']
|
$info['singon']
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ class HeaderParser {
|
||||||
*/
|
*/
|
||||||
public function parseString(string $head): Header {
|
public function parseString(string $head): Header {
|
||||||
$info = @unpack(
|
$info = @unpack(
|
||||||
'A8type/Iversion/Iprotocol/A260server/A260nick/A260map/A260game/fduration/Vticks/Vframes/Vsigon',
|
'A8type/Iversion/Iprotocol/A260server/A260nick/A260map/A260game/fduration/Vticks/Vframes/Vsingon',
|
||||||
$head
|
$head
|
||||||
);
|
);
|
||||||
if (!isset($info['type']) || 'HL2DEMO' !== $info['type']) {
|
if (!isset($info['type']) || 'HL2DEMO' !== $info['type']) {
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ use InvalidArgumentException;
|
||||||
* Processes the raw demo.js output to something more suitable for our purpose
|
* Processes the raw demo.js output to something more suitable for our purpose
|
||||||
*/
|
*/
|
||||||
class Parser {
|
class Parser {
|
||||||
const CLASSES = [
|
public const CLASSES = [
|
||||||
1 => 'scout',
|
1 => 'scout',
|
||||||
2 => 'sniper',
|
2 => 'sniper',
|
||||||
3 => 'soldier',
|
3 => 'soldier',
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ declare(strict_types=1);
|
||||||
namespace Demostf\API\Demo;
|
namespace Demostf\API\Demo;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use GuzzleHttp\Exception\RequestException;
|
use JsonException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wrapper around demo.js parser.
|
* Wrapper around demo.js parser.
|
||||||
|
|
@ -28,13 +28,13 @@ class RawParser {
|
||||||
try {
|
try {
|
||||||
$command = $this->parserPath . ' ' . escapeshellarg($path);
|
$command = $this->parserPath . ' ' . escapeshellarg($path);
|
||||||
$output = shell_exec($command);
|
$output = shell_exec($command);
|
||||||
$result = \GuzzleHttp\json_decode($output, true);
|
$result = json_decode($output, true, 512, \JSON_THROW_ON_ERROR);
|
||||||
if (null === $result) {
|
if (null === $result) {
|
||||||
throw new Exception('Failed to parse demo, unexpected result from parser');
|
throw new Exception('Failed to parse demo, unexpected result from parser');
|
||||||
} else {
|
} else {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
} catch (RequestException $e) {
|
} catch (JsonException $e) {
|
||||||
throw new Exception('Failed to parse demo, ' . $e->getMessage());
|
throw new Exception('Failed to parse demo, ' . $e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
/**
|
/**
|
||||||
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
|
* @copyright Copyright (c) 2020 Robin Appelman <robin@icewind.nl>
|
||||||
*
|
|
||||||
* @license GNU AGPL version 3 or any later version
|
* @license GNU AGPL version 3 or any later version
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
|
@ -18,7 +17,6 @@ declare(strict_types=1);
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace Demostf\API\Error;
|
namespace Demostf\API\Error;
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ class DemoListProvider extends BaseProvider {
|
||||||
$query->select('p.demo_id')
|
$query->select('p.demo_id')
|
||||||
->from('players', 'p');
|
->from('players', 'p');
|
||||||
|
|
||||||
if (\count($userIds) != count($players)) {
|
if (\count($userIds) != \count($players)) {
|
||||||
// one of more user ids don't have any demos
|
// one of more user ids don't have any demos
|
||||||
return [];
|
return [];
|
||||||
} elseif (\count($userIds) > 1) {
|
} elseif (\count($userIds) > 1) {
|
||||||
|
|
@ -117,11 +117,11 @@ class DemoListProvider extends BaseProvider {
|
||||||
}
|
}
|
||||||
if (isset($where['before'])) {
|
if (isset($where['before'])) {
|
||||||
$query->andWhere($query->expr()->lt('created_at',
|
$query->andWhere($query->expr()->lt('created_at',
|
||||||
$query->createNamedParameter($where['before']->format(DATE_ATOM))));
|
$query->createNamedParameter($where['before']->format(\DATE_ATOM))));
|
||||||
}
|
}
|
||||||
if (isset($where['after'])) {
|
if (isset($where['after'])) {
|
||||||
$query->andWhere($query->expr()->gt('created_at',
|
$query->andWhere($query->expr()->gt('created_at',
|
||||||
$query->createNamedParameter($where['after']->format(DATE_ATOM))));
|
$query->createNamedParameter($where['after']->format(\DATE_ATOM))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -133,7 +133,7 @@ class DemoListProvider extends BaseProvider {
|
||||||
* @return Demo[]
|
* @return Demo[]
|
||||||
*/
|
*/
|
||||||
public function listDemos(int $page, array $where = [], string $order = 'DESC'): array {
|
public function listDemos(int $page, array $where = [], string $order = 'DESC'): array {
|
||||||
if (isset($where['players']) and \is_array($where['players']) and \count($where['players']) > 0) {
|
if (isset($where['players']) && \is_array($where['players']) && \count($where['players']) > 0) {
|
||||||
return $this->listProfile($page, $where);
|
return $this->listProfile($page, $where);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -155,7 +155,25 @@ class DemoListProvider extends BaseProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array[] $rows
|
* @param array{
|
||||||
|
* 'id': string,
|
||||||
|
* 'url': string,
|
||||||
|
* 'name': string,
|
||||||
|
* 'server': string,
|
||||||
|
* 'duration': string,
|
||||||
|
* 'nick': string,
|
||||||
|
* 'map': string,
|
||||||
|
* 'created_at': string,
|
||||||
|
* 'red': string,
|
||||||
|
* 'blu': string,
|
||||||
|
* 'scoreRed': string,
|
||||||
|
* 'scoreBlue': string,
|
||||||
|
* 'playerCount': string,
|
||||||
|
* 'uploader': string,
|
||||||
|
* 'hash': string,
|
||||||
|
* 'backend': string,
|
||||||
|
* 'path': string,
|
||||||
|
* }[] $rows
|
||||||
*
|
*
|
||||||
* @return Demo[]
|
* @return Demo[]
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ use Doctrine\DBAL\Connection;
|
||||||
use PDO;
|
use PDO;
|
||||||
|
|
||||||
class DemoProvider extends BaseProvider {
|
class DemoProvider extends BaseProvider {
|
||||||
const VERSION = 4;
|
public const VERSION = 4;
|
||||||
|
|
||||||
private UserProvider $userProvider;
|
private UserProvider $userProvider;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,6 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Demostf\API\Providers;
|
namespace Demostf\API\Providers;
|
||||||
|
|
||||||
use PDO;
|
|
||||||
|
|
||||||
class InfoProvider extends BaseProvider {
|
class InfoProvider extends BaseProvider {
|
||||||
/**
|
/**
|
||||||
* @return string[]
|
* @return string[]
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,7 @@ class UserProvider extends BaseProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getById(int $userId): ?User {
|
public function getById(int $userId): ?User {
|
||||||
if ($userId > pow(2, 31)) {
|
if ($userId > 2 ** 31) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
// first search in the view which contains the most used name for the users
|
// first search in the view which contains the most used name for the users
|
||||||
|
|
|
||||||
|
|
@ -4,8 +4,6 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Demostf\API;
|
namespace Demostf\API;
|
||||||
|
|
||||||
use Demostf\API\Error\InvalidHashException;
|
|
||||||
use Demostf\API\Error\InvalidKeyException;
|
|
||||||
use Flight;
|
use Flight;
|
||||||
use flight\net\Response;
|
use flight\net\Response;
|
||||||
|
|
||||||
|
|
@ -75,7 +73,7 @@ Flight::map('error', function (\Throwable $ex) {
|
||||||
$code = $ex->getCode();
|
$code = $ex->getCode();
|
||||||
}
|
}
|
||||||
$response = Flight::response();
|
$response = Flight::response();
|
||||||
if (array_key_exists($code, Response::$codes)) {
|
if (\array_key_exists($code, Response::$codes)) {
|
||||||
$response->status($code);
|
$response->status($code);
|
||||||
} else {
|
} else {
|
||||||
$response->status(500);
|
$response->status(500);
|
||||||
|
|
|
||||||
32
src/init.php
32
src/init.php
|
|
@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
use Demostf\API\Container;
|
use Demostf\API\Container;
|
||||||
use Doctrine\DBAL\DriverManager;
|
use Doctrine\DBAL\DriverManager;
|
||||||
|
use Dotenv\Dotenv;
|
||||||
|
|
||||||
function get_magic_quotes_gpc(): bool {
|
function get_magic_quotes_gpc(): bool {
|
||||||
return false;
|
return false;
|
||||||
|
|
@ -12,20 +13,29 @@ function get_magic_quotes_gpc(): bool {
|
||||||
$autoloader = require __DIR__ . '/../vendor/autoload.php';
|
$autoloader = require __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
if (!getenv('DB_TYPE')) {
|
if (!getenv('DB_TYPE')) {
|
||||||
Dotenv::load(__DIR__ . '/../');
|
Dotenv::createImmutable(__DIR__ . '/../')->safeLoad();
|
||||||
}
|
}
|
||||||
|
|
||||||
$connectionParams = [
|
$driver = getenv('DB_TYPE') ?: '';
|
||||||
'dbname' => getenv('DB_DATABASE'),
|
if ($driver === 'pgsql') {
|
||||||
'user' => getenv('DB_USERNAME'),
|
$driver = 'pdo_pgsql';
|
||||||
'password' => getenv('DB_PASSWORD'),
|
|
||||||
'host' => getenv('DB_HOST'),
|
|
||||||
'port' => getenv('DB_PORT'),
|
|
||||||
'driver' => getenv('DB_TYPE'),
|
|
||||||
];
|
|
||||||
if ('pgsql' === $connectionParams['driver']) {
|
|
||||||
$connectionParams['driver'] = 'pdo_pgsql';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$availableDrivers = DriverManager::getAvailableDrivers();
|
||||||
|
if (!in_array($driver, $availableDrivers)) {
|
||||||
|
throw new \Exception("Unsupported driver " . $driver);
|
||||||
|
}
|
||||||
|
/** @var key-of<DriverManager::DRIVER_MAP> $driver */
|
||||||
|
|
||||||
|
$connectionParams = [
|
||||||
|
'dbname' => getenv('DB_DATABASE') ?: '',
|
||||||
|
'user' => getenv('DB_USERNAME') ?: '',
|
||||||
|
'password' => getenv('DB_PASSWORD') ?: '',
|
||||||
|
'host' => getenv('DB_HOST') ?: '',
|
||||||
|
'port' => (int) getenv('DB_PORT'),
|
||||||
|
'driver' => $driver,
|
||||||
|
];
|
||||||
|
|
||||||
$db = DriverManager::getConnection($connectionParams);
|
$db = DriverManager::getConnection($connectionParams);
|
||||||
$host = getenv('BASE_HOST') ?: '';
|
$host = getenv('BASE_HOST') ?: '';
|
||||||
$storeRoot = getenv('DEMO_ROOT') ?: '';
|
$storeRoot = getenv('DEMO_ROOT') ?: '';
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ Flight::map('error', function (\Throwable $ex) {
|
||||||
}
|
}
|
||||||
/** @var Response $response */
|
/** @var Response $response */
|
||||||
$response = Flight::response();
|
$response = Flight::response();
|
||||||
if (array_key_exists($code, Response::$codes)) {
|
if (\array_key_exists($code, Response::$codes)) {
|
||||||
$response->status($code);
|
$response->status($code);
|
||||||
} else {
|
} else {
|
||||||
$response->status(500);
|
$response->status(500);
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ class DemoControllerTest extends ControllerTest {
|
||||||
/** @var DemoListProvider|MockObject */
|
/** @var DemoListProvider|MockObject */
|
||||||
private $demoListProvider;
|
private $demoListProvider;
|
||||||
|
|
||||||
public function setUp(): void {
|
protected function setUp(): void {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->demoStore = $this->createMock(DemoStore::class);
|
$this->demoStore = $this->createMock(DemoStore::class);
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ class HeaderParserTest extends TestCase {
|
||||||
$this->assertEquals($expected->getMap(), $parsed->getMap());
|
$this->assertEquals($expected->getMap(), $parsed->getMap());
|
||||||
$this->assertEquals($expected->getNick(), $parsed->getNick());
|
$this->assertEquals($expected->getNick(), $parsed->getNick());
|
||||||
$this->assertEquals($expected->getProtocol(), $parsed->getProtocol());
|
$this->assertEquals($expected->getProtocol(), $parsed->getProtocol());
|
||||||
$this->assertEquals($expected->getSigon(), $parsed->getSigon());
|
$this->assertEquals($expected->getSingOn(), $parsed->getSingOn());
|
||||||
$this->assertEquals($expected->getType(), $parsed->getType());
|
$this->assertEquals($expected->getType(), $parsed->getType());
|
||||||
$this->assertEquals($expected->getVersion(), $parsed->getVersion());
|
$this->assertEquals($expected->getVersion(), $parsed->getVersion());
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ class ParserTest extends TestCase {
|
||||||
/** @var RawParser|MockObject */
|
/** @var RawParser|MockObject */
|
||||||
private $rawParser;
|
private $rawParser;
|
||||||
|
|
||||||
public function setUp(): void {
|
protected function setUp(): void {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->rawParser = $this->getMockBuilder(RawParser::class)
|
$this->rawParser = $this->getMockBuilder(RawParser::class)
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ class ChatProviderTest extends TestCase {
|
||||||
/** @var ChatProvider */
|
/** @var ChatProvider */
|
||||||
private $provider;
|
private $provider;
|
||||||
|
|
||||||
public function setUp(): void {
|
protected function setUp(): void {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->provider = new ChatProvider($this->getDatabaseConnection());
|
$this->provider = new ChatProvider($this->getDatabaseConnection());
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ class DemoListProviderTest extends TestCase {
|
||||||
/** @var UserProvider */
|
/** @var UserProvider */
|
||||||
private $userProvider;
|
private $userProvider;
|
||||||
|
|
||||||
public function setUp(): void {
|
protected function setUp(): void {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->demoListProvider = new DemoListProvider($this->getDatabaseConnection());
|
$this->demoListProvider = new DemoListProvider($this->getDatabaseConnection());
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ class DemoProviderTest extends TestCase {
|
||||||
/** @var PlayerProvider */
|
/** @var PlayerProvider */
|
||||||
private $playerProvider;
|
private $playerProvider;
|
||||||
|
|
||||||
public function setUp(): void {
|
protected function setUp(): void {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->userProvider = new UserProvider($this->getDatabaseConnection(), $this->getRandomGenerator());
|
$this->userProvider = new UserProvider($this->getDatabaseConnection(), $this->getRandomGenerator());
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ class UploadProviderTest extends TestCase {
|
||||||
/**
|
/**
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
*/
|
*/
|
||||||
public function setUp(): void {
|
protected function setUp(): void {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->rawParser = $this->getMockBuilder(RawParser::class)
|
$this->rawParser = $this->getMockBuilder(RawParser::class)
|
||||||
|
|
@ -111,7 +111,7 @@ class UploadProviderTest extends TestCase {
|
||||||
rmdir($dir);
|
rmdir($dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tearDown(): void {
|
protected function tearDown(): void {
|
||||||
$this->rmdirr($this->tmpDir);
|
$this->rmdirr($this->tmpDir);
|
||||||
|
|
||||||
parent::tearDown();
|
parent::tearDown();
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ class UserProviderTest extends TestCase {
|
||||||
/** @var SteamId */
|
/** @var SteamId */
|
||||||
private $steamId;
|
private $steamId;
|
||||||
|
|
||||||
public function setUp(): void {
|
protected function setUp(): void {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->steamId = $this->getSteamId('76561198024494988', 'Icewind');
|
$this->steamId = $this->getSteamId('76561198024494988', 'Icewind');
|
||||||
|
|
|
||||||
|
|
@ -31,11 +31,11 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
|
||||||
return $this->database;
|
return $this->database;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setUp(): void {
|
protected function setUp(): void {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tearDown(): void {
|
protected function tearDown(): void {
|
||||||
$this->clearDatabase();
|
$this->clearDatabase();
|
||||||
parent::tearDown();
|
parent::tearDown();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,14 +32,14 @@ if ('/upload' === $_SERVER['REQUEST_URI']) {
|
||||||
require __DIR__ . '/../src/public/index.php';
|
require __DIR__ . '/../src/public/index.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
function clearDatabase(\Doctrine\DBAL\Connection $connection) {
|
function clearDatabase(Doctrine\DBAL\Connection $connection) {
|
||||||
$tables = $connection->getSchemaManager()->listTables();
|
$tables = $connection->getSchemaManager()->listTables();
|
||||||
foreach ($tables as $table) {
|
foreach ($tables as $table) {
|
||||||
truncateTable($connection, $table->getName());
|
truncateTable($connection, $table->getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function truncateTable(\Doctrine\DBAL\Connection $connection, string $tableName) {
|
function truncateTable(Doctrine\DBAL\Connection $connection, string $tableName) {
|
||||||
$sql = sprintf('TRUNCATE TABLE %s;', $tableName);
|
$sql = sprintf('TRUNCATE TABLE %s;', $tableName);
|
||||||
$connection->query($sql);
|
$connection->query($sql);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue