2024-03-31 18:08:04 +02:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
use Amp\Mysql\MysqlConfig;
|
|
|
|
use Amp\Postgres\PostgresConfig;
|
|
|
|
use Amp\Redis\RedisConfig;
|
|
|
|
use danog\AsyncOrm\Annotations\OrmMappedArray;
|
|
|
|
use danog\AsyncOrm\DbArray;
|
|
|
|
use danog\AsyncOrm\DbAutoProperties;
|
2024-03-31 19:11:42 +02:00
|
|
|
use danog\AsyncOrm\DbObject;
|
2024-03-31 18:08:04 +02:00
|
|
|
use danog\AsyncOrm\KeyType;
|
|
|
|
use danog\AsyncOrm\Settings;
|
|
|
|
use danog\AsyncOrm\Settings\MysqlSettings;
|
|
|
|
use danog\AsyncOrm\Settings\PostgresSettings;
|
|
|
|
use danog\AsyncOrm\Settings\RedisSettings;
|
|
|
|
use danog\AsyncOrm\ValueType;
|
|
|
|
|
|
|
|
require __DIR__ . '/../vendor/autoload.php';
|
|
|
|
|
2024-03-31 19:11:42 +02:00
|
|
|
// Any of the following database backends can be used,
|
|
|
|
// remove the ones you don't need.
|
2024-03-31 18:08:04 +02:00
|
|
|
$settings = new MysqlSettings(
|
|
|
|
new MysqlConfig(
|
|
|
|
host: "/var/run/mysqld/mysqld.sock",
|
|
|
|
user: 'user',
|
|
|
|
password: 'password',
|
|
|
|
database: 'database'
|
|
|
|
),
|
|
|
|
cacheTtl: 100
|
|
|
|
);
|
|
|
|
$settings = new PostgresSettings(
|
|
|
|
new PostgresConfig(
|
|
|
|
host: "127.0.0.1",
|
|
|
|
user: "user",
|
|
|
|
password: "password",
|
|
|
|
database: "database"
|
|
|
|
),
|
|
|
|
cacheTtl: 100
|
|
|
|
);
|
|
|
|
$settings = new RedisSettings(
|
|
|
|
RedisConfig::fromUri("redis://127.0.0.1"),
|
|
|
|
cacheTtl: 100
|
|
|
|
);
|
|
|
|
|
2024-03-31 19:11:42 +02:00
|
|
|
/**
|
|
|
|
* An object stored in a database.
|
|
|
|
*/
|
|
|
|
class MyObject extends DbObject
|
|
|
|
{
|
|
|
|
public function __construct(
|
|
|
|
private string $value
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
public function setValue(string $value): void
|
|
|
|
{
|
|
|
|
$this->value = $value;
|
|
|
|
}
|
|
|
|
public function getValue(): string
|
|
|
|
{
|
|
|
|
return $this->value;
|
|
|
|
}
|
|
|
|
}
|
2024-03-31 18:08:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Main class of your application.
|
|
|
|
*/
|
|
|
|
final class Application
|
|
|
|
{
|
|
|
|
use DbAutoProperties;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This field is automatically connected to the database using the specified Settings.
|
2024-03-31 19:38:58 +02:00
|
|
|
*
|
2024-03-31 19:37:48 +02:00
|
|
|
* @var DbArray<string, MyObject>
|
2024-03-31 18:08:04 +02:00
|
|
|
*/
|
2024-03-31 19:37:48 +02:00
|
|
|
#[OrmMappedArray(KeyType::STRING, ValueType::OBJECT)]
|
2024-03-31 19:11:42 +02:00
|
|
|
private DbArray $dbProperty1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This field is automatically connected to the database using the specified Settings.
|
2024-03-31 19:38:58 +02:00
|
|
|
*
|
2024-03-31 19:37:48 +02:00
|
|
|
* @var DbArray<string, int>
|
2024-03-31 19:11:42 +02:00
|
|
|
*/
|
2024-03-31 19:37:48 +02:00
|
|
|
#[OrmMappedArray(KeyType::STRING, ValueType::INT)]
|
2024-03-31 19:11:42 +02:00
|
|
|
private DbArray $dbProperty2;
|
2024-03-31 18:08:04 +02:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
Settings $settings,
|
|
|
|
string $tablePrefix
|
|
|
|
) {
|
|
|
|
$this->initDbProperties($settings, $tablePrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function businessLogic(): void
|
|
|
|
{
|
2024-03-31 19:37:48 +02:00
|
|
|
$this->dbProperty1['someOtherKey'] = new MyObject("initialValue");
|
2024-03-31 19:11:42 +02:00
|
|
|
|
2024-03-31 19:37:48 +02:00
|
|
|
// Can store integers, strings, arrays or objects depending on the specified ValueType
|
|
|
|
$this->dbProperty2['someKey'] = 123;
|
2024-03-31 19:38:58 +02:00
|
|
|
var_dump($this->dbProperty2['someKey']);
|
2024-03-31 19:11:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function businessLogic2(string $value): void
|
|
|
|
{
|
2024-03-31 19:37:48 +02:00
|
|
|
$obj = $this->dbProperty1['someOtherKey'];
|
2024-03-31 19:11:42 +02:00
|
|
|
$obj->setValue($value);
|
|
|
|
$obj->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function businessLogic3(): string
|
|
|
|
{
|
2024-03-31 19:37:48 +02:00
|
|
|
return $this->dbProperty1['someOtherKey']->getValue();
|
2024-03-31 18:08:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function shutdown(): void
|
|
|
|
{
|
|
|
|
// Flush all database caches, saving all changes.
|
|
|
|
$this->saveDbProperties();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$app = new Application($settings, 'tablePrefix');
|
|
|
|
$app->businessLogic();
|
2024-03-31 19:11:42 +02:00
|
|
|
$app->businessLogic2("newValue");
|
|
|
|
var_dump($app->businessLogic3());
|
2024-03-31 18:28:23 +02:00
|
|
|
$app->shutdown();
|