blob: f458951e79f77f31b2a97672173d76fe9f35b656 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3namespace LdapRecord;
4
5/**
6 * @method static $this reset()
7 * @method static Connection[] all()
8 * @method static Connection[] allConnections()
9 * @method static Connection getDefaultConnection()
10 * @method static Connection get(string|null $name = null)
11 * @method static Connection getConnection(string|null $name = null)
12 * @method static bool exists(string $name)
13 * @method static $this remove(string|null $name = null)
14 * @method static $this removeConnection(string|null $name = null)
15 * @method static $this setDefault(string|null $name = null)
16 * @method static $this setDefaultConnection(string|null $name = null)
17 * @method static $this add(Connection $connection, string|null $name = null)
18 * @method static $this addConnection(Connection $connection, string|null $name = null)
19 */
20class Container
21{
22 /**
23 * The current container instance.
24 *
25 * @var Container
26 */
27 protected static $instance;
28
29 /**
30 * The connection manager instance.
31 *
32 * @var ConnectionManager
33 */
34 protected $manager;
35
36 /**
37 * The methods to passthru, for compatibility.
38 *
39 * @var array
40 */
41 protected $passthru = [
42 'reset', 'flush',
43 'add', 'addConnection',
44 'remove', 'removeConnection',
45 'setDefault', 'setDefaultConnection',
46 ];
47
48 /**
49 * Forward missing static calls onto the current instance.
50 *
51 * @param string $method
52 * @param mixed $args
53 *
54 * @return mixed
55 */
56 public static function __callStatic($method, $args)
57 {
58 return static::getInstance()->{$method}(...$args);
59 }
60
61 /**
62 * Get or set the current instance of the container.
63 *
64 * @return Container
65 */
66 public static function getInstance()
67 {
68 return static::$instance ?? static::getNewInstance();
69 }
70
71 /**
72 * Set the container instance.
73 *
74 * @param Container|null $container
75 *
76 * @return Container|null
77 */
78 public static function setInstance(self $container = null)
79 {
80 return static::$instance = $container;
81 }
82
83 /**
84 * Set and get a new instance of the container.
85 *
86 * @return Container
87 */
88 public static function getNewInstance()
89 {
90 return static::setInstance(new static());
91 }
92
93 /**
94 * Constructor.
95 *
96 * @return void
97 */
98 public function __construct()
99 {
100 $this->manager = new ConnectionManager();
101 }
102
103 /**
104 * Forward missing method calls onto the connection manager.
105 *
106 * @param string $method
107 * @param mixed $args
108 *
109 * @return mixed
110 */
111 public function __call($method, $args)
112 {
113 $value = $this->manager->{$method}(...$args);
114
115 return in_array($method, $this->passthru) ? $this : $value;
116 }
117
118 /**
119 * Get the connection manager.
120 *
121 * @return ConnectionManager
122 */
123 public function manager()
124 {
125 return $this->manager;
126 }
127}