Skip to content

Commit 6db3b9c

Browse files
author
liutao
committed
feat: format
1 parent 854f72a commit 6db3b9c

File tree

22 files changed

+189
-62
lines changed

22 files changed

+189
-62
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
"illuminate/database": "^9.0",
2828
"symfony/console": "^6",
2929
"symfony/process": "^6",
30-
"respect/validation": "^2.2",
3130
"symfony/finder": "^6",
3231
"guzzlehttp/guzzle": "^7.7",
3332
"symfony/dotenv": "^6.0",
3433
"monolog/monolog": "^3.4",
35-
"ext-simplexml": "*"
34+
"ext-simplexml": "*",
35+
"symfony/validator": "^7.2"
3636
}
3737
}

src/Application.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ final class Application
2323
{
2424
private static ContainerInterface $container;
2525

26-
private function __construct()
27-
{
28-
}
26+
private function __construct() {}
2927

3028
/**
3129
* @throws Throwable
@@ -40,8 +38,6 @@ public static function boot(): Application
4038
}
4139

4240
/**
43-
* @param OutputInterface $output
44-
* @return Application
4541
* @throws ContainerExceptionInterface
4642
* @throws NotFoundExceptionInterface
4743
* @throws ReflectionException
@@ -85,6 +81,7 @@ private function init(): void
8581
$this->initDatabase($config['database']);
8682
$this->scanAttributes($config['app']['scanner']);
8783
$router = $this->getRouter($config['routes']);
84+
$router->middlewares($config['middlewares']);
8885
Application::getContainer()->add(Router::class, $router);
8986
}
9087

src/Attribute/Attributes/CMD.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@
77
use Attribute;
88

99
#[Attribute(Attribute::TARGET_CLASS)]
10-
class CMD
11-
{
12-
}
10+
class CMD {}

src/Commands/StartCommand.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ private function roadrunnerStart(OutputInterface $output, ServerConfig $serverCo
7070
}
7171

7272
/**
73-
* @param OutputInterface $output
74-
* @return int
7573
* @throws ContainerExceptionInterface
7674
* @throws NotFoundExceptionInterface
7775
* @throws ReflectionException
@@ -84,8 +82,6 @@ private function workermanStart(OutputInterface $output): int
8482
}
8583

8684
/**
87-
* @param OutputInterface $output
88-
* @return int
8985
* @throws ContainerExceptionInterface
9086
* @throws NotFoundExceptionInterface
9187
* @throws ReflectionException

src/Commands/SwaggerGenerateCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
declare(strict_types=1);
34

45
namespace MicroPHP\Framework\Commands;
@@ -16,11 +17,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
1617
{
1718
Swagger::gen();
1819
$output->writeln('<info>swagger was generated</info>');
20+
1921
return Command::SUCCESS;
2022
}
2123

2224
protected function configure(): void
2325
{
2426
$this->setName('swagger:gen')->setDescription('Generate swagger file');
2527
}
26-
}
28+
}

src/Config/Config.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ public static function get(string $key, $default = null)
2626
}
2727

2828
/**
29-
* @param mixed $directory
30-
*
3129
* @throws ReflectionException
3230
*/
33-
public static function load($directory): array
31+
public static function load(string $directory): array
3432
{
3533
static::$config = (new ConfigProviderScanner())->scan();
3634

src/Container/Container.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
namespace MicroPHP\Framework\Container;
66

7-
class Container extends \League\Container\Container implements ContainerInterface
8-
{
9-
}
7+
class Container extends \League\Container\Container implements ContainerInterface {}

src/Container/ContainerInterface.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,4 @@
66

77
use League\Container\DefinitionContainerInterface;
88

9-
interface ContainerInterface extends DefinitionContainerInterface
10-
{
11-
}
9+
interface ContainerInterface extends DefinitionContainerInterface {}

src/Controller.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,9 @@
66

77
use JsonSerializable;
88
use MicroPHP\Framework\Http\Response;
9-
use Respect\Validation\Validator;
109

1110
class Controller
1211
{
13-
public function validate(array $rules, array $data): void
14-
{
15-
Validator::allOf(...$rules)->assert($data);
16-
}
17-
1812
protected function json(mixed $data): Response
1913
{
2014
if ($data instanceof JsonSerializable) {

src/Dto/ConstraintsTrait.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MicroPHP\Framework\Dto;
6+
7+
use MicroPHP\Data\Util\Str;
8+
use MicroPHP\Framework\Dto\Request\BaseReq;
9+
use MicroPHP\Framework\Exception\ValidateException;
10+
use Symfony\Component\Validator\Constraint;
11+
use Symfony\Component\Validator\Constraints\NotNull;
12+
use Symfony\Component\Validator\ConstraintViolationListInterface;
13+
use Symfony\Component\Validator\Mapping\ClassMetadata;
14+
use Symfony\Component\Validator\Validation;
15+
16+
trait ConstraintsTrait
17+
{
18+
/**
19+
* @var null|array<string, Constraint>
20+
*/
21+
protected static ?array $_constraints = null;
22+
23+
/**
24+
* 添加默认验证器
25+
*/
26+
public function _initConstraints(): void
27+
{
28+
if (is_null($this::$_constraints)) {
29+
$attributeRules = [];
30+
foreach ($this->getStaticReflection()->getProperties() as $property) {
31+
if ($this->isInsideProperty($property)) {
32+
continue;
33+
}
34+
$snakePropertyName = Str::snake($property->getName());
35+
if (! $property->getType()->allowsNull() && ! $property->hasDefaultValue()) {
36+
$attributeRules[$snakePropertyName][] = [new NotNull()];
37+
}
38+
}
39+
$this::$_constraints = $attributeRules;
40+
}
41+
}
42+
43+
/**
44+
* 自定义验证器
45+
*/
46+
public static function loadValidatorMetadata(?ClassMetadata $metadata = null): void
47+
{
48+
foreach (static::$_constraints ?: [] as $property => $constraints) {
49+
$metadata->addPropertyConstraint($property, $constraints);
50+
}
51+
}
52+
53+
/**
54+
* @throws ValidateException
55+
*/
56+
protected function validate(BaseReq $data): void
57+
{
58+
$data->_initConstraints();
59+
$validator = Validation::createValidatorBuilder()
60+
->enableAttributeMapping()
61+
->getValidator();
62+
$this->throw($validator->validate($data));
63+
$this->throw($validator->validate($data));
64+
}
65+
66+
/**
67+
* @throws ValidateException
68+
*/
69+
protected function throw(ConstraintViolationListInterface $errors): void
70+
{
71+
foreach ($errors as $violation) {
72+
throw new ValidateException($violation->getPropertyPath() . ': ' . $violation->getMessage() . PHP_EOL);
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)