|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Blast Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) 2015-2017 Libre Informatique |
|
7
|
|
|
* |
|
8
|
|
|
* This file is licenced under the GNU LGPL v3. |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Blast\Bundle\CoreBundle\CodeGenerator; |
|
14
|
|
|
|
|
15
|
|
|
class CodeGeneratorRegistry |
|
16
|
|
|
{ |
|
17
|
|
|
private static $generators = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Registers an entity code generator service. |
|
21
|
|
|
* |
|
22
|
|
|
* @param CodeGeneratorInterface $codeGenerator the entity code generator service |
|
23
|
|
|
* |
|
24
|
|
|
* @throws \Exception |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function register(CodeGeneratorInterface $codeGenerator) |
|
27
|
|
|
{ |
|
28
|
|
|
$class = get_class($codeGenerator); |
|
29
|
|
|
if (!defined("$class::ENTITY_CLASS")) { |
|
30
|
|
|
throw new \Exception($class . ' must define a ENTITY_CLASS constant.'); |
|
31
|
|
|
} |
|
32
|
|
|
if (!defined("$class::ENTITY_FIELD")) { |
|
33
|
|
|
throw new \Exception($class . ' must define a ENTITY_FIELD constant.'); |
|
34
|
|
|
} |
|
35
|
|
|
self::$generators[$codeGenerator::ENTITY_CLASS][$codeGenerator::ENTITY_FIELD] = $codeGenerator; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Returns the last registered entity code generator service id for a given entity class and field. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $entityClass |
|
42
|
|
|
* @param string $entityField |
|
43
|
|
|
* |
|
44
|
|
|
* @return CodeGeneratorInterface |
|
45
|
|
|
* |
|
46
|
|
|
* @throws \Exception |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function getCodeGenerator($entityClass, $entityField = 'code') |
|
49
|
|
|
{ |
|
50
|
|
|
if (!isset(self::$generators[$entityClass][$entityField])) { |
|
51
|
|
|
throw new \Exception("There is no registered entity code generator for class $entityClass and field $entityField"); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return self::$generators[$entityClass][$entityField]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns registred code generators for specifyed entity class. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $entityClass |
|
61
|
|
|
* |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
public static function getCodeGenerators($entityClass = null) |
|
65
|
|
|
{ |
|
66
|
|
|
if ($entityClass) { |
|
|
|
|
|
|
67
|
|
|
if (!isset(self::$generators[$entityClass])) { |
|
68
|
|
|
throw new \Exception("There is no registered entity code generator for class $entityClass"); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return self::$generators[$entityClass]; |
|
72
|
|
|
} else { |
|
73
|
|
|
return self::$generators; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $entityClass |
|
79
|
|
|
* |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function hasGeneratorForClass($entityClass) |
|
83
|
|
|
{ |
|
84
|
|
|
return !empty(self::$generators[$entityClass]); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: