Added new serialization method
This commit is contained in:
parent
118c01eea0
commit
fefbe41f29
16
README.md
16
README.md
@ -623,9 +623,23 @@ An istance of MadelineProto can be safely serialized or unserialized. To seriali
|
||||
$MadelineProto = \danog\MadelineProto\Serialization::deserialize('session.madeline');
|
||||
// Do stuff
|
||||
\danog\MadelineProto\Serialization::serialize('session.madeline', $MadelineProto);
|
||||
// or
|
||||
$MadelineProto->serialize('session.madeline');
|
||||
```
|
||||
|
||||
THe deserialize method accepts a second optional parameter, `$no_updates`, that can be set to true to avoid fetching updates on deserialization, and postpone parsing of updates received through the socket until the next deserialization.
|
||||
Or
|
||||
|
||||
```
|
||||
$MadelineProto = \danog\MadelineProto\Serialization::deserialize('session.madeline');
|
||||
$MadelineProto->session = 'session.madeline';
|
||||
```
|
||||
|
||||
This way, if the scripts shutsdown normally (without ctrl+c or fatal errors/exceptions), the session will be serialized automatically.
|
||||
|
||||
It is still recommended to serialize the session at every update.
|
||||
|
||||
|
||||
The deserialize method accepts a second optional parameter, `$no_updates`, that can be set to true to avoid fetching updates on deserialization, and postpone parsing of updates received through the socket until the next deserialization.
|
||||
That class serializes using [MagicalSerializer](https://github.com/danog/MagicalSerializer).
|
||||
The same should be done when serializing to another destination manually, to avoid conflicts with other PHP scripts that are trying to serialize another instance of the class.
|
||||
|
||||
|
@ -15,6 +15,7 @@ namespace danog\MadelineProto;
|
||||
class API extends APIFactory
|
||||
{
|
||||
use \danog\Serializable;
|
||||
public $session;
|
||||
|
||||
public function ___construct($params = [])
|
||||
{
|
||||
@ -44,6 +45,9 @@ class API extends APIFactory
|
||||
if (\danog\MadelineProto\Logger::$has_thread && is_object(\Thread::getCurrentThread())) {
|
||||
return;
|
||||
}
|
||||
if (!is_null($this->session)) {
|
||||
$this->serialize($this->session);
|
||||
}
|
||||
restore_error_handler();
|
||||
}
|
||||
|
||||
@ -91,6 +95,7 @@ class API extends APIFactory
|
||||
|
||||
public function serialize($filename)
|
||||
{
|
||||
Logger::log(['Serializing MadelineProto...']);
|
||||
return Serialization::serialize($filename, $this);
|
||||
}
|
||||
}
|
||||
|
@ -14,4 +14,16 @@ namespace danog\MadelineProto;
|
||||
|
||||
class PTSException extends \Exception
|
||||
{
|
||||
use TL\PrettyException;
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return get_class($this).($this->message !== '' ? ': ' : '').$this->message.PHP_EOL.'TL Trace:'.PHP_EOL.PHP_EOL.$this->getTLTrace().PHP_EOL;
|
||||
}
|
||||
|
||||
public function __construct($message, $file = '')
|
||||
{
|
||||
parent::__construct($message);
|
||||
$this->prettify_tl($file);
|
||||
}
|
||||
}
|
||||
|
@ -56,13 +56,6 @@ if ($MadelineProto === false) {
|
||||
echo 'Loading MadelineProto...'.PHP_EOL;
|
||||
$MadelineProto = new \danog\MadelineProto\API($settings);
|
||||
if (getenv('TRAVIS_COMMIT') == '') {
|
||||
$checkedPhone = $MadelineProto->auth->checkPhone(// auth.checkPhone becomes auth->checkPhone
|
||||
[
|
||||
'phone_number' => getenv('MTPROTO_NUMBER'),
|
||||
]
|
||||
);
|
||||
|
||||
\danog\MadelineProto\Logger::log([$checkedPhone], \danog\MadelineProto\Logger::NOTICE);
|
||||
$sentCode = $MadelineProto->phone_login(readline('Enter your phone number: '));
|
||||
\danog\MadelineProto\Logger::log([$sentCode], \danog\MadelineProto\Logger::NOTICE);
|
||||
echo 'Enter the code you received: ';
|
||||
@ -81,12 +74,11 @@ if ($MadelineProto === false) {
|
||||
$authorization = $MadelineProto->complete_signup(readline('Please enter your first name: '), readline('Please enter your last name (can be empty): '));
|
||||
}
|
||||
|
||||
echo 'Serializing MadelineProto to session.madeline...'.PHP_EOL;
|
||||
echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('session.madeline', $MadelineProto).' bytes'.PHP_EOL;
|
||||
} else {
|
||||
$MadelineProto->bot_login(getenv('BOT_TOKEN'));
|
||||
}
|
||||
}
|
||||
$MadelineProto->session = 'session.madeline';
|
||||
\danog\MadelineProto\Logger::log(['hey'], \danog\MadelineProto\Logger::ULTRA_VERBOSE);
|
||||
\danog\MadelineProto\Logger::log(['hey'], \danog\MadelineProto\Logger::VERBOSE);
|
||||
\danog\MadelineProto\Logger::log(['hey'], \danog\MadelineProto\Logger::NOTICE);
|
||||
@ -101,6 +93,7 @@ echo 'Serializing MadelineProto to session.madeline...'.PHP_EOL; echo 'Wrote '.\
|
||||
$m = new \danog\MadelineProto\API($settings);
|
||||
$m->import_authorization($MadelineProto->export_authorization());
|
||||
*/
|
||||
die;
|
||||
if (stripos(readline('Do you want to make a call? (y/n): '), 'y') !== false) {
|
||||
$controller = $MadelineProto->request_call(getenv('TEST_SECRET_CHAT'))->play('input.raw')->then('input.raw')->playOnHold(['input.raw'])->setOutputFile('output.raw');
|
||||
while ($controller->getCallState() < \danog\MadelineProto\VoIP::CALL_STATE_READY) {
|
||||
@ -200,7 +193,6 @@ $media = [];
|
||||
|
||||
// Sticker
|
||||
$inputFile = $MadelineProto->upload('tests/lel.webp');
|
||||
$inputFile['_'] = 'inputPhotoEmpty';
|
||||
var_dump($inputFile);
|
||||
$media['sticker'] = ['_' => 'inputMediaUploadedDocument', 'file' => $inputFile, 'mime_type' => mime_content_type('tests/lel.webp'), 'caption' => 'test', 'attributes' => [['_' => 'documentAttributeSticker', 'alt' => 'LEL', 'stickerset' => ['_' => 'inputStickerSetEmpty']]]];
|
||||
|
||||
@ -229,9 +221,6 @@ foreach (json_decode(getenv('TEST_DESTINATION_GROUPS'), true) as $peer) {
|
||||
}
|
||||
}
|
||||
//var_dump($MadelineProto->API->get_updates());
|
||||
echo 'Serializing MadelineProto to session.madeline...'.PHP_EOL;
|
||||
echo 'Wrote '.\danog\MadelineProto\Serialization::serialize('session.madeline', $MadelineProto).' bytes'.PHP_EOL;
|
||||
echo 'Size of MadelineProto instance is '.strlen(serialize($MadelineProto)).' bytes'.PHP_EOL;
|
||||
|
||||
if ($bot_token = getenv('BOT_TOKEN')) {
|
||||
$MadelineProto = new \danog\MadelineProto\API($settings);
|
||||
|
Loading…
x
Reference in New Issue
Block a user