浏览代码

moved to static uses

tags/0.1.0
Robin Thoni 9 年前
父节点
当前提交
79f0368e0a
共有 5 个文件被更改,包括 49 次插入61 次删除
  1. 1
    2
      src/Generator/business.php.twig
  2. 21
    53
      src/Utils/LuBusiness.php
  3. 12
    3
      src/Utils/LuController.php
  4. 8
    1
      src/Utils/LuDataAccess.php
  5. 7
    2
      src/Utils/LuModel.php

+ 1
- 2
src/Generator/business.php.twig 查看文件

@@ -7,9 +7,8 @@ use App\Http\DataAccess\{{ data_access_name.as_it }};
7 7
 use App\Http\DBO\{{ dbo_name.as_it }};
8 8
 
9 9
 class {{ business_name.as_it }} extends LuBusiness {
10
-    public function __construct(array $parametersSource)
10
+    public function __construct()
11 11
     {
12
-        parent::__construct($parametersSource);
13 12
         $this->dataAccess = new {{ data_access_name.as_it }}();
14 13
     }
15 14
 }

+ 21
- 53
src/Utils/LuBusiness.php 查看文件

@@ -3,52 +3,40 @@
3 3
 namespace Luticate\Utils;
4 4
 
5 5
 class LuBusiness {
6
-    /**
7
-     * @var array
8
-     */
9
-    private $parameters = [];
10 6
 
11 7
     /**
12 8
      * @var LuDataAccess
13 9
      */
14 10
     protected $dataAccess = null;
15 11
 
16
-    /**
17
-     * @param array $parametersSource
18
-     */
19
-    public function __construct(array $parametersSource)
20
-    {
21
-        $this->parameters = $parametersSource;
22
-    }
23
-
24 12
     /**
25 13
      * @param string $name
26 14
      * @param mixed  $default
27 15
      * @return mixed
28 16
      */
29
-    protected function getParam($name, $default = null)
17
+    public static function getParam($name, $default = null)
30 18
     {
31
-        return array_key_exists($name, $this->parameters) ? $this->parameters[$name] : $default;
19
+        return array_key_exists($name, LuController::$parameters) ? LuController::$parameters[$name] : $default;
32 20
     }
33 21
 
34 22
     /**
35 23
      * @param string $name
36 24
      * @param mixed  $value
37 25
      */
38
-    public function setParam($name, $value)
26
+    public static function setParam($name, $value)
39 27
     {
40
-        $this->parameters[$name] = $value;
28
+        LuController::$parameters[$name] = $value;
41 29
     }
42 30
 
43 31
     /**
44 32
      * @param string[] $params
45 33
      * @return bool
46 34
      */
47
-    public function hasParam(array $params)
35
+    public static function hasParam(array $params)
48 36
     {
49 37
         foreach ($params as $p)
50 38
         {
51
-            if (!array_key_exists($p, $this->parameters))
39
+            if (!array_key_exists($p, LuController::$parameters))
52 40
                 return false;
53 41
         }
54 42
         return true;
@@ -59,9 +47,9 @@ class LuBusiness {
59 47
      * @param mixed $validate
60 48
      * @return string
61 49
      */
62
-    public function checkParam($param, $validate = null)
50
+    public static function checkParam($param, $validate = null)
63 51
     {
64
-        $value = $this->getParam($param);
52
+        $value = self::getParam($param);
65 53
         if (is_null($value) || trim($value) == '')
66 54
             abort(400, 'Missing parameter: ' . $param);
67 55
         if ($validate != null) {
@@ -80,7 +68,7 @@ class LuBusiness {
80 68
     /**
81 69
      * @param string
82 70
      */
83
-    public function unauthorized($reason = 'Unauthorized')
71
+    public static function unauthorized($reason = 'Unauthorized')
84 72
     {
85 73
         abort(401, $reason);
86 74
     }
@@ -88,7 +76,7 @@ class LuBusiness {
88 76
     /**
89 77
      * @param string
90 78
      */
91
-    public function notFound($reason = 'Resource not found')
79
+    public static function notFound($reason = 'Resource not found')
92 80
     {
93 81
         abort(404, $reason);
94 82
     }
@@ -98,9 +86,9 @@ class LuBusiness {
98 86
      * @param $values string[]
99 87
      * @return string
100 88
      */
101
-    public function getParamInArray($param, $values)
89
+    public static function getParamInArray($param, $values)
102 90
     {
103
-        $value = $this->getParam($param);
91
+        $value = self::getParam($param);
104 92
         if (is_null($value) || !in_array($value, $values))
105 93
             $value = $values[0];
106 94
         return $value;
@@ -111,14 +99,21 @@ class LuBusiness {
111 99
      * @param $default int
112 100
      * @return int
113 101
      */
114
-    public function getParamInt($param, $default = 0)
102
+    public static function getParamInt($param, $default = 0)
115 103
     {
116
-        $value = $this->getParam($param);
104
+        $value = self::getParam($param);
117 105
         if (!is_null($value) && is_numeric($value))
118 106
             return (int)$value;
119 107
         return $default;
120 108
     }
121 109
 
110
+    public static function __callStatic($name, $arguments)
111
+    {
112
+        $class = get_called_class();
113
+        $me = new $class();
114
+        return call_user_func_array(array($me, $name), $arguments);
115
+    }
116
+
122 117
     /**
123 118
      * @param $page int
124 119
      * @param $perPage int
@@ -151,31 +146,4 @@ class LuBusiness {
151 146
     {
152 147
         $this->dataAccess->deleteById($id);
153 148
     }
154
-
155
-    /**
156
-     * @param $data LuDbo
157
-     * @return int
158
-     */
159
-    public function addId($data)
160
-    {
161
-        return $this->dataAccess->addId($data);
162
-    }
163
-
164
-    /**
165
-     * @param $data LuDbo
166
-     */
167
-    public function add($data)
168
-    {
169
-        $this->dataAccess->add($data);
170
-    }
171
-
172
-    /**
173
-     * @param $id int
174
-     * @param $data LuDbo
175
-     * @return LuDbo|null
176
-     */
177
-    public function editById($id, $data)
178
-    {
179
-        return $this->dataAccess->editById($id, $data);
180
-    }
181 149
 }

+ 12
- 3
src/Utils/LuController.php 查看文件

@@ -6,17 +6,26 @@ use Laravel\Lumen\Routing\Controller as BaseController;
6 6
 
7 7
 class LuController extends BaseController
8 8
 {
9
+    public static $parameters = null;
10
+
9 11
     function execute($businessClass, $businessMethod)
10 12
     {
11 13
         $params = app('request')->route()[2];
12
-        $business = new $businessClass(array_merge($_GET, $_POST, $params));
14
+        self::$parameters = array_merge($_GET, $_POST, $params);
15
+        $business = new $businessClass();
13 16
 
14 17
         $reflect = new \ReflectionMethod($businessClass, $businessMethod);
15 18
         $params = $reflect->getParameters();
16 19
 
17 20
         $args = array();
18
-        foreach ($params as $param)
19
-            $args[$param->getName()] = $business->checkParam($param->getName());
21
+        foreach ($params as $param) {
22
+            if ($param->isOptional()) {
23
+                $args[$param->getName()] = LuBusiness::getParam($param->getName(), $param->getDefaultValue());
24
+            }
25
+            else {
26
+                $args[$param->getName()] = LuBusiness::checkParam($param->getName());
27
+            }
28
+        }
20 29
 
21 30
         return LuOutputFormatter::formatSuccess(call_user_func_array(array($business, $businessMethod), $args));
22 31
     }

+ 8
- 1
src/Utils/LuDataAccess.php 查看文件

@@ -8,6 +8,13 @@ abstract class LuDataAccess {
8 8
     {
9 9
     }
10 10
 
11
+    public static function __callStatic($name, $arguments)
12
+    {
13
+        $class = get_called_class();
14
+        $me = new $class();
15
+        return call_user_func_array(array($me, $name), $arguments);
16
+    }
17
+
11 18
     /**
12 19
      * @var LuModel
13 20
      */
@@ -30,7 +37,7 @@ abstract class LuDataAccess {
30 37
      * @param $data LuModel[]
31 38
      * @return LuDbo[]
32 39
      */
33
-    protected function arrayToDbo($data)
40
+    protected static function arrayToDbo($data)
34 41
     {
35 42
         $tab = [];
36 43
         foreach ($data as $q) {

+ 7
- 2
src/Utils/LuModel.php 查看文件

@@ -8,8 +8,13 @@ use Illuminate\Database\Eloquent\Model;
8 8
 /**
9 9
  * LuModel
10 10
  *
11
- * @method Collection where($column, $operator, $value)
12
- * @method Collection orderBy($column, $order)
11
+ * @method static LuModel where($column, $operator, $value)
12
+ * @method static LuModel orderBy($column, $order)
13
+ * @method static LuModel take($count)
14
+ * @method static LuModel offset($offset)
15
+ * @method static LuModel[] get()
16
+ * @method static LuModel first()
17
+ * @method static int count()
13 18
  */
14 19
 abstract class LuModel extends Model
15 20
 {

正在加载...
取消
保存