1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) Florian Krämer |
4
|
|
|
* |
5
|
|
|
* Licensed under The MIT License |
6
|
|
|
* For full copyright and license information, please see the LICENSE.txt |
7
|
|
|
* Redistributions of files must retain the above copyright notice. |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Florian Krämer |
10
|
|
|
* @link https://github.com/burzum/cakephp-service-layer |
11
|
|
|
* @since 1.0.0 |
12
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License |
13
|
|
|
*/ |
14
|
|
|
declare(strict_types = 1); |
15
|
|
|
|
16
|
|
|
namespace Burzum\CakeServiceLayer\DomainModel; |
17
|
|
|
|
18
|
|
|
use Cake\Core\App; |
19
|
|
|
use Cake\Core\ObjectRegistry; |
20
|
|
|
use RuntimeException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Domain Model Locator |
24
|
|
|
* |
25
|
|
|
* CakePHP style locator to load service classes |
26
|
|
|
*/ |
27
|
|
|
class DomainModelLocator extends ObjectRegistry |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Should resolve the class name for a given object type. |
31
|
|
|
* |
32
|
|
|
* @param string $class The class to resolve. |
33
|
|
|
* @return string|null The resolved name or false for failure. |
34
|
|
|
*/ |
35
|
2 |
|
protected function _resolveClassName(string $class): ?string |
36
|
|
|
{ |
37
|
2 |
|
return App::className($class, 'DomainModel'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Throw an exception when the requested object name is missing. |
42
|
|
|
* |
43
|
|
|
* @param string $class The class that is missing. |
44
|
|
|
* @param string $plugin The plugin $class is missing from. |
45
|
|
|
* @return void |
46
|
|
|
* @throws \Exception |
47
|
|
|
*/ |
48
|
1 |
|
protected function _throwMissingClassError(string $class, ?string $plugin): void |
49
|
|
|
{ |
50
|
1 |
|
if (!empty($plugin)) { |
51
|
|
|
$message = sprintf( |
52
|
|
|
'Domain Model class `%s` in plugin `%s` not found.', |
53
|
|
|
$class, |
54
|
|
|
$plugin |
55
|
|
|
); |
56
|
|
|
} else { |
57
|
1 |
|
$message = sprintf( |
58
|
1 |
|
'Domain Model class `%s` not found.', |
59
|
|
|
$class |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
throw new RuntimeException($message); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create an instance of a given classname. |
68
|
|
|
* |
69
|
|
|
* The passed config array will be used as constructor args for the new |
70
|
|
|
* object. |
71
|
|
|
* |
72
|
|
|
* @param string $class The class to build. |
73
|
|
|
* @param string $alias The alias of the object. |
74
|
|
|
* @param array $config The Configuration settings for construction |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
1 |
|
protected function _create($class, $alias, $config) |
78
|
|
|
{ |
79
|
1 |
|
if (empty($config)) { |
80
|
1 |
|
return new $class; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return new $class(...$config); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|