Pārlūkot izejas kodu

Model doc; business and data access getAll; route middleware

tags/0.1.0
Robin Thoni 8 gadus atpakaļ
vecāks
revīzija
567f2cf3c9
4 mainītis faili ar 87 papildinājumiem un 80 dzēšanām
  1. 5
    13
      src/Utils/LuBusiness.php
  2. 16
    39
      src/Utils/LuDataAccess.php
  3. 4
    0
      src/Utils/LuModel.php
  4. 62
    28
      src/Utils/LuRoute.php

+ 5
- 13
src/Utils/LuBusiness.php Parādīt failu

101
         return $default;
101
         return $default;
102
     }
102
     }
103
 
103
 
104
-    /**
105
-     * @param $page int
106
-     * @param $perPage int
107
-     * @param $column string
108
-     * @param $order string
109
-     * @param $query string
110
-     * @return LuDbo[]
111
-     */
112
-    public static function search($page, $perPage, $column, $order, $query = null)
113
-    {
114
-        return static::getDataAccess()->search($page, $perPage, $column, $order, $query);
115
-    }
116
-
117
     /**
104
     /**
118
      * @param $id int
105
      * @param $id int
119
      * @return LuDbo|null
106
      * @return LuDbo|null
141
     {
128
     {
142
         return null;
129
         return null;
143
     }
130
     }
131
+
132
+    public static function getAll($page = 0, $perPage = PHP_INT_MAX)
133
+    {
134
+        return static::getDataAccess()->getAll($page, $perPage);
135
+    }
144
 }
136
 }

+ 16
- 39
src/Utils/LuDataAccess.php Parādīt failu

3
 namespace Luticate\Utils;
3
 namespace Luticate\Utils;
4
 
4
 
5
 abstract class LuDataAccess {
5
 abstract class LuDataAccess {
6
-    /**
7
-     * @param $page int
8
-     * @param $perPage int
9
-     * @param $column string
10
-     * @param $order string
11
-     * @param $query string
12
-     * @return LuModel[]
13
-     */
14
-    protected static function search_($page, $perPage, $column, $order, $query)
15
-    {
16
-        return static::getModel();
17
-    }
18
-
19
     /**
6
     /**
20
      * @param $data LuModel[]
7
      * @param $data LuModel[]
21
      * @return LuDbo[]
8
      * @return LuDbo[]
31
         return $tab;
18
         return $tab;
32
     }
19
     }
33
 
20
 
34
-    /**
35
-     * @param $page int
36
-     * @param $perPage int
37
-     * @param $column string
38
-     * @param $order string
39
-     * @param $query string
40
-     * @return LuDbo[]
41
-     */
42
-    public static function search($page, $perPage, $column, $order, $query = null)
43
-    {
44
-        if (is_null($query))
45
-            $data = static::getModel();
46
-        else
47
-            $data = static::search_($page, $perPage, $column, $order, $query);
48
-
49
-        $data = $data->orderBy($column, $order)->take($perPage)->offset($page * $perPage)->get();
50
-
51
-        $tab = self::arrayToDbo($data);
52
-        return $tab;
53
-    }
54
-
55
     /**
21
     /**
56
      * @param $id int
22
      * @param $id int
57
      * @return LuModel
23
      * @return LuModel
120
      * @param $orders array
86
      * @param $orders array
121
      * @param int $page
87
      * @param int $page
122
      * @param int $perPage
88
      * @param int $perPage
123
-     * @return array
89
+     * @return LuMultipleDbo
124
      */
90
      */
125
     public static function getMultiple($predicates, $orders, $page = 0, $perPage = PHP_INT_MAX)
91
     public static function getMultiple($predicates, $orders, $page = 0, $perPage = PHP_INT_MAX)
126
     {
92
     {
127
         $model = static::getModel();
93
         $model = static::getModel();
128
-        foreach($predicates as $predicate)
129
-        {
94
+        foreach($predicates as $predicate) {
130
             $model = $model->where($predicate[0], $predicate[1], $predicate[2]);
95
             $model = $model->where($predicate[0], $predicate[1], $predicate[2]);
131
         }
96
         }
132
         $count = $model->count();
97
         $count = $model->count();
133
-        foreach($orders as $order)
134
-        {
98
+        foreach($orders as $order) {
135
             $model = $model->orderBy($order[0], $order[1]);
99
             $model = $model->orderBy($order[0], $order[1]);
136
         }
100
         }
137
         $data = $model->take($perPage)->offset($page * $perPage)->get();
101
         $data = $model->take($perPage)->offset($page * $perPage)->get();
147
     {
111
     {
148
         return null;
112
         return null;
149
     }
113
     }
114
+
115
+    /**
116
+     * @return array
117
+     */
118
+    private static function getOrderBy()
119
+    {
120
+        return array();
121
+    }
122
+
123
+    public static function getAll($page = 0, $perPage = PHP_INT_MAX)
124
+    {
125
+        return self::getMultiple(array(), static::getOrderBy(), $page, $perPage);
126
+    }
150
 }
127
 }

+ 4
- 0
src/Utils/LuModel.php Parādīt failu

13
  * @method static LuModel orderBy($column, $order)
13
  * @method static LuModel orderBy($column, $order)
14
  * @method static LuModel take($count)
14
  * @method static LuModel take($count)
15
  * @method static LuModel offset($offset)
15
  * @method static LuModel offset($offset)
16
+ * @method static LuModel rightJoin($table, $column1, $operator, $column2)
17
+ * @method static LuModel leftJoin($table, $column1, $operator, $column2)
18
+ * @method static LuModel join($table, $column1, $operator, $column2)
19
+ * @method static LuModel groupBy($column)
16
  * @method static LuModel[] get()
