This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
TemplateModelFinder.php
206 lines (191 loc) · 8 KB
/
TemplateModelFinder.php
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
<?php
/**
* @link https://github.com/yii2tech
* @copyright Copyright (c) 2015 Yii2tech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace yii2tech\activemail;
use yii\base\Component;
use Yii;
use yii\base\InvalidConfigException;
use yii\di\Instance;
use yii\helpers\FileHelper;
/**
* TemplateModelFinder allows finding active messages and template models.
* It could be useful while creating administration panel for the mail template management.
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
class TemplateModelFinder extends Component
{
/**
* @var string namespace which under all active message classes declared.
*/
public $activeMessageNamespace = 'app\mail\active';
/**
* @var string path to directory, which contains active message classes source files.
* If not set path composed from [[activeMessageNamespace]] will be used.
*/
public $activeMessageFilePath;
/**
* @var TemplateStorage|string|array the mail template storage object or the application component ID.
* After the TemplateModelFinder object is created, if you want to change this property, you should only assign it
* with an object.
*/
public $mailTemplateStorage = 'mailTemplateStorage';
/**
* @var string name of the ActiveRecord class, which should be used for template finding.
* This class should match [[\yii\db\ActiveRecordInterface]] interface.
* If not set and [[mailTemplateStorage]] refers to [[TemplateStorageActiveRecord]], this value as well as
* [[templateDataAttributes]] and [[templateNameAttribute]] will be copied from the storage instance.
*/
public $activeRecordClass;
/**
* @var array list of ActiveRecord attributes, which should compose the template data.
* Only these fields will be selected while querying template row.
* You may adjust fields list according to the actual ActiveRecord class.
*/
public $templateDataAttributes = ['subject', 'bodyHtml'];
/**
* @var string name of the ActiveRecord attribute, which stores the template name.
*/
public $templateNameAttribute = 'name';
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->mailTemplateStorage = Instance::ensure($this->mailTemplateStorage, TemplateStorage::className());
if ($this->mailTemplateStorage instanceof TemplateStorageActiveRecord) {
if ($this->activeRecordClass === null) {
$this->activeRecordClass = $this->mailTemplateStorage->activeRecordClass;
$this->templateDataAttributes = $this->mailTemplateStorage->templateDataAttributes;
$this->templateNameAttribute = $this->mailTemplateStorage->templateNameAttribute;
}
}
}
/**
* Finds all active messages available for this application.
* @return ActiveMessage[] list of active messages.
*/
public function findAllActiveMessages()
{
$activeMessageNamespace = trim($this->activeMessageNamespace, '\\');
if (empty($this->activeMessageFilePath)) {
$activeMessageFilePath = Yii::getAlias('@' . str_replace('\\', DIRECTORY_SEPARATOR, $this->activeMessageNamespace));
} else {
$activeMessageFilePath = Yii::getAlias($this->activeMessageFilePath);
}
$files = FileHelper::findFiles($activeMessageFilePath, ['only' => ['*.php']]);
$activeMessages = [];
foreach ($files as $file) {
$className = $activeMessageNamespace . '\\' . basename($file, '.php');
try {
$reflection = new \ReflectionClass($className);
} catch (\Exception $exception) {
continue;
}
if ($reflection->isAbstract() || !$reflection->isSubclassOf(ActiveMessage::className())) {
continue;
}
$activeMessages[] = $reflection->newInstance();
}
return $activeMessages;
}
/**
* Finds active message by template name.
* @param string $templateName template name.
* @return ActiveMessage|null active message instance.
*/
public function findActiveMessage($templateName)
{
$activeMessages = $this->findAllActiveMessages();
foreach ($activeMessages as $activeMessage) {
if ($activeMessage->templateName() === $templateName) {
return $activeMessage;
}
}
return null;
}
/**
* Finds template models for all available template names.
* If existing model not found, new one will be created.
* @return \yii\db\ActiveRecordInterface[] list of template models.
* @throws InvalidConfigException on invalid configuration.
*/
public function findAllTemplateModels()
{
$activeMessages = $this->findAllActiveMessages();
if (empty($activeMessages)) {
return [];
}
$existingMailTemplateModels = $this->createQuery()->all();
return $this->combineActiveMessagesWithTemplates($activeMessages, $existingMailTemplateModels);
}
/**
* Finds template model for specified template name.
* If existing model not found, new one will be created.
* @param string $templateName mail template name.
* @return \yii\db\ActiveRecordInterface|null template model instance.
*/
public function findTemplateModel($templateName)
{
$activeMessage = $this->findActiveMessage($templateName);
if (!is_object($activeMessage)) {
return null;
}
$existingMailTemplateModels = $this->createQuery()->where([$this->templateNameAttribute => $templateName])->all();
$models = $this->combineActiveMessagesWithTemplates([$activeMessage], $existingMailTemplateModels);
return array_shift($models);
}
/**
* Creates ActiveQuery for [[activeRecordClass]].
* @return \yii\db\ActiveQueryInterface active query instance.
* @throws InvalidConfigException on invalid configuration.
*/
protected function createQuery()
{
/* @var $activeRecordClass \yii\db\ActiveRecordInterface */
$activeRecordClass = $this->activeRecordClass;
if (empty($activeRecordClass)) {
throw new InvalidConfigException('"' . get_class($this) . '::activeRecordClass" should be specified.');
}
return $activeRecordClass::find();
}
/**
* Combines active messages with template models, ensuring template model for each active message.
* @param ActiveMessage[] $activeMessages active messages.
* @param \yii\db\ActiveRecordInterface[] $templateModels existing template models.
* @return \yii\db\ActiveRecordInterface[] list of template models.
* @throws InvalidConfigException on invalid configuration.
*/
protected function combineActiveMessagesWithTemplates(array $activeMessages, array $templateModels)
{
$templateNameAttribute = $this->templateNameAttribute;
if (empty($templateNameAttribute)) {
throw new InvalidConfigException('"' . get_class($this) . '::templateNameAttribute" should be specified.');
}
$result = [];
foreach ($activeMessages as $activeMessage) {
$matchFound = false;
foreach ($templateModels as $existingTemplateModelKey => $existingTemplateModel) {
if ($existingTemplateModel->{$templateNameAttribute} == $activeMessage->templateName()) {
$result[] = $existingTemplateModel;
unset($templateModels[$existingTemplateModelKey]);
$matchFound = true;
break;
}
}
if (!$matchFound) {
$newTemplateModel = new $this->activeRecordClass();
foreach ($this->templateDataAttributes as $attribute) {
$newTemplateModel->$attribute = $activeMessage->$attribute;
}
$result[] = $newTemplateModel;
}
}
return $result;
}
}