Skip to content

Commit

Permalink
Refactor public API
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jan 27, 2023
1 parent 9ea2b38 commit f5c94fc
Show file tree
Hide file tree
Showing 257 changed files with 7,214 additions and 8,167 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ madeline.php
composer.lock
b.php
telegram-cli*
src/danog/MadelineProto/Fuzzer.php
src/Fuzzer.php
fuzzer.php
tests/500mb
*.save
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
],
"autoload": {
"psr-4": {
"danog\\MadelineProto\\": "src/danog/MadelineProto"
"danog\\MadelineProto\\": "src"
},
"files": [
"src/polyfill.php",
Expand Down
17 changes: 10 additions & 7 deletions examples/bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public function onUpdateNewMessage(array $update): void
// Chat id
$id = $this->getId($update);

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

Expand All @@ -139,17 +140,19 @@ public function onUpdateNewMessage(array $update): void
}
}

// Test MadelineProto's built-in database driver, which automatically maps to MySQL/PostgreSQL/Redis
// properties mentioned in the MyEventHandler::$dbProperties property!

// Can be anything serializable: an array, an int, an object, ...
$myData = [];

if (isset($this->dataStoredOnDb['yourKey'])) {
// Always when fetching data
$myData = $this->dataStoredOnDb['yourKey'];
if (isset($this->dataStoredOnDb['k1'])) {
$myData = $this->dataStoredOnDb['k1'];
}
$this->dataStoredOnDb['yourKey'] = $myData + ['moreStuff' => 'yay'];
$this->dataStoredOnDb['k1'] = $myData + ['moreStuff' => 'yay'];

$this->dataStoredOnDb['otherKey'] = 0;
unset($this->dataStoredOnDb['otherKey']);
$this->dataStoredOnDb['k2'] = 0;
unset($this->dataStoredOnDb['k2']);

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

Expand Down
2 changes: 1 addition & 1 deletion examples/secret_bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public function onUpdateNewEncryptedMessage(array $update): void
while ($i < 10) {
$this->logger("SENDING MESSAGE $i TO ".$update['message']['chat_id']);
// You can also use the sendEncrypted parameter for more options in secret chats
$this->messages->sendMessage(['peer' => $update, 'message' => (string) ($i++)]);
$this->messages->sendMessage(peer: $update, message: (string) ($i++));
}
$this->sent[$update['message']['chat_id']] = true;
}
Expand Down
8 changes: 4 additions & 4 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="dev-master@">
<file src="src/danog/MadelineProto/Conversion.php">
<file src="src/Conversion.php">
<UndefinedDocblockClass occurrences="1">
<code>$MadelineProto-&gt;help-&gt;getConfig()</code>
</UndefinedDocblockClass>
</file>
<file src="src/danog/MadelineProto/Db/DriverArray.php">
<file src="src/Db/DriverArray.php">
<UndefinedMethod occurrences="1">
<code>getName</code>
</UndefinedMethod>
</file>
<file src="src/danog/MadelineProto/Db/MemoryArray.php">
<file src="src/Db/MemoryArray.php">
<MethodSignatureMismatch occurrences="2">
<code>MemoryArray</code>
<code>MemoryArray</code>
</MethodSignatureMismatch>
</file>
<file src="src/danog/MadelineProto/Stream/Common/UdpBufferedStream.php">
<file src="src/Stream/Common/UdpBufferedStream.php">
<InvalidPropertyAssignmentValue occurrences="1">
<code>$ctx-&gt;getStream($header)</code>
</InvalidPropertyAssignmentValue>
Expand Down
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
<file name="src/danog/MadelineProto/InternalDoc.php" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
Expand Down
4 changes: 2 additions & 2 deletions src/danog/MadelineProto/API.php → src/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
/**
* Main API wrapper for MadelineProto.
*/
final class API extends InternalDoc
final class API extends AbstractAPI
{
/**
* Release version.
Expand Down Expand Up @@ -176,7 +176,7 @@ public function __construct(string $session, array|SettingsAbstract $settings =
/**
* Reconnect to full instance.
*/
protected function reconnectFull()
protected function reconnectFull(): bool
{
if ($this->wrapper->getAPI() instanceof Client) {
$this->wrapper->logger('Restarting to full instance...');
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,10 @@

use Amp\Future\UnhandledFutureError;
use Amp\SignalException;
use InvalidArgumentException;
use Revolt\EventLoop;

abstract class AbstractAPIFactory
abstract class AbstractAPI extends InternalDoc
{
/**
* Namespace.
*
* @internal
*/
private string $namespace = '';

/**
* API wrapper (to avoid circular references).
*/
protected APIWrapper $wrapper;

/**
* Export APIFactory instance with the specified namespace.
*/
protected function exportNamespaces(): void
{
$class = \array_reverse(\array_values(\class_parents(static::class)))[1];

foreach (\get_class_vars(APIFactory::class) as $key => $var) {
if (\in_array($key, ['namespace', 'methods', 'wrapper'])) {
continue;
}
$instance = new $class;
$instance->namespace = $key.'.';
$instance->wrapper = $this->wrapper;
$this->{$key} = $instance;
}
}
/**
* Enable or disable async.
*
Expand All @@ -64,29 +34,6 @@ protected function exportNamespaces(): void
public function async(bool $async): void
{
}
/**
* Call async wrapper function.
*
* @param string $name Method name
* @param array $arguments Arguments
* @internal
*/
public function __call(string $name, array $arguments)
{
if ($arguments && !isset($arguments[0])) {
$arguments = [$arguments];
}

$name = $this->namespace.$name;
$aargs = isset($arguments[1]) && \is_array($arguments[1]) ? $arguments[1] : [];
$aargs['apifactory'] = true;
$args = isset($arguments[0]) && \is_array($arguments[0]) ? $arguments[0] : [];
if (isset($args[0]) && !isset($args['multiple'])) {
throw new InvalidArgumentException('Parameter names must be provided!');
}
return $this->wrapper->getAPI()->methodCallAsyncRead($name, $args, $aargs);
}

/**
* Start MadelineProto and the event handler (enables async).
*
Expand Down Expand Up @@ -130,6 +77,9 @@ protected function startAndLoopInternal(string $eventHandler): void
}
}
}

abstract protected function reconnectFull(): bool;

private function startAndLoopLogic(string $eventHandler, bool &$started): void
{
$this->start();
Expand Down
Loading

0 comments on commit f5c94fc

Please sign in to comment.