20
  * @method static LuModel[] get()
17
  * @method static LuModel first()
21
  * @method static LuModel first()
18
  * @method static int count()
22
  * @method static int count()

+ 62
- 28
src/Utils/LuRoute.php Parādīt failu

2
 
2
 
3
 namespace Luticate\Utils;
3
 namespace Luticate\Utils;
4
 
4
 
5
-use Illuminate\Http\Request;
6
-
7
 class LuRoute {
5
 class LuRoute {
8
     /**
6
     /**
9
-     * @var callable
7
+     * @var string[]
10
      */
8
      */
11
-    private $_middleware;
9
+    private $middleware = array();
12
 
10
 
13
     /**
11
     /**
14
-     * @param $middleware callable
12
+     * @param $middleware string
15
      */
13
      */
16
-    public function setMiddleware($middleware)
14
+    public function addMiddleware($middleware)
17
     {
15
     {
18
-        $this->_middleware = $middleware;
16
+        $this->middleware[] = $middleware;
19
     }
17
     }
20
 
18
 
21
-    private function getOptions($url, $business, $method, $permissions = array())
19
+    private function getOptions($url, $business, $method, $permissions, $middleware)
22
     {
20
     {
23
-        $middleware = $this->_middleware;
24
-        return [function(Request $request) use($business, $method, $permissions, $middleware)
21
+        if (!is_array($permissions)) {
22
+            $permissions = array($permissions);
23
+        }
24
+        if (!is_array($middleware)) {
25
+            $middleware = array($middleware);
26
+        }
27
+        $permissions_string = implode(",", $permissions);
28
+        $middleware_string = [];
29
+        foreach (array_merge($this->middleware, $middleware) as $mid) {
30
+            $middleware_string[] = $mid . ":" . $permissions_string;
31
+        }
32
+        return [function() use($business, $method, $permissions)
25
         {
33
         {
26
-            if (!is_array($permissions)) {
27
-                $permissions = array($permissions);
28
-            }
29
-
30
-            if ($middleware != null && !$middleware($permissions, $request)) {
31
-                abort(401);
32
-            }
33
-
34
             $controller = new LuController();
34
             $controller = new LuController();
35
 
35
 
36
-            if (strpos($business, "\\") === false)
36
+            if (strpos($business, "\\") === false) {
37
                 $business = "App\\Http\\Business\\" . $business . "Business";
37
                 $business = "App\\Http\\Business\\" . $business . "Business";
38
+            }
38
 
39
 
39
             return $controller->execute($business, $method);
40
             return $controller->execute($business, $method);
40
-        }];
41
+        },
42
+            'middleware' => $middleware_string];
41
     }
43
     }
42
 
44
 
43
-    public function get($url, $business, $method, $permissions = array())
45
+    /**
46
+     * @param $url string
47
+     * @param $business string
48
+     * @param $method string
49
+     * @param array $permissions string|string[]
50
+     * @param array $middleware string|string[]
51
+     * @return mixed
52
+     */
53
+    public function get($url, $business, $method, $permissions = array(), $middleware = array())
44
     {
54
     {
45
         global $app;
55
         global $app;
46
-        return $app->get($url, $this->getOptions($url, $business, $method, $permissions));
56
+        return $app->get($url, $this->getOptions($url, $business, $method, $permissions, $middleware));
47
     }
57
     }
48
 
58
 
49
-    public function post($url, $business, $method, $permissions = array())
59
+    /**
60
+     * @param $url string
61
+     * @param $business string
62
+     * @param $method string
63
+     * @param array $permissions string|string[]
64
+     * @param array $middleware string|string[]
65
+     * @return mixed
66
+     */
67
+    public function post($url, $business, $method, $permissions = array(), $middleware = array())
50
     {
68
     {
51
         global $app;
69
         global $app;
52
-        return $app->post($url, $this->getOptions($url, $business, $method, $permissions));
70
+        return $app->post($url, $this->getOptions($url, $business, $method, $permissions, $middleware));
53
     }
71
     }
54
 
72
 
55
-    public function put($url, $business, $method, $permissions = array())
73
+    /**
74
+     * @param $url string
75
+     * @param $business string
76
+     * @param $method string
77
+     * @param array $permissions string|string[]
78
+     * @param array $middleware string|string[]
79
+     * @return mixed
80
+     */
81
+    public function put($url, $business, $method, $permissions = array(), $middleware = array())
56
     {
82
     {
57
         global $app;
83
         global $app;
58
-        return $app->put($url, $this->getOptions($url, $business, $method, $permissions));
84
+        return $app->put($url, $this->getOptions($url, $business, $method, $permissions, $middleware));
59
     }
85
     }
60
 
86
 
61
-    public function delete($url, $business, $method, $permissions = array())
87
+    /**
88
+     * @param $url string
89
+     * @param $business string
90
+     * @param $method string
91
+     * @param array $permissions string|string[]
92
+     * @param array $middleware string|string[]
93
+     * @return mixed
94
+     */
95
+    public function delete($url, $business, $method, $permissions = array(), $middleware = array())
62
     {
96
     {
63
         global $app;
97
         global $app;
64
-        return $app->delete($url, $this->getOptions($url, $business, $method, $permissions));
98
+        return $app->delete($url, $this->getOptions($url, $business, $method, $permissions, $middleware));
65
     }
99
     }
66
 }
100
 }

Notiek ielāde…
Atcelt
Saglabāt