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