-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseControl.php
More file actions
233 lines (203 loc) · 6.19 KB
/
BaseControl.php
File metadata and controls
233 lines (203 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
namespace SnapCRUD;
use Nette\Reflection\ClassType,
Nette\DI;
/**
* BaseControl
* @author Eduard Kracmar <kracmar@dannax.sk>
* @copyright Copyright (c) 2006-2011 Eduard Kracmar, DANNAX (http://www.dannax.sk)
* @abstract
*/
abstract class BaseControl extends \Nette\Application\UI\Control
{
/**
* @var string
*/
protected $templateFilename;
/**
* @var DI\Container
*/
protected $context;
protected $ident;
/** Events */
public $onEdit = array();
public $onSave = array();
public $onAdd = array();
public $onBeforeSave = array();
public $onBeforeUpdate = array();
public $onBeforeInsert = array();
public $onAfterUpdate = array();
public $onAfterInsert = array();
public $onAfterSave = array();
public $onRestore = array();
public $onBefore = array();
public $onAfter = array();
protected function attached($control)
{
parent::attached($control);
$this->setContext($control->getContext());
}
/**
* Sets context
* @param DI\Container $context
* @return this
*/
public function setContext(DI\Container $context)
{
$this->context = new DI\Container();
$this->context->parameters['productionMode'] = $context->parameters['productionMode'];
$this->ident = \preg_replace('#[\\\/:]+#', '_', ClassType::from($this)->getNamespaceName() . '_' .
$this->getPresenter()->getName() . '_' .
$this->getPresenter()->getAction() . '_' .
$this->getUniqueId());
# lazy cache
$control = $this;
$this->context->addService('cache', function() use ($control, $context)
{
return $control->createServiceCache($context);
});
# lazy session
$this->context->addService('sessionSection', function() use ($control, $context)
{
return $control->createServiceSessionSection($context);
});
# lazy cacheStorage
$this->context->addService('cacheStorage', function() use ($context)
{
return $context->cacheStorage;
});
# translator
if ($context->hasService('translator')) {
$this->context->addService('translator', $context->translator);
$this->template->setTranslator($this->context->translator);
}
# lazy texy
if ($context->hasService('texy')) {
$this->context->addService('texy', function() use ($context)
{
return $context->texy;
});
}
# lazy latteEngine
if ($context->hasService('latteEngine')) {
$this->context->addService('latteEngine', function() use ($context)
{
return $context->latteEngine;
});
}
# lazy doctrine
if ($context->hasService('doctrine')) {
$this->context->addService('doctrine', function() use ($context)
{
return $context->doctrine;
});
}
# lazy Nette\Database
if ($context->hasService('database')) {
$this->context->addService('database', function() use ($context)
{
return $context->database;
});
}
# lazy datafeed
if ($context->hasService('datafeed')) {
$this->context->addService('datafeed', function() use ($context)
{
return $context->datafeed;
});
}
$this->context->parameters['wwwDir'] = $context->parameters['wwwDir'];
return $this;
}
/**
* Gets context
* @return DI\Container
*/
public function getContext()
{
return $this->context;
}
/**
* Factory for cache (in case of reconfiguring and easy overloading)
* @param DI\Container $context
*/
public function createServiceCache(DI\Container $context)
{
return new \Nette\Caching\Cache($context->cacheStorage, $this->ident);
}
/**
* Factory for session section (in case of reconfiguring and easy overloading)
* @param DI\Container $context
*/
public function createServiceSessionSection(DI\Container $context)
{
return $context->session->getSection($this->ident);
}
protected function getAntecessorsFilenames()
{
$pointer = ClassType::from($this);
$result = array();
do {
array_push($result, $pointer->getFileName());
$pointer = $pointer->getParentClass();
} while ($pointer !== null);
return $result;
}
/**
* Gets template filename
* @return string
*/
protected function getTemplateFilename()
{
if (!$this->templateFilename) {
$this->templateFilename = dirname(ClassType::from($this)->getFileName()) . '/' . ClassType::from($this)->getShortName() . '.latte';
}
return $this->templateFilename;
}
/**
* Sets template file
* @var string $templateFilename
* @return this
*/
public function setTemplateFilename($templateFilename)
{
$this->templateFilename = $templateFilename;
return $this;
}
/**
* Sets locate template file
* @param string $templateFilename
* @return this
*/
public function setLocaleTemplateFilename($templateFilename)
{
$this->setTemplateFilename(__DIR__ . '.' . $templateFilename);
return $this;
}
/**
* Build cache key
*/
protected function buildKey()
{
$args = \func_get_args();
$parts = array();
foreach ($args as $arg) {
if (is_array($arg)) {
$parts[] = implode('|', $arg);
} else {
$parts[] = $arg;
}
}
return md5($this->getPresenter()->getContext()->parameters['application']['md5Salt'] . implode('|', $parts));
}
/**
* @inheritdoc
* @param \Nette\Templating\Template
* @return void
*/
public function templatePrepareFilters($template)
{
$template->registerFilter($this->context->latteEngine);
$template->registerHelper('texy', array($this->context->texy, 'process'));
}
}