Skip to content

Commit f5c94fc

Browse files
committed
Refactor public API
1 parent 9ea2b38 commit f5c94fc

File tree

257 files changed

+7214
-8167
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

257 files changed

+7214
-8167
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ madeline.php
101101
composer.lock
102102
b.php
103103
telegram-cli*
104-
src/danog/MadelineProto/Fuzzer.php
104+
src/Fuzzer.php
105105
fuzzer.php
106106
tests/500mb
107107
*.save

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
],
8787
"autoload": {
8888
"psr-4": {
89-
"danog\\MadelineProto\\": "src/danog/MadelineProto"
89+
"danog\\MadelineProto\\": "src"
9090
},
9191
"files": [
9292
"src/polyfill.php",

examples/bot.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ public function onUpdateNewMessage(array $update): void
113113
// Chat id
114114
$id = $this->getId($update);
115115

116-
// You can also use the built-in MadelineProto MySQL async driver!
116+
// In this example code, send the "This userbot is powered by MadelineProto!" message only once per chat.
117+
// Ignore all further messages coming from this chat.
117118
if (!isset($this->notifiedChats[$id])) {
118119
$this->notifiedChats[$id] = true;
119120

@@ -139,17 +140,19 @@ public function onUpdateNewMessage(array $update): void
139140
}
140141
}
141142

143+
// Test MadelineProto's built-in database driver, which automatically maps to MySQL/PostgreSQL/Redis
144+
// properties mentioned in the MyEventHandler::$dbProperties property!
145+
142146
// Can be anything serializable: an array, an int, an object, ...
143147
$myData = [];
144148

145-
if (isset($this->dataStoredOnDb['yourKey'])) {
146-
// Always when fetching data
147-
$myData = $this->dataStoredOnDb['yourKey'];
149+
if (isset($this->dataStoredOnDb['k1'])) {
150+
$myData = $this->dataStoredOnDb['k1'];
148151
}
149-
$this->dataStoredOnDb['yourKey'] = $myData + ['moreStuff' => 'yay'];
152+
$this->dataStoredOnDb['k1'] = $myData + ['moreStuff' => 'yay'];
150153

151-
$this->dataStoredOnDb['otherKey'] = 0;
152-
unset($this->dataStoredOnDb['otherKey']);
154+
$this->dataStoredOnDb['k2'] = 0;
155+
unset($this->dataStoredOnDb['k2']);
153156

154157
$this->logger("Count: ".count($this->dataStoredOnDb));
155158

examples/secret_bot.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public function onUpdateNewEncryptedMessage(array $update): void
308308
while ($i < 10) {
309309
$this->logger("SENDING MESSAGE $i TO ".$update['message']['chat_id']);
310310
// You can also use the sendEncrypted parameter for more options in secret chats
311-
$this->messages->sendMessage(['peer' => $update, 'message' => (string) ($i++)]);
311+
$this->messages->sendMessage(peer: $update, message: (string) ($i++));
312312
}
313313
$this->sent[$update['message']['chat_id']] = true;
314314
}

psalm-baseline.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<files psalm-version="dev-master@">
3-
<file src="src/danog/MadelineProto/Conversion.php">
3+
<file src="src/Conversion.php">
44
<UndefinedDocblockClass occurrences="1">
55
<code>$MadelineProto-&gt;help-&gt;getConfig()</code>
66
</UndefinedDocblockClass>
77
</file>
8-
<file src="src/danog/MadelineProto/Db/DriverArray.php">
8+
<file src="src/Db/DriverArray.php">
99
<UndefinedMethod occurrences="1">
1010
<code>getName</code>
1111
</UndefinedMethod>
1212
</file>
13-
<file src="src/danog/MadelineProto/Db/MemoryArray.php">
13+
<file src="src/Db/MemoryArray.php">
1414
<MethodSignatureMismatch occurrences="2">
1515
<code>MemoryArray</code>
1616
<code>MemoryArray</code>
1717
</MethodSignatureMismatch>
1818
</file>
19-
<file src="src/danog/MadelineProto/Stream/Common/UdpBufferedStream.php">
19+
<file src="src/Stream/Common/UdpBufferedStream.php">
2020
<InvalidPropertyAssignmentValue occurrences="1">
2121
<code>$ctx-&gt;getStream($header)</code>
2222
</InvalidPropertyAssignmentValue>

psalm.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
<directory name="src" />
1212
<ignoreFiles>
1313
<directory name="vendor" />
14-
<file name="src/danog/MadelineProto/InternalDoc.php" />
1514
</ignoreFiles>
1615
</projectFiles>
1716
<issueHandlers>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
/**
4646
* Main API wrapper for MadelineProto.
4747
*/
48-
final class API extends InternalDoc
48+
final class API extends AbstractAPI
4949
{
5050
/**
5151
* Release version.
@@ -176,7 +176,7 @@ public function __construct(string $session, array|SettingsAbstract $settings =
176176
/**
177177
* Reconnect to full instance.
178178
*/
179-
protected function reconnectFull()
179+
protected function reconnectFull(): bool
180180
{
181181
if ($this->wrapper->getAPI() instanceof Client) {
182182
$this->wrapper->logger('Restarting to full instance...');

src/danog/MadelineProto/AbstractAPIFactory.php renamed to src/AbstractAPI.php

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,10 @@
2222

2323
use Amp\Future\UnhandledFutureError;
2424
use Amp\SignalException;
25-
use InvalidArgumentException;
2625
use Revolt\EventLoop;
2726

28-
abstract class AbstractAPIFactory
27+
abstract class AbstractAPI extends InternalDoc
2928
{
30-
/**
31-
* Namespace.
32-
*
33-
* @internal
34-
*/
35-
private string $namespace = '';
36-
37-
/**
38-
* API wrapper (to avoid circular references).
39-
*/
40-
protected APIWrapper $wrapper;
41-
42-
/**
43-
* Export APIFactory instance with the specified namespace.
44-
*/
45-
protected function exportNamespaces(): void
46-
{
47-
$class = \array_reverse(\array_values(\class_parents(static::class)))[1];
48-
49-
foreach (\get_class_vars(APIFactory::class) as $key => $var) {
50-
if (\in_array($key, ['namespace', 'methods', 'wrapper'])) {
51-
continue;
52-
}
53-
$instance = new $class;
54-
$instance->namespace = $key.'.';
55-
$instance->wrapper = $this->wrapper;
56-
$this->{$key} = $instance;
57-
}
58-
}
5929
/**
6030
* Enable or disable async.
6131
*
@@ -64,29 +34,6 @@ protected function exportNamespaces(): void
6434
public function async(bool $async): void
6535
{
6636
}
67-
/**
68-
* Call async wrapper function.
69-
*
70-
* @param string $name Method name
71-
* @param array $arguments Arguments
72-
* @internal
73-
*/
74-
public function __call(string $name, array $arguments)
75-
{
76-
if ($arguments && !isset($arguments[0])) {
77-
$arguments = [$arguments];
78-
}
79-
80-
$name = $this->namespace.$name;
81-
$aargs = isset($arguments[1]) && \is_array($arguments[1]) ? $arguments[1] : [];
82-
$aargs['apifactory'] = true;
83-
$args = isset($arguments[0]) && \is_array($arguments[0]) ? $arguments[0] : [];
84-
if (isset($args[0]) && !isset($args['multiple'])) {
85-
throw new InvalidArgumentException('Parameter names must be provided!');
86-
}
87-
return $this->wrapper->getAPI()->methodCallAsyncRead($name, $args, $aargs);
88-
}
89-
9037
/**
9138
* Start MadelineProto and the event handler (enables async).
9239
*
@@ -130,6 +77,9 @@ protected function startAndLoopInternal(string $eventHandler): void
13077
}
13178
}
13279
}
80+
81+
abstract protected function reconnectFull(): bool;
82+
13383
private function startAndLoopLogic(string $eventHandler, bool &$started): void
13484
{
13585
$this->start();

0 commit comments

Comments
 (0)