1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
abstract class Ajde_Resource extends Ajde_Object_Standard |
4
|
|
|
{ |
5
|
|
|
const TYPE_JAVASCRIPT = 'js'; |
6
|
|
|
const TYPE_STYLESHEET = 'css'; |
7
|
|
|
|
8
|
|
|
public function __construct($type) |
9
|
|
|
{ |
10
|
|
|
$this->setType($type); |
|
|
|
|
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
public function __toString() |
14
|
|
|
{ |
15
|
|
|
return implode(', ', $this->_data); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
abstract public function getFilename(); |
19
|
|
|
|
20
|
|
|
abstract protected function getLinkUrl(); |
21
|
|
|
|
22
|
|
|
protected static function exist($filename) |
23
|
|
|
{ |
24
|
|
|
if (is_file(self::realpath($filename))) { |
25
|
|
|
return true; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return false; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected static function realpath($filename) |
32
|
|
|
{ |
33
|
|
|
// dump($filename, realpath(LOCAL_ROOT . $filename)); |
34
|
|
|
return realpath(LOCAL_ROOT.$filename); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function encodeFingerprint($array) |
38
|
|
|
{ |
39
|
|
|
return self::_urlEncode(serialize($array)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function decodeFingerprint($fingerprint) |
43
|
|
|
{ |
44
|
|
|
return unserialize(self::_urlDecode($fingerprint)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public static function _urlDecode($string) |
48
|
|
|
{ |
49
|
|
|
return base64_decode($string); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public static function _urlEncode($string) |
53
|
|
|
{ |
54
|
|
|
return base64_encode($string); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public static function getLinkTemplateFilename($type, $format = 'null') |
58
|
|
|
{ |
59
|
|
|
if (Ajde::app()->getDocument()->hasLayout()) { |
|
|
|
|
60
|
|
|
$layout = Ajde::app()->getDocument()->getLayout(); |
61
|
|
|
} else { |
62
|
|
|
$layout = new Ajde_Layout(config('layout.frontend')); |
63
|
|
|
} |
64
|
|
|
$format = issetor($format, 'html'); |
|
|
|
|
65
|
|
|
|
66
|
|
|
$dirPrefixPatterns = [ |
67
|
|
|
APP_DIR, |
68
|
|
|
CORE_DIR, |
69
|
|
|
]; |
70
|
|
|
foreach ($dirPrefixPatterns as $dirPrefixPattern) { |
71
|
|
|
$prefixedLayout = $dirPrefixPattern.LAYOUT_DIR; |
72
|
|
|
if (self::exist($prefixedLayout.$layout->getName().'/link/'.$type.'.'.$format.'.php')) { |
73
|
|
|
return $prefixedLayout.$layout->getName().'/link/'.$type.'.'.$format.'.php'; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getType() |
81
|
|
|
{ |
82
|
|
|
return $this->get('type'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function setPosition($position) |
86
|
|
|
{ |
87
|
|
|
$this->set('position', $position); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getPosition() |
91
|
|
|
{ |
92
|
|
|
return $this->get('position'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function _getLinkTemplateFilename() |
96
|
|
|
{ |
97
|
|
|
$format = $this->hasFormat() ? $this->getFormat() : null; |
|
|
|
|
98
|
|
|
|
99
|
|
|
return self::getLinkTemplateFilename($this->getType(), $format); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getLinkCode() |
103
|
|
|
{ |
104
|
|
|
ob_start(); |
105
|
|
|
|
106
|
|
|
// variables for use in included link template |
107
|
|
|
$url = $this->getLinkUrl(); |
108
|
|
|
$arguments = $this->hasArguments() ? $this->getArguments() : ''; |
|
|
|
|
109
|
|
|
|
110
|
|
|
// create temporary resource for link filename |
111
|
|
|
$linkFilename = $this->_getLinkTemplateFilename(); |
112
|
|
|
|
113
|
|
|
// TODO: performance gain? |
114
|
|
|
// Ajde_Cache::getInstance()->addFile($linkFilename); |
115
|
|
|
if ($linkFilename) { |
|
|
|
|
116
|
|
|
include LOCAL_ROOT.$linkFilename; |
117
|
|
|
} else { |
118
|
|
|
throw new Ajde_Exception('Link filename for '.$url.' not found'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$contents = ob_get_contents(); |
122
|
|
|
ob_end_clean(); |
123
|
|
|
|
124
|
|
|
return $contents; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function getContents() |
128
|
|
|
{ |
129
|
|
|
ob_start(); |
130
|
|
|
|
131
|
|
|
$filename = $this->getFilename(); |
132
|
|
|
|
133
|
|
|
Ajde_Cache::getInstance()->addFile($filename); |
134
|
|
|
if ($this->exist($filename)) { |
135
|
|
|
include $this->realpath($filename); |
136
|
|
|
} else { |
137
|
|
|
throw new Exception("Couldn't find resource ".$filename); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$contents = ob_get_contents(); |
141
|
|
|
ob_end_clean(); |
142
|
|
|
|
143
|
|
|
return $contents; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
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: