-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceModel.php
More file actions
81 lines (65 loc) · 1.77 KB
/
ServiceModel.php
File metadata and controls
81 lines (65 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace SyncEngine\Model\Abstract;
use SyncEngine\Controller\DefaultController;
use SyncEngine\Model\Trait\Module;
use SyncEngine\Service\Locator\AbstractServiceModelLocator;
use SyncEngine\Service\Locator\Modules;
abstract class ServiceModel extends AbstractModel
{
use Module;
/**
* @required
*/
const SERVICE = '';
public static function create( string $name ): ?static
{
// @todo Factory.
return static::getServiceModelLocator()->get( $name );
}
public static function get( string $name ): ?static
{
return static::getServiceModelLocator()->get( $name );
}
public static function getAll(): ?array
{
return static::getServiceModelLocator()->getAll();
}
public static function getModelName( string $name = '' ): string
{
if ( $name ) {
if ( class_exists( $name ) && method_exists( $name , 'getModelName' ) ) {
return $name::getModelName();
}
return parent::getModelName( $name );
}
return parent::getModelName(
static::getServiceModelLocator()->getModelClass()::getClassName()
);
}
public static function getServiceModelLocator(): AbstractServiceModelLocator
{
if ( ! static::SERVICE ) {
throw new \ErrorException( 'No service locator defined.' );
}
return DefaultController::getContainer()->get( static::SERVICE );
}
public function getClassLocator(): string
{
return static::_getClassLocator();
}
/**
* @return string
* @internal Used for auto wiring.
*/
final public static function _getClassLocator(): string
{
$ref = ( new \ReflectionClass( static::class ) );
$name = $ref->getShortName();
$namespace = $ref->getNamespaceName();
if ( Modules::isFromModule( $namespace ) ) {
$module = Modules::getModulePackageName( $namespace );
return $module . ':' . $name;
}
return $name;
}
}