├── .phpunit-watcher.yml ├── .styleci.yml ├── LICENSE.md ├── README.md ├── composer.json ├── config ├── common.php ├── params.php └── web.php ├── infection.json.dist ├── psalm.xml └── src ├── AbstractController.php ├── ResponseFactory ├── AbstractResponseFactory.php └── JsonResponseFactory.php ├── ResponseFactoryInterface.php ├── RestControllerTrait.php └── RestGroup.php /.phpunit-watcher.yml: -------------------------------------------------------------------------------- 1 | watch: 2 | directories: 3 | - src 4 | - tests 5 | fileMask: '*.php' 6 | notifications: 7 | passingTests: false 8 | failingTests: false 9 | phpunit: 10 | binaryPath: vendor/bin/phpunit 11 | timeout: 180 12 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: psr12 2 | risky: true 3 | 4 | version: 8 5 | 6 | finder: 7 | exclude: 8 | - docs 9 | - vendor 10 | - resources 11 | - views 12 | - public 13 | - templates 14 | not-name: 15 | - UnionCar.php 16 | - TimerUnionTypes.php 17 | - schema1.php 18 | 19 | enabled: 20 | - alpha_ordered_traits 21 | - array_indentation 22 | - array_push 23 | - combine_consecutive_issets 24 | - combine_consecutive_unsets 25 | - combine_nested_dirname 26 | - declare_strict_types 27 | - dir_constant 28 | - final_static_access 29 | - fully_qualified_strict_types 30 | - function_to_constant 31 | - hash_to_slash_comment 32 | - is_null 33 | - logical_operators 34 | - magic_constant_casing 35 | - magic_method_casing 36 | - method_separation 37 | - modernize_types_casting 38 | - native_function_casing 39 | - native_function_type_declaration_casing 40 | - no_alias_functions 41 | - no_empty_comment 42 | - no_empty_phpdoc 43 | - no_empty_statement 44 | - no_extra_block_blank_lines 45 | - no_short_bool_cast 46 | - no_superfluous_elseif 47 | - no_unneeded_control_parentheses 48 | - no_unneeded_curly_braces 49 | - no_unneeded_final_method 50 | - no_unset_cast 51 | - no_unused_imports 52 | - no_unused_lambda_imports 53 | - no_useless_else 54 | - no_useless_return 55 | - normalize_index_brace 56 | - php_unit_dedicate_assert 57 | - php_unit_dedicate_assert_internal_type 58 | - php_unit_expectation 59 | - php_unit_mock 60 | - php_unit_mock_short_will_return 61 | - php_unit_namespaced 62 | - php_unit_no_expectation_annotation 63 | - phpdoc_no_empty_return 64 | - phpdoc_no_useless_inheritdoc 65 | - phpdoc_order 66 | - phpdoc_property 67 | - phpdoc_scalar 68 | - phpdoc_separation 69 | - phpdoc_singular_inheritdoc 70 | - phpdoc_trim 71 | - phpdoc_trim_consecutive_blank_line_separation 72 | - phpdoc_type_to_var 73 | - phpdoc_types 74 | - phpdoc_types_order 75 | - print_to_echo 76 | - regular_callable_call 77 | - return_assignment 78 | - self_accessor 79 | - self_static_accessor 80 | - set_type_to_cast 81 | - short_array_syntax 82 | - short_list_syntax 83 | - simplified_if_return 84 | - single_quote 85 | - standardize_not_equals 86 | - ternary_to_null_coalescing 87 | - trailing_comma_in_multiline_array 88 | - unalign_double_arrow 89 | - unalign_equals 90 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright © 2008 by Yii Software (https://www.yiiframework.com/) 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions 6 | are met: 7 | 8 | * Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | * Redistributions in binary form must reproduce the above copyright 11 | notice, this list of conditions and the following disclaimer in 12 | the documentation and/or other materials provided with the 13 | distribution. 14 | * Neither the name of Yii Software nor the names of its 15 | contributors may be used to endorse or promote products derived 16 | from this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22 | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 28 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 | POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | Yii Framework 4 | 5 |

Yii Framework REST API Extension

6 |
7 |

8 | 9 | > ⚠️The package is deprecated. All its functionality is now integrated into [yiisoft/app-api](https://github.com/yiisoft/app-api). 10 | 11 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "yiisoft/yii-api", 3 | "description": "Yii framework API support", 4 | "keywords": [ 5 | "yii", 6 | "framework", 7 | "rest", 8 | "api" 9 | ], 10 | "type": "library", 11 | "license": "BSD-3-Clause", 12 | "support": { 13 | "issues": "https://github.com/yiisoft/yii-api/issues", 14 | "forum": "http://www.yiiframework.com/forum/", 15 | "wiki": "http://www.yiiframework.com/wiki/", 16 | "irc": "irc://irc.freenode.net/yii", 17 | "source": "https://github.com/yiisoft/yii-api" 18 | }, 19 | "minimum-stability": "dev", 20 | "prefer-stable": true, 21 | "require": { 22 | "php": "^7.4|^8.0", 23 | "yiisoft/http": "^1.0", 24 | "yiisoft/router": "^3.0@dev", 25 | "yiisoft/serializer": "^3.0@dev" 26 | }, 27 | "require-dev": { 28 | "nyholm/psr7": "^1.0", 29 | "phan/phan": "^3.0", 30 | "phpunit/phpunit": "^9.4", 31 | "roave/infection-static-analysis-plugin": "^1.5", 32 | "spatie/phpunit-watcher": "^1.23", 33 | "vimeo/psalm": "^4.2", 34 | "yiisoft/cache": "^3.0@dev", 35 | "yiisoft/composer-config-plugin": "^1.0@dev", 36 | "yiisoft/di": "^3.0@dev", 37 | "yiisoft/log": "^3.0@dev" 38 | }, 39 | "autoload": { 40 | "psr-4": { 41 | "Yiisoft\\Yii\\Api\\": "src" 42 | } 43 | }, 44 | "autoload-dev": { 45 | "psr-4": { 46 | "Yiisoft\\Yii\\Api\\Tests\\": "tests" 47 | } 48 | }, 49 | "extra": { 50 | "branch-alias": { 51 | "dev-master": "3.0.x-dev" 52 | }, 53 | "config-plugin": { 54 | "params": "config/params.php", 55 | "common": "config/common.php", 56 | "web": "config/web.php", 57 | "tests": "$web" 58 | } 59 | }, 60 | "scripts": { 61 | "phan": "phan --progress-bar -o analysis.txt", 62 | "test": "phpunit --testdox --no-interaction", 63 | "test-watch": "phpunit-watcher watch" 64 | }, 65 | "config": { 66 | "sort-packages": true 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /config/common.php: -------------------------------------------------------------------------------- 1 | \Yiisoft\Yii\Rest\ResponseFactory\JsonResponseFactory::class, 7 | ]; 8 | -------------------------------------------------------------------------------- /config/params.php: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /src/AbstractController.php: -------------------------------------------------------------------------------- 1 | responseFactory = $responseFactory; 21 | } 22 | 23 | protected function createResponse($data): ResponseInterface 24 | { 25 | return $this->responseFactory->createResponse($data); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/ResponseFactory/AbstractResponseFactory.php: -------------------------------------------------------------------------------- 1 | responseFactory = $factory; 20 | } 21 | 22 | public function createResponse($data): ResponseInterface 23 | { 24 | $stream = $this->convertData($data); 25 | 26 | return $this->responseFactory 27 | ->createResponse() 28 | ->withHeader(Header::CONTENT_TYPE, $this->getContentType()) 29 | ->withBody($stream); 30 | } 31 | 32 | abstract protected function convertData($data): StreamInterface; 33 | 34 | abstract protected function getContentType(): string; 35 | } 36 | -------------------------------------------------------------------------------- /src/ResponseFactory/JsonResponseFactory.php: -------------------------------------------------------------------------------- 1 | jsonSerializer = $jsonSerializer; 24 | $this->streamFactory = $streamFactory; 25 | } 26 | 27 | protected function convertData($data): StreamInterface 28 | { 29 | $content = $this->jsonSerializer->serialize($data); 30 | 31 | return $this->streamFactory->createStream($content); 32 | } 33 | 34 | protected function getContentType(): string 35 | { 36 | return 'application/json'; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/ResponseFactoryInterface.php: -------------------------------------------------------------------------------- 1 | Method::GET, 28 | 'list' => Method::GET, 29 | 'post' => Method::POST, 30 | 'put' => Method::PUT, 31 | 'delete' => Method::DELETE, 32 | 'patch' => Method::PATCH, 33 | 'head' => Method::HEAD, 34 | 'options' => Method::OPTIONS, 35 | ]; 36 | $routes = []; 37 | 38 | foreach ($methods as $methodName => $httpMethod) { 39 | if ($reflection->hasMethod($methodName)) { 40 | $pattern = $methodName === 'list' ? '' : '/{id:[^/]+}'; 41 | $routes[] = Route::methods([$httpMethod], $pattern, [$controller, $methodName]); 42 | } 43 | } 44 | 45 | return $routes; 46 | } 47 | } 48 | --------------------------------------------------------------------------------