and let’s assume the following content of Bar.php:
// Bar.phpnamespaceOtherDir;useSomeDir\Foo;// This now conflicts the class OtherDir\Foo
If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the
same runtime, you will see a PHP error such as the following:
PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as OtherDir/Foo.php does not necessarily have to be loaded and the
error is only triggered if it is loaded before OtherDir/Bar.php, this problem
might go unnoticed for a while. In order to prevent this error from surfacing,
you must import the namespace with a different alias:
// Bar.phpnamespaceOtherDir;useSomeDir\FooasSomeDirFoo;// There is no conflict anymore.
Loading history...
9
use FondBot\Channels\Exceptions\InvalidConfiguration;
10
11
class DriverManager
12
{
13
/** @var Driver[] */
14
private $drivers;
15
16
/**
17
* Add driver.
18
*
19
* @param string $alias
20
* @param Driver $instance
21
*/
22
2
public function add(string $alias, Driver $instance): void
23
{
24
2
$this->drivers[$alias] = $instance;
25
2
}
26
27
/**
28
* Get driver instance.
29
*
30
* @param Channel $channel
31
*
32
* @param array $request
33
* @param array $headers
34
* @param array $parameters
35
*
36
* @return Driver
37
*/
38
2
public function get(Channel $channel, array $request = [], array $headers = [], array $parameters = []): Driver
39
{
40
2
$driver = $this->drivers[$channel->getDriver()];
41
42
2
$this->validateParameters($channel, $driver);
43
44
1
$driver->fill($parameters, $request, $headers);
45
46
1
return $driver;
47
}
48
49
/**
50
* Validate channel parameters with driver requirements.
51
*
52
* @param Channel $channel
53
* @param Driver $driver
54
*/
55
private function validateParameters(Channel $channel, Driver $driver): void
56
{
57
2
collect($driver->getConfig())->each(function (string $parameter) use ($channel) {
58
2
if (!array_key_exists($parameter, $channel->getParameters())) {
59
1
throw new InvalidConfiguration('Invalid `'.$channel->getName().'` channel configuration.');
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: