Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
ClassMerger.php
Go to the documentation of this file.
1<?php
2
3
5
6
8use Ubiquity\orm\creator\Model;
10
22 protected $class;
23 protected $classCode;
24 protected Model $model;
25 protected $merged;
26 protected $extCode;
27
28 public function __construct(string $class,Model $model){
29 $this->class=$class;
30 $this->classCode=UIntrospection::getClassCode($class);
31 $this->model=$model;
32 $this->merged=false;
33 $this->extCode='';
34 }
35
36 public function merge(){
37 if(!$this->merged) {
38 $r = new \ReflectionClass($this->class);
39 $properties = $r->getProperties();
40 $newMembers = $this->model->getMembers();
41 $annotsEngine = $this->model->getAnnotsEngine();
42 foreach ($properties as $property) {
43 $propName = $property->getName();
44 $propComparator = new MemberComparator($this->class, $propName);
45 if (isset($newMembers[$propName])) {
46 $newMember = $newMembers[$propName];
47 $propComparator->compareTo($newMember);
48 $preservedAttributes = $propComparator->compareAttributes();
49 if (\count($preservedAttributes) > 0) {
50 $newMember->addAnnotations($preservedAttributes);
51 }
52 } else {
53 if ($propComparator->maintain()) {
54 $m = new Member($this->model, $annotsEngine, $propName);
55 $m->setTransient();
56 $this->model->addMember($m);
57 }
58 }
59 }
60 if(\method_exists(\ReflectionMethod::class, 'getAttributes')){
61 $actualMethods=$r->getMethods();
62 $newMethods=$this->model->getMethods();
63 foreach ($actualMethods as $reflectionMethod){
64 $code='';
65 $methodName=$reflectionMethod->getName();
66 if(\array_search($methodName,$newMethods)===false){
67 $code=UIntrospection::getMethodCode($reflectionMethod,$this->classCode);
68 $annotations=$reflectionMethod->getAttributes();
69 if(\count($annotations)>0) {
70 $code = $this->model->getAnnotsEngine()->getAnnotationsStr($annotations).$code;
71 }
72 }
73 $this->extCode.=$code;
74 }
75 }
76 $this->merged = true;
77 }
78 }
79
80 private function removeBlank($str){
81 return \str_replace ([' ',PHP_EOL,"\t"], '', $str);
82 }
83
84 public function getMethodCode($methodName,$newCode){
85 if(\method_exists($this->class,$methodName)){
86 $r=new \ReflectionMethod($this->class.'::'.$methodName);
87 $oldCode=UIntrospection::getMethodCode($r,$this->classCode);
88 if(\method_exists(\ReflectionMethod::class,'getAttributes')){
89 $annotations=$r->getAttributes();
90 if(\count($annotations)>0) {
91 $oldCode = $this->model->getAnnotsEngine()->getAnnotationsStr($annotations).$oldCode;
92 }
93 if($this->removeBlank($oldCode)!==$this->removeBlank($newCode)){
94 return $oldCode;
95 }
96 }
97 }
98 return $newCode;
99 }
100
104 public function getExtCode(): string {
105 return $this->extCode;
106 }
107
108}
__construct(string $class, Model $model)
getMethodCode($methodName, $newCode)
Ubiquity\utils\base$UIntrospection This class is part of Ubiquity.