1
|
|
|
<?php |
2
|
|
|
//[PHPCOMPRESSOR(remove,start)] |
3
|
|
|
/** |
4
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
5
|
|
|
* on 22.03.16 at 17:50 |
6
|
|
|
*/ |
7
|
|
|
namespace samsoncms\api\generator; |
8
|
|
|
|
9
|
|
|
use samsonphp\generator\Generator; |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Generic object-oriented programming class generator. |
13
|
|
|
* |
14
|
|
|
* @package samsoncms\api\generator |
15
|
|
|
*/ |
16
|
|
|
abstract class OOP |
17
|
|
|
{ |
18
|
|
|
/** @var Generator Code generation instance */ |
19
|
|
|
protected $generator; |
20
|
|
|
|
21
|
|
|
/** @var Metadata Entity query metadata */ |
22
|
|
|
protected $metadata; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* OOP constructor. |
26
|
|
|
* |
27
|
|
|
* @param Generator $generator Code generation instance |
28
|
|
|
* @param Metadata $metadata Entity query metadata |
29
|
|
|
*/ |
30
|
|
|
public function __construct(Generator $generator, Metadata $metadata) |
31
|
|
|
{ |
32
|
|
|
$this->metadata = $metadata; |
33
|
|
|
$this->generator = $generator; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Generic class generation. |
38
|
|
|
* |
39
|
|
|
* @param Metadata $metadata Entity metadata |
40
|
|
|
* |
41
|
|
|
* @return string Generated PHP class code |
42
|
|
|
*/ |
43
|
|
|
public function generate(Metadata $metadata = null) |
44
|
|
|
{ |
45
|
|
|
$metadata = null === $metadata ? $this->metadata : $metadata; |
46
|
|
|
|
47
|
|
|
$this->createUses($metadata); |
48
|
|
|
$this->createDefinition($metadata); |
49
|
|
|
$this->createConstants($metadata); |
50
|
|
|
$this->createStaticFields($metadata); |
51
|
|
|
$this->createStaticMethods($metadata); |
52
|
|
|
$this->createFields($metadata); |
53
|
|
|
$this->createMethods($metadata); |
54
|
|
|
$this->createConstructor($metadata); |
55
|
|
|
|
56
|
|
|
return $this->generator->endClass()->flush(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Class uses generation part. |
61
|
|
|
* |
62
|
|
|
* @param Metadata $metadata Entity metadata |
63
|
|
|
*/ |
64
|
|
|
protected function createUses(Metadata $metadata) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Class definition generation part. |
71
|
|
|
* |
72
|
|
|
* @param Metadata $metadata Entity metadata |
73
|
|
|
*/ |
74
|
|
|
abstract protected function createDefinition(Metadata $metadata); |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Class constants generation part. |
78
|
|
|
* |
79
|
|
|
* @param Metadata $metadata Entity metadata |
80
|
|
|
*/ |
81
|
|
|
protected function createConstants(Metadata $metadata) |
82
|
|
|
{ |
83
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Class static fields generation part. |
88
|
|
|
* |
89
|
|
|
* @param Metadata $metadata Entity metadata |
90
|
|
|
*/ |
91
|
|
|
protected function createStaticFields(Metadata $metadata) |
92
|
|
|
{ |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Class static methods generation part. |
98
|
|
|
* |
99
|
|
|
* @param Metadata $metadata Entity metadata |
100
|
|
|
*/ |
101
|
|
|
protected function createStaticMethods(Metadata $metadata) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Class fields generation part. |
108
|
|
|
* |
109
|
|
|
* @param Metadata $metadata Entity metadata |
110
|
|
|
*/ |
111
|
|
|
protected function createFields(Metadata $metadata) |
112
|
|
|
{ |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Class methods generation part. |
118
|
|
|
* |
119
|
|
|
* @param Metadata $metadata Entity metadata |
120
|
|
|
*/ |
121
|
|
|
protected function createMethods(Metadata $metadata) |
122
|
|
|
{ |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Class constructor generation part. |
128
|
|
|
* |
129
|
|
|
* @param Metadata $metadata Entity metadata |
130
|
|
|
*/ |
131
|
|
|
protected function createConstructor(Metadata $metadata) |
132
|
|
|
{ |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
137
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: