Skip to content

Commit b568e6a

Browse files
authored
Merge pull request #3 from onix-systems-php/hyperf-3.1
hyperf-3.1: Update code with detailed comments and improve code style
2 parents 5d0cfc2 + b8f68d3 commit b568e6a

27 files changed

+197
-123
lines changed

.php-cs-fixer.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
<?php
22

33
$header = <<<'EOF'
4-
This file is part of Hyperf.
4+
This file is part of the extension library for Hyperf.
55
6-
@link https://www.hyperf.io
7-
@document https://hyperf.wiki
8-
96
@license https://github.com/hyperf/hyperf/blob/master/LICENSE
107
EOF;
118

@@ -23,13 +20,13 @@
2320
'location' => 'after_declare_strict',
2421
],
2522
'array_syntax' => [
26-
'syntax' => 'short'
23+
'syntax' => 'short',
2724
],
2825
'list_syntax' => [
29-
'syntax' => 'short'
26+
'syntax' => 'short',
3027
],
3128
'concat_space' => [
32-
'spacing' => 'one'
29+
'spacing' => 'one',
3330
],
3431
'blank_line_before_statement' => [
3532
'statements' => [
@@ -38,7 +35,7 @@
3835
],
3936
'general_phpdoc_annotation_remove' => [
4037
'annotations' => [
41-
'author'
38+
'author',
4239
],
4340
],
4441
'ordered_imports' => [

composer.json

+10-11
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@
2222
"ext-fileinfo": "*",
2323
"ext-gd": "*",
2424
"ext-json": "*",
25-
"onix-systems-php/hyperf-core": ">=1.1.0",
26-
"onix-systems-php/hyperf-actions-log": "^1.1",
27-
"hyperf/database": "^3.0",
28-
"hyperf/config": "^3.0",
29-
"hyperf/contract": "^3.0",
30-
"hyperf/db-connection": "^3.0",
31-
"hyperf/filesystem": "^3.0",
32-
"hyperf/guzzle": "^3.0",
33-
"hyperf/translation": "^3.0",
34-
"hyperf/validation": "^3.0",
35-
"hyperf/utils": "^3.0",
25+
"onix-systems-php/hyperf-core": ">=1.2.0",
26+
"onix-systems-php/hyperf-actions-log": "^1.2",
27+
"hyperf/database": "^3.1",
28+
"hyperf/config": "^3.1",
29+
"hyperf/contract": "^3.1",
30+
"hyperf/db-connection": "^3.1",
31+
"hyperf/filesystem": "^3.1",
32+
"hyperf/guzzle": "^3.1",
33+
"hyperf/translation": "^3.1",
34+
"hyperf/validation": "^3.1",
3635
"intervention/image": "^2.7",
3736
"league/flysystem": "^2.0|^3.0"
3837
},

publish/config/file_upload.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
<?php
22

33
declare(strict_types=1);
4-
4+
/**
5+
* This file is part of the extension library for Hyperf.
6+
*
7+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
8+
*/
59
use OnixSystemsPHP\HyperfCore\Constants\Time;
610

11+
use function Hyperf\Support\env;
12+
713
return [
814
'mime_types' => [
915
'image/png',

publish/migrations/2022_04_07_082256_files.php

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22

33
declare(strict_types=1);
4+
/**
5+
* This file is part of the extension library for Hyperf.
6+
*
7+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
8+
*/
49
use Hyperf\Database\Migrations\Migration;
510
use Hyperf\Database\Schema\Blueprint;
611
use Hyperf\Database\Schema\Schema;

publish/routes.php

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22

33
declare(strict_types=1);
4+
/**
5+
* This file is part of the extension library for Hyperf.
6+
*
7+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
8+
*/
49
use Hyperf\HttpServer\Router\Router;
510
use OnixSystemsPHP\HyperfFileUpload\Controller\FileController;
611

src/ConfigProvider.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
<?php
22

33
declare(strict_types=1);
4+
/**
5+
* This file is part of the extension library for Hyperf.
6+
*
7+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
8+
*/
9+
410
namespace OnixSystemsPHP\HyperfFileUpload;
511

612
class ConfigProvider
713
{
814
public function __invoke(): array
915
{
10-
1116
return [
1217
'dependencies' => [
1318
],

src/Controller/FileController.php

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

33
declare(strict_types=1);
4+
/**
5+
* This file is part of the extension library for Hyperf.
6+
*
7+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
8+
*/
9+
410
namespace OnixSystemsPHP\HyperfFileUpload\Controller;
511

612
use OnixSystemsPHP\HyperfCore\Contract\CoreAuthenticatableProvider;
@@ -16,16 +22,14 @@ class FileController extends AbstractController
1622
{
1723
public function __construct(
1824
private CoreAuthenticatableProvider $authenticatableProvider,
19-
) {
20-
}
21-
25+
) {}
2226

2327
#[OA\Post(
2428
path: '/v1/file',
2529
operationId: 'upload',
2630
summary: 'Upload file',
2731
requestBody: new OA\RequestBody(required: true, content: [
28-
new OA\MediaType(mediaType: 'multipart/form-data', schema: new OA\Schema(ref: '#/components/schemas/RequestFileUpload'))
32+
new OA\MediaType(mediaType: 'multipart/form-data', schema: new OA\Schema(ref: '#/components/schemas/RequestFileUpload')),
2933
]),
3034
tags: ['file'],
3135
parameters: [new OA\Parameter(ref: '#/components/parameters/Locale')],
@@ -34,8 +38,8 @@ public function __construct(
3438
new OA\Property(property: 'status', type: 'string'),
3539
new OA\Property(property: 'data', ref: '#/components/schemas/ResourceFile'),
3640
])),
37-
new OA\Response(response: 422, ref: '#/components/responses/422'),
38-
new OA\Response(response: 500, ref: '#/components/responses/500'),
41+
new OA\Response(ref: '#/components/responses/422', response: 422),
42+
new OA\Response(ref: '#/components/responses/500', response: 500),
3943
],
4044
)]
4145
public function create(RequestFileUpload $request, AddFileService $addFileService): ResourceFile
@@ -45,7 +49,6 @@ public function create(RequestFileUpload $request, AddFileService $addFileServic
4549
return new ResourceFile($file);
4650
}
4751

48-
4952
#[OA\Post(
5053
path: '/v1/file/url',
5154
operationId: 'uploadExternal',
@@ -58,9 +61,9 @@ public function create(RequestFileUpload $request, AddFileService $addFileServic
5861
new OA\Property(property: 'status', type: 'string'),
5962
new OA\Property(property: 'data', ref: '#/components/schemas/ResourceFile'),
6063
])),
61-
new OA\Response(response: 400, ref: '#/components/responses/400'),
62-
new OA\Response(response: 422, ref: '#/components/responses/422'),
63-
new OA\Response(response: 500, ref: '#/components/responses/500'),
64+
new OA\Response(ref: '#/components/responses/400', response: 400),
65+
new OA\Response(ref: '#/components/responses/422', response: 422),
66+
new OA\Response(ref: '#/components/responses/500', response: 500),
6467
],
6568
)]
6669
public function createFromUrl(

src/Model/Behaviour/FileRelations.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
<?php
22

33
declare(strict_types=1);
4+
/**
5+
* This file is part of the extension library for Hyperf.
6+
*
7+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
8+
*/
9+
410
namespace OnixSystemsPHP\HyperfFileUpload\Model\Behaviour;
511

612
use Carbon\Carbon;
713
use Hyperf\Config\Annotation\Value;
14+
use Hyperf\Context\ApplicationContext;
815
use Hyperf\Contract\ConfigInterface;
916
use Hyperf\Database\Model\Builder;
1017
use Hyperf\Database\Model\Events\Saved;
1118
use Hyperf\Database\Model\Events\Saving;
1219
use Hyperf\Database\Model\Model;
1320
use Hyperf\Filesystem\FilesystemFactory;
1421
use Hyperf\HttpMessage\Exception\BadRequestHttpException;
15-
use Hyperf\Utils\ApplicationContext;
16-
use Hyperf\Utils\Str;
22+
use Hyperf\Stringable\Str;
1723
use Intervention\Image\ImageManager;
1824
use OnixSystemsPHP\HyperfCore\Contract\CoreAuthenticatable;
1925
use OnixSystemsPHP\HyperfCore\Contract\CoreAuthenticatableProvider;
@@ -328,7 +334,7 @@ private function getAllowedToLinkFileModel(Saving $event, string $relationName,
328334
};
329335
}
330336

