|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebTheory\Saveyour\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use WebTheory\Factory\Traits\ClassResolverTrait; |
|
7
|
|
|
use WebTheory\Factory\Traits\SmartFactoryTrait; |
|
8
|
|
|
use WebTheory\Saveyour\Contracts\Data\FieldDataManagerInterface; |
|
9
|
|
|
use WebTheory\Saveyour\Contracts\Factory\FieldDataManagerResolverFactoryInterface; |
|
10
|
|
|
use WebTheory\Saveyour\Data\FieldDataManagerCallback; |
|
11
|
|
|
|
|
12
|
|
|
class DataManagerFactory implements FieldDataManagerResolverFactoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
use SmartFactoryTrait; |
|
|
|
|
|
|
15
|
|
|
use ClassResolverTrait; |
|
16
|
|
|
|
|
17
|
|
|
private array $managers = []; |
|
18
|
|
|
|
|
19
|
|
|
protected array $namespaces = []; |
|
20
|
|
|
|
|
21
|
|
|
public const NAMESPACES = [ |
|
22
|
|
|
'webtheory.saveyour' => "WebTheory\\Saveyour\\Managers", |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
public const MANAGERS = [ |
|
26
|
|
|
'callback' => FieldDataManagerCallback::class, |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
protected const CONVENTION = '%sFieldDataManager'; |
|
30
|
|
|
|
|
31
|
3 |
|
public function __construct(array $namespaces = [], array $managers = []) |
|
32
|
|
|
{ |
|
33
|
3 |
|
$this->namespaces = $namespaces + static::NAMESPACES; |
|
34
|
3 |
|
$this->managers = $managers + static::MANAGERS; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getManagers() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->managers; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return $this |
|
44
|
|
|
*/ |
|
45
|
|
|
public function addManager(string $arg, string $manager): DataManagerFactory |
|
46
|
|
|
{ |
|
47
|
|
|
$this->managers[$arg] = $manager; |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return $this |
|
54
|
|
|
*/ |
|
55
|
|
|
public function addManagers(array $managers): DataManagerFactory |
|
56
|
|
|
{ |
|
57
|
|
|
$this->managers = $managers + $this->managers; |
|
58
|
|
|
|
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
public function create(string $manager, array $args = []): FieldDataManagerInterface |
|
63
|
|
|
{ |
|
64
|
3 |
|
$manager = $this->managers[$manager] ?? null; |
|
65
|
|
|
|
|
66
|
3 |
|
if (isset($manager)) { |
|
67
|
3 |
|
$manager = $this->build($manager, $args); |
|
68
|
|
|
} elseif ($class = $this->getClass($manager)) { |
|
|
|
|
|
|
69
|
|
|
$manager = $this->build($class, $args); |
|
70
|
|
|
} else { |
|
71
|
|
|
throw new Exception("{$manager} is not a recognized data manager"); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
return $manager; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|