Dependencies upgrade, minor fixes

This commit is contained in:
Sys 2021-06-22 12:00:17 +02:00
parent 5aee50caf9
commit 59fa72b6e9
No known key found for this signature in database
GPG Key ID: 3CD2C29F8AB39BFD
6 changed files with 625 additions and 37 deletions

View File

@ -3,6 +3,8 @@
[![License](http://poser.pugx.org/sysbot/tgscraper/license)](https://packagist.org/packages/sysbot/tgscraper) [![License](http://poser.pugx.org/sysbot/tgscraper/license)](https://packagist.org/packages/sysbot/tgscraper)
![Required PHP Version](https://img.shields.io/badge/php-%E2%89%A58.0-brightgreen) ![Required PHP Version](https://img.shields.io/badge/php-%E2%89%A58.0-brightgreen)
[![Latest Stable Version](http://poser.pugx.org/sysbot/tgscraper/v)](https://packagist.org/packages/sysbot/tgscraper) [![Latest Stable Version](http://poser.pugx.org/sysbot/tgscraper/v)](https://packagist.org/packages/sysbot/tgscraper)
![Dependencies](https://img.shields.io/librariesio/github/Sysbot-org/tgscraper)
![Code Quality](https://img.shields.io/scrutinizer/quality/g/Sysbot-org/tgscraper)
A PHP library used to extract JSON data (and auto-generate PHP classes) A PHP library used to extract JSON data (and auto-generate PHP classes)
from [Telegram bot API documentation page](https://core.telegram.org/bots/api). from [Telegram bot API documentation page](https://core.telegram.org/bots/api).

View File

@ -46,7 +46,7 @@ if (is_dir($output)) {
try { try {
echo '> Extracting JSON scheme.' . PHP_EOL; echo '> Extracting JSON scheme.' . PHP_EOL;
$data = $generator->toJson(); $data = $generator->toJson();
} catch (Exception $e) { } catch (Throwable $e) {
echo '> ERROR: Unable to extract scheme: ' . $e->getMessage() . PHP_EOL; echo '> ERROR: Unable to extract scheme: ' . $e->getMessage() . PHP_EOL;
exit(1); exit(1);
} }
@ -54,6 +54,7 @@ if (is_dir($output)) {
echo sprintf('> Loaded data from "%s".%s', $scheme, PHP_EOL); echo sprintf('> Loaded data from "%s".%s', $scheme, PHP_EOL);
} }
echo sprintf('> Outputting PHP stubs to %s.%s', realpath($output), PHP_EOL); echo sprintf('> Outputting PHP stubs to %s.%s', realpath($output), PHP_EOL);
/** @noinspection PhpUnhandledExceptionInspection */
$result = $generator->toStubs($output, $namespace, $data); $result = $generator->toStubs($output, $namespace, $data);
if (!$result) { if (!$result) {
echo '> ERROR: unable to create stubs.' . PHP_EOL; echo '> ERROR: unable to create stubs.' . PHP_EOL;
@ -64,11 +65,12 @@ if (is_dir($output)) {
try { try {
echo '> Extracting JSON scheme.' . PHP_EOL; echo '> Extracting JSON scheme.' . PHP_EOL;
$data = $generator->toJson(); $data = $generator->toJson();
} catch (Exception $e) { } catch (Throwable $e) {
echo '> ERROR: Unable to extract scheme: ' . $e->getMessage() . PHP_EOL; echo '> ERROR: Unable to extract scheme: ' . $e->getMessage() . PHP_EOL;
exit(1); exit(1);
} }
echo sprintf('> Outputting PHP stubs to %s.%s', realpath($output), PHP_EOL); echo sprintf('> Outputting PHP stubs to %s.%s', realpath($output), PHP_EOL);
/** @noinspection PhpUnhandledExceptionInspection */
$result = $generator->toStubs($output, $namespace, $data); $result = $generator->toStubs($output, $namespace, $data);
if (!$result) { if (!$result) {
echo '> ERROR: unable to create stubs.' . PHP_EOL; echo '> ERROR: unable to create stubs.' . PHP_EOL;
@ -82,7 +84,7 @@ echo sprintf('> Outputting JSON schema to %s.%s', realpath($output), PHP_EOL);
try { try {
$data = $generator->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); $data = $generator->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
} catch (Exception $e) { } catch (Throwable $e) {
echo '> ERROR: Unable to generate scheme:' . $e->getMessage() . PHP_EOL; echo '> ERROR: Unable to generate scheme:' . $e->getMessage() . PHP_EOL;
exit(1); exit(1);
} }

View File

@ -6,7 +6,7 @@
"php": ">=8.0", "php": ">=8.0",
"ext-json": "*", "ext-json": "*",
"nette/php-generator": "^3.5", "nette/php-generator": "^3.5",
"paquettg/php-html-parser": "^2.2" "paquettg/php-html-parser": "^3.1"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

589
composer.lock generated
View File

@ -4,8 +4,301 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "bb4b87080b33e528f8e53f4c0a4568cd", "content-hash": "e3461482771ef0bfc9ff36941c968aa2",
"packages": [ "packages": [
{
"name": "guzzlehttp/guzzle",
"version": "7.3.0",
"source": {
"type": "git",
"url": "https://github.com/guzzle/guzzle.git",
"reference": "7008573787b430c1c1f650e3722d9bba59967628"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/7008573787b430c1c1f650e3722d9bba59967628",
"reference": "7008573787b430c1c1f650e3722d9bba59967628",
"shasum": ""
},
"require": {
"ext-json": "*",
"guzzlehttp/promises": "^1.4",
"guzzlehttp/psr7": "^1.7 || ^2.0",
"php": "^7.2.5 || ^8.0",
"psr/http-client": "^1.0"
},
"provide": {
"psr/http-client-implementation": "1.0"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.4.1",
"ext-curl": "*",
"php-http/client-integration-tests": "^3.0",
"phpunit/phpunit": "^8.5.5 || ^9.3.5",
"psr/log": "^1.1"
},
"suggest": {
"ext-curl": "Required for CURL handler support",
"ext-intl": "Required for Internationalized Domain Name (IDN) support",
"psr/log": "Required for using the Log middleware"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "7.3-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Márk Sági-Kazár",
"email": "mark.sagikazar@gmail.com",
"homepage": "https://sagikazarmark.hu"
}
],
"description": "Guzzle is a PHP HTTP client library",
"homepage": "http://guzzlephp.org/",
"keywords": [
"client",
"curl",
"framework",
"http",
"http client",
"psr-18",
"psr-7",
"rest",
"web service"
],
"support": {
"issues": "https://github.com/guzzle/guzzle/issues",
"source": "https://github.com/guzzle/guzzle/tree/7.3.0"
},
"funding": [
{
"url": "https://github.com/GrahamCampbell",
"type": "github"
},
{
"url": "https://github.com/Nyholm",
"type": "github"
},
{
"url": "https://github.com/alexeyshockov",
"type": "github"
},
{
"url": "https://github.com/gmponos",
"type": "github"
}
],
"time": "2021-03-23T11:33:13+00:00"
},
{
"name": "guzzlehttp/promises",
"version": "1.4.1",
"source": {
"type": "git",
"url": "https://github.com/guzzle/promises.git",
"reference": "8e7d04f1f6450fef59366c399cfad4b9383aa30d"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/promises/zipball/8e7d04f1f6450fef59366c399cfad4b9383aa30d",
"reference": "8e7d04f1f6450fef59366c399cfad4b9383aa30d",
"shasum": ""
},
"require": {
"php": ">=5.5"
},
"require-dev": {
"symfony/phpunit-bridge": "^4.4 || ^5.1"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.4-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Promise\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
}
],
"description": "Guzzle promises library",
"keywords": [
"promise"
],
"support": {
"issues": "https://github.com/guzzle/promises/issues",
"source": "https://github.com/guzzle/promises/tree/1.4.1"
},
"time": "2021-03-07T09:25:29+00:00"
},
{
"name": "guzzlehttp/psr7",
"version": "1.8.2",
"source": {
"type": "git",
"url": "https://github.com/guzzle/psr7.git",
"reference": "dc960a912984efb74d0a90222870c72c87f10c91"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/guzzle/psr7/zipball/dc960a912984efb74d0a90222870c72c87f10c91",
"reference": "dc960a912984efb74d0a90222870c72c87f10c91",
"shasum": ""
},
"require": {
"php": ">=5.4.0",
"psr/http-message": "~1.0",
"ralouphie/getallheaders": "^2.0.5 || ^3.0.0"
},
"provide": {
"psr/http-message-implementation": "1.0"
},
"require-dev": {
"ext-zlib": "*",
"phpunit/phpunit": "~4.8.36 || ^5.7.27 || ^6.5.14 || ^7.5.20 || ^8.5.8 || ^9.3.10"
},
"suggest": {
"laminas/laminas-httphandlerrunner": "Emit PSR-7 responses"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.7-dev"
}
},
"autoload": {
"psr-4": {
"GuzzleHttp\\Psr7\\": "src/"
},
"files": [
"src/functions_include.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Michael Dowling",
"email": "mtdowling@gmail.com",
"homepage": "https://github.com/mtdowling"
},
{
"name": "Tobias Schultze",
"homepage": "https://github.com/Tobion"
}
],
"description": "PSR-7 message implementation that also provides common utility methods",
"keywords": [
"http",
"message",
"psr-7",
"request",
"response",
"stream",
"uri",
"url"
],
"support": {
"issues": "https://github.com/guzzle/psr7/issues",
"source": "https://github.com/guzzle/psr7/tree/1.8.2"
},
"time": "2021-04-26T09:17:50+00:00"
},
{
"name": "myclabs/php-enum",
"version": "1.8.0",
"source": {
"type": "git",
"url": "https://github.com/myclabs/php-enum.git",
"reference": "46cf3d8498b095bd33727b13fd5707263af99421"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/myclabs/php-enum/zipball/46cf3d8498b095bd33727b13fd5707263af99421",
"reference": "46cf3d8498b095bd33727b13fd5707263af99421",
"shasum": ""
},
"require": {
"ext-json": "*",
"php": "^7.3 || ^8.0"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"squizlabs/php_codesniffer": "1.*",
"vimeo/psalm": "^4.5.1"
},
"type": "library",
"autoload": {
"psr-4": {
"MyCLabs\\Enum\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP Enum contributors",
"homepage": "https://github.com/myclabs/php-enum/graphs/contributors"
}
],
"description": "PHP Enum implementation",
"homepage": "http://github.com/myclabs/php-enum",
"keywords": [
"enum"
],
"support": {
"issues": "https://github.com/myclabs/php-enum/issues",
"source": "https://github.com/myclabs/php-enum/tree/1.8.0"
},
"funding": [
{
"url": "https://github.com/mnapoli",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/myclabs/php-enum",
"type": "tidelift"
}
],
"time": "2021-02-15T16:11:48+00:00"
},
{ {
"name": "nette/php-generator", "name": "nette/php-generator",
"version": "v3.5.3", "version": "v3.5.3",
@ -161,30 +454,34 @@
}, },
{ {
"name": "paquettg/php-html-parser", "name": "paquettg/php-html-parser",
"version": "2.2.1", "version": "3.1.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/paquettg/php-html-parser.git", "url": "https://github.com/paquettg/php-html-parser.git",
"reference": "668c770fc5724ea3f15b8791435f054835be8d5e" "reference": "4e01a438ad5961cc2d7427eb9798d213c8a12629"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/paquettg/php-html-parser/zipball/668c770fc5724ea3f15b8791435f054835be8d5e", "url": "https://api.github.com/repos/paquettg/php-html-parser/zipball/4e01a438ad5961cc2d7427eb9798d213c8a12629",
"reference": "668c770fc5724ea3f15b8791435f054835be8d5e", "reference": "4e01a438ad5961cc2d7427eb9798d213c8a12629",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"ext-curl": "*", "ext-curl": "*",
"ext-mbstring": "*", "ext-mbstring": "*",
"ext-zlib": "*", "ext-zlib": "*",
"guzzlehttp/guzzle": "^7.0",
"guzzlehttp/psr7": "^1.6",
"myclabs/php-enum": "^1.7",
"paquettg/string-encode": "~1.0.0", "paquettg/string-encode": "~1.0.0",
"php": ">=7.1" "php": ">=7.2",
"php-http/httplug": "^2.1"
}, },
"require-dev": { "require-dev": {
"friendsofphp/php-cs-fixer": "^2.16",
"infection/infection": "^0.13.4", "infection/infection": "^0.13.4",
"mockery/mockery": "^1.2", "mockery/mockery": "^1.2",
"phan/phan": "^2.4", "phan/phan": "^2.4",
"php-coveralls/php-coveralls": "^2.1",
"phpunit/phpunit": "^7.5.1" "phpunit/phpunit": "^7.5.1"
}, },
"type": "library", "type": "library",
@ -213,9 +510,15 @@
], ],
"support": { "support": {
"issues": "https://github.com/paquettg/php-html-parser/issues", "issues": "https://github.com/paquettg/php-html-parser/issues",
"source": "https://github.com/paquettg/php-html-parser/tree/2.2.1" "source": "https://github.com/paquettg/php-html-parser/tree/3.1.1"
}, },
"time": "2020-01-20T12:59:15+00:00" "funding": [
{
"url": "https://tidelift.com/funding/github/packagist/paquettg/php-html-parser",
"type": "tidelift"
}
],
"time": "2020-11-01T20:34:43+00:00"
}, },
{ {
"name": "paquettg/string-encode", "name": "paquettg/string-encode",
@ -266,6 +569,274 @@
"source": "https://github.com/paquettg/string-encoder/tree/1.0.1" "source": "https://github.com/paquettg/string-encoder/tree/1.0.1"
}, },
"time": "2018-12-21T02:25:09+00:00" "time": "2018-12-21T02:25:09+00:00"
},
{
"name": "php-http/httplug",
"version": "2.2.0",
"source": {
"type": "git",
"url": "https://github.com/php-http/httplug.git",
"reference": "191a0a1b41ed026b717421931f8d3bd2514ffbf9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-http/httplug/zipball/191a0a1b41ed026b717421931f8d3bd2514ffbf9",
"reference": "191a0a1b41ed026b717421931f8d3bd2514ffbf9",
"shasum": ""
},
"require": {
"php": "^7.1 || ^8.0",
"php-http/promise": "^1.1",
"psr/http-client": "^1.0",
"psr/http-message": "^1.0"
},
"require-dev": {
"friends-of-phpspec/phpspec-code-coverage": "^4.1",
"phpspec/phpspec": "^5.1 || ^6.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "2.x-dev"
}
},
"autoload": {
"psr-4": {
"Http\\Client\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Eric GELOEN",
"email": "geloen.eric@gmail.com"
},
{
"name": "Márk Sági-Kazár",
"email": "mark.sagikazar@gmail.com",
"homepage": "https://sagikazarmark.hu"
}
],
"description": "HTTPlug, the HTTP client abstraction for PHP",
"homepage": "http://httplug.io",
"keywords": [
"client",
"http"
],
"support": {
"issues": "https://github.com/php-http/httplug/issues",
"source": "https://github.com/php-http/httplug/tree/master"
},
"time": "2020-07-13T15:43:23+00:00"
},
{
"name": "php-http/promise",
"version": "1.1.0",
"source": {
"type": "git",
"url": "https://github.com/php-http/promise.git",
"reference": "4c4c1f9b7289a2ec57cde7f1e9762a5789506f88"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-http/promise/zipball/4c4c1f9b7289a2ec57cde7f1e9762a5789506f88",
"reference": "4c4c1f9b7289a2ec57cde7f1e9762a5789506f88",
"shasum": ""
},
"require": {
"php": "^7.1 || ^8.0"
},
"require-dev": {
"friends-of-phpspec/phpspec-code-coverage": "^4.3.2",
"phpspec/phpspec": "^5.1.2 || ^6.2"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.1-dev"
}
},
"autoload": {
"psr-4": {
"Http\\Promise\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Joel Wurtz",
"email": "joel.wurtz@gmail.com"
},
{
"name": "Márk Sági-Kazár",
"email": "mark.sagikazar@gmail.com"
}
],
"description": "Promise used for asynchronous HTTP requests",
"homepage": "http://httplug.io",
"keywords": [
"promise"
],
"support": {
"issues": "https://github.com/php-http/promise/issues",
"source": "https://github.com/php-http/promise/tree/1.1.0"
},
"time": "2020-07-07T09:29:14+00:00"
},
{
"name": "psr/http-client",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-client.git",
"reference": "2dfb5f6c5eff0e91e20e913f8c5452ed95b86621"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-client/zipball/2dfb5f6c5eff0e91e20e913f8c5452ed95b86621",
"reference": "2dfb5f6c5eff0e91e20e913f8c5452ed95b86621",
"shasum": ""
},
"require": {
"php": "^7.0 || ^8.0",
"psr/http-message": "^1.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Client\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP clients",
"homepage": "https://github.com/php-fig/http-client",
"keywords": [
"http",
"http-client",
"psr",
"psr-18"
],
"support": {
"source": "https://github.com/php-fig/http-client/tree/master"
},
"time": "2020-06-29T06:28:15+00:00"
},
{
"name": "psr/http-message",
"version": "1.0.1",
"source": {
"type": "git",
"url": "https://github.com/php-fig/http-message.git",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363",
"reference": "f6561bf28d520154e4b0ec72be95418abe6d9363",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"autoload": {
"psr-4": {
"Psr\\Http\\Message\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "PHP-FIG",
"homepage": "http://www.php-fig.org/"
}
],
"description": "Common interface for HTTP messages",
"homepage": "https://github.com/php-fig/http-message",
"keywords": [
"http",
"http-message",
"psr",
"psr-7",
"request",
"response"
],
"support": {
"source": "https://github.com/php-fig/http-message/tree/master"
},
"time": "2016-08-06T14:39:51+00:00"
},
{
"name": "ralouphie/getallheaders",
"version": "3.0.3",
"source": {
"type": "git",
"url": "https://github.com/ralouphie/getallheaders.git",
"reference": "120b605dfeb996808c31b6477290a714d356e822"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/ralouphie/getallheaders/zipball/120b605dfeb996808c31b6477290a714d356e822",
"reference": "120b605dfeb996808c31b6477290a714d356e822",
"shasum": ""
},
"require": {
"php": ">=5.6"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.1",
"phpunit/phpunit": "^5 || ^6.5"
},
"type": "library",
"autoload": {
"files": [
"src/getallheaders.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Ralph Khattar",
"email": "ralph.khattar@gmail.com"
}
],
"description": "A polyfill for getallheaders.",
"support": {
"issues": "https://github.com/ralouphie/getallheaders/issues",
"source": "https://github.com/ralouphie/getallheaders/tree/develop"
},
"time": "2019-03-08T08:55:37+00:00"
} }
], ],
"packages-dev": [], "packages-dev": [],

View File

@ -5,13 +5,14 @@ namespace TgScraper;
use Exception; use Exception;
use JsonException; use JsonException;
use PHPHtmlParser\Dom; use PHPHtmlParser\Dom;
use PHPHtmlParser\Exceptions\{ChildNotFoundException, use PHPHtmlParser\Exceptions\ChildNotFoundException;
CircularException, use PHPHtmlParser\Exceptions\CircularException;
CurlException, use PHPHtmlParser\Exceptions\ContentLengthException;
NotLoadedException, use PHPHtmlParser\Exceptions\LogicalException;
ParentNotFoundException, use PHPHtmlParser\Exceptions\NotLoadedException;
StrictException use PHPHtmlParser\Exceptions\ParentNotFoundException;
}; use PHPHtmlParser\Exceptions\StrictException;
use Psr\Http\Client\ClientExceptionInterface;
class Generator class Generator
{ {
@ -32,6 +33,7 @@ class Generator
* @param string $namespace * @param string $namespace
* @param string|null $scheme * @param string|null $scheme
* @return bool * @return bool
* @throws ClientExceptionInterface
*/ */
public function toStubs(string $directory = '', string $namespace = '', string $scheme = null): bool public function toStubs(string $directory = '', string $namespace = '', string $scheme = null): bool
{ {
@ -46,11 +48,11 @@ class Generator
if (!empty($scheme)) { if (!empty($scheme)) {
try { try {
$data = json_decode($scheme, true, flags: JSON_THROW_ON_ERROR); $data = json_decode($scheme, true, flags: JSON_THROW_ON_ERROR);
} catch (JsonException $e) { } /** @noinspection PhpRedundantCatchClauseInspection */ catch (JsonException) {
$data = null; $data = null;
} }
} }
$data = $data ?? self::extractScheme(); $data = $data ?? $this->extractScheme();
$creator = new StubCreator($data, $namespace); $creator = new StubCreator($data, $namespace);
$code = $creator->generateCode(); $code = $creator->generateCode();
foreach ($code['types'] as $className => $type) { foreach ($code['types'] as $className => $type) {
@ -59,7 +61,6 @@ class Generator
} }
file_put_contents($directory . '/API.php', $code['api']); file_put_contents($directory . '/API.php', $code['api']);
} catch (Exception $e) { } catch (Exception $e) {
var_dump($e);
echo $e->getMessage() . PHP_EOL; echo $e->getMessage() . PHP_EOL;
return false; return false;
} }
@ -74,7 +75,7 @@ class Generator
private static function getTargetDirectory(string $path): string private static function getTargetDirectory(string $path): string
{ {
$path = realpath($path); $path = realpath($path);
if (false == $path) { if (false === $path) {
if (!mkdir($path)) { if (!mkdir($path)) {
$path = __DIR__ . '/../generated'; $path = __DIR__ . '/../generated';
if (!file_exists($path)) { if (!file_exists($path)) {
@ -82,7 +83,7 @@ class Generator
} }
} }
} }
if (realpath($path) == false) { if (realpath($path) === false) {
throw new Exception('Could not create target directory'); throw new Exception('Could not create target directory');
} }
return $path; return $path;
@ -92,10 +93,12 @@ class Generator
* @return array * @return array
* @throws ChildNotFoundException * @throws ChildNotFoundException
* @throws CircularException * @throws CircularException
* @throws CurlException * @throws ContentLengthException
* @throws LogicalException
* @throws NotLoadedException * @throws NotLoadedException
* @throws ParentNotFoundException * @throws ParentNotFoundException
* @throws StrictException * @throws StrictException
* @throws ClientExceptionInterface
*/ */
private function extractScheme(): array private function extractScheme(): array
{ {
@ -103,7 +106,7 @@ class Generator
$dom->loadFromURL($this->url); $dom->loadFromURL($this->url);
$elements = $dom->find('h4'); $elements = $dom->find('h4');
$data = []; $data = [];
/* @var Dom\AbstractNode $element */ /* @var Dom\Node\AbstractNode $element */
foreach ($elements as $element) { foreach ($elements as $element) {
if (!str_contains($name = $element->text, ' ')) { if (!str_contains($name = $element->text, ' ')) {
$isMethod = self::isMethod($name); $isMethod = self::isMethod($name);
@ -114,7 +117,7 @@ class Generator
while (true) { while (true) {
try { try {
$element = $element->nextSibling(); $element = $element->nextSibling();
} catch (ChildNotFoundException $e) { } catch (ChildNotFoundException) {
break; break;
} }
$tag = $element->tag->name() ?? null; $tag = $element->tag->name() ?? null;
@ -129,7 +132,7 @@ class Generator
break; break;
} }
} }
/* @var Dom\AbstractNode $element */ /* @var Dom\Node\AbstractNode $element */
$data[$path][] = self::generateElement( $data[$path][] = self::generateElement(
$name, $name,
trim($description), trim($description),
@ -149,19 +152,20 @@ class Generator
/** /**
* @param string $name * @param string $name
* @param string $description * @param string $description
* @param Dom\Collection|null $unparsedFields * @param Dom\Node\Collection|null $unparsedFields
* @param bool $isMethod * @param bool $isMethod
* @return array * @return array
* @throws ChildNotFoundException * @throws ChildNotFoundException
* @throws CircularException * @throws CircularException
* @throws CurlException * @throws ContentLengthException
* @throws LogicalException
* @throws NotLoadedException * @throws NotLoadedException
* @throws StrictException * @throws StrictException
*/ */
private static function generateElement( private static function generateElement(
string $name, string $name,
string $description, string $description,
?Dom\Collection $unparsedFields, ?Dom\Node\Collection $unparsedFields,
bool $isMethod bool $isMethod
): array { ): array {
$fields = self::parseFields($unparsedFields, $isMethod); $fields = self::parseFields($unparsedFields, $isMethod);
@ -185,13 +189,13 @@ class Generator
} }
/** /**
* @param Dom\Collection|null $fields * @param Dom\Node\Collection|null $fields
* @param bool $isMethod * @param bool $isMethod
* @return array * @return array
* @throws ChildNotFoundException * @throws ChildNotFoundException
* @throws NotLoadedException * @throws NotLoadedException
*/ */
private static function parseFields(?Dom\Collection $fields, bool $isMethod): array private static function parseFields(?Dom\Node\Collection $fields, bool $isMethod): array
{ {
$parsedFields = []; $parsedFields = [];
$fields = $fields ?? []; $fields = $fields ?? [];
@ -264,9 +268,10 @@ class Generator
* @return array * @return array
* @throws ChildNotFoundException * @throws ChildNotFoundException
* @throws CircularException * @throws CircularException
* @throws CurlException
* @throws NotLoadedException * @throws NotLoadedException
* @throws StrictException * @throws StrictException
* @throws ContentLengthException
* @throws LogicalException
* @noinspection PhpUndefinedFieldInspection * @noinspection PhpUndefinedFieldInspection
*/ */
private static function parseReturnTypes(string $description): array private static function parseReturnTypes(string $description): array
@ -281,7 +286,7 @@ class Generator
); );
foreach ($phrases as $phrase) { foreach ($phrases as $phrase) {
$dom = new Dom; $dom = new Dom;
$dom->load($phrase); $dom->loadStr($phrase);
$a = $dom->find('a'); $a = $dom->find('a');
$em = $dom->find('em'); $em = $dom->find('em');
foreach ($a as $element) { foreach ($a as $element) {
@ -313,7 +318,9 @@ class Generator
* @return string * @return string
* @throws ChildNotFoundException * @throws ChildNotFoundException
* @throws CircularException * @throws CircularException
* @throws CurlException * @throws ClientExceptionInterface
* @throws ContentLengthException
* @throws LogicalException
* @throws NotLoadedException * @throws NotLoadedException
* @throws ParentNotFoundException * @throws ParentNotFoundException
* @throws StrictException * @throws StrictException

View File

@ -6,6 +6,8 @@ namespace TgScraper;
use InvalidArgumentException; use InvalidArgumentException;
use JetBrains\PhpStorm\ArrayShape;
use JetBrains\PhpStorm\Pure;
use Nette\PhpGenerator\Helpers; use Nette\PhpGenerator\Helpers;
use Nette\PhpGenerator\PhpFile; use Nette\PhpGenerator\PhpFile;
use Nette\PhpGenerator\PhpNamespace; use Nette\PhpGenerator\PhpNamespace;
@ -51,6 +53,7 @@ class StubCreator
* @param PhpNamespace $phpNamespace * @param PhpNamespace $phpNamespace
* @return array * @return array
*/ */
#[Pure] #[ArrayShape(['types' => "string", 'comments' => "string"])]
private function parseFieldTypes(array $fieldTypes, PhpNamespace $phpNamespace): array private function parseFieldTypes(array $fieldTypes, PhpNamespace $phpNamespace): array
{ {
$types = []; $types = [];
@ -78,6 +81,7 @@ class StubCreator
* @param PhpNamespace $phpNamespace * @param PhpNamespace $phpNamespace
* @return array * @return array
*/ */
#[ArrayShape(['types' => "string", 'comments' => "string"])]
private function parseApiFieldTypes(array $apiTypes, PhpNamespace $phpNamespace): array private function parseApiFieldTypes(array $apiTypes, PhpNamespace $phpNamespace): array
{ {
$types = []; $types = [];
@ -105,6 +109,7 @@ class StubCreator
* @param string $namespace * @param string $namespace
* @return PhpFile[] * @return PhpFile[]
*/ */
#[ArrayShape(['Response' => "\Nette\PhpGenerator\PhpFile"])]
private function generateDefaultTypes(string $namespace): array private function generateDefaultTypes(string $namespace): array
{ {
$file = new PhpFile; $file = new PhpFile;
@ -219,6 +224,7 @@ class StubCreator
/** /**
* @return array * @return array
*/ */
#[ArrayShape(['types' => "\Nette\PhpGenerator\PhpFile[]", 'api' => "string"])]
public function generateCode(): array public function generateCode(): array
{ {
return [ return [