From 4bf53de7cdaaf743b2013d5dda007e22dbd9a09e Mon Sep 17 00:00:00 2001 From: Sys <28715512+sys-001@users.noreply.github.com> Date: Wed, 23 Jun 2021 17:03:12 +0200 Subject: [PATCH] Added YAML and (partial) Docker support --- .dockerignore | 5 + .gitignore | 80 +------------- Dockerfile | 11 ++ ReturnTypeExtractor.php | 1 - bin/tgscraper | 29 +++++- composer.json | 3 +- composer.lock | 223 +++++++++++++++++++++++++++++++++++++++- src/Generator.php | 17 +++ 8 files changed, 284 insertions(+), 85 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile delete mode 100644 ReturnTypeExtractor.php diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..136dc06 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +composer.phar +/vendor/ +.idea/ +LICENSE +README.md diff --git a/.gitignore b/.gitignore index e2cdcb7..8733f8b 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..bfabe30 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM composer:latest AS tgscraper + +MAINTAINER Sys + +WORKDIR /app +COPY . . +RUN composer install +WORKDIR /artifacts +VOLUME /artifacts + +ENTRYPOINT ["php", "/app/bin/tgscraper"] \ No newline at end of file diff --git a/ReturnTypeExtractor.php b/ReturnTypeExtractor.php deleted file mode 100644 index b3d9bbc..0000000 --- a/ReturnTypeExtractor.php +++ /dev/null @@ -1 +0,0 @@ - [-u ] [-n ] [-s ]' . PHP_EOL . PHP_EOL; echo 'Options:' . PHP_EOL; echo ' -n namespace to use (for PHP stubs only)' . PHP_EOL; - echo ' -o output file/directory (the directory must exist) for JSON schema or PHP stubs' . PHP_EOL; + echo ' -o output file/directory (the directory must exist) for JSON/YAML schema or PHP stubs' . PHP_EOL; echo ' -s path to custom scheme to use (for PHP stubs only)' . PHP_EOL; echo ' -u 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 { diff --git a/composer.json b/composer.json index c3bb3af..3287c18 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/composer.lock b/composer.lock index b3c0f28..71d0153 100644 --- a/composer.lock +++ b/composer.lock @@ -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": [], diff --git a/src/Generator.php b/src/Generator.php index 3cef54b..8827132 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -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); + } + }