ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
11 
20 {
24  private $ui_factory;
25 
29  private $tpl_factory;
30 
34  private $lng;
35 
39  private $js_binding;
40 
45  {
46  $this->ui_factory = $ui_factory;
47  $this->tpl_factory = $tpl_factory;
48  $this->lng = $lng;
49  $this->js_binding = $js_binding;
50  }
51 
55  public function registerResources(ResourceRegistry $registry)
56  {
57  }
58 
66  final protected function getUIFactory()
67  {
68  return $this->ui_factory;
69  }
70 
77  final public function txt($id)
78  {
79  return $this->lng->txt($id);
80  }
81 
86  final public function toJS($key)
87  {
88  $this->lng->toJS($key);
89  }
90 
96  public function getLangKey()
97  {
98  return $this->lng->getLangKey();
99  }
100 
101 
105  final protected function getJavascriptBinding()
106  {
107  return $this->js_binding;
108  }
109 
122  final protected function getTemplate($name, $purge_unfilled_vars, $purge_unused_blocks)
123  {
124  $path = $this->getTemplatePath($name);
125  return $this->tpl_factory->getTemplate($path, $purge_unfilled_vars, $purge_unused_blocks);
126  }
127 
134  protected function getTemplatePath($name)
135  {
136  $component = $this->getMyComponent();
137  return "src/UI/templates/default/$component/$name";
138  }
139 
149  final protected function bindJavaScript(JavaScriptBindable $component)
150  {
151  if ($component instanceof Triggerer) {
152  $component = $this->addTriggererOnLoadCode($component);
153  }
154  return $this->bindOnloadCode($component);
155  }
156 
163  private function bindOnloadCode(JavaScriptBindable $component)
164  {
165  $binder = $component->getOnLoadCode();
166  if ($binder === null) {
167  return null;
168  }
169  $id = $this->js_binding->createId();
170  $on_load_code = $binder($id);
171  if (!is_string($on_load_code)) {
172  throw new \LogicException(
173  "Expected JavaScript binder to return string" .
174  " (used component: " . get_class($component) . ")"
175  );
176  }
177  $this->js_binding->addOnLoadCode($on_load_code);
178  return $id;
179  }
180 
187  private function addTriggererOnLoadCode(Triggerer $triggerer)
188  {
189  $triggered_signals = $triggerer->getTriggeredSignals();
190  if (count($triggered_signals) == 0) {
191  return $triggerer;
192  }
193  return $triggerer->withAdditionalOnLoadCode(function ($id) use ($triggered_signals) {
194  $code = "";
195  foreach ($triggered_signals as $triggered_signal) {
196  $signal = $triggered_signal->getSignal();
197  $event = $triggered_signal->getEvent();
198  $options = json_encode($signal->getOptions());
199  $code .=
200  "$('#{$id}').on('{$event}', function(event) {
201  $(this).trigger('{$signal}',
202  {
203  'id' : '{$signal}', 'event' : '{$event}',
204  'triggerer' : $(this),
205  'options' : JSON.parse('{$options}')
206  }
207  );
208  return false;
209  });";
210  }
211  return $code;
212  });
213  }
214 
223  final protected function checkComponent(Component $component)
224  {
225  $interfaces = $this->getComponentInterfaceName();
226  if (!is_array($interfaces)) {
227  throw new \LogicException(
228  "Expected array, found '" . (string) (null) . "' when rendering."
229  );
230  }
231 
232  foreach ($interfaces as $interface) {
233  if ($component instanceof $interface) {
234  return;
235  }
236  }
237  $ifs = implode(", ", $interfaces);
238  throw new \LogicException(
239  "Expected $ifs, found '" . get_class($component) . "' when rendering."
240  );
241  }
242 
250  abstract protected function getComponentInterfaceName();
251 
252  // TODO: We might want to cache this.
253  private function getMyComponent()
254  {
255  $class = get_class($this);
256  $matches = array();
257  // Extract component
258  $re = "%ILIAS\\\\UI\\\\Implementation\\\\Component\\\\(\\w+)\\\\(\\w+)%";
259  if (preg_match($re, $class, $matches) !== 1) {
260  throw new \LogicException(
261  "The Renderer needs to be located in ILIAS\\UI\\Implementation\\Component\\*."
262  );
263  }
264  return $matches[1];
265  }
266 }
Registry for resources required by rendered output like Javascript or CSS.
getTemplatePath($name)
Get the path to the template of this component.
addTriggererOnLoadCode(Triggerer $triggerer)
Add onload-code for triggerer.
checkComponent(Component $component)
Check if a given component fits this renderer and throw if that is not the case. ...
$code
Definition: example_050.php:99
An entity that renders components to a string output.
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.
if(!array_key_exists('StateId', $_REQUEST)) $id
toJS($key)
Add language var to client side (il.Language)
bindOnloadCode(JavaScriptBindable $component)
Bind the JavaScript onload-code.
getComponentInterfaceName()
Get the name of the component-interface this renderer is supposed to render.
if($format !==null) $name
Definition: metadata.php:146
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.
withAdditionalOnLoadCode(\Closure $binder)
Add some onload-code to the component instead of replacing the existing one.
getTriggeredSignals()
Get all triggered signals of this component.
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.
$key
Definition: croninfo.php:18
if(!isset($_REQUEST['ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20
bindJavaScript(JavaScriptBindable $component)
Bind the component to JavaScript.