|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Phalcon Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Phalcon Team <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Phalcon\Storage; |
|
15
|
|
|
|
|
16
|
|
|
use Phalcon\Factory\AbstractFactory; |
|
17
|
|
|
use Phalcon\Factory\Exception; |
|
|
|
|
|
|
18
|
|
|
use Phalcon\Storage\Serializer\SerializerInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class SerializerFactory |
|
22
|
|
|
* |
|
23
|
|
|
* @package Phalcon\Storage |
|
24
|
|
|
*/ |
|
25
|
|
|
class SerializerFactory extends AbstractFactory |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* SerializerFactory constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param array $services |
|
31
|
|
|
*/ |
|
32
|
190 |
|
public function __construct(array $services = []) |
|
33
|
|
|
{ |
|
34
|
190 |
|
$this->init($services); |
|
35
|
190 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $name |
|
39
|
|
|
* |
|
40
|
|
|
* @return SerializerInterface |
|
41
|
|
|
* @throws Exception |
|
42
|
|
|
*/ |
|
43
|
120 |
|
public function newInstance(string $name): SerializerInterface |
|
44
|
|
|
{ |
|
45
|
120 |
|
$this->checkService($name); |
|
46
|
|
|
|
|
47
|
119 |
|
$definition = $this->mapper[$name]; |
|
48
|
|
|
|
|
49
|
119 |
|
return new $definition(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return array |
|
54
|
|
|
*/ |
|
55
|
190 |
|
protected function getAdapters(): array |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
190 |
|
"base64" => 'Phalcon\Storage\Serializer\Base64', |
|
59
|
|
|
"igbinary" => 'Phalcon\Storage\Serializer\Igbinary', |
|
60
|
|
|
"json" => 'Phalcon\Storage\Serializer\Json', |
|
61
|
|
|
"msgpack" => 'Phalcon\Storage\Serializer\Msgpack', |
|
62
|
|
|
"none" => 'Phalcon\Storage\Serializer\None', |
|
63
|
|
|
"php" => 'Phalcon\Storage\Serializer\Php', |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: