Commit b2a1aec arutyunyan
committed
1 parent cc57c86 commit b2a1aec Copy full SHA for b2a1aec
File tree 4 files changed +68
-6
lines changed
4 files changed +68
-6
lines changed Original file line number Diff line number Diff line change 1
1
# Change log
2
2
3
+ ## v7.3.5 - June 8, 2022
4
+
5
+ - Чтение аннотаций сервиса
6
+
7
+ ---
8
+
3
9
## v7.3.4 - June 8, 2022
4
10
5
11
- Добавил поддержку SuQL Service
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace suql \annotation ;
4
+
5
+ /**
6
+ * Разбор аннотаций параметров сервиса
7
+ *
8
+ * @author sagittaracc <sagittaracc@gmail.com>
9
+ */
10
+ class ServiceAnnotation
11
+ {
12
+ /**
13
+ * @const string регулярное выражение для парсинга аннотации
14
+ */
15
+ const REGEX = '/#\s*\[Request\(uri="(?<uri>.*?)"\s*,\s*method="(?<method>\w+)"\)\]/msi ' ;
16
+ /**
17
+ * @var string
18
+ */
19
+ public $ uri ;
20
+ /**
21
+ * @var string
22
+ */
23
+ public $ method ;
24
+ /**
25
+ * @var string из какой модели читать аннотацию
26
+ */
27
+ private $ modelNameToReadFrom ;
28
+ /**
29
+ * Задает из какой модели читать аннотацию
30
+ * @param string $modelName имя класса модели
31
+ * @return self
32
+ */
33
+ public static function from ($ modelName )
34
+ {
35
+ $ instance = new static ();
36
+ $ instance ->modelNameToReadFrom = $ modelName ;
37
+ return $ instance ;
38
+ }
39
+ /**
40
+ * Разбор запрошенной аннотации
41
+ * @return self
42
+ */
43
+ public function read ()
44
+ {
45
+ $ model = new \ReflectionClass ($ this ->modelNameToReadFrom );
46
+ $ file = file_get_contents ($ model ->getFileName ());
47
+
48
+ preg_match (static ::REGEX , $ file , $ matches );
49
+
50
+ if (!empty ($ matches )) {
51
+ $ this ->uri = $ matches ['uri ' ];
52
+ $ this ->method = $ matches ['method ' ];
53
+ }
54
+
55
+ return $ this ;
56
+ }
57
+ }
Original file line number Diff line number Diff line change 3
3
namespace suql \syntax \entity ;
4
4
5
5
use GuzzleHttp \Client ;
6
+ use suql \annotation \ServiceAnnotation ;
6
7
use suql \syntax \ServiceInterface ;
7
8
8
9
abstract class SuQLService extends SuQLArray implements ServiceInterface
@@ -32,8 +33,9 @@ public static function find($body = [])
32
33
{
33
34
$ instance = new static ();
34
35
35
- // $instance->uri = read from annotation
36
- // $instance->method = read from annotation
36
+ $ annotation = ServiceAnnotation::from ($ instance )->read ();
37
+ $ instance ->uri = $ annotation ->uri ;
38
+ $ instance ->method = $ annotation ->method ;
37
39
$ instance ->uri = $ instance ->method === 'GET ' ? $ instance ->uri . '? ' . http_build_query ($ body ) : $ instance ->uri ;
38
40
$ instance ->body = $ instance ->method === 'POST ' ? $ body : [];
39
41
Original file line number Diff line number Diff line change 5
5
use suql \db \Container ;
6
6
use suql \syntax \entity \SuQLService ;
7
7
8
+ # [Request(uri="http://jsonplaceholder.typicode.com/posts", method="GET")]
8
9
class Query27 extends SuQLService
9
10
{
10
- protected $ uri = 'http://jsonplaceholder.typicode.com/posts ' ;
11
- protected $ method = 'GET ' ;
12
- protected $ body = [];
13
-
14
11
public function getDb ()
15
12
{
16
13
return Container::get ('db_test ' );
You can’t perform that action at this time.
0 commit comments