Skip to content

Commit

Permalink
Remove legacy array settings
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Aug 13, 2023
1 parent bca492e commit aeb9182
Show file tree
Hide file tree
Showing 29 changed files with 20 additions and 402 deletions.
2 changes: 1 addition & 1 deletion docs
Submodule docs updated 36 files
+10 −8 docs/API_docs/methods/index.md
+76 −47 docs/PHP/danog/MadelineProto/API.md
+3 −3 docs/PHP/danog/MadelineProto/Conversion.md
+0 −210 docs/PHP/danog/MadelineProto/Db/DbArray.md
+103 −5 docs/PHP/danog/MadelineProto/Db/DbType.md
+73 −44 docs/PHP/danog/MadelineProto/EventHandler.md
+3 −15 docs/PHP/danog/MadelineProto/MyTelegramOrgWrapper.md
+79 −0 docs/PHP/danog/MadelineProto/Ogg.md
+73 −44 docs/PHP/danog/MadelineProto/PluginEventHandler.md
+0 −12 docs/PHP/danog/MadelineProto/Settings/AppInfo.md
+0 −12 docs/PHP/danog/MadelineProto/Settings/Auth.md
+0 −12 docs/PHP/danog/MadelineProto/Settings/Connection.md
+12 −12 docs/PHP/danog/MadelineProto/Settings/Database/DriverDatabaseAbstract.md
+7 −0 docs/PHP/danog/MadelineProto/Settings/Database/Memory.md
+2 −7 docs/PHP/danog/MadelineProto/Settings/Database/Mysql.md
+2 −7 docs/PHP/danog/MadelineProto/Settings/Database/Postgres.md
+2 −7 docs/PHP/danog/MadelineProto/Settings/Database/Redis.md
+12 −12 docs/PHP/danog/MadelineProto/Settings/Database/SqlAbstract.md
+12 −0 docs/PHP/danog/MadelineProto/Settings/DatabaseAbstract.md
+0 −12 docs/PHP/danog/MadelineProto/Settings/Files.md
+0 −12 docs/PHP/danog/MadelineProto/Settings/Ipc.md
+0 −12 docs/PHP/danog/MadelineProto/Settings/Logger.md
+0 −12 docs/PHP/danog/MadelineProto/Settings/Peer.md
+0 −12 docs/PHP/danog/MadelineProto/Settings/SecretChats.md
+0 −12 docs/PHP/danog/MadelineProto/Settings/Serialization.md
+0 −12 docs/PHP/danog/MadelineProto/Settings/TLSchema.md
+73 −44 docs/PHP/danog/MadelineProto/SimpleEventHandler.md
+46 −0 docs/PHP/danog/MadelineProto/VoIP/CallState.md
+42 −0 docs/PHP/danog/MadelineProto/VoIP/DiscardReason.md
+24 −17 docs/PHP/danog/MadelineProto/VoIP/Endpoint.md
+46 −0 docs/PHP/danog/MadelineProto/VoIP/VoIPState.md
+5 −2 docs/PHP/index.md
+1 −3 docs/docs/CREATING_A_CLIENT.md
+31 −0 docs/docs/FILTERS.md
+17 −1 docs/docs/UPDATES.md
+35 −34 docs/index.md
6 changes: 3 additions & 3 deletions src/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,12 @@ public function setWebApiTemplate(string $template): void
* Constructor function.
*
* @param string $session Session name
* @param array|SettingsAbstract $settings Settings
* @param SettingsAbstract $settings Settings
*/
public function __construct(string $session, array|SettingsAbstract $settings = [])
public function __construct(string $session, ?SettingsAbstract $settings = null)
{
Magic::start(light: true);
$settings = Settings::parseFromLegacy($settings);
$settings ??= new SettingsEmpty;
$this->session = new SessionPaths($session);
$this->wrapper = new APIWrapper($this->session);
$this->exportNamespaces();
Expand Down
5 changes: 1 addition & 4 deletions src/AnnotationsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

use AssertionError;
use danog\ClassFinder\ClassFinder;
use danog\MadelineProto\Settings\TLSchema;
use danog\MadelineProto\TL\TL;
use ReflectionClass;
use ReflectionMethod;
Expand Down Expand Up @@ -56,9 +55,7 @@ public function __construct(Logger $logger, array $settings, array $reflectionCl
$this->namespace = $namespace;
/** @psalm-suppress InvalidArgument */
$this->TL = new TL();
$tlSchema = new TLSchema;
$tlSchema->mergeArray($settings);
$this->TL->init($tlSchema);
$this->TL->init($settings['TL']);
$this->blacklist = \json_decode(\file_get_contents(__DIR__.'/../docs/template/disallow.json'), true);
$this->blacklistHard = $this->blacklist;
unset($this->blacklistHard['messages.getHistory'], $this->blacklistHard['channels.getMessages'], $this->blacklistHard['messages.getMessages']);
Expand Down
8 changes: 6 additions & 2 deletions src/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ final class Conversion
*
* @param array<int, string> $authorization Authorization info
*/
public static function importAuthorization(array $authorization, int $main_dc_id, string $session, SettingsAbstract|array $settings): API
public static function importAuthorization(array $authorization, int $main_dc_id, string $session, ?SettingsAbstract $settings = null): API
{
$settings = Settings::parseFromLegacyFull($settings);
$settingsFull = new Settings;
if ($settings) {
$settingsFull->merge($settings);
}
$settings = $settingsFull;
$settings->getLogger()->setLevel(Logger::ULTRA_VERBOSE);
$settings->getAuth()->setPfs(true);
$MadelineProto = new API($session, $settings);
Expand Down
2 changes: 2 additions & 0 deletions src/Db/DbArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*
* @extends ArrayAccess<TKey, TValue>
* @extends DbType<TKey, TValue>
*
* @internal
*/
interface DbArray extends DbType, ArrayAccess
{
Expand Down
2 changes: 2 additions & 0 deletions src/Db/DbArrayTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

/**
* DB array trait.
*
* @internal
*/
trait DbArrayTrait
{
Expand Down
2 changes: 1 addition & 1 deletion src/Db/Driver/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
use Amp\Mysql\MysqlConfig;
use Amp\Mysql\MysqlConnectionPool;
use Amp\Sync\LocalKeyedMutex;
use danog\MadelineProto\Exception;
use danog\MadelineProto\Logger;
use danog\MadelineProto\Settings\Database\Mysql as DatabaseMysql;
use danog\MadelineProto\Exception;
use PDO;
use Throwable;

Expand Down
5 changes: 1 addition & 4 deletions src/DocsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

use danog\MadelineProto\DocsBuilder\Constructors;
use danog\MadelineProto\DocsBuilder\Methods;
use danog\MadelineProto\Settings\TLSchema;
use danog\MadelineProto\TL\TL;

// This code was written a few years ago: it is garbage, and has to be rewritten
Expand Down Expand Up @@ -56,9 +55,7 @@ public function __construct(Logger $logger, array $settings)
\set_error_handler(['\\danog\\MadelineProto\\Exception', 'ExceptionErrorHandler']);
/** @psalm-suppress InvalidArgument */
$this->TL = new TL(null);
$new = new TLSchema;
$new->mergeArray($settings);
$this->TL->init($new);
$this->TL->init($settings['TL']);
if (isset($settings['tl_schema']['td']) && !isset($settings['tl_schema']['telegram'])) {
$this->td = true;
}
Expand Down
3 changes: 1 addition & 2 deletions src/MyTelegramOrgWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ public function __sleep(): array
/**
* Constructor.
*/
public function __construct(array|SettingsAbstract $settings)
public function __construct(SettingsAbstract $settings)
{
$settings = Settings::parseFromLegacy($settings);
if (!$settings instanceof Settings) {
$settings = new Settings;
$settings->merge($this->settings);
Expand Down
75 changes: 0 additions & 75 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
use danog\MadelineProto\Settings\Auth;
use danog\MadelineProto\Settings\Connection;
use danog\MadelineProto\Settings\Database\Memory as DatabaseMemory;
use danog\MadelineProto\Settings\Database\Mysql;
use danog\MadelineProto\Settings\Database\Postgres;
use danog\MadelineProto\Settings\Database\Redis;
use danog\MadelineProto\Settings\DatabaseAbstract;
use danog\MadelineProto\Settings\Files;
use danog\MadelineProto\Settings\Ipc;
Expand Down Expand Up @@ -97,40 +94,6 @@ final class Settings extends SettingsAbstract
*/
protected VoIP $voip;

/**
* Create settings object from possibly legacy settings array.
*
* @internal
* @param SettingsAbstract|array $settings Settings
*/
public static function parseFromLegacy(SettingsAbstract|array $settings): SettingsAbstract
{
if (\is_array($settings)) {
if (empty($settings)) {
return new SettingsEmpty;
}
$settingsNew = new Settings;
$settingsNew->mergeArray($settings);
return $settingsNew;
}
return $settings;
}
/**
* Create full settings object from possibly legacy settings array.
*
* @internal
* @param SettingsAbstract|array $settings Settings
*/
public static function parseFromLegacyFull(SettingsAbstract|array $settings): Settings
{
$settings = self::parseFromLegacy($settings);
if (!$settings instanceof Settings) {
$newSettings = new Settings;
$newSettings->merge($settings);
$settings = $newSettings;
}
return $settings;
}
/**
* Constructor.
*/
Expand All @@ -157,44 +120,6 @@ public function __wakeup(): void
$this->voip = new VoIP;
}
}
/**
* Merge legacy array settings.
*
* @param array $settings Settings
* @internal
*/
public function mergeArray(array $settings): void
{
$this->appInfo->mergeArray($settings);
$this->auth->mergeArray($settings);
$this->connection->mergeArray($settings);
$this->files->mergeArray($settings);
$this->logger->mergeArray($settings);
$this->peer->mergeArray($settings);
$this->rpc->mergeArray($settings);
$this->secretChats->mergeArray($settings);
$this->serialization->mergeArray($settings);
$this->schema->mergeArray($settings);
$this->ipc->mergeArray($settings);
$this->voip->mergeArray($settings);

switch ($settings['db']['type'] ?? 'memory') {
case 'memory':
$this->db = new DatabaseMemory;
break;
case 'mysql':
$this->db = new Mysql;
break;
case 'postgres':
$this->db = new Postgres;
break;
case 'redis':
$this->db = new Redis;
break;
}
$this->db->mergeArray($settings);
}

/**
* Merge another instance of settings.
*
Expand Down
17 changes: 0 additions & 17 deletions src/Settings/AppInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,23 +102,6 @@ public function init(): void
}
}

public function mergeArray(array $settings): void
{
foreach (self::toCamel([
'api_id',
'api_hash',
'device_model',
'system_version',
'app_version',
'lang_code',
'lang_pack',
]) as $object => $array) {
if (isset($settings['app_info'][$array])) {
$this->{$object}($settings['app_info'][$array]);
}
}
}

/**
* Check if the settings have API ID/hash information.
*/
Expand Down
14 changes: 0 additions & 14 deletions src/Settings/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,6 @@ final class Auth extends SettingsAbstract
*/
protected int $maxAuthTries = 5;

public function mergeArray(array $settings): void
{
foreach (self::toCamel([
'default_temp_auth_key_expires_in',
]) as $object => $array) {
if (isset($settings['authorization'][$array])) {
$this->{$object}($settings['authorization'][$array]);
}
}
if (isset($settings['connection_settings']['all']['pfs'])) {
$this->setPfs($settings['connection_settings']['all']['pfs']);
}
}

/**
* Get validity period of the binding of temporary and permanent keys.
*/
Expand Down
106 changes: 0 additions & 106 deletions src/Settings/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,13 @@
use danog\MadelineProto\Exception;
use danog\MadelineProto\Magic;
use danog\MadelineProto\SettingsAbstract;
use danog\MadelineProto\Stream\Common\UdpBufferedStream;
use danog\MadelineProto\Stream\MTProtoBufferInterface;
use danog\MadelineProto\Stream\MTProtoTransport\AbridgedStream;
use danog\MadelineProto\Stream\MTProtoTransport\FullStream;
use danog\MadelineProto\Stream\MTProtoTransport\HttpsStream;
use danog\MadelineProto\Stream\MTProtoTransport\HttpStream;
use danog\MadelineProto\Stream\MTProtoTransport\IntermediatePaddedStream;
use danog\MadelineProto\Stream\MTProtoTransport\ObfuscatedStream;
use danog\MadelineProto\Stream\Proxy\HttpProxy;
use danog\MadelineProto\Stream\Proxy\SocksProxy;
use danog\MadelineProto\Stream\RawStreamInterface;
use danog\MadelineProto\Stream\StreamInterface;
use danog\MadelineProto\Stream\Transport\DefaultStream;
use danog\MadelineProto\Stream\Transport\WssStream;
use danog\MadelineProto\Stream\Transport\WsStream;

/**
* Connection settings.
Expand Down Expand Up @@ -142,104 +134,6 @@ final class Connection extends SettingsAbstract
5 => 'flora',
];

public function mergeArray(array $settings): void
{
if (isset($settings['connection']['ssl_subdomains'])) {
$this->setSslSubdomains($settings['connection']['ssl_subdomains']);
}
$settings = $settings['connection_settings'] ?? [];
if (isset($settings['media_socket_count']['max'])) {
$this->setMaxMediaSocketCount($settings['media_socket_count']['max']);
}
foreach (self::toCamel([
'robin_period',
'default_dc',
'pfs',
]) as $object => $array) {
if (isset($settings[$array])) {
$this->{$object}($settings[$array]);
}
}

$settings = $settings['all'] ?? [];
foreach (self::toCamel([
'test_mode',
'ipv6',
'timeout',
'obfuscated',
]) as $object => $array) {
if (isset($settings[$array])) {
$this->{$object}($settings[$array]);
}
}

if (isset($settings['do_not_retry'])) {
$this->setRetry(false);
}
if (isset($settings['proxy'])) {
$isProxyArray = \is_iterable($settings['proxy']);
foreach ($isProxyArray ? $settings['proxy'] : [$settings['proxy']] as $key => $proxy) {
if ($proxy === '\\Socket') {
$proxy = DefaultStream::class;
} elseif ($proxy === '\\SocksProxy') {
$proxy = SocksProxy::class;
} elseif ($proxy === '\\HttpProxy') {
$proxy = HttpProxy::class;
} elseif ($proxy === '\\MTProxySocket') {
$proxy = ObfuscatedStream::class;
}
if ($proxy !== DefaultStream::class) {
$this->addProxy($proxy, $isProxyArray ? $settings['proxy_extra'][$key] : $settings['proxy_extra']);
}
}
}
if (isset($settings['transport'])) {
$transport = $settings['transport'];
if ($transport === 'tcp') {
$transport = DefaultStream::class;
} elseif ($transport === 'ws') {
$transport = WsStream::class;
} elseif ($transport === 'wss') {
$transport = WssStream::class;
}
$this->setTransport($transport);
}
if (isset($settings['protocol'])) {
$protocol = $settings['protocol'];
switch ($protocol) {
case 'abridged':
case 'tcp_abridged':
$protocol = AbridgedStream::class;
break;
case 'intermediate':
case 'tcp_intermediate':
$protocol = AbridgedStream::class;
break;
case 'obfuscated2':
$this->setObfuscated(true);
// no break
case 'intermediate_padded':
case 'tcp_intermediate_padded':
$protocol = IntermediatePaddedStream::class;
break;
case 'full':
case 'tcp_full':
$protocol = FullStream::class;
break;
case 'http':
$protocol = HttpStream::class;
break;
case 'https':
$protocol = HttpsStream::class;
break;
case 'udp':
$protocol = UdpBufferedStream::class;
break;
}
$this->setProtocol($protocol);
}
}

public function __construct()
{
$this->init();
Expand Down
Loading

0 comments on commit aeb9182

Please sign in to comment.