Skip to content

Commit b2a1aec

Browse files
author
arutyunyan
committed
Чтение аннотаций сервиса
1 parent cc57c86 commit b2a1aec

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change log
22

3+
## v7.3.5 - June 8, 2022
4+
5+
- Чтение аннотаций сервиса
6+
7+
---
8+
39
## v7.3.4 - June 8, 2022
410

511
- Добавил поддержку SuQL Service

src/annotation/ServiceAnnotation.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}

src/syntax/entity/SuQLService.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace suql\syntax\entity;
44

55
use GuzzleHttp\Client;
6+
use suql\annotation\ServiceAnnotation;
67
use suql\syntax\ServiceInterface;
78

89
abstract class SuQLService extends SuQLArray implements ServiceInterface
@@ -32,8 +33,9 @@ public static function find($body = [])
3233
{
3334
$instance = new static();
3435

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;
3739
$instance->uri = $instance->method === 'GET' ? $instance->uri . '?' . http_build_query($body) : $instance->uri;
3840
$instance->body = $instance->method === 'POST' ? $body : [];
3941

tests/suql/models/Query27.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@
55
use suql\db\Container;
66
use suql\syntax\entity\SuQLService;
77

8+
# [Request(uri="http://jsonplaceholder.typicode.com/posts", method="GET")]
89
class Query27 extends SuQLService
910
{
10-
protected $uri = 'http://jsonplaceholder.typicode.com/posts';
11-
protected $method = 'GET';
12-
protected $body = [];
13-
1411
public function getDb()
1512
{
1613
return Container::get('db_test');

0 commit comments

Comments
 (0)