331-
private function getAuth(): CoreAuthenticatable|null
337+
private function getAuth(): null|CoreAuthenticatable
332338
{
333339
return ApplicationContext::getContainer()->get(CoreAuthenticatableProvider::class)?->user();
334340
}

src/Model/File.php

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

33
declare(strict_types=1);
4+
/**
5+
* This file is part of the extension library for Hyperf.
6+
*
7+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
8+
*/
9+
410
namespace OnixSystemsPHP\HyperfFileUpload\Model;
511

612
use Carbon\Carbon;

src/Repository/FileRepository.php

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

33
declare(strict_types=1);
4+
/**
5+
* This file is part of the extension library for Hyperf.
6+
*
7+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
8+
*/
9+
410
namespace OnixSystemsPHP\HyperfFileUpload\Repository;
511

612
use Carbon\Carbon;
@@ -13,6 +19,7 @@
1319
* @method File update(File $model, array $data)
1420
* @method File save(File $model)
1521
* @method bool delete(File $model)
22+
* @method bool forceDelete(File $model)
1623
* @method Builder|FileRepository finder(string $type, ...$parameters)
1724
* @method null|File fetchOne(bool $lock, bool $force)
1825
*/

src/Request/RequestExternalFileUpload.php

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

33
declare(strict_types=1);
4+
/**
5+
* This file is part of the extension library for Hyperf.
6+
*
7+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
8+
*/
9+
410
namespace OnixSystemsPHP\HyperfFileUpload\Request;
511

