From 3fb34ad18e7b3adab1f31b3b47c7cc914e8edf02 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 15 May 2020 22:57:38 +0100 Subject: [PATCH] remove particle/validator dependency, require respect/validation, close #3228 this fixes a few crashes that could appear from invalid plugin_list.yml setups, which was the reason #3228 was opened to begin with. Respect also has nicer error messages, as long as you don't use the static API :) --- composer.json | 4 +- composer.lock | 253 +++++++++++++++++++++++++--------- src/plugin/PluginGraylist.php | 32 +++-- 3 files changed, 209 insertions(+), 80 deletions(-) diff --git a/composer.json b/composer.json index 06b474b94..5b77fda8c 100644 --- a/composer.json +++ b/composer.json @@ -45,9 +45,9 @@ "pocketmine/uuid": "^0.1.0", "pocketmine/color": "^0.1.0", "adhocore/json-comment": "^0.1.0", - "particle/validator": "^2.3", "netresearch/jsonmapper": "^2.0", - "ocramius/package-versions": "^1.5" + "ocramius/package-versions": "^1.5", + "respect/validation": "^2.0" }, "require-dev": { "phpstan/phpstan": "^0.12.25", diff --git a/composer.lock b/composer.lock index 4009aa716..9c0ccda23 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": "4bd35ad045d13f81b88bef943137c79f", + "content-hash": "873133d73021dccbdf46b9ffac2e07a6", "packages": [ { "name": "adhocore/json-comment", @@ -288,68 +288,6 @@ "description": "Composer plugin that provides efficient querying for installed package versions (no runtime IO)", "time": "2019-07-17T15:49:50+00:00" }, - { - "name": "particle/validator", - "version": "v2.3.4", - "source": { - "type": "git", - "url": "https://github.com/particle-php/Validator.git", - "reference": "657c7543e51938dd9d114750e49d695129527a7a" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/particle-php/Validator/zipball/657c7543e51938dd9d114750e49d695129527a7a", - "reference": "657c7543e51938dd9d114750e49d695129527a7a", - "shasum": "" - }, - "require": { - "php": ">=5.4" - }, - "require-dev": { - "byrokrat/checkdigit": "^1.0", - "giggsey/libphonenumber-for-php": "^7.2", - "phpunit/phpunit": "~4.0", - "squizlabs/php_codesniffer": "2.*" - }, - "suggest": { - "byrokrat/checkdigit": "If you want to use CreditCard validation rule, this library must be installed.", - "giggsey/libphonenumber-for-php": "If you want to use Phone validation rule, this library must be installed." - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "2.0-dev" - } - }, - "autoload": { - "psr-4": { - "Particle\\Validator\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Berry Langerak", - "email": "berry@berryllium.nl", - "role": "Developer" - }, - { - "name": "Rick van der Staaij", - "homepage": "http://rickvanderstaaij.nl", - "role": "Developer" - } - ], - "description": "Flexible and highly usable validation library with no dependencies.", - "homepage": "http://github.com/particle-php/validator", - "keywords": [ - "validation", - "validator" - ], - "time": "2019-01-07T13:39:13+00:00" - }, { "name": "pocketmine/binaryutils", "version": "dev-master", @@ -820,6 +758,195 @@ ], "description": "Basic UUID implementation used by PocketMine-MP and related projects", "time": "2020-05-10T12:38:41+00:00" + }, + { + "name": "respect/stringifier", + "version": "0.2.0", + "source": { + "type": "git", + "url": "https://github.com/Respect/Stringifier.git", + "reference": "e55af3c8aeaeaa2abb5fa47a58a8e9688cc23b59" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Respect/Stringifier/zipball/e55af3c8aeaeaa2abb5fa47a58a8e9688cc23b59", + "reference": "e55af3c8aeaeaa2abb5fa47a58a8e9688cc23b59", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^2.8", + "malukenho/docheader": "^0.1.7", + "phpunit/phpunit": "^6.4" + }, + "type": "library", + "autoload": { + "psr-4": { + "Respect\\Stringifier\\": "src/" + }, + "files": [ + "src/stringify.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Respect/Stringifier Contributors", + "homepage": "https://github.com/Respect/Stringifier/graphs/contributors" + } + ], + "description": "Converts any value to a string", + "homepage": "http://respect.github.io/Stringifier/", + "keywords": [ + "respect", + "stringifier", + "stringify" + ], + "time": "2017-12-29T19:39:25+00:00" + }, + { + "name": "respect/validation", + "version": "2.0.3", + "source": { + "type": "git", + "url": "https://github.com/Respect/Validation.git", + "reference": "3463343b14a7fa5ba931f03b5dcb8efcbc0ddf9c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Respect/Validation/zipball/3463343b14a7fa5ba931f03b5dcb8efcbc0ddf9c", + "reference": "3463343b14a7fa5ba931f03b5dcb8efcbc0ddf9c", + "shasum": "" + }, + "require": { + "php": ">=7.2", + "respect/stringifier": "^0.2.0", + "symfony/polyfill-mbstring": "^1.2" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^0.5.0", + "egulias/email-validator": "^2.1", + "malukenho/docheader": "^0.1", + "mikey179/vfsstream": "^1.6", + "phpstan/phpstan": "^0.11", + "phpstan/phpstan-deprecation-rules": "^0.11.0", + "phpstan/phpstan-phpunit": "^0.11.0", + "phpunit/phpunit": "^7.5", + "respect/coding-standard": "^1.0", + "squizlabs/php_codesniffer": "^3.5", + "symfony/validator": "^3.0||^4.0", + "zendframework/zend-validator": "^2.1" + }, + "suggest": { + "egulias/email-validator": "Strict (RFC compliant) email validation", + "ext-bcmath": "Arbitrary Precision Mathematics", + "ext-fileinfo": "File Information", + "ext-mbstring": "Multibyte String Functions", + "symfony/validator": "Use Symfony validator through Respect\\Validation", + "zendframework/zend-validator": "Use Zend Framework validator through Respect\\Validation" + }, + "type": "library", + "autoload": { + "psr-4": { + "Respect\\Validation\\": "library/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Respect/Validation Contributors", + "homepage": "https://github.com/Respect/Validation/graphs/contributors" + } + ], + "description": "The most awesome validation engine ever created for PHP", + "homepage": "http://respect.github.io/Validation/", + "keywords": [ + "respect", + "validation", + "validator" + ], + "time": "2020-05-13T16:41:55+00:00" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.17.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "fa79b11539418b02fc5e1897267673ba2c19419c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/fa79b11539418b02fc5e1897267673ba2c19419c", + "reference": "fa79b11539418b02fc5e1897267673ba2c19419c", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.17-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + }, + "files": [ + "bootstrap.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": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "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": "2020-05-12T16:47:27+00:00" } ], "packages-dev": [ diff --git a/src/plugin/PluginGraylist.php b/src/plugin/PluginGraylist.php index 8436ffc7f..72cbbd4a2 100644 --- a/src/plugin/PluginGraylist.php +++ b/src/plugin/PluginGraylist.php @@ -23,11 +23,15 @@ declare(strict_types=1); namespace pocketmine\plugin; -use Particle\Validator\Validator; -use function array_filter; +use Respect\Validation\Exceptions\NestedValidationException; +use Respect\Validation\Rules\AllOf; +use Respect\Validation\Rules\ArrayType; +use Respect\Validation\Rules\Each; +use Respect\Validation\Rules\In; +use Respect\Validation\Rules\Key; +use Respect\Validation\Rules\StringType; +use Respect\Validation\Validator; use function array_flip; -use function count; -use function implode; class PluginGraylist{ @@ -66,17 +70,15 @@ class PluginGraylist{ * @param mixed[] $array */ public static function fromArray(array $array) : PluginGraylist{ - $v = new Validator(); - $v->required("mode")->inArray(['whitelist', 'blacklist'], true); - $v->required("plugins")->isArray()->allowEmpty(true)->callback(function(array $elements) : bool{ return count(array_filter($elements, '\is_string')) === count($elements); }); - - $result = $v->validate($array); - if($result->isNotValid()){ - $messages = []; - foreach($result->getFailures() as $f){ - $messages[] = $f->format(); - } - throw new \InvalidArgumentException("Invalid data: " . implode(", ", $messages)); + $validator = new Validator( + new Key("mode", new In(['whitelist', 'blacklist'], true), false), + new Key("plugins", new AllOf(new ArrayType(), new Each(new StringType())), false) + ); + $validator->setName('plugin_list.yml'); + try{ + $validator->assert($array); + }catch(NestedValidationException $e){ + throw new \InvalidArgumentException($e->getFullMessage(), 0, $e); } return new PluginGraylist($array["plugins"], $array["mode"] === 'whitelist'); }