Added YAML and (partial) Docker support

This commit is contained in:
Sys 2021-06-23 17:03:12 +02:00
parent e13e886fd0
commit 4bf53de7cd
No known key found for this signature in database
GPG Key ID: 3CD2C29F8AB39BFD
8 changed files with 284 additions and 85 deletions

5
.dockerignore Normal file
View File

@ -0,0 +1,5 @@
composer.phar
/vendor/
.idea/
LICENSE
README.md

80
.gitignore vendored
View File

@ -1,81 +1,3 @@
### Composer template
composer.phar
/vendor/
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
.idea/

11
Dockerfile Normal file
View File

@ -0,0 +1,11 @@
FROM composer:latest AS tgscraper
MAINTAINER Sys <sys@sys001.ml>
WORKDIR /app
COPY . .
RUN composer install
WORKDIR /artifacts
VOLUME /artifacts
ENTRYPOINT ["php", "/app/bin/tgscraper"]

View File

@ -1 +0,0 @@
<?php

View File

@ -1,4 +1,4 @@
#!/usr/bin/env php8.0
#!/usr/bin/env php
<?php
require_once __DIR__ . '/../vendor/autoload.php';
@ -12,10 +12,10 @@ if (array_key_exists('h', $args)) {
echo 'Usage: tgscraper -o <output> [-u <url>] [-n <namespace>] [-s <scheme>]' . PHP_EOL . PHP_EOL;
echo 'Options:' . PHP_EOL;
echo ' -n <namespace> namespace to use (for PHP stubs only)' . PHP_EOL;
echo ' -o <output> output file/directory (the directory must exist) for JSON schema or PHP stubs' . PHP_EOL;
echo ' -o <output> output file/directory (the directory must exist) for JSON/YAML schema or PHP stubs' . PHP_EOL;
echo ' -s <scheme> path to custom scheme to use (for PHP stubs only)' . PHP_EOL;
echo ' -u <url> URL to use for fetching bot API data' . PHP_EOL . PHP_EOL;
echo 'Note: based on the "-o" value, the script will automatically detect whether to output a JSON schema or the PHP stubs.' . PHP_EOL;
echo 'Note: based on the "-o" value, the script will automatically detect whether to output a JSON/YAML schema or the PHP stubs.' . PHP_EOL;
exit;
}
@ -80,6 +80,29 @@ if (is_dir($output)) {
echo sprintf('> Using "%s" as URL.%s', $url, PHP_EOL);
touch($output);
$extension = pathinfo($output, PATHINFO_EXTENSION);
if (in_array($extension, ['yml', 'yaml'])) {
echo sprintf('> Outputting YAML schema to %s.%s', realpath($output), PHP_EOL);
try {
$data = $generator->toYaml();
} catch (Throwable $e) {
echo '> ERROR: Unable to generate scheme:' . $e->getMessage() . PHP_EOL;
exit(1);
}
$error = file_put_contents($output, $data) === false;
if ($error) {
echo '> ERROR: unable to write to file.' . PHP_EOL;
}
exit($error);
}
echo sprintf('> Outputting JSON schema to %s.%s', realpath($output), PHP_EOL);
try {

View File

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

223
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "e3461482771ef0bfc9ff36941c968aa2",
"content-hash": "ce91774666a1ea15b98376aed25cee10",
"packages": [
{
"name": "guzzlehttp/guzzle",
@ -837,6 +837,227 @@
"source": "https://github.com/ralouphie/getallheaders/tree/develop"
},
"time": "2019-03-08T08:55:37+00:00"
},
{
"name": "symfony/deprecation-contracts",
"version": "v2.4.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/deprecation-contracts.git",
"reference": "5f38c8804a9e97d23e0c8d63341088cd8a22d627"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/5f38c8804a9e97d23e0c8d63341088cd8a22d627",
"reference": "5f38c8804a9e97d23e0c8d63341088cd8a22d627",
"shasum": ""
},
"require": {
"php": ">=7.1"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "2.4-dev"
},
"thanks": {
"name": "symfony/contracts",
"url": "https://github.com/symfony/contracts"
}
},
"autoload": {
"files": [
"function.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "A generic function and convention to trigger deprecation notices",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/deprecation-contracts/tree/v2.4.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2021-03-23T23:28:01+00:00"
},
{
"name": "symfony/polyfill-ctype",
"version": "v1.23.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-ctype.git",
"reference": "46cd95797e9df938fdd2b03693b5fca5e64b01ce"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/46cd95797e9df938fdd2b03693b5fca5e64b01ce",
"reference": "46cd95797e9df938fdd2b03693b5fca5e64b01ce",
"shasum": ""
},
"require": {
"php": ">=7.1"
},
"suggest": {
"ext-ctype": "For best performance"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "1.23-dev"
},
"thanks": {
"name": "symfony/polyfill",
"url": "https://github.com/symfony/polyfill"
}
},
"autoload": {
"psr-4": {
"Symfony\\Polyfill\\Ctype\\": ""
},
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Gert de Pagter",
"email": "BackEndTea@gmail.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for ctype functions",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"ctype",
"polyfill",
"portable"
],
"support": {
"source": "https://github.com/symfony/polyfill-ctype/tree/v1.23.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2021-02-19T12:13:01+00:00"
},
{
"name": "symfony/yaml",
"version": "v5.3.2",
"source": {
"type": "git",
"url": "https://github.com/symfony/yaml.git",
"reference": "71719ab2409401711d619765aa255f9d352a59b2"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/yaml/zipball/71719ab2409401711d619765aa255f9d352a59b2",
"reference": "71719ab2409401711d619765aa255f9d352a59b2",
"shasum": ""
},
"require": {
"php": ">=7.2.5",
"symfony/deprecation-contracts": "^2.1",
"symfony/polyfill-ctype": "~1.8"
},
"conflict": {
"symfony/console": "<4.4"
},
"require-dev": {
"symfony/console": "^4.4|^5.0"
},
"suggest": {
"symfony/console": "For validating YAML files using the lint command"
},
"bin": [
"Resources/bin/yaml-lint"
],
"type": "library",
"autoload": {
"psr-4": {
"Symfony\\Component\\Yaml\\": ""
},
"exclude-from-classmap": [
"/Tests/"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Loads and dumps YAML files",
"homepage": "https://symfony.com",
"support": {
"source": "https://github.com/symfony/yaml/tree/v5.3.2"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2021-06-06T09:51:56+00:00"
}
],
"packages-dev": [],

View File

@ -13,6 +13,7 @@ use PHPHtmlParser\Exceptions\NotLoadedException;
use PHPHtmlParser\Exceptions\ParentNotFoundException;
use PHPHtmlParser\Exceptions\StrictException;
use Psr\Http\Client\ClientExceptionInterface;
use Symfony\Component\Yaml\Yaml;
class Generator
{
@ -331,4 +332,20 @@ class Generator
return json_encode($scheme, $options);
}
/**
* @throws ChildNotFoundException
* @throws CircularException
* @throws ParentNotFoundException
* @throws StrictException
* @throws ClientExceptionInterface
* @throws NotLoadedException
* @throws ContentLengthException
* @throws LogicalException
*/
public function toYaml(int $inline = 6, int $indent = 4, int $flags = 0): string
{
$scheme = $this->extractScheme();
return Yaml::dump($scheme, $inline, $indent, $flags);
}
}