| 1 | 2 | simandl | <?php |
| 2 | | | |
| 3 | | | namespace Phem\Libraries\UI\Model; |
| 4 | | | |
| 5 | | | use Doctrine\Common\Annotations\AnnotationReader; |
| 6 | | | use Phem\Core\Collection; |
| 7 | | | use Phem\Core\Object; |
| 8 | | | use Phem\Environment\EnvironmentManager; |
| 9 | | | use Phem\Libraries\UI\Annotations\ContentAttr; |
| 10 | | | use Phem\Libraries\UI\Annotations\DomAttr; |
| 11 | | | use Phem\Libraries\UI\Annotations\NoUpdateAttr; |
| 12 | | | use Phem\Libraries\UI\Annotations\StyleAttr; |
| 13 | | | use Phem\ToolSuite; |
| 14 | | | use Ratchet\Wamp\Exception; |
| 15 | | | use ReflectionProperty; |
| 16 | | | |
| 17 | | | /** |
| 18 | | | * @author Jakub PetrĹžĂlka <petrzilka@czweb.net> |
| 19 | | | */ |
| 20 | | | abstract class Component extends Object |
| 21 | | | { |
| 22 | | | abstract public function getTemplateName(); |
| 23 | | | |
| 24 | | | public function getFullName() |
| 25 | | | { |
| 26 | | | $parent = $this; |
| 27 | | | $nameArray = new Collection(); |
| 28 | | | |
| 29 | | | while (!is_a($parent, '\Phem\Libraries\UI\Model\Workspace')) |
| 30 | | | { |
| 31 | | | $nameArray->add($parent->getName()); |
| 32 | | | $parent = $parent->getParent(); |
| 33 | | | } |
| 34 | | | |
| 35 | | | $nameArray->reverse(false); |
| 36 | | | |
| 37 | | | $name = ""; |
| 38 | | | foreach ($nameArray as $namePart) |
| 39 | | | { |
| 40 | | | if ($name != "") |
| 41 | | | { |
| 42 | | | $name .= "_"; |
| 43 | | | } |
| 44 | | | $name .= $namePart; |
| 45 | | | } |
| 46 | | | return $name; |
| 47 | | | } |
| 48 | | | |
| 49 | | | public function getWorkspace() |
| 50 | | | { |
| 51 | | | $parent = $this; |
| 52 | | | while (!is_a($parent, '\Phem\Libraries\UI\Model\Workspace')) |
| 53 | | | { |
| 54 | | | $parent = $parent->getParent(); |
| 55 | | | } |
| 56 | | | return $parent; |
| 57 | | | } |
| 58 | | | |
| 59 | | | public function __call($method, $args) |
| 60 | | | { |
| 61 | | | if (!method_exists($this, $method)) |
| 62 | | | { |
| 63 | | | return; |
| 64 | | | } |
| 65 | | | |
| 66 | | | call_user_func_array(array($this, $method), $args); |
| 67 | | | if ((ToolSuite::startsWith($method, 'set') !== false) && ($method != "setValue")) |
| 68 | | | { |
| 69 | | | $attrName = lcfirst(substr($method, 3)); |
| 70 | | | |
| 71 | | | $reflectionProp = new ReflectionProperty($this, $attrName); |
| 72 | | | |
| 73 | | | $annotationReader = new AnnotationReader(); |
| 74 | | | $propAnnotations = $annotationReader |
| 75 | | | ->getPropertyAnnotations($reflectionProp); |
| 76 | | | |
| 77 | | | $foundAnnotations = new Collection(); |
| 78 | | | |
| 79 | | | $path = ""; |
| 80 | | | foreach ($propAnnotations as $annotation) |
| 81 | | | { |
| 82 | | | if (($annotation instanceof NoUpdateAttr) || ($annotation instanceof DomAttr) || ($annotation instanceof ContentAttr) || ($annotation instanceof StyleAttr)) |
| 83 | | | { |
| 84 | | | $foundAnnotations->add($annotation); |
| 85 | | | } |
| 86 | | | else if (($annotation instanceof \Phem\Libraries\UI\Annotations\DomPath)) |
| 87 | | | { |
| 88 | | | $path = $annotation->path; |
| 89 | | | } |
| 90 | | | } |
| 91 | | | |
| 92 | | | $fullName = $this->getFullName(); |
| 93 | | | if ($path != "") |
| 94 | | | { |
| 95 | | | $fullName .= "_".$path; |
| 96 | | | } |
| 97 | | | |
| 98 | | | if ($foundAnnotations->count() > 1) |
| 99 | | | { |
| 100 | | | throw new Exception(get_class($this) . "::" . $method . |
| 101 | | | "() - attribute must have either 0 or 1 annotation from NoUpdateAttr, DomAttr, ContentAttr, StyleAttr"); |
| 102 | | | } |
| 103 | | | else if ($foundAnnotations->count() == 1) |
| 104 | | | { |
| 105 | | | $annotation = $foundAnnotations->getFirst(); |
| 106 | | | |
| 107 | | | if ($annotation instanceof NoUpdateAttr) |
| 108 | | | { |
| 109 | | | return; |
| 110 | | | } |
| 111 | | | |
| 112 | | | $change = new Change(); |
| 113 | | | $change->setAttrName($annotation->name); |
| 114 | | | $change->setData($args[0]); |
| 115 | | | |
| 116 | | | $change->setObjectName($fullName); |
| 117 | | | |
| 118 | | | if ($annotation instanceof StyleAttr) |
| 119 | | | { |
| 120 | | | $change->setType(ChangeType::CHANGE_STYLE); |
| 121 | | | } |
| 122 | | | else if ($annotation instanceof DomAttr) |
| 123 | | | { |
| 124 | | | |
| 125 | | | $change->setType(ChangeType::CHANGE_ATTR); |
| 126 | | | } |
| 127 | | | else if ($annotation instanceof ContentAttr) |
| 128 | | | { |
| 129 | | | $change->setType(ChangeType::UPDATE_CONTENT); |
| 130 | | | } |
| 131 | | | } |
| 132 | | | else |
| 133 | | | { |
| 134 | | | $twig = EnvironmentManager::getTwig(); |
| 135 | | | |
| 136 | | | $data = $twig->render('@Phem/UI/' . $this->getTemplateName() . '.twig', array("component" => $this)); |
| 137 | | | |
| 138 | | | $change = new Change(); |
| 139 | | | $change->setType(ChangeType::REPLACE); |
| 140 | | | $change->setData($data); |
| 141 | | | $change->setObjectName($fullName); |
| 142 | | | } |
| 143 | | | |
| 144 | | | $this->getWorkspace()->getChanges()->set($this->getFullName() . "_" . $attrName, $change); |
| 145 | | | } |
| 146 | | | } |
| 147 | | | } |
| 148 | | | |