Skip to content

Commit 0d13f96

Browse files
authored
tl2php now generates RpcFunction serialization method stubs (#1403)
1 parent ba9a700 commit 0d13f96

18 files changed

Lines changed: 795 additions & 4 deletions

File tree

common/tl2php/gen-php-code.cpp

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <exception>
1111
#include <fstream>
1212
#include <ftw.h>
13+
#include <iomanip>
1314
#include <iterator>
1415
#include <map>
1516
#include <sstream>
@@ -326,12 +327,57 @@ struct FunctionGetTLFunctionName {
326327
const PhpClassRepresentation& class_repr;
327328

328329
friend std::ostream& operator<<(std::ostream& os, const FunctionGetTLFunctionName& self) {
330+
// we will remove ALL tl2php code soon, so we added a bit hackish code here
331+
const char* header1 = R"( /**
332+
* @kphp-inline
333+
*
334+
* @return TL\RpcFunctionFetcher
335+
*/
336+
public function typedStore())";
337+
const char* header2 = R"( /**
338+
* @kphp-inline
339+
*
340+
* @return TL\RpcFunctionFetcher
341+
*/
342+
public function typedFetch())";
343+
const char* header_magic = R"( /**
344+
* @kphp-inline
345+
*
346+
* @return int
347+
*/
348+
public function getTLFunctionMagic())";
349+
const char* body_null = R"({
350+
return null;
351+
}
352+
353+
)";
354+
os << header_magic;
355+
if (self.class_repr.is_interface) {
356+
os << ";" << std::endl;
357+
} else {
358+
std::stringstream ss;
359+
ss << std::hex << std::setw(8) << std::setfill('0') << self.class_repr.magic_id;
360+
os << " {" << std::endl << " return 0x" << ss.str() << ";" << std::endl << " }" << SkipLine{};
361+
}
329362
os << FunctionDeclaration{"getTLFunctionName", {}, "string", has_kphp_inline};
330363
if (self.class_repr.is_interface) {
331-
return os << ";" << std::endl;
364+
os << ";" << std::endl;
365+
} else {
366+
os << " {" << std::endl << " return '" << self.class_repr.tl_name << "';" << std::endl << " }" << SkipLine{};
332367
}
333-
334-
return os << " {" << std::endl << " return '" << self.class_repr.tl_name << "';" << std::endl << " }" << SkipLine{};
368+
os << header1;
369+
if (self.class_repr.is_interface) {
370+
os << ";" << std::endl;
371+
} else {
372+
os << body_null;
373+
}
374+
os << header2;
375+
if (self.class_repr.is_interface) {
376+
os << ";" << std::endl;
377+
} else {
378+
os << body_null;
379+
}
380+
return os;
335381
}
336382
};
337383

