1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class Ajde_Resource_Local extends Ajde_Resource |
4
|
|
|
{ |
5
|
|
|
private $_filename; |
6
|
|
|
|
7
|
|
|
public function __construct($type, $base, $action, $format = 'html', $arguments = '') |
8
|
|
|
{ |
9
|
|
|
$this->setBase($base); |
|
|
|
|
10
|
|
|
$this->setAction($action); |
|
|
|
|
11
|
|
|
$this->setFormat($format); |
|
|
|
|
12
|
|
|
$this->setArguments($arguments); |
|
|
|
|
13
|
|
|
parent::__construct($type); |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* |
18
|
|
|
* @param string $type |
19
|
|
|
* @param string $base |
20
|
|
|
* @param string $action |
21
|
|
|
* @param string $format (optional) |
22
|
|
|
* @return Ajde_Resource |
23
|
|
|
*/ |
24
|
|
|
public static function lazyCreate($type, $base, $action, $format = 'html') |
25
|
|
|
{ |
26
|
|
|
if (self::getFilenameFromStatic($base, $type, $action, $format)) { |
|
|
|
|
27
|
|
|
return new self($type, $base, $action, $format); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* |
35
|
|
|
* @param string $hash |
36
|
|
|
* @return Ajde_Resource |
37
|
|
|
* @throws Ajde_Core_Exception_Deprecated |
38
|
|
|
* @throws Ajde_Exception |
39
|
|
|
*/ |
40
|
|
|
public static function fromHash($hash) |
|
|
|
|
41
|
|
|
{ |
42
|
|
|
// TODO: |
43
|
|
|
throw new Ajde_Core_Exception_Deprecated(); |
44
|
|
|
$session = new Ajde_Session('AC.Resource'); |
|
|
|
|
45
|
|
|
|
46
|
|
|
return $session->get($hash); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function fromFingerprint($type, $fingerprint) |
50
|
|
|
{ |
51
|
|
|
$array = self::decodeFingerprint($fingerprint); |
52
|
|
|
extract($array); |
53
|
|
|
|
54
|
|
|
return new Ajde_Resource_Local($type, $b, $a, $f); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getFingerprint() |
58
|
|
|
{ |
59
|
|
|
$array = ['b' => $this->getBase(), 'a' => $this->getAction(), 'f' => $this->getFormat()]; |
60
|
|
|
|
61
|
|
|
return $this->encodeFingerprint($array); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getBase() |
65
|
|
|
{ |
66
|
|
|
return $this->get('base'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getAction() |
70
|
|
|
{ |
71
|
|
|
return $this->get('action'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getFormat() |
75
|
|
|
{ |
76
|
|
|
return $this->get('format'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getArguments() |
80
|
|
|
{ |
81
|
|
|
return $this->get('arguments'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected static function _getFilename($base, $type, $action, $format) |
85
|
|
|
{ |
86
|
|
|
$dirPrefixPatterns = [ |
87
|
|
|
CORE_DIR, |
88
|
|
|
APP_DIR |
89
|
|
|
]; |
90
|
|
|
$layoutDir = 'layout.' . Ajde::app()->getDocument()->getLayout()->getName() . DIRECTORY_SEPARATOR; |
91
|
|
|
$layoutPrefixPatterns = ['', $layoutDir]; |
92
|
|
|
|
93
|
|
|
$filename = false; |
94
|
|
|
|
95
|
|
|
foreach ($dirPrefixPatterns as $dirPrefixPattern) { |
96
|
|
|
|
97
|
|
|
foreach ($layoutPrefixPatterns as $layoutPrefixPattern) { |
98
|
|
|
|
99
|
|
|
$prefixedBase = $dirPrefixPattern . $base; |
100
|
|
|
$formatResource = $prefixedBase . 'res/' . $type . DIRECTORY_SEPARATOR . $layoutPrefixPattern . $action . '.' . $format . '.' . $type; |
101
|
|
|
|
102
|
|
|
if (self::exist($formatResource)) { |
103
|
|
|
|
104
|
|
|
$filename = $formatResource; |
105
|
|
|
|
106
|
|
|
} else { |
107
|
|
|
|
108
|
|
|
$noFormatResource = $prefixedBase . 'res/' . $type . DIRECTORY_SEPARATOR . $layoutPrefixPattern . $action . '.' . $type; |
109
|
|
|
if (self::exist($noFormatResource)) { |
110
|
|
|
$filename = $noFormatResource; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $filename; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getFilename() |
121
|
|
|
{ |
122
|
|
|
if (!isset($this->_filename)) { |
123
|
|
|
$this->_filename = $this->_getFilename($this->getBase(), $this->getType(), $this->getAction(), |
124
|
|
|
$this->getFormat()); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (!$this->_filename) { |
128
|
|
|
// TODO: |
129
|
|
|
throw new Ajde_Exception(sprintf('Resource %s could not be found', |
130
|
|
|
$this->getBase() . 'res/' . $this->getType() . DS . $this->getAction() . '[.' . $this->getFormat() . '].' . $this->getType())); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this->_filename; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public static function getFilenameFromStatic($base, $type, $action, $format) |
137
|
|
|
{ |
138
|
|
|
return self::_getFilename($base, $type, $action, $format); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function getLinkUrl() |
142
|
|
|
{ |
143
|
|
|
$base = '_core/component:resourceLocal'; |
144
|
|
|
if (config("app.debug") === true) { |
145
|
|
|
$url = $base . '/' . urlencode($this->getFingerprint()) . '.' . $this->getType() . '?' . str_replace([ |
146
|
|
|
'%2F', |
147
|
|
|
'%5C' |
148
|
|
|
], ':', urlencode($this->getFilename())); |
149
|
|
|
} else { |
150
|
|
|
$url = $base . '/' . urlencode($this->getFingerprint()) . '.' . $this->getType(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $url; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
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: