DatabaseManager.php
72 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The DatabaseManager class provides a high-level interface for managing database transactions. |
| 5 | * This class acts as a facade for transaction management, allowing you to |
| 6 | * begin, commit, and rollback transactions using the underlying database connection. |
| 7 | * It is designed to simplify transaction handling and ensure consistency across database operations. |
| 8 | * |
| 9 | * @package Framework |
| 10 | * @subpackage Database\Connection |
| 11 | * @since 1.0.0 |
| 12 | */ |
| 13 | namespace Kirki\Framework\Database\Connection; |
| 14 | |
| 15 | \defined('ABSPATH') || exit; |
| 16 | use Kirki\Framework\Supports\Traits\Macroable; |
| 17 | class DatabaseManager |
| 18 | { |
| 19 | use Macroable { |
| 20 | __call as macroCall; |
| 21 | } |
| 22 | /** |
| 23 | * The database connection instance. |
| 24 | * |
| 25 | * @var Connection |
| 26 | * |
| 27 | * @since 1.0.0 |
| 28 | */ |
| 29 | protected $connection; |
| 30 | /** |
| 31 | * Initialize a new DatabaseManager instance with a database connection. |
| 32 | * |
| 33 | * @param Connection $connection The database connection instance. |
| 34 | * |
| 35 | * @return void |
| 36 | * |
| 37 | * @since 1.0.0 |
| 38 | */ |
| 39 | public function __construct(Connection $connection) |
| 40 | { |
| 41 | $this->connection = $connection; |
| 42 | } |
| 43 | /** |
| 44 | * Get the database connection instance |
| 45 | * |
| 46 | * @return Connection The database connection instance |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | */ |
| 50 | public function connection() |
| 51 | { |
| 52 | return $this->connection; |
| 53 | } |
| 54 | /** |
| 55 | * Call a method on the database connection |
| 56 | * |
| 57 | * @param string $method The method name |
| 58 | * @param array $arguments The arguments for the method |
| 59 | * |
| 60 | * @return mixed The result of the method call |
| 61 | * |
| 62 | * @since 1.0.0 |
| 63 | */ |
| 64 | public function __call($method, $arguments) |
| 65 | { |
| 66 | if (static::has_macro($method)) { |
| 67 | return $this->macroCall($method, $arguments); |
| 68 | } |
| 69 | return $this->connection->{$method}(...$arguments); |
| 70 | } |
| 71 | } |
| 72 |