Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

105 lines
3.3KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\rest;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. use yii\db\ActiveRecordInterface;
  11. use yii\web\NotFoundHttpException;
  12. /**
  13. * Action is the base class for action classes that implement RESTful API.
  14. *
  15. * @author Qiang Xue <qiang.xue@gmail.com>
  16. * @since 2.0
  17. */
  18. class Action extends \yii\base\Action
  19. {
  20. /**
  21. * @var string class name of the model which will be handled by this action.
  22. * The model class must implement [[ActiveRecordInterface]].
  23. * This property must be set.
  24. */
  25. public $modelClass;
  26. /**
  27. * @var callable a PHP callable that will be called to return the model corresponding
  28. * to the specified primary key value. If not set, [[findModel()]] will be used instead.
  29. * The signature of the callable should be:
  30. *
  31. * ```php
  32. * function ($id, $action) {
  33. * // $id is the primary key value. If composite primary key, the key values
  34. * // will be separated by comma.
  35. * // $action is the action object currently running
  36. * }
  37. * ```
  38. *
  39. * The callable should return the model found, or throw an exception if not found.
  40. */
  41. public $findModel;
  42. /**
  43. * @var callable a PHP callable that will be called when running an action to determine
  44. * if the current user has the permission to execute the action. If not set, the access
  45. * check will not be performed. The signature of the callable should be as follows,
  46. *
  47. * ```php
  48. * function ($action, $model = null) {
  49. * // $model is the requested model instance.
  50. * // If null, it means no specific model (e.g. IndexAction)
  51. * }
  52. * ```
  53. */
  54. public $checkAccess;
  55. /**
  56. * @inheritdoc
  57. */
  58. public function init()
  59. {
  60. if ($this->modelClass === null) {
  61. throw new InvalidConfigException(get_class($this) . '::$modelClass must be set.');
  62. }
  63. }
  64. /**
  65. * Returns the data model based on the primary key given.
  66. * If the data model is not found, a 404 HTTP exception will be raised.
  67. * @param string $id the ID of the model to be loaded. If the model has a composite primary key,
  68. * the ID must be a string of the primary key values separated by commas.
  69. * The order of the primary key values should follow that returned by the `primaryKey()` method
  70. * of the model.
  71. * @return ActiveRecordInterface the model found
  72. * @throws NotFoundHttpException if the model cannot be found
  73. */
  74. public function findModel($id)
  75. {
  76. if ($this->findModel !== null) {
  77. return call_user_func($this->findModel, $id, $this);
  78. }
  79. /* @var $modelClass ActiveRecordInterface */
  80. $modelClass = $this->modelClass;
  81. $keys = $modelClass::primaryKey();
  82. if (count($keys) > 1) {
  83. $values = explode(',', $id);
  84. if (count($keys) === count($values)) {
  85. $model = $modelClass::findOne(array_combine($keys, $values));
  86. }
  87. } elseif ($id !== null) {
  88. $model = $modelClass::findOne($id);
  89. }
  90. if (isset($model)) {
  91. return $model;
  92. } else {
  93. throw new NotFoundHttpException("Object not found: $id");
  94. }
  95. }
  96. }