1 | <?php |
||
8 | class Mapper |
||
9 | { |
||
10 | private $client; |
||
11 | private $plugins = []; |
||
12 | private $schema; |
||
13 | private $bootstrap; |
||
14 | |||
15 | public function __construct(Client $client) |
||
19 | |||
20 | public function getPlugin($mixed) |
||
21 | { |
||
22 | if (!is_subclass_of($mixed, Plugin::class)) { |
||
23 | throw new Exception("Plugin should extend " . Plugin::class . " class"); |
||
24 | } |
||
25 | |||
26 | $plugin = is_object($mixed) ? $mixed : new $mixed($this); |
||
27 | $class = get_class($plugin); |
||
28 | |||
29 | if ($plugin == $mixed && array_key_exists($class, $this->plugins)) { |
||
30 | // overwrite plugin instance |
||
31 | throw new Exception($class.' is registered'); |
||
32 | } |
||
33 | |||
34 | if (!array_key_exists($class, $this->plugins)) { |
||
35 | $this->plugins[$class] = $plugin; |
||
36 | } |
||
37 | |||
38 | return $this->plugins[$class]; |
||
39 | } |
||
40 | |||
41 | public function create($space, $data) |
||
45 | |||
46 | public function findOne($space, $params = []) |
||
50 | |||
51 | public function findOrCreate($space, $params = []) |
||
55 | |||
56 | public function findOrFail($space, $params = []) |
||
60 | |||
61 | public function find($space, $params = []) |
||
65 | |||
66 | public function findRepository(Entity $instance) |
||
76 | |||
77 | public function getBootstrap() |
||
81 | |||
82 | public function getClient() |
||
86 | |||
87 | public function getMeta() |
||
93 | |||
94 | public function hasPlugin($class) |
||
98 | |||
99 | public function getPlugins() |
||
103 | |||
104 | public function getRepository($space) |
||
108 | |||
109 | public function getRepositories() |
||
119 | |||
120 | public function getSchema() |
||
124 | |||
125 | public function remove($space, $params = []) |
||
133 | |||
134 | public function save(Entity $instance) |
||
138 | |||
139 | public function setMeta($meta) |
||
147 | } |
||
148 |
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: