├── .travis.yml ├── .gitignore ├── src ├── Config.php ├── ActionInterface.php ├── Exceptions │ └── ActionException.php ├── Helpers.php ├── Facades │ └── Action.php ├── Action.php ├── ActionServiceProvider.php └── LaravelAction.php ├── examples ├── CreateWallet.php ├── CreateUser.php └── UserController.php ├── composer.json ├── LICENSE └── README.md /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | /tests 3 | /.idea 4 | /composer.lock -------------------------------------------------------------------------------- /src/Config.php: -------------------------------------------------------------------------------- 1 | 'Examples\\', 4 | ]; -------------------------------------------------------------------------------- /src/ActionInterface.php: -------------------------------------------------------------------------------- 1 | 'success']; 19 | } 20 | 21 | } -------------------------------------------------------------------------------- /src/Action.php: -------------------------------------------------------------------------------- 1 | 'success']; 19 | } 20 | 21 | public function after($request) 22 | { 23 | var_dump ($request); 24 | 25 | return Action::use ('CreateWallet', $request); 26 | } 27 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "crazycodes/laravel-action", 3 | "description": "Development Laravel Action Func", 4 | "keywords": [ 5 | "action", 6 | "laravel", 7 | "php" 8 | ], 9 | "require": { 10 | "php": ">=7.0.0", 11 | "laravel/framework": "~5.2||~5.3||~5.4||~5.5||~5.6" 12 | }, 13 | "license": "MIT", 14 | "minimum-stability": "dev", 15 | "autoload": { 16 | "psr-4": { 17 | "CrazyCodes\\": "src/" 18 | }, 19 | "files": [ 20 | "src/Helpers.php" 21 | ] 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /examples/UserController.php: -------------------------------------------------------------------------------- 1 | 'test', 17 | 'password' => 'test', 18 | ]; 19 | 20 | $result = Action::use ('CreateUser', $request); 21 | 22 | //全局方法 23 | //laravel_action ('CreateUser', $request); 24 | 25 | 26 | // return $result->toArray(); 27 | return $result->toJson (); 28 | } 29 | } -------------------------------------------------------------------------------- /src/ActionServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes ([ 12 | __DIR__ . '/Config.php' => config_path ('laravel-action.php'), 13 | ], 'config'); 14 | } 15 | 16 | public function register() 17 | { 18 | $this->app->singleton (LaravelAction::class, function () { 19 | return new LaravelAction(); 20 | }); 21 | 22 | $this->app->alias (LaravelAction::class, 'action'); 23 | } 24 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Mr. Zhang, 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel-Action 2 | Laravel-Action 将CUD独立成模块调用 3 | 4 | # Installation 5 | 下载composer包: ```composer require crazycodes/laravel-action``` 6 | 7 | 注入提供者到 ```config/app.php``` 8 | 9 | ``` 10 | 'providers' => [ 11 | // [...] 12 | CrazyCodes\ActionServiceProvider::class, 13 | ], 14 | ``` 15 | 注册 ```Action``` facade: 16 | ``` 17 | 'aliases' => [ 18 | // [...] 19 | 'Action' => CrazyCodes\Facades\Action::class, 20 | ], 21 | ``` 22 | 发布配置文件 23 | ``` 24 | php artisan vendor:publish --provider=CrazyCodes\ActionServiceProvider 25 | ``` 26 | 配置项就一个 27 | ``` 28 | actionNamespace //设置你的action所在的命名空间 29 | ``` 30 | 31 | # 使用 32 | 继承Action方法获取规范的命名 33 | ``` 34 | namespace CrazyCodes\Action; 35 | 36 | class CreateUser extends Action 37 | { 38 | 39 | } 40 | ``` 41 | 42 | 继承的Action准备了两个方法 43 | 44 | ## before 45 | ``` 46 | public function before($request) 47 | { 48 | return $request; 49 | } 50 | ``` 51 | Action被调用的同时会直接调用before方法执行。 52 | 53 | ## after 54 | ``` 55 | public function after($request) 56 | { 57 | return []; 58 | } 59 | 60 | ``` 61 | 可以选择不声明after方法。after主要用于调用其他Action 62 | 63 | ## 成员变量 64 | ``` 65 | public $beforeResultName = 'beforeResult'; 66 | public $afterResultName = 'afterResult'; 67 | ``` 68 | 用于获取返回的结果 69 | 70 | # 调用 71 | 可以通过Facade调用 72 | ``` 73 | Action::use('YourAction',发送的参数); 74 | ``` 75 | 或者使用全局函数 76 | ``` 77 | laravel_action('YourAction',发送的参数); 78 | ``` 79 | 80 | # 获取结果 81 | 得到的结果默认是对象。可以转换格式 82 | ``` 83 | function toJson(); 84 | function toArray(); 85 | ``` 86 | 结果展示 87 | Array 88 | ``` 89 | array:2 [ 90 | "beforeResult" => array:1 [ 91 | 0 => "aaa" 92 | ] 93 | "afterResult" => [] 94 | ] 95 | ``` 96 | JSON 97 | ``` 98 | {"beforeResult":["aaa"],"afterResult":[]} 99 | ``` 100 | 101 | # 后续 102 | demo和测试正在出,稍安勿躁 -------------------------------------------------------------------------------- /src/LaravelAction.php: -------------------------------------------------------------------------------- 1 | app = app (); 62 | 63 | } 64 | 65 | /** 66 | * @param string $className 67 | * @param array $request 68 | * 69 | * @return $this 70 | * @throws ActionException 71 | */ 72 | public function use($className = '', $request = []) 73 | { 74 | 75 | if (isset($className)) { 76 | $this->className = $className; 77 | $this->request = $request; 78 | 79 | try { 80 | $this->__include (); 81 | } catch (ActionException $e) { 82 | throw new ActionException($e->getMessage ()); 83 | } 84 | 85 | return $this; 86 | } 87 | 88 | } 89 | 90 | /** 91 | * @return string 92 | */ 93 | public function toJson() 94 | { 95 | return json_encode ([ 96 | $this->class->beforeResultName => $this->beforeResult, 97 | $this->class->afterResultName => $this->afterResult, 98 | ]); 99 | } 100 | 101 | /** 102 | * @return array 103 | */ 104 | public function toArray(): array 105 | { 106 | return [ 107 | $this->class->beforeResultName => $this->beforeResult, 108 | $this->class->afterResultName => $this->afterResult, 109 | ]; 110 | } 111 | 112 | 113 | /** 114 | * @throws ActionException 115 | */ 116 | protected function __include() 117 | { 118 | 119 | $this->className = config ('laravel-action.actionNamespace') . $this->className; 120 | 121 | if (!class_exists ($this->className)) { 122 | throw new ActionException('your class not undefined'); 123 | } 124 | 125 | $this->class = $this->app->make ($this->className); 126 | 127 | if (!method_exists ($this->class, 'before')) { 128 | throw new ActionException('your before func not undefined'); 129 | } 130 | 131 | $this->beforeResult = $this->class->before ($this->request); 132 | 133 | if (method_exists ($this->class, 'after')) { 134 | $this->afterResult = $this->class->after ($this->request); 135 | } 136 | 137 | } 138 | 139 | public function __destruct() 140 | { 141 | unset($this->class); 142 | } 143 | 144 | } --------------------------------------------------------------------------------