612
use Hyperf\Validation\Request\FormRequest;

src/Request/RequestFileUpload.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

33
declare(strict_types=1);
4+
/**
5+
* This file is part of the extension library for Hyperf.
6+
*
7+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
8+
*/
9+
410
namespace OnixSystemsPHP\HyperfFileUpload\Request;
511

612
use Hyperf\Validation\Request\FormRequest;
@@ -15,7 +21,7 @@
1521
schema: 'RequestFileAssign',
1622
properties: [
1723
new OA\Property(property: 'id', type: 'integer'),
18-
new OA\Property(property: 'delete_it', type: 'boolean', default: 'false')
24+
new OA\Property(property: 'delete_it', type: 'boolean', default: 'false'),
1925
],
2026
type: 'object',
2127
)]

src/Resource/ResourceFile.php

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

33
declare(strict_types=1);
4+
/**
5+
* This file is part of the extension library for Hyperf.
6+
*
7+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
8+
*/
9+
410
namespace OnixSystemsPHP\HyperfFileUpload\Resource;
511

612
use OnixSystemsPHP\HyperfCore\Resource\AbstractResource;

src/Service/AddExternalFileService.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
22

33
declare(strict_types=1);
4+
/**
5+
* This file is part of the extension library for Hyperf.
6+
*
7+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
8+
*/
9+
410
namespace OnixSystemsPHP\HyperfFileUpload\Service;
511

612
use Hyperf\DbConnection\Annotation\Transactional;
@@ -15,11 +21,10 @@ class AddExternalFileService
1521
public function __construct(
1622
private DownloadFileService $downloadFileService,
1723
private AddFileService $addFileService,
18-
) {
19-
}
24+
) {}
2025

2126
#[Transactional(attempts: 1)]
22-
public function run(string $url, CoreAuthenticatable|null $user): File
27+
public function run(string $url, null|CoreAuthenticatable $user): File
2328
{
2429
$filename = $this->downloadFileService->run($url, $user);
2530
$size = filesize($filename);

src/Service/AddFileService.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
<?php
22

33
declare(strict_types=1);
4+
/**
5+
* This file is part of the extension library for Hyperf.
6+
*
7+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
8+
*/
9+
410
namespace OnixSystemsPHP\HyperfFileUpload\Service;
511

612
use Hyperf\Contract\ConfigInterface;
713
use Hyperf\DbConnection\Annotation\Transactional;
814
use Hyperf\Filesystem\FilesystemFactory;
915
use Hyperf\HttpMessage\Upload\UploadedFile;
10-
use Hyperf\Utils\Str;
16+
use Hyperf\Stringable\Str;
1117
use OnixSystemsPHP\HyperfActionsLog\Event\Action;
1218
use OnixSystemsPHP\HyperfCore\Contract\CoreAuthenticatable;
1319
use OnixSystemsPHP\HyperfCore\Contract\CorePolicyGuard;
@@ -17,6 +23,8 @@
1723
use OnixSystemsPHP\HyperfFileUpload\Repository\FileRepository;
1824
use Psr\EventDispatcher\EventDispatcherInterface;
1925

26+
use function Hyperf\Translation\__;
27+
2028
#[Service]
2129
class AddFileService
2230
{
@@ -28,19 +36,17 @@ public function __construct(
2836
private FileRepository $rFile,
2937
private EventDispatcherInterface $eventDispatcher,
3038
private ?CorePolicyGuard $policyGuard,
31-
) {
32-
}
39+
) {}
3340

3441
#[Transactional(attempts: 1)]
35-
public function run(UploadedFile $uploadedFile, CoreAuthenticatable|null $user): File
42+
public function run(UploadedFile $uploadedFile, null|CoreAuthenticatable $user): File
3643
{
3744
$this->validate($uploadedFile);
3845
$file = $this->storeFile($uploadedFile, $user);
3946
$this->eventDispatcher->dispatch(new Action(self::ACTION, $file, ['file' => $file->url], $user));
4047
return $file;
4148
}
4249

43-
4450
private function validate(UploadedFile $uploadedFile): void
4551
{
4652
if ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
@@ -52,7 +58,7 @@ private function validate(UploadedFile $uploadedFile): void
5258
}
5359
}
5460

55-
private function storeFile(UploadedFile $uploadedFile, CoreAuthenticatable|null $user): File
61+
private function storeFile(UploadedFile $uploadedFile, null|CoreAuthenticatable $user): File
5662
{
5763
$storage = $this->config->get('file.default');
5864
$domain = $this->config->get("file_upload.storage.{$storage}.domain");

0 commit comments

Comments
 (0)