common/tl2php/php-classes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct PhpClassField {
3333

3434
struct PhpClassRepresentation {
3535
std::string tl_name;
36+
int magic_id{};
3637
std::string php_class_namespace;
3738
std::string php_class_name;
3839
const PhpClassRepresentation* parent{nullptr};

common/tl2php/tl-to-php-classes-converter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ std::unique_ptr<PhpClassRepresentation> TlToPhpClassesConverter::make_and_regist
152152
php_repr->class_fields = std::move(fields);
153153
php_repr->is_interface = false;
154154
php_repr->tl_name = name.tl_name;
155+
php_repr->magic_id = magic_id;
155156
const auto emplaced = php_classes_.all_classes.emplace(name.class_full_name, *php_repr).second;
156157
assert(emplaced);
157158
php_classes_.magic_to_classes.emplace(magic_id, *php_repr);

tests/phpt/modulite/007_composer_ok/packages/vk-rpc/src/rpc_global.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,28 @@ function globalFunctionInVkRpc() {}
66

77
if (0) {
88
class my_tl_f implements \VK\TL\RpcFunction {
9-
function getTLFunctionName(): string { return "my_tl_f"; }
9+
public function getTLFunctionName(): string { return "my_tl_f"; }
10+
public function getTLFunctionMagic(): int {
11+
return 0x12345678;
12+
}
13+
14+
/**
15+
* @kphp-inline
16+
*
17+
* @return VK\TL\RpcFunctionFetcher
18+
*/
19+
public function typedStore() {
20+
return null;
21+
}
22+
23+
/**
24+
* @kphp-inline
25+
*
26+
* @return VK\TL\RpcFunctionFetcher
27+
*/
28+
public function typedFetch() {
29+
return null;
30+
}
1031
}
1132
}
1233

tests/python/tests/rpc/php/VK/TL/RpcFunction.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,30 @@
1515
*/
1616
interface RpcFunction {
1717

18+
/**
19+
* @kphp-inline
20+
*
21+
* @return int
22+
*/
23+
public function getTLFunctionMagic();
1824
/**
1925
* @kphp-inline
2026
*
2127
* @return string
2228
*/
2329
public function getTLFunctionName();
30+
/**
31+
* @kphp-inline
32+
*
33+
* @return TL\RpcFunctionFetcher
34+
*/
35+
public function typedStore();
36+
/**
37+
* @kphp-inline
38+
*
39+
* @return TL\RpcFunctionFetcher
40+
*/
41+
public function typedFetch();
2442
}
2543

2644
/**

tests/python/tests/rpc/php/VK/TL/_common/Functions/rpcDestActor.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ public static function result(TL\RpcResponse $response) {
5555
return self::functionReturnValue($response->getResult());
5656
}
5757

58+
/**
59+
* @kphp-inline
60+
*
61+
* @return int
62+
*/
63+
public function getTLFunctionMagic() {
64+
return 0x7568aabd;
65+
}
66+
5867
/**
5968
* @kphp-inline
6069
*
@@ -64,6 +73,24 @@ public function getTLFunctionName() {
6473
return 'rpcDestActor';
6574
}
6675

76+
/**
77+
* @kphp-inline
78+
*
79+
* @return TL\RpcFunctionFetcher
80+
*/
81+
public function typedStore(){
82+
return null;
83+
}
84+
85+
/**
86+
* @kphp-inline
87+
*
88+
* @return TL\RpcFunctionFetcher
89+
*/
90+
public function typedFetch(){
91+
return null;
92+
}
93+
6794
}
6895

6996
/**

tests/python/tests/rpc/php/VK/TL/_common/Functions/rpcDestActorFlags.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,15 @@ public static function result(TL\RpcResponse $response) {
6565
return self::functionReturnValue($response->getResult());
6666
}
6767

68+
/**
69+
* @kphp-inline
70+
*
71+
* @return int
72+
*/
73+
public function getTLFunctionMagic() {
74+
return 0xf0a5acf7;
75+
}
76+
6877
/**
6978
* @kphp-inline
7079
*
@@ -74,6 +83,24 @@ public function getTLFunctionName() {
7483
return 'rpcDestActorFlags';
7584
}
7685

86+
/**
87+
* @kphp-inline
88+
*
89+
* @return TL\RpcFunctionFetcher
90+
*/
91+
public function typedStore(){
92+
return null;
93+
}
94+
95+
/**
96+
* @kphp-inline
97+
*
98+
* @return TL\RpcFunctionFetcher
99+
*/
100+
public function typedFetch(){
101+
return null;
102+
}
103+
77104
}
78105

79106
/**

tests/python/tests/rpc/php/VK/TL/_common/Functions/rpcDestFlags.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ public static function result(TL\RpcResponse $response) {
6060
return self::functionReturnValue($response->getResult());
6161
}
6262

63+
/**
64+
* @kphp-inline
65+
*
66+
* @return int
67+
*/
68+
public function getTLFunctionMagic() {
69+
return 0xe352035e;
70+
}
71+
6372
/**
6473
* @kphp-inline
6574
*
@@ -69,6 +78,24 @@ public function getTLFunctionName() {
6978
return 'rpcDestFlags';
7079
}
7180

81+
/**
82+
* @kphp-inline
83+
*
84+
* @return TL\RpcFunctionFetcher
85+
*/
86+
public function typedStore(){
87+
return null;
88+
}
89+
90+
/**
91+
* @kphp-inline
92+
*
93+
* @return TL\RpcFunctionFetcher
94+
*/
95+
public function typedFetch(){
96+
return null;
97+
}
98+
7299
}
73100

74101
/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* AUTOGENERATED, DO NOT EDIT! If you want to modify it, check tl schema.
5+
*
6+
* This autogenerated code represents tl class for typed RPC API.
7+
*/
8+
9+
namespace VK\TL\_common\Types;
10+
11+
/**
12+
* @kphp-tl-class
13+
*/
14+
class boolStat {
15+
16+
/** @var int */
17+
public $statTrue = 0;
18+
19+
/** @var int */
20+
public $statFalse = 0;
21+
22+
/** @var int */
23+
public $statUnknown = 0;
24+
25+
/**
26+
* @param int $statTrue
27+
* @param int $statFalse
28+
* @param int $statUnknown
29+
*/
30+
public function __construct($statTrue = 0, $statFalse = 0, $statUnknown = 0) {
31+
$this->statTrue = $statTrue;
32+
$this->statFalse = $statFalse;
33+
$this->statUnknown = $statUnknown;
34+
}
35+
36+
}

0 commit comments

Comments
 (0)