|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Kiboko\Plugin\SQL\Builder; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | + |
| 9 | +final class SharedConnection implements ConnectionBuilderInterface |
| 10 | +{ |
| 11 | + private ?bool $persistentConnection; |
| 12 | + |
| 13 | + public function __construct( |
| 14 | + private Node\Expr $dsn, |
| 15 | + private ?Node\Expr $username = null, |
| 16 | + private ?Node\Expr $password = null, |
| 17 | + private string $generatedNamespace = 'GyroscopsGenerated', |
| 18 | + ) { |
| 19 | + $this->persistentConnection = null; |
| 20 | + } |
| 21 | + |
| 22 | + public function withUsername(Node\Expr $username): self |
| 23 | + { |
| 24 | + $this->username = $username; |
| 25 | + |
| 26 | + return $this; |
| 27 | + } |
| 28 | + |
| 29 | + public function withPassword(Node\Expr $password): self |
| 30 | + { |
| 31 | + $this->password = $password; |
| 32 | + |
| 33 | + return $this; |
| 34 | + } |
| 35 | + |
| 36 | + public function withPersistentConnection(bool $option): self |
| 37 | + { |
| 38 | + $this->persistentConnection = $option; |
| 39 | + |
| 40 | + return $this; |
| 41 | + } |
| 42 | + |
| 43 | + public function getNode(): Node\Expr |
| 44 | + { |
| 45 | + return new Node\Expr\StaticCall( |
| 46 | + class: new Node\Name($this->generatedNamespace.'\\PDOPool'), |
| 47 | + name: new Node\Name('shared'), |
| 48 | + args: [ |
| 49 | + new Node\Arg(value: $this->dsn), |
| 50 | + $this->username ? new Node\Arg($this->username) : new Node\Expr\ConstFetch(new Node\Name('null')), |
| 51 | + $this->password ? new Node\Arg($this->password) : new Node\Expr\ConstFetch(new Node\Name('null')), |
| 52 | + new Node\Arg( |
| 53 | + value: new Node\Expr\Array_( |
| 54 | + items: [ |
| 55 | + $this->persistentConnection ? new Node\Expr\ArrayItem( |
| 56 | + value: new Node\Expr\ConstFetch(new Node\Name("{$this->persistentConnection}")), |
| 57 | + key: new Node\Expr\ClassConstFetch( |
| 58 | + class: new Node\Name\FullyQualified('PDO'), |
| 59 | + name: new Node\Identifier('ATTR_PERSISTENT') |
| 60 | + ) |
| 61 | + ) : null, |
| 62 | + ], |
| 63 | + attributes: [ |
| 64 | + 'kind' => Node\Expr\Array_::KIND_SHORT, |
| 65 | + ], |
| 66 | + ), |
| 67 | + ), |
| 68 | + ], |
| 69 | + ); |
| 70 | + } |
| 71 | +} |
0 commit comments