Commit 5166024bcab8626a5d773971fa2db5f874837bf4
1 parent
8a4792e4
extended classes
Showing
4 changed files
with
314 additions
and
2 deletions
Show diff stats
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * Created by PhpStorm. | ||
| 4 | + * User: Alex Savenko | ||
| 5 | + * Date: 14.02.2017 | ||
| 6 | + * Time: 16:32 | ||
| 7 | + */ | ||
| 8 | + | ||
| 9 | +namespace App\Mvc; | ||
| 10 | + | ||
| 11 | + | ||
| 12 | +use PhalconApi\Constants\HttpMethods; | ||
| 13 | +use PhalconRest\Api\ApiCollection; | ||
| 14 | + | ||
| 15 | +class ExtendedApiCollection extends ApiCollection { | ||
| 16 | + | ||
| 17 | + public function endpoint(ExtendedApiEndpoint $endpoint) | ||
| 18 | + { | ||
| 19 | + $this->endpointsByName[$endpoint->getName()] = $endpoint; | ||
| 20 | + | ||
| 21 | + switch ($endpoint->getHttpMethod()) { | ||
| 22 | + | ||
| 23 | + case HttpMethods::GET: | ||
| 24 | + | ||
| 25 | + $this->get($endpoint->getPath(), $endpoint->getHandlerMethod(), $this->createRouteName($endpoint)); | ||
| 26 | + break; | ||
| 27 | + | ||
| 28 | + case HttpMethods::POST: | ||
| 29 | + | ||
| 30 | + $this->post($endpoint->getPath(), $endpoint->getHandlerMethod(), $this->createRouteName($endpoint)); | ||
| 31 | + break; | ||
| 32 | + | ||
| 33 | + case HttpMethods::PUT: | ||
| 34 | + | ||
| 35 | + $this->put($endpoint->getPath(), $endpoint->getHandlerMethod(), $this->createRouteName($endpoint)); | ||
| 36 | + break; | ||
| 37 | + | ||
| 38 | + case HttpMethods::DELETE: | ||
| 39 | + | ||
| 40 | + $this->delete($endpoint->getPath(), $endpoint->getHandlerMethod(), $this->createRouteName($endpoint)); | ||
| 41 | + break; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + return $this; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | +} | ||
| 0 | \ No newline at end of file | 48 | \ No newline at end of file |
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * Created by PhpStorm. | ||
| 4 | + * User: Alex Savenko | ||
| 5 | + * Date: 14.02.2017 | ||
| 6 | + * Time: 16:15 | ||
| 7 | + */ | ||
| 8 | + | ||
| 9 | +namespace App\Mvc; | ||
| 10 | + | ||
| 11 | + | ||
| 12 | +use PhalconRest\Api\ApiEndpoint; | ||
| 13 | + | ||
| 14 | +class ExtendedApiEndpoint extends ApiEndpoint { | ||
| 15 | + | ||
| 16 | + protected $exampleRequest; | ||
| 17 | + | ||
| 18 | + /** | ||
| 19 | + * @param string $exampleRequest Example of the request of the endpoint | ||
| 20 | + * | ||
| 21 | + * @return $this | ||
| 22 | + */ | ||
| 23 | + public function exampleRequest($exampleRequest) | ||
| 24 | + { | ||
| 25 | + $this->exampleRequest = $exampleRequest; | ||
| 26 | + return $this; | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + /** | ||
| 30 | + * @return string Example of the request of the endpoint | ||
| 31 | + */ | ||
| 32 | + public function getExampleRequest() | ||
| 33 | + { | ||
| 34 | + return $this->exampleRequest; | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | +} | ||
| 0 | \ No newline at end of file | 38 | \ No newline at end of file |
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Mvc; | ||
| 4 | + | ||
| 5 | +use App\Mvc\ExtendedApiCollection; | ||
| 6 | +use App\Mvc\ExtendedApiEndpoint; | ||
| 7 | +use Phalcon\Di; | ||
| 8 | +use Phalcon\Mvc\Micro\CollectionInterface; | ||
| 9 | +use PhalconApi\Acl\MountableInterface; | ||
| 10 | +use PhalconRest\Constants\Services; | ||
| 11 | +use PhalconRest\Mvc\Controllers\CrudResourceController; | ||
| 12 | +use PhalconRest\Transformers\ModelTransformer; | ||
| 13 | + | ||
| 14 | +class ExtendedApiResource extends ExtendedApiCollection implements MountableInterface, CollectionInterface | ||
| 15 | +{ | ||
| 16 | + protected $model; | ||
| 17 | + protected $transformer; | ||
| 18 | + | ||
| 19 | + protected $itemKey; | ||
| 20 | + protected $collectionKey; | ||
| 21 | + | ||
| 22 | + protected $_modelPrimaryKey; | ||
| 23 | + | ||
| 24 | + /** | ||
| 25 | + * Returns resource with default values & all, find, create, update and delete endpoints pre-configured | ||
| 26 | + * | ||
| 27 | + * @param string $prefix Prefix for the resource (e.g. /user) | ||
| 28 | + * @param string $name Name for the resource (e.g. users) (optional) | ||
| 29 | + * | ||
| 30 | + * @return static | ||
| 31 | + */ | ||
| 32 | + public static function crud($prefix, $name = null) | ||
| 33 | + { | ||
| 34 | + return self::factory($prefix, $name) | ||
| 35 | + ->endpoint(ExtendedApiEndpoint::all()) | ||
| 36 | + ->endpoint(ExtendedApiEndpoint::find()) | ||
| 37 | + ->endpoint(ExtendedApiEndpoint::create()) | ||
| 38 | + ->endpoint(ExtendedApiEndpoint::update()) | ||
| 39 | + ->endpoint(ExtendedApiEndpoint::remove()); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + /** | ||
| 43 | + * Returns resource with default values | ||
| 44 | + * | ||
| 45 | + * @param string $prefix Prefix for the resource (e.g. /user) | ||
| 46 | + * @param string $name Name for the resource (e.g. users) (optional) | ||
| 47 | + * | ||
| 48 | + * @return static | ||
| 49 | + */ | ||
| 50 | + public static function factory($prefix, $name = null) | ||
| 51 | + { | ||
| 52 | + $calledClass = get_called_class(); | ||
| 53 | + | ||
| 54 | + /** @var ExtendedApiResource $resource */ | ||
| 55 | + $resource = new $calledClass($prefix); | ||
| 56 | + | ||
| 57 | + if (!$resource->getItemKey()) { | ||
| 58 | + $resource->itemKey('items'); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + if (!$resource->getCollectionKey()) { | ||
| 62 | + $resource->collectionKey('items'); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + if (!$resource->getTransformer()) { | ||
| 66 | + $resource->transformer(ModelTransformer::class); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + if (!$resource->getHandler()) { | ||
| 70 | + $resource->setHandler(CrudResourceController::class); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + if (!$resource->getName() && $name) { | ||
| 74 | + $resource->name($name); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + if ($name) { | ||
| 78 | + $resource->name($name); | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + return $resource; | ||
| 82 | + } | ||
| 83 | + | ||
| 84 | + /** | ||
| 85 | + * @param string $model Classname of the model | ||
| 86 | + * | ||
| 87 | + * @return static | ||
| 88 | + */ | ||
| 89 | + public function model($model) | ||
| 90 | + { | ||
| 91 | + $this->model = $model; | ||
| 92 | + return $this; | ||
| 93 | + } | ||
| 94 | + | ||
| 95 | + /** | ||
| 96 | + * @return string|null Classname of the model | ||
| 97 | + */ | ||
| 98 | + public function getModel() | ||
| 99 | + { | ||
| 100 | + return $this->model; | ||
| 101 | + } | ||
| 102 | + | ||
| 103 | + /** | ||
| 104 | + * @return string|null Primary key of the model | ||
| 105 | + */ | ||
| 106 | + public function getModelPrimaryKey() | ||
| 107 | + { | ||
| 108 | + if (!$this->model) { | ||
| 109 | + return null; | ||
| 110 | + } | ||
| 111 | + | ||
| 112 | + if (!$this->_modelPrimaryKey) { | ||
| 113 | + | ||
| 114 | + /** @var \Phalcon\Mvc\Model\MetaData $modelsMetaData */ | ||
| 115 | + $modelsMetaData = Di::getDefault()->get(Services::MODELS_METADATA); | ||
| 116 | + | ||
| 117 | + $modelClass = $this->model; | ||
| 118 | + | ||
| 119 | + $this->_modelPrimaryKey = $modelsMetaData->getIdentityField(new $modelClass); | ||
| 120 | + } | ||
| 121 | + | ||
| 122 | + return $this->_modelPrimaryKey; | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + /** | ||
| 126 | + * @param string $transformer Classname of the transformer | ||
| 127 | + * | ||
| 128 | + * @return static | ||
| 129 | + */ | ||
| 130 | + public function transformer($transformer) | ||
| 131 | + { | ||
| 132 | + $this->transformer = $transformer; | ||
| 133 | + return $this; | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + /** | ||
| 137 | + * @return string|null Classname of the transformer | ||
| 138 | + */ | ||
| 139 | + public function getTransformer() | ||
| 140 | + { | ||
| 141 | + return $this->transformer; | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + /** | ||
| 145 | + * @param string $singleKey Response key for single item | ||
| 146 | + * | ||
| 147 | + * @return static | ||
| 148 | + * | ||
| 149 | + * @deprecated Use itemKey() instead | ||
| 150 | + */ | ||
| 151 | + public function singleKey($singleKey) | ||
| 152 | + { | ||
| 153 | + return $this->itemKey($singleKey); | ||
| 154 | + } | ||
| 155 | + | ||
| 156 | + /** | ||
| 157 | + * @param string $itemKey Response key for single item | ||
| 158 | + * | ||
| 159 | + * @return static | ||
| 160 | + */ | ||
| 161 | + public function itemKey($itemKey) | ||
| 162 | + { | ||
| 163 | + $this->itemKey = $itemKey; | ||
| 164 | + return $this; | ||
| 165 | + } | ||
| 166 | + | ||
| 167 | + /** | ||
| 168 | + * @return string Response key for single item | ||
| 169 | + * | ||
| 170 | + * @deprecated Use getItemKey() instead | ||
| 171 | + */ | ||
| 172 | + public function getSingleKey() | ||
| 173 | + { | ||
| 174 | + return $this->getItemKey(); | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | + /** | ||
| 178 | + * @return string Response key for single item | ||
| 179 | + */ | ||
| 180 | + public function getItemKey() | ||
| 181 | + { | ||
| 182 | + return ($this->itemKey ?: $this->name) ?: 'item'; | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + /** | ||
| 186 | + * @param string $multipleKey Response key for multiple items | ||
| 187 | + * | ||
| 188 | + * @return static | ||
| 189 | + * | ||
| 190 | + * @deprecated Use collectionKey() instead | ||
| 191 | + */ | ||
| 192 | + public function multipleKey($multipleKey) | ||
| 193 | + { | ||
| 194 | + return $this->collectionKey($multipleKey); | ||
| 195 | + } | ||
| 196 | + | ||
| 197 | + /** | ||
| 198 | + * @param string $collectionKey Response key for multiple items | ||
| 199 | + * | ||
| 200 | + * @return static | ||
| 201 | + */ | ||
| 202 | + public function collectionKey($collectionKey) | ||
| 203 | + { | ||
| 204 | + $this->collectionKey = $collectionKey; | ||
| 205 | + return $this; | ||
| 206 | + } | ||
| 207 | + | ||
| 208 | + /** | ||
| 209 | + * @return string Response key for multiple items | ||
| 210 | + * | ||
| 211 | + * @deprecated Use getCollectionKey() instead | ||
| 212 | + */ | ||
| 213 | + public function getMultipleKey() | ||
| 214 | + { | ||
| 215 | + return $this->getCollectionKey(); | ||
| 216 | + } | ||
| 217 | + | ||
| 218 | + /** | ||
| 219 | + * @return string Response key for multiple items | ||
| 220 | + */ | ||
| 221 | + public function getCollectionKey() | ||
| 222 | + { | ||
| 223 | + return ($this->collectionKey ?: $this->name) ?: 'items'; | ||
| 224 | + } | ||
| 225 | +} |
app/library/App/Resources/GaResource.php
| @@ -11,10 +11,12 @@ namespace App\Resources; | @@ -11,10 +11,12 @@ namespace App\Resources; | ||
| 11 | 11 | ||
| 12 | use App\Constants\AclRoles; | 12 | use App\Constants\AclRoles; |
| 13 | use App\Controllers\GaController; | 13 | use App\Controllers\GaController; |
| 14 | +use App\Mvc\ExtendedApiResource; | ||
| 14 | use PhalconRest\Api\ApiEndpoint; | 15 | use PhalconRest\Api\ApiEndpoint; |
| 16 | +use App\Mvc\ExtendedApiEndpoint; | ||
| 15 | use PhalconRest\Api\ApiResource; | 17 | use PhalconRest\Api\ApiResource; |
| 16 | 18 | ||
| 17 | -class GaResource extends ApiResource { | 19 | +class GaResource extends ExtendedApiResource { |
| 18 | 20 | ||
| 19 | public function initialize() { | 21 | public function initialize() { |
| 20 | 22 | ||
| @@ -27,7 +29,7 @@ class GaResource extends ApiResource { | @@ -27,7 +29,7 @@ class GaResource extends ApiResource { | ||
| 27 | ->deny(AclRoles::UNAUTHORIZED) | 29 | ->deny(AclRoles::UNAUTHORIZED) |
| 28 | ->handler(GaController::class) | 30 | ->handler(GaController::class) |
| 29 | 31 | ||
| 30 | - ->endpoint(ApiEndpoint::get('', 'getAction') | 32 | + ->endpoint(ExtendedApiEndpoint::get('', 'getAction') |
| 31 | ->allow(AclRoles::USER) | 33 | ->allow(AclRoles::USER) |
| 32 | ->description('Returns data from Google Analytics Api') | 34 | ->description('Returns data from Google Analytics Api') |
| 33 | ->exampleResponse([ | 35 | ->exampleResponse([ |
| @@ -41,6 +43,7 @@ class GaResource extends ApiResource { | @@ -41,6 +43,7 @@ class GaResource extends ApiResource { | ||
| 41 | ], | 43 | ], |
| 42 | 'full_docs' => 'https://developers.google.com/analytics/devguides/reporting/core/dimsmets', | 44 | 'full_docs' => 'https://developers.google.com/analytics/devguides/reporting/core/dimsmets', |
| 43 | ]) | 45 | ]) |
| 46 | + ->exampleRequest(['123' => '321']) | ||
| 44 | ) | 47 | ) |
| 45 | ; | 48 | ; |
| 46 | 49 |