Skip to content

Commit 56f6248

Browse files
committed
[fix] Database connection, because, yiisoft/db released
1 parent 449de51 commit 56f6248

File tree

5 files changed

+103
-49
lines changed

5 files changed

+103
-49
lines changed

App.php

+71-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
use Config\BotConfig;
44
use Config\DbConfig;
5+
use Longman\TelegramBot\Entities\ServerResponse;
6+
use Longman\TelegramBot\Telegram;
57
use MrMuminov\PhpI18n\I18n;
68

79
class App
810
{
911
public static DbConfig $database;
12+
public static Telegram $telegram;
1013
public static bool $development = false;
1114
public static BotConfig $bot;
1215
public static I18n $i18n;
@@ -29,11 +32,77 @@ public function __construct(
2932

3033
public static function log(mixed $message): void
3134
{
32-
@fputs(@fopen('php://stdout', 'a+'), print_r($message, 1) . PHP_EOL);
35+
@fputs(@fopen(filename: 'php://stdout', mode: 'a+'), print_r($message, 1) . PHP_EOL);
3336
}
3437

3538
public static function debug(mixed $message): void
3639
{
37-
@fputs(@fopen('php://stdout', 'a+'), var_export($message, 1) . PHP_EOL);
40+
@fputs(@fopen(filename: 'php://stdout', mode: 'a+'), var_export($message, 1) . PHP_EOL);
41+
}
42+
43+
public static function init(): void
44+
{
45+
if (isset(self::$telegram)) {
46+
return;
47+
}
48+
if (!file_exists(__DIR__ . '/config.php')) {
49+
throw new Exception(
50+
message: "config.php file not found." . PHP_EOL .
51+
"Please, copy config.example.php to name config.php and try again."
52+
);
53+
}
54+
$config = require __DIR__ . '/config.php';
55+
(new App(
56+
database: new DbConfig(
57+
host: $config['database']['host'],
58+
port: $config['database']['port'],
59+
username: $config['database']['username'],
60+
password: $config['database']['password'],
61+
dbname: $config['database']['dbname'],
62+
attributes: $config['database']['attributes'] ?? [],
63+
),
64+
bot: new BotConfig(
65+
bot_api_token: $config['bot']['bot_api_token'],
66+
bot_username: $config['bot']['bot_username'],
67+
webhook_url: $config['bot']['webhook_url'],
68+
),
69+
i18n: new I18n([
70+
'path' => $config['i18n']['path'],
71+
'languages' => $config['i18n']['languages'],
72+
'language' => $config['i18n']['language'],
73+
]),
74+
params: $config['params'] ?? [],
75+
development: $config['development'] ?? false,
76+
));
77+
78+
79+
self::$telegram = new Telegram(
80+
api_key: App::$bot->bot_api_token,
81+
bot_username: App::$bot->bot_username,
82+
);
83+
84+
}
85+
86+
public static function setWebHook(
87+
string $url = null,
88+
mixed $certificate = null,
89+
string $ipAddress = null,
90+
string $maxConnections = null,
91+
string $allowedUpdates = null,
92+
string $dropPendingUpdates = null,
93+
string $secretToken = null,
94+
): ServerResponse
95+
{
96+
$data = [];
97+
if ($certificate) $data['certificate'] = $certificate;
98+
if ($ipAddress) $data['ip_address'] = $ipAddress;
99+
if ($maxConnections) $data['max_connections'] = $maxConnections;
100+
if ($allowedUpdates) $data['allowed_updates'] = $allowedUpdates;
101+
if ($dropPendingUpdates) $data['drop_pending_updates'] = $dropPendingUpdates;
102+
if ($secretToken) $data['secret_token'] = $secretToken;
103+
return self::$telegram->setWebhook(
104+
url: $url ?? App::$bot->webhook_url,
105+
data: $data,
106+
);
38107
}
39108
}

Config/DbConfig.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
use Yiisoft\Cache\ArrayCache;
66
use Yiisoft\Db\Cache\SchemaCache;
7-
use Yiisoft\Db\Pgsql\ConnectionPDO;
8-
use Yiisoft\Db\Pgsql\PDODriver;
7+
use Yiisoft\Db\Pgsql\Connection;
8+
use Yiisoft\Db\Pgsql\Driver;
99

1010
class DbConfig
1111
{
12-
public static ConnectionPDO $connection;
13-
public static PDODriver $driver;
12+
public static Connection $connection;
13+
public static Driver $driver;
1414

1515
public function __construct(
1616
private string $host = 'localhost',
@@ -22,7 +22,7 @@ public function __construct(
2222
private ?SchemaCache $schemaCache = null,
2323
)
2424
{
25-
self::$driver = new PDODriver(
25+
self::$driver = new Driver(
2626
dsn: 'pgsql:' .
2727
'host=' . $this->host . ';' .
2828
'port=' . $this->port . ';' .
@@ -36,7 +36,7 @@ public function __construct(
3636
psrCache: $arrayCache
3737
);
3838
}
39-
self::$connection = new ConnectionPDO(
39+
self::$connection = new Connection(
4040
driver: self::$driver,
4141
schemaCache: $this->schemaCache,
4242
);

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
"php": "^8.0",
66
"ext-pdo": "*",
77
"ext-json": "*",
8-
"yiisoft/db": "dev-master",
9-
"yiisoft/cache": "dev-master",
10-
"yiisoft/db-pgsql": "dev-master",
8+
"yiisoft/db": "^1.0.0",
9+
"yiisoft/cache": "^3.0.0",
10+
"yiisoft/db-pgsql": "^1.0.0",
1111
"mrmuminov/php-i18n": "^1.3",
1212
"longman/telegram-bot": "^0.78.0",
1313
"yiisoft/active-record": "dev-master"

index.php

+6-38
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,23 @@
11
<?php
22

3-
use Config\DbConfig;
4-
use Config\BotConfig;
53
use Commands\HelloCommand;
64
use Commands\StartCommand;
7-
use MrMuminov\PhpI18n\I18n;
8-
use Longman\TelegramBot\Telegram;
95
use Longman\TelegramBot\Exception\TelegramException;
106

117
require __DIR__ . '/vendor/autoload.php';
12-
$config = require __DIR__ . '/config.php';
13-
(new App(
14-
database: new DbConfig(
15-
host: $config['database']['host'],
16-
port: $config['database']['port'],
17-
username: $config['database']['username'],
18-
password: $config['database']['password'],
19-
dbname: $config['database']['dbname'],
20-
attributes: $config['database']['attributes'] ?? [],
21-
),
22-
bot: new BotConfig(
23-
bot_api_token: $config['bot']['bot_api_token'],
24-
bot_username: $config['bot']['bot_username'],
25-
webhook_url: $config['bot']['webhook_url'],
26-
),
27-
i18n: new I18n([
28-
'path' => $config['i18n']['path'],
29-
'languages' => $config['i18n']['languages'],
30-
'language' => $config['i18n']['language'],
31-
]),
32-
params: $config['params'] ?? [],
33-
development: $config['development'] ?? false,
34-
));
358

369
if (App::$development) {
37-
ini_set('display_errors', 1);
38-
error_reporting(E_ALL);
10+
ini_set(option: 'display_errors', value: 1);
11+
error_reporting(error_level: E_ALL);
3912
}
4013

4114
try {
42-
$telegram = new Telegram(App::$bot->bot_api_token, App::$bot->bot_username);
43-
$telegram->deleteWebhook();
44-
$telegram->setWebhook(App::$bot->webhook_url, [
45-
'drop_pending_updates' => true,
46-
]);
47-
$telegram->addCommandClasses([
15+
App::init();
16+
App::$telegram->addCommandClasses([
4817
StartCommand::class,
4918
HelloCommand::class,
5019
]);
51-
52-
$telegram->handle();
20+
App::$telegram->handle();
5321
} catch (TelegramException $e) {
54-
App::log($e);
22+
App::log($e);
5523
}

webhook.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Longman\TelegramBot\Exception\TelegramException;
4+
5+
require __DIR__ . '/vendor/autoload.php';
6+
7+
if (App::$development) {
8+
ini_set(option: 'display_errors', value: 1);
9+
error_reporting(error_level: E_ALL);
10+
}
11+
12+
try {
13+
App::init();
14+
App::log(App::setWebHook(dropPendingUpdates: true));
15+
} catch (TelegramException $e) {
16+
App::log($e);
17+
}

0 commit comments

Comments
 (0)