Wiki source code of #[RPC\Info]
Last modified by Ashterix on 2024/05/19 21:27
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{box cssClass="floatinginfobox"}} | ||
2 | == (% style="display:block; margin-top:-30px; text-align:center" %)Summary(%%) == | ||
3 | |||
4 | (% style="margin-right:auto" %) | ||
5 | |**Classname**|(% colspan="2" rowspan="1" %)Info | ||
6 | |**Namespace**|(% colspan="2" %)Ufo\RpcObject\RPC | ||
7 | |**Target**|(% colspan="2" rowspan="1" style="width:79px" %)class | ||
8 | |(% colspan="3" %)**Arguments:** | ||
9 | |(% colspan="1" rowspan="3" %)**$alias**|**type**|string | ||
10 | |**optional**|true | ||
11 | |**default**|null | ||
12 | |(% colspan="1" rowspan="3" %)**$concat**|**type**|string | ||
13 | |**optional**|true | ||
14 | |**default**|'.' | ||
15 | {{/box}} | ||
16 | |||
17 | = Class Aliases = | ||
18 | |||
19 | Sometimes it may be necessary to set an alternative name for an API method, for example, if you have a very long class name due to some standards or naming conventions in your project. | ||
20 | |||
21 | In such cases, you can add the attribute {{code language="none"}}#[RPC\Info]{{/code}} to your class, specifying an alias for the class. This will instruct the RPC server to associate this alias with the class. | ||
22 | |||
23 | {{code language="php" layout="LINENUMBERS"}} | ||
24 | <?php | ||
25 | namespace App\Api\Procedures; | ||
26 | |||
27 | use Ufo\RpcObject\RPC; | ||
28 | |||
29 | #[RPC\Info(alias: 'users')] | ||
30 | class MySpecificApiUserServiceWithLongClassName implements IRpcService | ||
31 | { | ||
32 | public function getList(): void {} | ||
33 | } | ||
34 | {{/code}} | ||
35 | |||
36 | In this case, the API method {{code language="none"}}users.getList{{/code}} will be available. | ||
37 | |||
38 | {{warning}} | ||
39 | Note that the original naming of the method will be removed from the ServiceMap and will no longer be accessible. | ||
40 | |||
41 | That is, //**MySpecificApiUserServiceWithLongClassName.getList**// will no longer work! | ||
42 | {{/warning}} | ||
43 | |||
44 | = Concatenation Symbol = | ||
45 | |||
46 | The Info attribute also accepts a second argument {{code language="none"}}concat{{/code}}, which specifies the character or characters that join the class name and method name when generating the ServiceMap for the RPC server. | ||
47 | |||
48 | The default value is "." (dot), but you can easily change it by specifying the symbol or characters you need. | ||
49 | |||
50 | {{code language="php" layout="LINENUMBERS"}} | ||
51 | <?php | ||
52 | namespace App\Api\Procedures; | ||
53 | |||
54 | use Ufo\RpcObject\RPC; | ||
55 | |||
56 | #[RPC\Info(alias: 'users', concat: '>')] | ||
57 | class MySpecificApiUserServiceWithLongClassName implements IRpcService | ||
58 | { | ||
59 | public function getList(): void {} | ||
60 | } | ||
61 | {{/code}} | ||
62 | |||
63 | The example code above will result in the API method {{code language="none"}}users>getList{{/code}} being available. | ||
64 | |||
65 | = Caveats = | ||
66 | |||
67 | {{warning}} | ||
68 | (% id="cke_bm_754S" style="display:none" %) (%%)NOTE that there is no additional validation regarding the use of aliases or concatenation symbols, so your actions could lead to the aliasing of another existing class. | ||
69 | |||
70 | For example, if in your application there is already a class {{code language="none"}}Foo{{/code}} with the method {{code language="none"}}bar{{/code}} added to the API as the method {{code language="none"}}Foo.bar{{/code}}, and you create a class {{code language="none"}}Baz{{/code}} (% id="cke_bm_372757S" style="display:none" %) (%%)with the method {{code language="none"}}bar{{/code}}(% id="cke_bm_372757E" style="display:none" %) (%%) and set its alias to {{code language="none"}}Foo{{/code}}, this will lead to the registration of two API services with the same name. The outcome of these actions is unpredictable. | ||
71 | {{/warning}} | ||
72 | |||
73 | {{error}} | ||
74 | By using this mechanism, you act at your own risk! | ||
75 | {{/error}} | ||
76 | |||
77 | = In future = | ||
78 | |||
79 | In the future, we plan to implement the ability to set aliases for each specific API method, not just classes. |