11<?php echo '<?php ' ;
22
33$ modelActions = [
4- 'index ' => yii \rest \IndexAction::class,
5- 'view ' => yii \rest \ViewAction::class,
6- 'create ' => yii \rest \CreateAction::class,
7- 'update ' => yii \rest \UpdateAction::class,
8- 'delete ' => yii \rest \UpdateAction::class,
4+ 'index ' => [
5+ 'class ' => yii \rest \IndexAction::class,
6+ ],
7+ 'view ' => [
8+ 'class ' => yii \rest \ViewAction::class,
9+ 'implementation ' => <<<'PHP'
10+ $model = $this->findModel($id);
11+ $this->checkAccess(ACTION_ID, $model);
12+ return $model;
13+ PHP
14+ ],
15+ 'create ' => [
16+ 'class ' => yii \rest \CreateAction::class,
17+ ],
18+ 'update ' => [
19+ 'class ' => yii \rest \UpdateAction::class,
20+ 'implementation ' => <<<'PHP'
21+ $model = $this->findModel($id);
22+ $this->checkAccess(ACTION_ID, $model);
23+
24+ $model->load(Yii::$app->getRequest()->getBodyParams(), '');
25+ if ($model->save() === false && !$model->hasErrors()) {
26+ throw new ServerErrorHttpException('Failed to update the object for unknown reason.');
27+ }
28+
29+ return $model;
30+ PHP
31+ ],
32+ 'delete ' => [
33+ 'class ' => yii \rest \DeleteAction::class,
34+ 'implementation ' => <<<'PHP'
35+ $model = $this->findModel($id);
36+ $this->checkAccess(ACTION_ID, $model);
37+
38+ if ($model->delete() === false) {
39+ throw new ServerErrorHttpException('Failed to delete the object for unknown reason.');
40+ }
41+
42+ \Yii::$app->response->setStatusCode(204);
43+ PHP
44+ ],
945];
46+ $ findModel = [];
1047
1148?>
1249
@@ -21,9 +58,9 @@ public function actions()
2158<?php
2259
2360foreach ($ actions as $ action ):
24- if (isset ($ modelActions [$ action ['id ' ]], $ action ['modelClass ' ])): ?>
61+ if (isset ($ modelActions [$ action ['id ' ]], $ action ['modelClass ' ]) && ( $ action [ ' idParam ' ] === null || $ action [ ' idParam ' ] === ' id ' ) ): ?>
2562 <?= var_export ($ action ['id ' ], true ) ?> => [
26- 'class' => \<?= $ modelActions [$ action ['id ' ]] ?> ::class,
63+ 'class' => \<?= $ modelActions [$ action ['id ' ]][ ' class ' ] ?> ::class,
2764 'modelClass' => <?= '\\' . $ action ['modelClass ' ] . '::class ' ?> ,
2865 'checkAccess' => [$this, 'checkAccess'],
2966 ],
@@ -84,23 +121,49 @@ public function checkAccess($action, $model = null, $params = [])
84121 {
85122 // TODO implement checkAccess
86123 }
87-
88124<?php
89125 foreach ($ actions as $ action ):
90126 if (isset ($ modelActions [$ action ['id ' ]], $ action ['modelClass ' ])) {
91- continue ;
127+ if ($ action ['idParam ' ] === null || $ action ['idParam ' ] === 'id ' ) {
128+ continue ;
129+ }
130+ if (isset ($ modelActions [$ action ['id ' ]]['implementation ' ])) {
131+ $ implementation = $ modelActions [$ action ['id ' ]]['implementation ' ];
132+ $ findModel [$ action ['modelClass ' ]] = 'find ' . \yii \helpers \StringHelper::basename ($ action ['modelClass ' ]) . 'Model ' ;
133+ $ implementation = str_replace ('findModel ' , $ findModel [$ action ['modelClass ' ]], $ implementation );
134+ $ implementation = str_replace ('$id ' , '$ ' .$ action ['idParam ' ], $ implementation );
135+ $ implementation = str_replace ('ACTION_ID ' , var_export ($ action ['id ' ], true ), $ implementation );
136+ }
92137 }
93138
94139 $ actionName = 'action ' . \yii \helpers \Inflector::id2camel ($ action ['id ' ]);
95140 $ actionParams = implode (', ' , array_map (function ($ p ) {
96141 return "\$$ p " ;
97142 }, $ action ['params ' ]));
98143 ?>
144+
99145 public function <?= $ actionName ?> (<?= $ actionParams ?> )
100146 {
101- // TODO implement <?= $ actionName ?>
147+ <?= $ implementation ?? ' // TODO implement ' . $ actionName ?>
102148
103149 }
150+ <?php endforeach ; ?>
151+ <?php foreach ($ findModel as $ modelName => $ methodName ): ?>
104152
153+ /**
154+ * Returns the <?= \yii \helpers \StringHelper::basename ($ modelName ) ?> model based on the primary key given.
155+ * If the data model is not found, a 404 HTTP exception will be raised.
156+ * @param string $id the ID of the model to be loaded.
157+ * @return \<?= $ modelName ?> the model found
158+ * @throws NotFoundHttpException if the model cannot be found.
159+ */
160+ public function <?= $ methodName ?> ($id)
161+ {
162+ $model = \<?= $ modelName ?> ::findOne($id);
163+ if ($model !== null) {
164+ return $model;
165+ }
166+ throw new NotFoundHttpException("Object not found: $id");
167+ }
105168<?php endforeach ; ?>
106169}
0 commit comments