From 5ae997c46411153d609df98a32552468933fe06f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E5=94=81?= <52o@qq52o.cn> Date: Wed, 25 Nov 2020 18:10:31 +0800 Subject: [PATCH 01/13] Fixed namespace for documents. (#2869) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix namespace * Update CHANGELOG-2.0.md Co-authored-by: 李铭昕 <715557344@qq.com> --- CHANGELOG-2.0.md | 1 + docs/zh-cn/db/quick-start.md | 1 + 2 files changed, 2 insertions(+) diff --git a/CHANGELOG-2.0.md b/CHANGELOG-2.0.md index 4921c0a36..ede0a6773 100644 --- a/CHANGELOG-2.0.md +++ b/CHANGELOG-2.0.md @@ -3,6 +3,7 @@ ## Added - [#2857](https://github.com/hyperf/hyperf/pull/2857) Support Consul ACL Token for Service Governance. +- [#2864](https://github.com/hyperf/hyperf/pull/2864) Added encryption component. ## Changed diff --git a/docs/zh-cn/db/quick-start.md b/docs/zh-cn/db/quick-start.md index dc3b994e8..0eb72d967 100644 --- a/docs/zh-cn/db/quick-start.md +++ b/docs/zh-cn/db/quick-start.md @@ -351,6 +351,7 @@ try{ Date: Wed, 25 Nov 2020 17:12:55 +0800 Subject: [PATCH 02/13] feat: the publish option of ConfigProvider allows publish directory --- src/devtool/src/VendorPublishCommand.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/devtool/src/VendorPublishCommand.php b/src/devtool/src/VendorPublishCommand.php index cbd8a7433..5067b91f1 100644 --- a/src/devtool/src/VendorPublishCommand.php +++ b/src/devtool/src/VendorPublishCommand.php @@ -118,10 +118,32 @@ class VendorPublishCommand extends SymfonyCommand if (! file_exists(dirname($destination))) { mkdir(dirname($destination), 0755, true); } - copy($source, $destination); + is_dir($source) ? $this->copyDirectory($source, $destination) : copy($source, $destination); $this->output->writeln(sprintf('[%s] publishes [%s] successfully.', $package, $id)); } return 0; } + + protected function copyDirectory($source, $destination) + { + $dir_handle = opendir($source); + @mkdir($destination, 0755, true); + while (($file = readdir($dir_handle)) !== false) { + if ($file === '.' || $file === '..') { + continue; + } + + $from = $source . DIRECTORY_SEPARATOR . $file; + $to = $destination . DIRECTORY_SEPARATOR . $file; + + if (is_dir($from)) { + $this->copyDirectory($from, $to); + continue; + } + + copy($from, $to); + } + closedir($dir_handle); + } } From 0016daff93690ed37690b9c880316d8a48e4bd93 Mon Sep 17 00:00:00 2001 From: nfangxu Date: Wed, 25 Nov 2020 18:03:33 +0800 Subject: [PATCH 03/13] perf: Use \Hyperf\Utils\Filesystem\Filesystem --- src/devtool/src/VendorPublishCommand.php | 44 +++++++++--------------- 1 file changed, 17 insertions(+), 27 deletions(-) diff --git a/src/devtool/src/VendorPublishCommand.php b/src/devtool/src/VendorPublishCommand.php index 5067b91f1..84b2f01e0 100644 --- a/src/devtool/src/VendorPublishCommand.php +++ b/src/devtool/src/VendorPublishCommand.php @@ -14,6 +14,7 @@ namespace Hyperf\Devtool; use Hyperf\Command\Annotation\Command; use Hyperf\Utils\Arr; use Hyperf\Utils\Composer; +use Hyperf\Utils\Filesystem\Filesystem; use Symfony\Component\Console\Command\Command as SymfonyCommand; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -35,9 +36,15 @@ class VendorPublishCommand extends SymfonyCommand */ protected $force = false; - public function __construct() + /** + * @var Filesystem + */ + protected $filesystem; + + public function __construct(Filesystem $filesystem) { parent::__construct('vendor:publish'); + $this->filesystem = $filesystem; } protected function configure() @@ -110,40 +117,23 @@ class VendorPublishCommand extends SymfonyCommand $source = $item['source']; $destination = $item['destination']; - if (! $this->force && file_exists($destination)) { + if (! $this->force && $this->filesystem->exists($destination)) { $this->output->writeln(sprintf('[%s] already exists.', $destination)); continue; } - if (! file_exists(dirname($destination))) { - mkdir(dirname($destination), 0755, true); + if (! $this->filesystem->exists($dirname = dirname($destination))) { + $this->filesystem->makeDirectory($dirname, 0755, true); + } + + if ($this->filesystem->isDirectory($source)) { + $this->filesystem->copyDirectory($source, $destination); + } else { + $this->filesystem->copy($source, $destination); } - is_dir($source) ? $this->copyDirectory($source, $destination) : copy($source, $destination); $this->output->writeln(sprintf('[%s] publishes [%s] successfully.', $package, $id)); } return 0; } - - protected function copyDirectory($source, $destination) - { - $dir_handle = opendir($source); - @mkdir($destination, 0755, true); - while (($file = readdir($dir_handle)) !== false) { - if ($file === '.' || $file === '..') { - continue; - } - - $from = $source . DIRECTORY_SEPARATOR . $file; - $to = $destination . DIRECTORY_SEPARATOR . $file; - - if (is_dir($from)) { - $this->copyDirectory($from, $to); - continue; - } - - copy($from, $to); - } - closedir($dir_handle); - } } From c580bcf3394934a214b6cb6dce4a3db128227bb1 Mon Sep 17 00:00:00 2001 From: nfangxu Date: Wed, 25 Nov 2020 18:06:41 +0800 Subject: [PATCH 04/13] perf: Use the constant 'DIRECTORY_SEPARATOR' instead of '/' --- src/utils/src/Filesystem/Filesystem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/src/Filesystem/Filesystem.php b/src/utils/src/Filesystem/Filesystem.php index f30d35934..17fcf5f12 100644 --- a/src/utils/src/Filesystem/Filesystem.php +++ b/src/utils/src/Filesystem/Filesystem.php @@ -435,7 +435,7 @@ class Filesystem // As we spin through items, we will check to see if the current file is actually // a directory or a file. When it is actually a directory we will need to call // back into this function recursively to keep copying these nested folders. - $target = $destination . '/' . $item->getBasename(); + $target = $destination . DIRECTORY_SEPARATOR . $item->getBasename(); if ($item->isDir()) { $path = $item->getPathname(); From 62d313f139e5c0bdea4b6f3a0b518a7a2857909e Mon Sep 17 00:00:00 2001 From: ijackwu Date: Wed, 25 Nov 2020 19:22:40 +0800 Subject: [PATCH 05/13] Optimized code for watcher (#2785) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: ijackwu Co-authored-by: 李铭昕 <715557344@qq.com> --- CHANGELOG-2.0.md | 1 + src/watcher/src/Driver/FindDriver.php | 4 ++-- src/watcher/src/Driver/FswatchDriver.php | 4 +++- src/watcher/src/Watcher.php | 3 +++ 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG-2.0.md b/CHANGELOG-2.0.md index ede0a6773..bcfee3adc 100644 --- a/CHANGELOG-2.0.md +++ b/CHANGELOG-2.0.md @@ -11,6 +11,7 @@ ## Optimized +- [#2785](https://github.com/hyperf/hyperf/pull/2785) Optimized code for watcher. - [#2861](https://github.com/hyperf/hyperf/pull/2861) Optimized guzzle coroutine handler which throw exception when the status code below zero. - [#2868](https://github.com/hyperf/hyperf/pull/2868) Optimized code for guzzle sink, which support resource not only string. diff --git a/src/watcher/src/Driver/FindDriver.php b/src/watcher/src/Driver/FindDriver.php index 8f91b875a..7610dd722 100644 --- a/src/watcher/src/Driver/FindDriver.php +++ b/src/watcher/src/Driver/FindDriver.php @@ -92,12 +92,12 @@ class FindDriver implements DriverInterface $dest = implode(' ', $targets); $ret = System::exec($this->getBin() . ' ' . $dest . ' -mmin ' . $minutes . ' -type f -print'); if ($ret['code'] === 0 && strlen($ret['output'])) { - $stdout = $ret['output']; + $stdout = trim($ret['output']); $lineArr = explode(PHP_EOL, $stdout); foreach ($lineArr as $line) { $pathName = $line; - $modifyTime = fileatime($pathName); + $modifyTime = filemtime($pathName); // modifyTime less than or equal to startTime continue if ($modifyTime <= $this->startTime) { continue; diff --git a/src/watcher/src/Driver/FswatchDriver.php b/src/watcher/src/Driver/FswatchDriver.php index 461693f31..5fc65a9ad 100644 --- a/src/watcher/src/Driver/FswatchDriver.php +++ b/src/watcher/src/Driver/FswatchDriver.php @@ -69,7 +69,9 @@ class FswatchDriver implements DriverInterface $cmd = 'fswatch '; if (! $this->isDarwin) { - $cmd .= '-m inotify_monitor '; + $cmd .= ' -m inotify_monitor'; + $cmd .= " -E --format '%p' -r "; + $cmd .= ' --event Created --event Updated --event Removed --event Renamed '; } return $cmd . implode(' ', $dir) . ' ' . implode(' ', $file); diff --git a/src/watcher/src/Watcher.php b/src/watcher/src/Watcher.php index e6b4eb15b..164994fb2 100644 --- a/src/watcher/src/Watcher.php +++ b/src/watcher/src/Watcher.php @@ -133,6 +133,9 @@ class Watcher $ret = System::exec($this->option->getBin() . ' vendor/hyperf/watcher/collector-reload.php ' . $file); if ($ret['code'] === 0) { $this->output->writeln('Class reload success.'); + } else { + $this->output->writeln('Class reload failed.'); + $this->output->writeln($ret['output'] ?? ''); } $result[] = $file; } From d026b9813498fdb25e81f7647371932c83c34470 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= Date: Wed, 25 Nov 2020 19:54:22 +0800 Subject: [PATCH 06/13] Fixed `scan.ignore_annotations` does not works when using watcher. (#2874) --- CHANGELOG-2.0.md | 4 ++++ src/watcher/src/Process.php | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG-2.0.md b/CHANGELOG-2.0.md index bcfee3adc..de8bac781 100644 --- a/CHANGELOG-2.0.md +++ b/CHANGELOG-2.0.md @@ -5,6 +5,10 @@ - [#2857](https://github.com/hyperf/hyperf/pull/2857) Support Consul ACL Token for Service Governance. - [#2864](https://github.com/hyperf/hyperf/pull/2864) Added encryption component. +## Fixed + +- [#2874](https://github.com/hyperf/hyperf/pull/2874) Fixed `scan.ignore_annotations` does not works when using watcher. + ## Changed - [#2851](https://github.com/hyperf/hyperf/pull/2851) Changed default engine of view config. diff --git a/src/watcher/src/Process.php b/src/watcher/src/Process.php index 2047e9a6a..6cd3dc523 100644 --- a/src/watcher/src/Process.php +++ b/src/watcher/src/Process.php @@ -68,8 +68,8 @@ class Process $this->file = $file; $this->ast = new Ast(); $this->reflection = new BetterReflection(); + $this->config = $this->initScanConfig(); $this->reader = new AnnotationReader(); - $this->config = ScanConfig::instance('/'); $this->filesystem = new Filesystem(); } @@ -177,4 +177,16 @@ class Process } return $meta; } + + protected function initScanConfig(): ScanConfig + { + $config = ScanConfig::instance(BASE_PATH . '/config/'); + foreach ($config->getIgnoreAnnotations() as $annotation) { + AnnotationReader::addGlobalIgnoredName($annotation); + } + foreach ($config->getGlobalImports() as $alias => $annotation) { + AnnotationReader::addGlobalImports($alias, $annotation); + } + return $config; + } } From c360e79d7b6732b2c4de6dbfe9cf3f08a10d230d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= Date: Wed, 25 Nov 2020 20:23:49 +0800 Subject: [PATCH 07/13] Added option `no-restart` for watcher. (#2875) --- CHANGELOG-2.0.md | 2 +- docs/zh-cn/encryption.md | 37 --- src/encryption/.gitattributes | 2 - src/encryption/.github/workflows/release.yml | 25 -- src/encryption/LICENSE | 22 -- src/encryption/composer.json | 36 --- src/encryption/publish/encryption.php | 17 -- src/encryption/src/ConfigProvider.php | 34 --- .../src/Contract/EncrypterInterface.php | 33 --- src/encryption/src/Encrypter.php | 229 ------------------ src/encryption/src/EncrypterFactory.php | 38 --- src/encryption/src/EncrypterInvoker.php | 24 -- .../src/Exception/DecryptException.php | 16 -- .../src/Exception/EncryptException.php | 16 -- .../Exception/InvalidArgumentException.php | 16 -- src/encryption/tests/EncrypterTest.php | 74 ------ src/watcher/src/Command/WatchCommand.php | 2 + src/watcher/src/Option.php | 13 +- src/watcher/src/Watcher.php | 3 + 19 files changed, 18 insertions(+), 621 deletions(-) delete mode 100644 docs/zh-cn/encryption.md delete mode 100644 src/encryption/.gitattributes delete mode 100644 src/encryption/.github/workflows/release.yml delete mode 100644 src/encryption/LICENSE delete mode 100644 src/encryption/composer.json delete mode 100644 src/encryption/publish/encryption.php delete mode 100644 src/encryption/src/ConfigProvider.php delete mode 100644 src/encryption/src/Contract/EncrypterInterface.php delete mode 100644 src/encryption/src/Encrypter.php delete mode 100644 src/encryption/src/EncrypterFactory.php delete mode 100644 src/encryption/src/EncrypterInvoker.php delete mode 100644 src/encryption/src/Exception/DecryptException.php delete mode 100644 src/encryption/src/Exception/EncryptException.php delete mode 100644 src/encryption/src/Exception/InvalidArgumentException.php delete mode 100644 src/encryption/tests/EncrypterTest.php diff --git a/CHANGELOG-2.0.md b/CHANGELOG-2.0.md index de8bac781..fcb2f87de 100644 --- a/CHANGELOG-2.0.md +++ b/CHANGELOG-2.0.md @@ -3,7 +3,7 @@ ## Added - [#2857](https://github.com/hyperf/hyperf/pull/2857) Support Consul ACL Token for Service Governance. -- [#2864](https://github.com/hyperf/hyperf/pull/2864) Added encryption component. +- [#2875](https://github.com/hyperf/hyperf/pull/2875) Added option `no-restart` for watcher. ## Fixed diff --git a/docs/zh-cn/encryption.md b/docs/zh-cn/encryption.md deleted file mode 100644 index 614882938..000000000 --- a/docs/zh-cn/encryption.md +++ /dev/null @@ -1,37 +0,0 @@ -# 加密解密 - -[hyperf/encryption](https://github.com/hyperf/encryption) 借鉴于 `Laravel Encryption` 组件,十分感谢 `Laravel` 开发组对 `PHP` 社区的贡献。 - -## 简介 - -`Encryption` 是基于 `OpenSSL` 实现加密解密组件。 - -## 配置 - -加密器可以根据实际情况,配置多组 `key` 和 `cipher`。 - -```php - [ - 'key' => 'Hyperf', - 'cipher' => 'AES-128-CBC', - ], -]; - -``` - -## 使用 - -```php -get(EncrypterInterface::class); -$encrypt = $encrypter->encrypt($input); -$raw = $encrypter->decrypt($encrypt); -``` diff --git a/src/encryption/.gitattributes b/src/encryption/.gitattributes deleted file mode 100644 index 27b765f55..000000000 --- a/src/encryption/.gitattributes +++ /dev/null @@ -1,2 +0,0 @@ -/tests export-ignore -/.github export-ignore diff --git a/src/encryption/.github/workflows/release.yml b/src/encryption/.github/workflows/release.yml deleted file mode 100644 index 0f7d23faa..000000000 --- a/src/encryption/.github/workflows/release.yml +++ /dev/null @@ -1,25 +0,0 @@ -on: - push: - # Sequence of patterns matched against refs/tags - tags: - - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 - -name: Release - -jobs: - release: - name: Release - runs-on: ubuntu-latest - steps: - - name: Checkout code - uses: actions/checkout@v2 - - name: Create Release - id: create_release - uses: actions/create-release@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - tag_name: ${{ github.ref }} - release_name: Release ${{ github.ref }} - draft: false - prerelease: false diff --git a/src/encryption/LICENSE b/src/encryption/LICENSE deleted file mode 100644 index 10b073834..000000000 --- a/src/encryption/LICENSE +++ /dev/null @@ -1,22 +0,0 @@ -The MIT License (MIT) - -Copyright (c) Taylor Otwell -Copyright (c) Hyperf - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/src/encryption/composer.json b/src/encryption/composer.json deleted file mode 100644 index 387d05d5d..000000000 --- a/src/encryption/composer.json +++ /dev/null @@ -1,36 +0,0 @@ -{ - "name": "hyperf/encryption", - "type": "library", - "license": "MIT", - "keywords": [ - "php", - "hyperf", - "encryption" - ], - "description": "", - "autoload": { - "psr-4": { - "Hyperf\\Encryption\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { - "HyperfTest\\Encryption\\": "tests/" - } - }, - "require": { - "php": ">=7.2", - "ext-json": "*", - "ext-openssl": "*", - "hyperf/contract": "~2.0.0", - "psr/container": "^1.0" - }, - "config": { - "sort-packages": true - }, - "extra": { - "hyperf": { - "config": "Hyperf\\Encryption\\ConfigProvider" - } - } -} diff --git a/src/encryption/publish/encryption.php b/src/encryption/publish/encryption.php deleted file mode 100644 index 12ac9aa60..000000000 --- a/src/encryption/publish/encryption.php +++ /dev/null @@ -1,17 +0,0 @@ - [ - 'key' => 'Hyperf', - 'cipher' => 'AES-128-CBC', - ], -]; diff --git a/src/encryption/src/ConfigProvider.php b/src/encryption/src/ConfigProvider.php deleted file mode 100644 index 109149bad..000000000 --- a/src/encryption/src/ConfigProvider.php +++ /dev/null @@ -1,34 +0,0 @@ - [ - EncrypterInterface::class => EncrypterInvoker::class, - ], - 'publish' => [ - [ - 'id' => 'config', - 'description' => 'The config for encryption.', - 'source' => __DIR__ . '/../publish/encryption.php', - 'destination' => BASE_PATH . '/config/autoload/encryption.php', - ], - ], - ]; - } -} diff --git a/src/encryption/src/Contract/EncrypterInterface.php b/src/encryption/src/Contract/EncrypterInterface.php deleted file mode 100644 index db4edad3c..000000000 --- a/src/encryption/src/Contract/EncrypterInterface.php +++ /dev/null @@ -1,33 +0,0 @@ -key = $key; - $this->cipher = $cipher; - } - - /** - * Encrypt the given value. - * - * @param mixed $value - * @throws \Exception - */ - public function encrypt($value, bool $serialize = true): string - { - $iv = random_bytes(openssl_cipher_iv_length($this->cipher)); - - // First we will encrypt the value using OpenSSL. After this is encrypted we - // will proceed to calculating a MAC for the encrypted value so that this - // value can be verified later as not having been changed by the users. - $value = \openssl_encrypt( - $serialize ? serialize($value) : $value, - $this->cipher, - $this->key, - 0, - $iv - ); - - if ($value === false) { - throw new EncryptException('Could not encrypt the data.'); - } - - // Once we get the encrypted value we'll go ahead and base64_encode the input - // vector and create the MAC for the encrypted value so we can then verify - // its authenticity. Then, we'll JSON the data into the "payload" array. - $mac = $this->hash($iv = base64_encode($iv), $value); - - $json = json_encode(compact('iv', 'value', 'mac'), JSON_UNESCAPED_SLASHES); - - if (json_last_error() !== JSON_ERROR_NONE) { - throw new EncryptException('Could not encrypt the data.'); - } - - return base64_encode($json); - } - - /** - * Encrypt a string without serialization. - * - * @param string $value - * @throws \Exception - * @return string - */ - public function encryptString($value) - { - return $this->encrypt($value, false); - } - - /** - * Decrypt the given value. - * - * @return mixed - */ - public function decrypt(string $payload, bool $unserialize = true) - { - $payload = $this->getJsonPayload($payload); - - $iv = base64_decode($payload['iv']); - - // Here we will decrypt the value. If we are able to successfully decrypt it - // we will then unserialize it and return it out to the caller. If we are - // unable to decrypt this value we will throw out an exception message. - $decrypted = \openssl_decrypt( - $payload['value'], - $this->cipher, - $this->key, - 0, - $iv - ); - - if ($decrypted === false) { - throw new DecryptException('Could not decrypt the data.'); - } - - return $unserialize ? unserialize($decrypted) : $decrypted; - } - - /** - * Decrypt the given string without unserialization. - * - * @param string $payload - * @return string - */ - public function decryptString($payload) - { - return $this->decrypt($payload, false); - } - - /** - * Get the encryption key. - * - * @return string - */ - public function getKey() - { - return $this->key; - } - - /** - * Create a MAC for the given value. - * - * @param string $iv - * @param mixed $value - * @return string - */ - protected function hash($iv, $value) - { - return hash_hmac('sha256', $iv . $value, $this->key); - } - - /** - * Get the JSON array from the given payload. - * - * @param string $payload - * @return array - */ - protected function getJsonPayload($payload) - { - $payload = json_decode(base64_decode($payload), true); - - // If the payload is not valid JSON or does not have the proper keys set we will - // assume it is invalid and bail out of the routine since we will not be able - // to decrypt the given value. We'll also check the MAC for this encryption. - if (! $this->validPayload($payload)) { - throw new DecryptException('The payload is invalid.'); - } - - if (! $this->validMac($payload)) { - throw new DecryptException('The MAC is invalid.'); - } - - return $payload; - } - - /** - * Verify that the encryption payload is valid. - * - * @param mixed $payload - * @return bool - */ - protected function validPayload($payload) - { - return is_array($payload) && isset($payload['iv'], $payload['value'], $payload['mac']) && - strlen(base64_decode($payload['iv'], true)) === openssl_cipher_iv_length($this->cipher); - } - - /** - * Determine if the MAC for the given payload is valid. - * - * @return bool - */ - protected function validMac(array $payload) - { - $calculated = $this->calculateMac($payload, $bytes = random_bytes(16)); - - return hash_equals( - hash_hmac('sha256', $payload['mac'], $bytes, true), - $calculated - ); - } - - /** - * Calculate the hash of the given payload. - * - * @param array $payload - * @param string $bytes - * @return string - */ - protected function calculateMac($payload, $bytes) - { - return hash_hmac( - 'sha256', - $this->hash($payload['iv'], $payload['value']), - $bytes, - true - ); - } -} diff --git a/src/encryption/src/EncrypterFactory.php b/src/encryption/src/EncrypterFactory.php deleted file mode 100644 index 7d531bc31..000000000 --- a/src/encryption/src/EncrypterFactory.php +++ /dev/null @@ -1,38 +0,0 @@ -config = $container->get(ConfigInterface::class); - } - - public function get(string $name): EncrypterInterface - { - $encryption = $this->config->get('encryption.' . $name, []); - $key = $encryption['key'] ?? ''; - $cipher = $encryption['cipher'] ?? 'AES-128-CBC'; - - return new Encrypter($key, $cipher); - } -} diff --git a/src/encryption/src/EncrypterInvoker.php b/src/encryption/src/EncrypterInvoker.php deleted file mode 100644 index e937943b8..000000000 --- a/src/encryption/src/EncrypterInvoker.php +++ /dev/null @@ -1,24 +0,0 @@ -get(EncrypterFactory::class); - - return $factory->get('default'); - } -} diff --git a/src/encryption/src/Exception/DecryptException.php b/src/encryption/src/Exception/DecryptException.php deleted file mode 100644 index 0c0bbc3e0..000000000 --- a/src/encryption/src/Exception/DecryptException.php +++ /dev/null @@ -1,16 +0,0 @@ - [ - 'default' => [ - 'key' => '123456', - 'cipher' => 'AES-256-CBC', - ], - ], - ]); - $container->shouldReceive('get')->with(ConfigInterface::class)->andReturn($config); - $container->shouldReceive('get')->with(EncrypterFactory::class)->andReturnUsing(function () use ($container) { - return new EncrypterFactory($container); - }); - $container->shouldReceive('get')->with(EncrypterInterface::class)->andReturnUsing(function () use ($container) { - return (new EncrypterInvoker())($container); - }); - return $container; - } - - public function testEncodeString() - { - $input = 'Hello Word.'; - $container = $this->mockContainer(); - $encrypter = $container->get(EncrypterInterface::class); - $encrypt = $encrypter->encryptString($input); - $this->assertSame($input, $encrypter->decryptString($encrypt)); - } - - public function testEncodeObject() - { - $input = range(1, 3); - $container = $this->mockContainer(); - $encrypter = $container->get(EncrypterInterface::class); - $encrypt = $encrypter->encrypt($input); - $this->assertSame($input, $encrypter->decrypt($encrypt)); - } -} diff --git a/src/watcher/src/Command/WatchCommand.php b/src/watcher/src/Command/WatchCommand.php index 90e30ff60..af76af373 100644 --- a/src/watcher/src/Command/WatchCommand.php +++ b/src/watcher/src/Command/WatchCommand.php @@ -29,6 +29,7 @@ class WatchCommand extends Command $this->setDescription('watch command'); $this->addOption('file', 'F', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, '', []); $this->addOption('dir', 'D', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, '', []); + $this->addOption('no-restart', 'N', InputOption::VALUE_NONE, 'Whether no need to restart server'); } public function handle() @@ -36,6 +37,7 @@ class WatchCommand extends Command $option = make(Option::class, [ 'dir' => $this->input->getOption('dir'), 'file' => $this->input->getOption('file'), + 'restart' => ! $this->input->getOption('no-restart'), ]); $watcher = make(Watcher::class, [ diff --git a/src/watcher/src/Option.php b/src/watcher/src/Option.php index 041014c61..4a118264e 100644 --- a/src/watcher/src/Option.php +++ b/src/watcher/src/Option.php @@ -46,7 +46,12 @@ class Option */ protected $scanInterval = 2000; - public function __construct(ConfigInterface $config, array $dir, array $file) + /** + * @var bool + */ + protected $restart = true; + + public function __construct(ConfigInterface $config, array $dir, array $file, bool $restart = true) { $options = $config->get('watcher', []); @@ -59,6 +64,7 @@ class Option $this->watchDir = array_unique(array_merge($this->watchDir, $dir)); $this->watchFile = array_unique(array_merge($this->watchFile, $file)); + $this->restart = $restart; } public function getDriver(): string @@ -90,4 +96,9 @@ class Option { return $this->scanInterval > 0 ? $this->scanInterval : 2000; } + + public function isRestart(): bool + { + return $this->restart; + } } diff --git a/src/watcher/src/Watcher.php b/src/watcher/src/Watcher.php index 164994fb2..922eb79ef 100644 --- a/src/watcher/src/Watcher.php +++ b/src/watcher/src/Watcher.php @@ -150,6 +150,9 @@ class Watcher public function restart($isStart = true) { + if (! $this->option->isRestart()) { + return; + } $file = $this->config->get('server.settings.pid_file'); if (empty($file)) { throw new FileNotFoundException('The config of pid_file is not found.'); From f673bb69955eea3ff3ba5757e704ea2d2c060b6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= Date: Wed, 25 Nov 2020 20:36:31 +0800 Subject: [PATCH 08/13] Update CHANGELOG.md (#2866) --- CHANGELOG-2.0.md | 1 + composer.json | 4 ---- phpunit.xml | 2 -- 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/CHANGELOG-2.0.md b/CHANGELOG-2.0.md index fcb2f87de..8f41de07c 100644 --- a/CHANGELOG-2.0.md +++ b/CHANGELOG-2.0.md @@ -3,6 +3,7 @@ ## Added - [#2857](https://github.com/hyperf/hyperf/pull/2857) Support Consul ACL Token for Service Governance. +- [#2870](https://github.com/hyperf/hyperf/pull/2870) The publish option of `ConfigProvider` allows publish directory. - [#2875](https://github.com/hyperf/hyperf/pull/2875) Added option `no-restart` for watcher. ## Fixed diff --git a/composer.json b/composer.json index edae02c0e..f3e5c1d91 100644 --- a/composer.json +++ b/composer.json @@ -103,7 +103,6 @@ "hyperf/di": "self.version", "hyperf/dispatcher": "self.version", "hyperf/elasticsearch": "self.version", - "hyperf/encryption": "self.version", "hyperf/etcd": "self.version", "hyperf/event": "self.version", "hyperf/exception-handler": "self.version", @@ -180,7 +179,6 @@ "Hyperf\\Di\\": "src/di/src/", "Hyperf\\Dispatcher\\": "src/dispatcher/src/", "Hyperf\\Elasticsearch\\": "src/elasticsearch/src/", - "Hyperf\\Encryption\\": "src/encryption/src/", "Hyperf\\Etcd\\": "src/etcd/src/", "Hyperf\\Event\\": "src/event/src/", "Hyperf\\ExceptionHandler\\": "src/exception-handler/src/", @@ -263,7 +261,6 @@ "HyperfTest\\Di\\": "src/di/tests/", "HyperfTest\\Dispatcher\\": "src/dispatcher/tests/", "HyperfTest\\Elasticsearch\\": "src/elasticsearch/tests/", - "HyperfTest\\Encryption\\": "src/encryption/tests/", "HyperfTest\\Etcd\\": "src/etcd/tests/", "HyperfTest\\Event\\": "src/event/tests/", "HyperfTest\\ExceptionHandler\\": "src/exception-handler/tests/", @@ -340,7 +337,6 @@ "Hyperf\\Devtool\\ConfigProvider", "Hyperf\\Di\\ConfigProvider", "Hyperf\\Dispatcher\\ConfigProvider", - "Hyperf\\Encryption\\ConfigProvider", "Hyperf\\Etcd\\ConfigProvider", "Hyperf\\Event\\ConfigProvider", "Hyperf\\ExceptionHandler\\ConfigProvider", diff --git a/phpunit.xml b/phpunit.xml index e9ed15ceb..998597535 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -25,7 +25,6 @@ ./src/di/tests ./src/dispatcher/tests ./src/elasticsearch/tests - ./src/encryption/tests ./src/etcd/tests ./src/event/tests ./src/exception-handler/tests @@ -81,7 +80,6 @@ ./src/di/src ./src/dispatcher/src ./src/elasticsearch/src - ./src/encryption/src ./src/event/src ./src/grpc-client/src ./src/guzzle/src From d69d7f42373ce5e004e1379e9646006f83783b24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Wed, 25 Nov 2020 23:53:42 +0800 Subject: [PATCH 09/13] Fixed config of nsqd does not works. --- src/nsq/src/Nsqd/Client.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nsq/src/Nsqd/Client.php b/src/nsq/src/Nsqd/Client.php index 867db1486..42fbaeeb2 100644 --- a/src/nsq/src/Nsqd/Client.php +++ b/src/nsq/src/Nsqd/Client.php @@ -22,9 +22,9 @@ class Client implements ClientInterface */ protected $options = []; - public function __construct(ConfigInterface $config) + public function __construct(ConfigInterface $config, string $pool = 'default') { - $nsq = $config->get('nsq', []); + $nsq = $config->get('nsq.' . $pool, []); $options = $nsq['nsqd']['options'] ?? []; if (! isset($options['base_uri'])) { $options['base_uri'] = sprintf('http://%s:%s', $nsq['host'] ?? '127.0.0.1', $nsq['nsqd']['port'] ?? 4151); From c4d12d1e06882ea244a235d7ce22a95b40c91837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Thu, 26 Nov 2020 09:47:08 +0800 Subject: [PATCH 10/13] Added test cases for nsq. --- CHANGELOG-2.0.md | 1 + phpunit.xml | 1 + src/nsq/tests/ConsumerManagerTest.php | 6 ++--- src/nsq/tests/HttpClientTest.php | 34 +++++++++++++++------------ 4 files changed, 24 insertions(+), 18 deletions(-) diff --git a/CHANGELOG-2.0.md b/CHANGELOG-2.0.md index 8f41de07c..a99428cbd 100644 --- a/CHANGELOG-2.0.md +++ b/CHANGELOG-2.0.md @@ -9,6 +9,7 @@ ## Fixed - [#2874](https://github.com/hyperf/hyperf/pull/2874) Fixed `scan.ignore_annotations` does not works when using watcher. +- [#2878](https://github.com/hyperf/hyperf/pull/2878) Fixed config of nsqd does not works. ## Changed diff --git a/phpunit.xml b/phpunit.xml index 998597535..ffb219d9a 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -38,6 +38,7 @@ ./src/logger/tests ./src/metric/tests ./src/model-cache/tests + ./src/nsq/tests ./src/paginator/tests ./src/pool/tests ./src/process/tests diff --git a/src/nsq/tests/ConsumerManagerTest.php b/src/nsq/tests/ConsumerManagerTest.php index 8ce627185..04f508d13 100644 --- a/src/nsq/tests/ConsumerManagerTest.php +++ b/src/nsq/tests/ConsumerManagerTest.php @@ -61,7 +61,7 @@ class ConsumerManagerTest extends TestCase $hasRegistered = true; /** @var AbstractConsumer $consumer */ $consumer = $item->getConsumer(); - $this->assertTrue($item->isEnable()); + $this->assertTrue($item->isEnable(new \stdClass())); $this->assertSame($name, $consumer->getName()); $this->assertSame($channel, $consumer->getChannel()); $this->assertSame($topic, $consumer->getTopic()); @@ -96,7 +96,7 @@ class ConsumerManagerTest extends TestCase foreach (ProcessManager::all() as $item) { if (method_exists($item, 'getConsumer')) { /* @var AbstractConsumer $consumer */ - $this->assertFalse($item->isEnable()); + $this->assertFalse($item->isEnable(new \stdClass())); break; } } @@ -123,7 +123,7 @@ class ConsumerManagerTest extends TestCase $manager->run(); foreach (ProcessManager::all() as $item) { if (method_exists($item, 'getConsumer') && ($item->getConsumer() instanceof DisabledDemoConsumer)) { - $this->assertFalse($item->isEnable()); + $this->assertFalse($item->isEnable(new \stdClass())); break; } } diff --git a/src/nsq/tests/HttpClientTest.php b/src/nsq/tests/HttpClientTest.php index 7ae02ea7a..77c9cac4e 100644 --- a/src/nsq/tests/HttpClientTest.php +++ b/src/nsq/tests/HttpClientTest.php @@ -11,7 +11,7 @@ declare(strict_types=1); */ namespace HyperfTest\Nsq; -use Hyperf\Contract\ConfigInterface; +use Hyperf\Config\Config; use Hyperf\Guzzle\CoroutineHandler; use Hyperf\Nsq\Nsqd\Client; use HyperfTest\Nsq\Stub\CoroutineHandlerStub; @@ -31,9 +31,7 @@ class HttpClientTest extends TestCase public function testHttpClientWithEmptyConfig() { - $config = Mockery::mock(ConfigInterface::class); - $config->shouldReceive('get')->with('nsq', [])->andReturn([]); - + $config = new Config([]); $client = new Client($config); $this->assertSame('http://127.0.0.1:4151', $client->getOptions()['base_uri']); $this->assertInstanceOf(CoroutineHandler::class, $client->getOptions()['handler']); @@ -41,11 +39,14 @@ class HttpClientTest extends TestCase public function testHttpClientWithHost() { - $config = Mockery::mock(ConfigInterface::class); - $config->shouldReceive('get')->with('nsq', [])->andReturn([ - 'host' => '192.168.1.1', - 'nsqd' => [ - 'port' => 14151, + $config = new Config([ + 'nsq' => [ + 'default' => [ + 'host' => '192.168.1.1', + 'nsqd' => [ + 'port' => 14151, + ], + ], ], ]); @@ -56,12 +57,15 @@ class HttpClientTest extends TestCase public function testHttpClientWithOptions() { - $config = Mockery::mock(ConfigInterface::class); - $config->shouldReceive('get')->with('nsq', [])->andReturn([ - 'nsqd' => [ - 'options' => [ - 'base_uri' => 'https://nsq.hyperf.io', - 'handler' => new CoroutineHandlerStub(), + $config = new Config([ + 'nsq' => [ + 'default' => [ + 'nsqd' => [ + 'options' => [ + 'base_uri' => 'https://nsq.hyperf.io', + 'handler' => new CoroutineHandlerStub(), + ], + ], ], ], ]); From af87f4f783e0bedf78fe2b5143cae5912d86a8a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E5=94=81?= <52o@qq52o.cn> Date: Fri, 27 Nov 2020 15:54:25 +0800 Subject: [PATCH 11/13] Fix sidebar (#2886) --- docs/zh-cn/summary.md | 1 - 1 file changed, 1 deletion(-) diff --git a/docs/zh-cn/summary.md b/docs/zh-cn/summary.md index d3bf5a9fa..d9263ef65 100644 --- a/docs/zh-cn/summary.md +++ b/docs/zh-cn/summary.md @@ -116,7 +116,6 @@ * [Watcher](zh-cn/watcher.md) * [开发者工具](zh-cn/devtool.md) * [Swoole Tracker](zh-cn/swoole-tracker.md) - * [加密解密](zh-cn/encryption.md) * 应用部署 From 232a517171234293f57368f2b5d8c08bc3bbd35c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=9C=9D=E6=99=96?= Date: Fri, 27 Nov 2020 18:53:54 +0800 Subject: [PATCH 12/13] Update release-planning.md --- docs/zh-cn/release-planning.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/zh-cn/release-planning.md b/docs/zh-cn/release-planning.md index 27e3228c3..83046d060 100644 --- a/docs/zh-cn/release-planning.md +++ b/docs/zh-cn/release-planning.md @@ -4,7 +4,9 @@ | 版本 | 状态 | 积极支持截止时间 | 安全维护截止时间 | 发布或预计发布时间 | | ---- | -------- | ---------------- | ---------------- | ------------------ | -| 2.0 | 积极支持中 | / | / | 2020-06-22 | +| 3.0 | 研发中 | / | / | 2021-06-20 | +| 2.1 | Beta版试用中 | / | / | 2020-12-31 | +| 2.0 | 积极支持中 | 2020-12-31 | 2021-06-30 | 2020-06-22 | | 1.1 | 安全维护中 | 2020-06-23 | 2020-12-31 | | | 1.0 | 停止维护 | 2019-10-08 | 2019-12-31 | 2019-06-20 | From 43ef242840cec5f769af9f5198c322167f3c2e69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=9C=9D=E6=99=96?= Date: Fri, 27 Nov 2020 18:55:12 +0800 Subject: [PATCH 13/13] Update release-planning.md --- docs/zh-cn/release-planning.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/zh-cn/release-planning.md b/docs/zh-cn/release-planning.md index 83046d060..c6e3c121b 100644 --- a/docs/zh-cn/release-planning.md +++ b/docs/zh-cn/release-planning.md @@ -5,9 +5,9 @@ | 版本 | 状态 | 积极支持截止时间 | 安全维护截止时间 | 发布或预计发布时间 | | ---- | -------- | ---------------- | ---------------- | ------------------ | | 3.0 | 研发中 | / | / | 2021-06-20 | -| 2.1 | Beta版试用中 | / | / | 2020-12-31 | +| 2.1 | Beta版试用中 | / | / | 2020-12-31 | | 2.0 | 积极支持中 | 2020-12-31 | 2021-06-30 | 2020-06-22 | -| 1.1 | 安全维护中 | 2020-06-23 | 2020-12-31 | | +| 1.1 | 安全维护中 | 2020-06-23 | 2020-12-31 | 2019-10-08 | | 1.0 | 停止维护 | 2019-10-08 | 2019-12-31 | 2019-06-20 | * 积极支持将包含常规迭代周期的 BUG 修复、安全问题修复、功能迭代和功能新增;