1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Ajde_Component_Js extends Ajde_Component_Resource |
4
|
|
|
{ |
5
|
|
|
public static function processStatic(Ajde_Template_Parser $parser, $attributes) |
6
|
|
|
{ |
7
|
|
|
$instance = new self($parser, $attributes); |
8
|
|
|
|
9
|
|
|
return $instance->process(); |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
public function process() |
13
|
|
|
{ |
14
|
|
|
// TODO: check for required attributes |
15
|
|
|
// if (!array_key_exists('library', $this->attributes) || !array_key_exists('version', $this->attributes)) { |
16
|
|
|
// throw new Ajde_Component_Exception(); |
17
|
|
|
// } |
18
|
|
|
if (array_key_exists('library', $this->attributes)) { |
19
|
|
|
$this->requireJsLibrary($this->attributes['library'], $this->attributes['version']); |
20
|
|
|
} elseif (array_key_exists('action', $this->attributes)) { |
21
|
|
|
$this->requireResource( |
22
|
|
|
Ajde_Resource_Local::TYPE_JAVASCRIPT, |
23
|
|
|
$this->attributes['action'], |
24
|
|
|
issetor($this->attributes['format'], null), |
25
|
|
|
issetor($this->attributes['base'], null), |
26
|
|
|
issetor($this->attributes['position'], null), |
27
|
|
|
issetor($this->attributes['arguments'], '') |
28
|
|
|
); |
29
|
|
|
} elseif (array_key_exists('filename', $this->attributes)) { |
30
|
|
|
$this->requirePublicResource( |
31
|
|
|
Ajde_Resource_Local::TYPE_JAVASCRIPT, |
32
|
|
|
$this->attributes['filename'], |
33
|
|
|
issetor($this->attributes['position'], null), |
34
|
|
|
issetor($this->attributes['arguments'], '') |
35
|
|
|
); |
36
|
|
|
} elseif (array_key_exists('url', $this->attributes)) { |
37
|
|
|
$this->requireRemoteResource( |
38
|
|
|
Ajde_Resource_Local::TYPE_JAVASCRIPT, |
39
|
|
|
$this->attributes['url'], |
40
|
|
|
issetor($this->attributes['position'], null), |
41
|
|
|
issetor($this->attributes['arguments'], '') |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function requireJsLibrary($library, $version = false) |
47
|
|
|
{ |
48
|
|
|
if ($version === false) { |
49
|
|
|
$url = Ajde_Resource_JsLibrary::getCdnJsUrl($library); |
50
|
|
|
} else { |
51
|
|
|
$url = Ajde_Resource_JsLibrary::getUrl($library, $version); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$resource = new Ajde_Resource_Remote(Ajde_Resource::TYPE_JAVASCRIPT, $url); |
55
|
|
|
$this->getParser()->getDocument()->addResource($resource, Ajde_Document_Format_Html::RESOURCE_POSITION_TOP); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: