Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Manager.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support;
4
5 use Closure;
6 use Illuminate\Contracts\Container\Container;
7 use InvalidArgumentException;
8
9 abstract class Manager
10 {
11 /**
12 * The container instance.
13 *
14 * @var \Illuminate\Contracts\Container\Container
15 */
16 protected $container;
17
18 /**
19 * The configuration repository instance.
20 *
21 * @var \Illuminate\Contracts\Config\Repository
22 */
23 protected $config;
24
25 /**
26 * The registered custom driver creators.
27 *
28 * @var array
29 */
30 protected $customCreators = [];
31
32 /**
33 * The array of created "drivers".
34 *
35 * @var array
36 */
37 protected $drivers = [];
38
39 /**
40 * Create a new manager instance.
41 *
42 * @param \Illuminate\Contracts\Container\Container $container
43 */
44 public function __construct(Container $container)
45 {
46 $this->container = $container;
47 $this->config = $container->make('config');
48 }
49
50 /**
51 * Get the default driver name.
52 *
53 * @return string|null
54 */
55 abstract public function getDefaultDriver();
56
57 /**
58 * Get a driver instance.
59 *
60 * @param string|null $driver
61 * @return mixed
62 *
63 * @throws \InvalidArgumentException
64 */
65 public function driver($driver = null)
66 {
67 $driver = $driver ?: $this->getDefaultDriver();
68
69 if (is_null($driver)) {
70 throw new InvalidArgumentException(sprintf(
71 'Unable to resolve NULL driver for [%s].', static::class
72 ));
73 }
74
75 // If the given driver has not been created before, we will create the instances
76 // here and cache it so we can return it next time very quickly. If there is
77 // already a driver created by this name, we'll just return that instance.
78 return $this->drivers[$driver] ??= $this->createDriver($driver);
79 }
80
81 /**
82 * Create a new driver instance.
83 *
84 * @param string $driver
85 * @return mixed
86 *
87 * @throws \InvalidArgumentException
88 */
89 protected function createDriver($driver)
90 {
91 // First, we will determine if a custom driver creator exists for the given driver and
92 // if it does not we will check for a creator method for the driver. Custom creator
93 // callbacks allow developers to build their own "drivers" easily using Closures.
94 if (isset($this->customCreators[$driver])) {
95 return $this->callCustomCreator($driver);
96 }
97
98 $method = 'create'.Str::studly($driver).'Driver';
99
100 if (method_exists($this, $method)) {
101 return $this->$method();
102 }
103
104 throw new InvalidArgumentException("Driver [$driver] not supported.");
105 }
106
107 /**
108 * Call a custom driver creator.
109 *
110 * @param string $driver
111 * @return mixed
112 */
113 protected function callCustomCreator($driver)
114 {
115 return $this->customCreators[$driver]($this->container);
116 }
117
118 /**
119 * Register a custom driver creator Closure.
120 *
121 * @param string $driver
122 * @param \Closure $callback
123 * @return $this
124 */
125 public function extend($driver, Closure $callback)
126 {
127 $this->customCreators[$driver] = $callback;
128
129 return $this;
130 }
131
132 /**
133 * Get all of the created "drivers".
134 *
135 * @return array
136 */
137 public function getDrivers()
138 {
139 return $this->drivers;
140 }
141
142 /**
143 * Get the container instance used by the manager.
144 *
145 * @return \Illuminate\Contracts\Container\Container
146 */
147 public function getContainer()
148 {
149 return $this->container;
150 }
151
152 /**
153 * Set the container instance used by the manager.
154 *
155 * @param \Illuminate\Contracts\Container\Container $container
156 * @return $this
157 */
158 public function setContainer(Container $container)
159 {
160 $this->container = $container;
161
162 return $this;
163 }
164
165 /**
166 * Forget all of the resolved driver instances.
167 *
168 * @return $this
169 */
170 public function forgetDrivers()
171 {
172 $this->drivers = [];
173
174 return $this;
175 }
176
177 /**
178 * Dynamically call the default driver instance.
179 *
180 * @param string $method
181 * @param array $parameters
182 * @return mixed
183 */
184 public function __call($method, $parameters)
185 {
186 return $this->driver()->$method(...$parameters);
187 }
188 }