ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
AbstractComponentRenderer.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2016 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
6 
10 
18 abstract class AbstractComponentRenderer implements ComponentRenderer {
22  private $ui_factory;
23 
27  private $tpl_factory;
28 
32  private $lng;
33 
37  private $js_binding;
38 
43  $this->ui_factory = $ui_factory;
44  $this->tpl_factory = $tpl_factory;
45  $this->lng = $lng;
46  $this->js_binding = $js_binding;
47  }
48 
52  public function registerResources(ResourceRegistry $registry) {
53  }
54 
62  final protected function getUIFactory() {
63  return $this->ui_factory;
64  }
65 
72  final public function txt($id) {
73  return $this->lng->txt($id);
74  }
75 
88  final protected function getTemplate($name, $purge_unfilled_vars, $purge_unused_blocks) {
89  $component = $this->getMyComponent();
90  $path = "src/UI/templates/default/$component/$name";
91  return $this->tpl_factory->getTemplate($path, $purge_unfilled_vars, $purge_unused_blocks);
92  }
93 
103  final protected function bindJavaScript(JavaScriptBindable $component) {
104  $binder = $component->getOnLoadCode();
105  if ($binder === null) {
106  return null;
107  }
108  $id = $this->js_binding->createId();
109  $on_load_code = $binder($id);
110  if (!is_string($on_load_code)) {
111  throw new \LogicException(
112  "Expected JavaScript binder to return string".
113  " (used component: ".get_class($component).")");
114  }
115  $this->js_binding->addOnLoadCode($on_load_code);
116  return $id;
117  }
118 
119 
128  final protected function checkComponent(Component $component) {
129  $interfaces = $this->getComponentInterfaceName();
130  if(!is_array($interfaces)){
131  throw new \LogicException(
132  "Expected array, found '".(string)(null)."' when rendering.");
133  }
134 
135  foreach ($interfaces as $interface) {
136  if ($component instanceof $interface) {
137  return;
138  }
139  }
140  $ifs = implode(", ", $interfaces);
141  throw new \LogicException(
142  "Expected $ifs, found '".get_class($component)."' when rendering.");
143  }
144 
152  abstract protected function getComponentInterfaceName();
153 
154  // TODO: We might want to cache this.
155  private function getMyComponent() {
156  $class = get_class($this);
157  $matches = array();
158  // Extract component
159  $re = "%ILIAS\\\\UI\\\\Implementation\\\\Component\\\\(\\w+)\\\\(\\w+)%";
160  if (preg_match($re, $class, $matches) !== 1) {
161  throw new \LogicException(
162  "The Renderer needs to be located in ILIAS\\UI\\Implementation\\Component\\*.");
163  }
164  return $matches[1];
165  }
166 }
Registry for resources required by rendered output like Javascript or CSS.
$path
Definition: aliased.php:25
checkComponent(Component $component)
Check if a given component fits this renderer and throw if that is not the case. ...
An entity that renders components to a string output.
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
getComponentInterfaceName()
Get the name of the component-interface this renderer is supposed to render.
This is how the factory for UI elements looks.
Definition: Factory.php:15
getOnLoadCode()
Get the currently bound on load code.
__construct(Factory $ui_factory, TemplateFactory $tpl_factory, \ilLanguage $lng, JavaScriptBinding $js_binding)
Component renderers must only depend on a UI-Factory and a Template Factory.
Provides methods to interface with javascript.
Create styles array
The data for the language used.
language handling
getTemplate($name, $purge_unfilled_vars, $purge_unused_blocks)
Get template of component this renderer is made for.
registerResources(ResourceRegistry $registry)
Announce resources this renderer requires.null
Interface for a factory that provides templates.
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.