@@ -13,13 +13,13 @@ discard block |
||
| 13 | 13 | */ |
| 14 | 14 | public function __construct(array $threads, callable $waitAll) |
| 15 | 15 | { |
| 16 | - foreach ($threads as $thread){ |
|
| 16 | + foreach ($threads as $thread) { |
|
| 17 | 17 | $pos = count($this->threadResults); |
| 18 | - $this->threadResults[] = [null,null]; |
|
| 19 | - $this->threads[] = function ()use($thread, $pos){ |
|
| 20 | - try{ |
|
| 18 | + $this->threadResults[] = [null, null]; |
|
| 19 | + $this->threads[] = function()use($thread, $pos){ |
|
| 20 | + try { |
|
| 21 | 21 | $this->threadResults[$pos][0] = $thread(); |
| 22 | - }catch (\Exception $e){ |
|
| 22 | + }catch (\Exception $e) { |
|
| 23 | 23 | $this->threadResults[$pos][1] = $e; |
| 24 | 24 | } |
| 25 | 25 | }; |
@@ -29,25 +29,25 @@ discard block |
||
| 29 | 29 | |
| 30 | 30 | public function run() |
| 31 | 31 | { |
| 32 | - while ($thread = array_pop($this->threads)){ |
|
| 32 | + while ($thread = array_pop($this->threads)) { |
|
| 33 | 33 | $thread(); |
| 34 | 34 | }; |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - public function wait($waitAble){ |
|
| 37 | + public function wait($waitAble) { |
|
| 38 | 38 | array_push($this->waits, $waitAble); |
| 39 | 39 | $this->run(); |
| 40 | 40 | |
| 41 | - if(count($this->waits)){ |
|
| 41 | + if (count($this->waits)) { |
|
| 42 | 42 | $waitAll = $this->waitAll; |
| 43 | 43 | $this->waitResults = $waitAll($this->waits); |
| 44 | 44 | $this->waits = []; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - $res = array_pop($this->waitResults); |
|
| 48 | - if(isset($res[1])){ |
|
| 47 | + $res = array_pop($this->waitResults); |
|
| 48 | + if (isset($res[1])) { |
|
| 49 | 49 | \PhpBoot\abort(new RpcException($res['reason'])); |
| 50 | - }else{ |
|
| 50 | + }else { |
|
| 51 | 51 | return $res[0]; |
| 52 | 52 | } |
| 53 | 53 | } |
@@ -93,11 +93,11 @@ discard block |
||
| 93 | 93 | |
| 94 | 94 | Request::class => \DI\factory([Application::class, 'createRequestFromGlobals']), |
| 95 | 95 | ]; |
| 96 | - if(function_exists('apc_fetch')){ |
|
| 96 | + if (function_exists('apc_fetch')) { |
|
| 97 | 97 | $default += [ |
| 98 | 98 | Cache::class => \DI\object(ApcCache::class) |
| 99 | 99 | ]; |
| 100 | - }else{ |
|
| 100 | + }else { |
|
| 101 | 101 | $default += [ |
| 102 | 102 | Cache::class => \DI\object(FilesystemCache::class) |
| 103 | 103 | ->constructorParameter('directory', sys_get_temp_dir()) |
@@ -139,11 +139,11 @@ discard block |
||
| 139 | 139 | * @param string[] $hooks hook class names |
| 140 | 140 | * @return void |
| 141 | 141 | */ |
| 142 | - public function loadRoutesFromClass($className, $hooks=[]) |
|
| 142 | + public function loadRoutesFromClass($className, $hooks = []) |
|
| 143 | 143 | { |
| 144 | 144 | $cache = new CheckableCache($this->cache); |
| 145 | 145 | |
| 146 | - $key = 'loadRoutesFromClass:' . md5(__CLASS__ . ':' . $className); |
|
| 146 | + $key = 'loadRoutesFromClass:'.md5(__CLASS__.':'.$className); |
|
| 147 | 147 | $routes = $cache->get($key, $this); |
| 148 | 148 | |
| 149 | 149 | $controller = null; |
@@ -160,9 +160,9 @@ discard block |
||
| 160 | 160 | $this->routes[] = [ |
| 161 | 161 | $method, |
| 162 | 162 | $uri, |
| 163 | - function (Application $app, Request $request) use ($cache, $className, $actionName, $controller) { |
|
| 163 | + function(Application $app, Request $request) use ($cache, $className, $actionName, $controller) { |
|
| 164 | 164 | |
| 165 | - $key = 'loadRoutesFromClass:route:' . md5(__CLASS__ . ':' . $className . ':' . $actionName); |
|
| 165 | + $key = 'loadRoutesFromClass:route:'.md5(__CLASS__.':'.$className.':'.$actionName); |
|
| 166 | 166 | |
| 167 | 167 | $routeInstance = $cache->get($key, $this); |
| 168 | 168 | if ($routeInstance == $this) { |
@@ -190,11 +190,11 @@ discard block |
||
| 190 | 190 | * @param string[] $hooks |
| 191 | 191 | * @return void |
| 192 | 192 | */ |
| 193 | - public function loadRoutesFromPath($fromPath, $namespace = '', $hooks=[]) |
|
| 193 | + public function loadRoutesFromPath($fromPath, $namespace = '', $hooks = []) |
|
| 194 | 194 | { |
| 195 | 195 | $dir = @dir($fromPath) or abort("dir $fromPath not exist"); |
| 196 | 196 | |
| 197 | - $getEach = function () use ($dir) { |
|
| 197 | + $getEach = function() use ($dir) { |
|
| 198 | 198 | $name = $dir->read(); |
| 199 | 199 | if (!$name) { |
| 200 | 200 | return $name; |
@@ -206,11 +206,11 @@ discard block |
||
| 206 | 206 | if ($entry == '.' || $entry == '..') { |
| 207 | 207 | continue; |
| 208 | 208 | } |
| 209 | - $path = $fromPath . '/' . str_replace('\\', '/', $entry); |
|
| 209 | + $path = $fromPath.'/'.str_replace('\\', '/', $entry); |
|
| 210 | 210 | if (is_file($path) && substr_compare($entry, '.php', strlen($entry) - 4, 4, true) == 0) { |
| 211 | - $class_name = $namespace . '\\' . substr($entry, 0, strlen($entry) - 4); |
|
| 211 | + $class_name = $namespace.'\\'.substr($entry, 0, strlen($entry) - 4); |
|
| 212 | 212 | $this->loadRoutesFromClass($class_name, $hooks); |
| 213 | - } else { |
|
| 213 | + }else { |
|
| 214 | 214 | //\Log::debug($path.' ignored'); |
| 215 | 215 | } |
| 216 | 216 | } |
@@ -223,7 +223,7 @@ discard block |
||
| 223 | 223 | * @param callable $handler function(Application $app, Request $request):Response |
| 224 | 224 | * @param string[] $hooks |
| 225 | 225 | */ |
| 226 | - public function addRoute($method, $uri, callable $handler, $hooks=[]) |
|
| 226 | + public function addRoute($method, $uri, callable $handler, $hooks = []) |
|
| 227 | 227 | { |
| 228 | 228 | $this->routes[] = [$method, $uri, $handler, $hooks]; |
| 229 | 229 | } |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | { |
| 250 | 250 | // TODO 把 Route里的异常处理 ExceptionRenderer 移到这里更妥? |
| 251 | 251 | $renderer = $this->get(ExceptionRenderer::class); |
| 252 | - try{ |
|
| 252 | + try { |
|
| 253 | 253 | if ($request == null) { |
| 254 | 254 | $request = $this->make(Request::class); |
| 255 | 255 | } |
@@ -259,7 +259,7 @@ discard block |
||
| 259 | 259 | } |
| 260 | 260 | $uri = rawurldecode($uri); |
| 261 | 261 | |
| 262 | - $next = function (Request $request)use($uri){ |
|
| 262 | + $next = function(Request $request)use($uri){ |
|
| 263 | 263 | $dispatcher = $this->getDispatcher(); |
| 264 | 264 | $res = $dispatcher->dispatch($request->getMethod(), $uri); |
| 265 | 265 | if ($res[0] == Dispatcher::FOUND) { |
@@ -268,10 +268,10 @@ discard block |
||
| 268 | 268 | $request->attributes->add($res[2]); |
| 269 | 269 | } |
| 270 | 270 | list($handler, $hooks) = $res[1]; |
| 271 | - $next = function (Request $request)use($handler){ |
|
| 271 | + $next = function(Request $request)use($handler){ |
|
| 272 | 272 | return $handler($this, $request); |
| 273 | 273 | }; |
| 274 | - foreach (array_reverse($hooks) as $hookName){ |
|
| 274 | + foreach (array_reverse($hooks) as $hookName) { |
|
| 275 | 275 | $next = function($request)use($hookName, $next){ |
| 276 | 276 | $hook = $this->get($hookName); |
| 277 | 277 | /**@var $hook HookInterface*/ |
@@ -284,12 +284,12 @@ discard block |
||
| 284 | 284 | \PhpBoot\abort(new NotFoundHttpException(), [$request->getMethod(), $uri]); |
| 285 | 285 | } elseif ($res[0] == Dispatcher::METHOD_NOT_ALLOWED) { |
| 286 | 286 | \PhpBoot\abort(new MethodNotAllowedHttpException($res[1]), [$request->getMethod(), $uri]); |
| 287 | - } else { |
|
| 287 | + }else { |
|
| 288 | 288 | \PhpBoot\abort("unknown dispatch return {$res[0]}"); |
| 289 | 289 | } |
| 290 | 290 | }; |
| 291 | 291 | |
| 292 | - foreach (array_reverse($this->getGlobalHooks()) as $hookName){ |
|
| 292 | + foreach (array_reverse($this->getGlobalHooks()) as $hookName) { |
|
| 293 | 293 | $next = function($request)use($hookName, $next){ |
| 294 | 294 | $hook = $this->get($hookName); |
| 295 | 295 | /**@var $hook HookInterface*/ |
@@ -304,7 +304,7 @@ discard block |
||
| 304 | 304 | } |
| 305 | 305 | return $response; |
| 306 | 306 | |
| 307 | - }catch (\Exception $e){ |
|
| 307 | + }catch (\Exception $e) { |
|
| 308 | 308 | $renderer->render($e); |
| 309 | 309 | } |
| 310 | 310 | |
@@ -21,20 +21,20 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | public function __invoke(ControllerContainer $container, $ann, EntityContainerBuilder $entityBuilder) |
| 23 | 23 | { |
| 24 | - if(!$ann->parent){ |
|
| 24 | + if (!$ann->parent) { |
|
| 25 | 25 | //Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()} should be used with parent route"); |
| 26 | 26 | return; |
| 27 | 27 | } |
| 28 | 28 | $target = $ann->parent->name; |
| 29 | 29 | $route = $container->getRoute($target); |
| 30 | - if(!$route){ |
|
| 30 | + if (!$route) { |
|
| 31 | 31 | //Logger::debug("The annotation \"@{$ann->name} {$ann->description}\" of {$container->getClassName()}::$target should be used with parent route"); |
| 32 | - return ; |
|
| 32 | + return; |
|
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | $params = new AnnotationParams($ann->description, 2); |
| 36 | 36 | $type = $doc = null; |
| 37 | - if(count($params)>0){ |
|
| 37 | + if (count($params)>0) { |
|
| 38 | 38 | $type = TypeHint::normalize($params[0], $container->getClassName()); |
| 39 | 39 | } |
| 40 | 40 | $doc = $params->getRawParam(1, ''); |
@@ -42,10 +42,10 @@ discard block |
||
| 42 | 42 | list($_, $meta) = $route |
| 43 | 43 | ->getResponseHandler() |
| 44 | 44 | ->getMappingBySource('return'); |
| 45 | - if($meta){ |
|
| 45 | + if ($meta) { |
|
| 46 | 46 | $meta->description = $doc; |
| 47 | 47 | $meta->type = $type; |
| 48 | - $meta->container = $type == 'void'?null:ContainerFactory::create($entityBuilder, $type); |
|
| 48 | + $meta->container = $type == 'void' ?null:ContainerFactory::create($entityBuilder, $type); |
|
| 49 | 49 | } |
| 50 | 50 | } |
| 51 | 51 | } |
| 52 | 52 | \ No newline at end of file |
@@ -55,12 +55,12 @@ |
||
| 55 | 55 | */ |
| 56 | 56 | public function rule($rule, $fields) |
| 57 | 57 | { |
| 58 | - if(is_string($rule)){ |
|
| 58 | + if (is_string($rule)) { |
|
| 59 | 59 | $rules = explode('|', $rule); |
| 60 | - foreach ($rules as $r){ |
|
| 60 | + foreach ($rules as $r) { |
|
| 61 | 61 | $params = explode(':', trim($r)); |
| 62 | 62 | $rule = $params[0]; |
| 63 | - $params = isset($params[1])?explode(',', $params[1]):[]; |
|
| 63 | + $params = isset($params[1]) ?explode(',', $params[1]) : []; |
|
| 64 | 64 | |
| 65 | 65 | call_user_func_array([$this, 'parent::rule'], array_merge([$rule, $fields], $params)); |
| 66 | 66 | |
@@ -29,9 +29,9 @@ discard block |
||
| 29 | 29 | ->from($this->entity->getTable()) |
| 30 | 30 | ->where("`{$this->entity->getPK()}` = ?", $id) |
| 31 | 31 | ->getFirst(); |
| 32 | - if($row){ |
|
| 32 | + if ($row) { |
|
| 33 | 33 | return $this->entity->make($row, false); |
| 34 | - }else{ |
|
| 34 | + }else { |
|
| 35 | 35 | return null; |
| 36 | 36 | } |
| 37 | 37 | } |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | * @param mixed|null $_ |
| 54 | 54 | * @return \PhpBoot\DB\rules\basic\WhereRule |
| 55 | 55 | */ |
| 56 | - public function deleteWhere($conditions, $_=null) |
|
| 56 | + public function deleteWhere($conditions, $_ = null) |
|
| 57 | 57 | { |
| 58 | 58 | $query = $this->db->deleteFrom($this->entity->getTable()); |
| 59 | 59 | return call_user_func_array([$query, 'where'], func_get_args()); |
@@ -75,12 +75,12 @@ discard block |
||
| 75 | 75 | * @param string $_ |
| 76 | 76 | * @return \PhpBoot\DB\rules\select\WhereRule |
| 77 | 77 | */ |
| 78 | - public function findWhere($conditions=null, $_=null) |
|
| 78 | + public function findWhere($conditions = null, $_ = null) |
|
| 79 | 79 | { |
| 80 | - $query = $this->db->select($this->getColumns()) |
|
| 80 | + $query = $this->db->select($this->getColumns()) |
|
| 81 | 81 | ->from($this->entity->getTable()); |
| 82 | - $query->context->resultHandler = function ($result){ |
|
| 83 | - foreach ($result as &$i){ |
|
| 82 | + $query->context->resultHandler = function($result) { |
|
| 83 | + foreach ($result as &$i) { |
|
| 84 | 84 | $i = $this->entity->make($i, false); |
| 85 | 85 | } |
| 86 | 86 | return $result; |
@@ -107,16 +107,16 @@ discard block |
||
| 107 | 107 | * @param string $_ |
| 108 | 108 | * @return \PhpBoot\DB\rules\basic\WhereRule |
| 109 | 109 | */ |
| 110 | - public function updateWhere($values, $conditions, $_=null) |
|
| 110 | + public function updateWhere($values, $conditions, $_ = null) |
|
| 111 | 111 | { |
| 112 | - $query = $this->db->update($this->entity->getTable())->set($values); |
|
| 113 | - return call_user_func_array([$query, 'where'], array_slice(func_get_args(),1)); |
|
| 112 | + $query = $this->db->update($this->entity->getTable())->set($values); |
|
| 113 | + return call_user_func_array([$query, 'where'], array_slice(func_get_args(), 1)); |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | protected function getColumns() |
| 117 | 117 | { |
| 118 | 118 | $columns = []; |
| 119 | - foreach ($this->entity->getProperties() as $p){ |
|
| 119 | + foreach ($this->entity->getProperties() as $p) { |
|
| 120 | 120 | $columns[] = $p->name; |
| 121 | 121 | } |
| 122 | 122 | return $columns; |
@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | * |
| 51 | 51 | * @author caoym <[email protected]> |
| 52 | 52 | */ |
| 53 | -class DB{ |
|
| 53 | +class DB { |
|
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | 56 | * DB constructor. |
@@ -90,18 +90,18 @@ discard block |
||
| 90 | 90 | * @param string $column0 |
| 91 | 91 | * @return \PhpBoot\DB\rules\select\FromRule |
| 92 | 92 | */ |
| 93 | - function select($column0=null, $_=null){ |
|
| 93 | + function select($column0 = null, $_ = null) { |
|
| 94 | 94 | $obj = new SelectRule(new Context($this->connection)); |
| 95 | - if($column0 == null){ |
|
| 95 | + if ($column0 == null) { |
|
| 96 | 96 | $args = ['*']; |
| 97 | - }elseif(is_array($column0)){ |
|
| 97 | + }elseif (is_array($column0)) { |
|
| 98 | 98 | $args = $column0; |
| 99 | - }else{ |
|
| 99 | + }else { |
|
| 100 | 100 | $args = func_get_args(); |
| 101 | 101 | } |
| 102 | - foreach ($args as &$arg){ |
|
| 102 | + foreach ($args as &$arg) { |
|
| 103 | 103 | $arg = DB::wrap($arg); |
| 104 | - if($arg == '*'){ |
|
| 104 | + if ($arg == '*') { |
|
| 105 | 105 | continue; |
| 106 | 106 | } |
| 107 | 107 | } |
@@ -132,8 +132,8 @@ discard block |
||
| 132 | 132 | * @param string $table |
| 133 | 133 | * @return \PhpBoot\DB\rules\basic\WhereRule |
| 134 | 134 | */ |
| 135 | - public function deleteFrom($table){ |
|
| 136 | - $obj = new DeleteRule(new Context($this->connection)); |
|
| 135 | + public function deleteFrom($table) { |
|
| 136 | + $obj = new DeleteRule(new Context($this->connection)); |
|
| 137 | 137 | return $obj->deleteFrom($table); |
| 138 | 138 | } |
| 139 | 139 | /** |
@@ -141,8 +141,8 @@ discard block |
||
| 141 | 141 | * @param string $table |
| 142 | 142 | * @return \PhpBoot\DB\rules\replace\ValuesRule |
| 143 | 143 | */ |
| 144 | - public function replaceInto($table){ |
|
| 145 | - $obj = new ReplaceIntoRule(new Context($this->connection)); |
|
| 144 | + public function replaceInto($table) { |
|
| 145 | + $obj = new ReplaceIntoRule(new Context($this->connection)); |
|
| 146 | 146 | return $obj->replaceInto($table); |
| 147 | 147 | } |
| 148 | 148 | |
@@ -153,16 +153,16 @@ discard block |
||
| 153 | 153 | */ |
| 154 | 154 | public function transaction(callable $callback) |
| 155 | 155 | { |
| 156 | - if($this->inTransaction){ |
|
| 156 | + if ($this->inTransaction) { |
|
| 157 | 157 | return $callback($this); |
| 158 | 158 | } |
| 159 | 159 | $this->getConnection()->beginTransaction() or \PhpBoot\abort('beginTransaction failed'); |
| 160 | 160 | $this->inTransaction = true; |
| 161 | - try{ |
|
| 161 | + try { |
|
| 162 | 162 | $res = $callback($this); |
| 163 | 163 | $this->getConnection()->commit() or \PhpBoot\abort('commit failed'); |
| 164 | 164 | return $res; |
| 165 | - }catch (\Exception $e){ |
|
| 165 | + }catch (\Exception $e) { |
|
| 166 | 166 | $this->getConnection()->rollBack(); |
| 167 | 167 | Logger::warning('commit failed with '.get_class($e).' '.$e->getMessage()); |
| 168 | 168 | throw $e; |
@@ -183,12 +183,12 @@ discard block |
||
| 183 | 183 | * @param string $str |
| 184 | 184 | * @return Raw |
| 185 | 185 | */ |
| 186 | - static public function raw($str){ |
|
| 186 | + static public function raw($str) { |
|
| 187 | 187 | return new Raw($str); |
| 188 | 188 | } |
| 189 | 189 | static public function wrap($value) |
| 190 | 190 | { |
| 191 | - if($value instanceof Raw){ |
|
| 191 | + if ($value instanceof Raw) { |
|
| 192 | 192 | return $value->get(); |
| 193 | 193 | } |
| 194 | 194 | $value = trim($value); |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | return $value; |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | - if(strpos($value, '.') !== false && !preg_match('/\\s+/', $value)){ |
|
| 199 | + if (strpos($value, '.') !== false && !preg_match('/\\s+/', $value)) { |
|
| 200 | 200 | return $value; |
| 201 | 201 | } |
| 202 | 202 | return '`'.str_replace('`', '``', $value).'`'; |
@@ -209,8 +209,8 @@ discard block |
||
| 209 | 209 | { |
| 210 | 210 | return $this->app; |
| 211 | 211 | } |
| 212 | - const ORDER_BY_ASC ='ASC'; |
|
| 213 | - const ORDER_BY_DESC ='DESC'; |
|
| 212 | + const ORDER_BY_ASC = 'ASC'; |
|
| 213 | + const ORDER_BY_DESC = 'DESC'; |
|
| 214 | 214 | |
| 215 | 215 | /** |
| 216 | 216 | * @var \PDO |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | * @param string|false $asDict |
| 40 | 40 | * @return array |
| 41 | 41 | */ |
| 42 | - public function get($asDict=false) { |
|
| 42 | + public function get($asDict = false) { |
|
| 43 | 43 | return ExecImpl::get($this->context, $asDict); |
| 44 | 44 | } |
| 45 | 45 | |
@@ -53,9 +53,9 @@ discard block |
||
| 53 | 53 | * Execute sql and get one response |
| 54 | 54 | * @return null |
| 55 | 55 | */ |
| 56 | - public function getFirst(){ |
|
| 56 | + public function getFirst() { |
|
| 57 | 57 | $res = ExecImpl::get($this->context); |
| 58 | - if(count($res)){ |
|
| 58 | + if (count($res)) { |
|
| 59 | 59 | return $res[0]; |
| 60 | 60 | } |
| 61 | 61 | return null; |
@@ -68,8 +68,8 @@ discard block |
||
| 68 | 68 | * @param string $table |
| 69 | 69 | * @return \PhpBoot\DB\rules\select\JoinRule |
| 70 | 70 | */ |
| 71 | - public function from($table, $as=null){ |
|
| 72 | - FromImpl::from($this->context, $table,$as); |
|
| 71 | + public function from($table, $as = null) { |
|
| 72 | + FromImpl::from($this->context, $table, $as); |
|
| 73 | 73 | return new JoinRule($this->context); |
| 74 | 74 | } |
| 75 | 75 | } |
@@ -80,7 +80,7 @@ discard block |
||
| 80 | 80 | * @param string $column |
| 81 | 81 | * @return \PhpBoot\DB\rules\select\GetRule |
| 82 | 82 | */ |
| 83 | - public function of($column){ |
|
| 83 | + public function of($column) { |
|
| 84 | 84 | ForUpdateOfImpl::of($this->context, $column); |
| 85 | 85 | return new GetRule($this->context); |
| 86 | 86 | } |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * forUpdate() => 'FOR UPDATE' |
| 92 | 92 | * @return \PhpBoot\DB\rules\select\ForUpdateOfRule |
| 93 | 93 | */ |
| 94 | - public function forUpdate(){ |
|
| 94 | + public function forUpdate() { |
|
| 95 | 95 | ForUpdateImpl::forUpdate($this->context); |
| 96 | 96 | return new ForUpdateOfRule($this->context); |
| 97 | 97 | } |
@@ -113,7 +113,7 @@ discard block |
||
| 113 | 113 | |
| 114 | 114 | class OrderByRule extends LimitRule |
| 115 | 115 | { |
| 116 | - public function __construct($context){ |
|
| 116 | + public function __construct($context) { |
|
| 117 | 117 | parent::__construct($context); |
| 118 | 118 | $this->order = new OrderByImpl(); |
| 119 | 119 | } |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | * @param string $order Sql::ORDER_BY_ASC or Sql::ORDER_BY_DESC |
| 129 | 129 | * @return \PhpBoot\DB\rules\select\OrderByRule |
| 130 | 130 | */ |
| 131 | - public function orderBy($column, $order=null) { |
|
| 131 | + public function orderBy($column, $order = null) { |
|
| 132 | 132 | $this->order->orderBy($this->context, $column, $order); |
| 133 | 133 | return $this; |
| 134 | 134 | } |
@@ -174,17 +174,17 @@ discard block |
||
| 174 | 174 | * @param string $_ |
| 175 | 175 | * @return \PhpBoot\DB\rules\select\HavingRule |
| 176 | 176 | */ |
| 177 | - public function having($expr, $_=null) { |
|
| 178 | - if(is_callable($expr)){ |
|
| 179 | - $callback = function ($context)use($expr){ |
|
| 177 | + public function having($expr, $_ = null) { |
|
| 178 | + if (is_callable($expr)) { |
|
| 179 | + $callback = function($context)use($expr){ |
|
| 180 | 180 | $rule = new ScopedQuery($context); |
| 181 | 181 | $expr($rule); |
| 182 | 182 | }; |
| 183 | 183 | $expr = $callback; |
| 184 | 184 | } |
| 185 | - if($this->isTheFirst){ |
|
| 185 | + if ($this->isTheFirst) { |
|
| 186 | 186 | WhereImpl::where($this->context, 'HAVING', $expr, array_slice(func_get_args(), 1)); |
| 187 | - }else{ |
|
| 187 | + }else { |
|
| 188 | 188 | WhereImpl::where($this->context, 'AND', $expr, array_slice(func_get_args(), 1)); |
| 189 | 189 | } |
| 190 | 190 | |
@@ -211,9 +211,9 @@ discard block |
||
| 211 | 211 | * @param string $_ |
| 212 | 212 | * @return \PhpBoot\DB\rules\select\HavingRule |
| 213 | 213 | */ |
| 214 | - public function orHaving($expr, $_=null) { |
|
| 215 | - if(is_callable($expr)){ |
|
| 216 | - $callback = function ($context)use($expr){ |
|
| 214 | + public function orHaving($expr, $_ = null) { |
|
| 215 | + if (is_callable($expr)) { |
|
| 216 | + $callback = function($context)use($expr){ |
|
| 217 | 217 | $rule = new ScopedQuery($context); |
| 218 | 218 | $expr($rule); |
| 219 | 219 | }; |
@@ -263,17 +263,17 @@ discard block |
||
| 263 | 263 | * @param mixed $_ |
| 264 | 264 | * @return \PhpBoot\DB\rules\select\NextWhereRule |
| 265 | 265 | */ |
| 266 | - public function where($conditions=null, $_=null) { |
|
| 267 | - if(is_callable($conditions)){ |
|
| 268 | - $callback = function ($context)use($conditions){ |
|
| 266 | + public function where($conditions = null, $_ = null) { |
|
| 267 | + if (is_callable($conditions)) { |
|
| 268 | + $callback = function($context)use($conditions){ |
|
| 269 | 269 | $rule = new ScopedQuery($context); |
| 270 | 270 | $conditions($rule); |
| 271 | 271 | }; |
| 272 | 272 | $conditions = $callback; |
| 273 | 273 | } |
| 274 | - if($this->isTheFirst){ |
|
| 275 | - WhereImpl::where($this->context, 'WHERE' ,$conditions, array_slice(func_get_args(), 1)); |
|
| 276 | - }else{ |
|
| 274 | + if ($this->isTheFirst) { |
|
| 275 | + WhereImpl::where($this->context, 'WHERE', $conditions, array_slice(func_get_args(), 1)); |
|
| 276 | + }else { |
|
| 277 | 277 | WhereImpl::where($this->context, 'AND', $conditions, array_slice(func_get_args(), 1)); |
| 278 | 278 | } |
| 279 | 279 | return new NextWhereRule($this->context, false); |
@@ -304,9 +304,9 @@ discard block |
||
| 304 | 304 | * |
| 305 | 305 | * @TODO orWhere 只能跟在 Where 后 |
| 306 | 306 | */ |
| 307 | - public function orWhere($conditions=null, $_=null) { |
|
| 308 | - if(is_callable($conditions)){ |
|
| 309 | - $callback = function ($context)use($conditions){ |
|
| 307 | + public function orWhere($conditions = null, $_ = null) { |
|
| 308 | + if (is_callable($conditions)) { |
|
| 309 | + $callback = function($context)use($conditions){ |
|
| 310 | 310 | $rule = new ScopedQuery($context); |
| 311 | 311 | $conditions($rule); |
| 312 | 312 | }; |
@@ -325,8 +325,8 @@ discard block |
||
| 325 | 325 | * @param string $table |
| 326 | 326 | * @return \PhpBoot\DB\rules\select\JoinOnRule |
| 327 | 327 | */ |
| 328 | - public function join($table){ |
|
| 329 | - JoinImpl::join($this->context,null, $table); |
|
| 328 | + public function join($table) { |
|
| 329 | + JoinImpl::join($this->context, null, $table); |
|
| 330 | 330 | return new JoinOnRule($this->context); |
| 331 | 331 | } |
| 332 | 332 | /** |
@@ -334,8 +334,8 @@ discard block |
||
| 334 | 334 | * @param string $table |
| 335 | 335 | * @return \PhpBoot\DB\rules\select\JoinOnRule |
| 336 | 336 | */ |
| 337 | - public function leftJoin($table){ |
|
| 338 | - JoinImpl::join($this->context,'LEFT', $table); |
|
| 337 | + public function leftJoin($table) { |
|
| 338 | + JoinImpl::join($this->context, 'LEFT', $table); |
|
| 339 | 339 | return new JoinOnRule($this->context); |
| 340 | 340 | } |
| 341 | 341 | /** |
@@ -344,7 +344,7 @@ discard block |
||
| 344 | 344 | * @return \PhpBoot\DB\rules\select\JoinOnRule |
| 345 | 345 | */ |
| 346 | 346 | public function rightJoin($table) { |
| 347 | - JoinImpl::join($this->context,'RIGHT', $table); |
|
| 347 | + JoinImpl::join($this->context, 'RIGHT', $table); |
|
| 348 | 348 | return new JoinOnRule($this->context); |
| 349 | 349 | } |
| 350 | 350 | /** |
@@ -353,7 +353,7 @@ discard block |
||
| 353 | 353 | * @return \PhpBoot\DB\rules\select\JoinOnRule |
| 354 | 354 | */ |
| 355 | 355 | public function innerJoin($table) { |
| 356 | - JoinImpl::join($this->context,'INNER', $table); |
|
| 356 | + JoinImpl::join($this->context, 'INNER', $table); |
|
| 357 | 357 | return new JoinOnRule($this->context); |
| 358 | 358 | } |
| 359 | 359 | } |
@@ -365,7 +365,7 @@ discard block |
||
| 365 | 365 | * @param string $condition |
| 366 | 366 | * @return \PhpBoot\DB\rules\select\JoinRule |
| 367 | 367 | */ |
| 368 | - public function on($condition){ |
|
| 368 | + public function on($condition) { |
|
| 369 | 369 | JoinOnImpl::on($this->context, $condition); |
| 370 | 370 | return new JoinRule($this->context); |
| 371 | 371 | } |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | |
| 12 | 12 | class BasicRule |
| 13 | 13 | { |
| 14 | - public function __construct(Context $context){ |
|
| 14 | + public function __construct(Context $context) { |
|
| 15 | 15 | $this->context = $context; |
| 16 | 16 | } |
| 17 | 17 | |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | |
| 49 | 49 | class OrderByRule extends LimitRule |
| 50 | 50 | { |
| 51 | - public function __construct($context){ |
|
| 51 | + public function __construct($context) { |
|
| 52 | 52 | parent::__construct($context); |
| 53 | 53 | $this->impl = new OrderByImpl(); |
| 54 | 54 | } |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | * |
| 64 | 64 | * @return \PhpBoot\DB\rules\basic\LimitRule |
| 65 | 65 | */ |
| 66 | - public function orderBy($column, $order=null) { |
|
| 66 | + public function orderBy($column, $order = null) { |
|
| 67 | 67 | $this->impl->orderBy($this->context, $column, $order); |
| 68 | 68 | return new LimitRule($this->context); |
| 69 | 69 | } |
@@ -95,17 +95,17 @@ discard block |
||
| 95 | 95 | * @param mixed $_ |
| 96 | 96 | * @return NextWhereRule |
| 97 | 97 | */ |
| 98 | - public function where($conditions=null, $_=null) { |
|
| 99 | - if(is_callable($conditions)){ |
|
| 100 | - $callback = function ($context)use($conditions){ |
|
| 98 | + public function where($conditions = null, $_ = null) { |
|
| 99 | + if (is_callable($conditions)) { |
|
| 100 | + $callback = function($context)use($conditions){ |
|
| 101 | 101 | $rule = new ScopedQuery($context); |
| 102 | 102 | $conditions($rule); |
| 103 | 103 | }; |
| 104 | 104 | $conditions = $callback; |
| 105 | 105 | } |
| 106 | - if($this->isTheFirst){ |
|
| 107 | - WhereImpl::where($this->context, 'WHERE' ,$conditions, array_slice(func_get_args(), 1)); |
|
| 108 | - }else{ |
|
| 106 | + if ($this->isTheFirst) { |
|
| 107 | + WhereImpl::where($this->context, 'WHERE', $conditions, array_slice(func_get_args(), 1)); |
|
| 108 | + }else { |
|
| 109 | 109 | WhereImpl::where($this->context, 'AND', $conditions, array_slice(func_get_args(), 1)); |
| 110 | 110 | } |
| 111 | 111 | return new NextWhereRule($this->context, false); |
@@ -133,9 +133,9 @@ discard block |
||
| 133 | 133 | * @param mixed $_ |
| 134 | 134 | * @return WhereRule |
| 135 | 135 | */ |
| 136 | - public function orWhere($conditions=null, $_=null) { |
|
| 137 | - if(is_callable($conditions)){ |
|
| 138 | - $callback = function ($context)use($conditions){ |
|
| 136 | + public function orWhere($conditions = null, $_ = null) { |
|
| 137 | + if (is_callable($conditions)) { |
|
| 138 | + $callback = function($context)use($conditions){ |
|
| 139 | 139 | $rule = new ScopedQuery($context); |
| 140 | 140 | $conditions($rule); |
| 141 | 141 | }; |
@@ -160,17 +160,17 @@ discard block |
||
| 160 | 160 | * @param null $_ |
| 161 | 161 | * @return NextScopedQuery |
| 162 | 162 | */ |
| 163 | - public function where($expr, $_= null){ |
|
| 164 | - if(is_callable($expr)){ |
|
| 165 | - $callback = function ($context)use($expr){ |
|
| 163 | + public function where($expr, $_ = null) { |
|
| 164 | + if (is_callable($expr)) { |
|
| 165 | + $callback = function($context)use($expr){ |
|
| 166 | 166 | $rule = new ScopedQuery($context, true); |
| 167 | 167 | $expr($rule); |
| 168 | 168 | }; |
| 169 | 169 | $expr = $callback; |
| 170 | 170 | } |
| 171 | - if($this->isTheFirst){ |
|
| 171 | + if ($this->isTheFirst) { |
|
| 172 | 172 | WhereImpl::where($this->context, '', $expr, array_slice(func_get_args(), 1)); |
| 173 | - }else{ |
|
| 173 | + }else { |
|
| 174 | 174 | WhereImpl::where($this->context, 'AND', $expr, array_slice(func_get_args(), 1)); |
| 175 | 175 | } |
| 176 | 176 | return new NextScopedQuery($this->context, false); |
@@ -186,9 +186,9 @@ discard block |
||
| 186 | 186 | * @param null $_ |
| 187 | 187 | * @return ScopedQuery |
| 188 | 188 | */ |
| 189 | - public function orWhere($expr, $_= null){ |
|
| 190 | - if(is_callable($expr)){ |
|
| 191 | - $callback = function ($context)use($expr){ |
|
| 189 | + public function orWhere($expr, $_ = null) { |
|
| 190 | + if (is_callable($expr)) { |
|
| 191 | + $callback = function($context)use($expr){ |
|
| 192 | 192 | $rule = new ScopedQuery($context, true); |
| 193 | 193 | $expr($rule); |
| 194 | 194 | }; |
@@ -7,14 +7,14 @@ discard block |
||
| 7 | 7 | use PhpBoot\DB\rules\basic\BasicRule; |
| 8 | 8 | use PhpBoot\DB\Context; |
| 9 | 9 | |
| 10 | -class ExecResult{ |
|
| 11 | - public function __construct($success, $pdo, $st){ |
|
| 10 | +class ExecResult { |
|
| 11 | + public function __construct($success, $pdo, $st) { |
|
| 12 | 12 | $this->pdo = $pdo; |
| 13 | 13 | $this->st = $st; |
| 14 | 14 | $this->success = $success; |
| 15 | 15 | $this->rows = $this->st->rowCount(); |
| 16 | 16 | } |
| 17 | - public function lastInsertId($name=null){ |
|
| 17 | + public function lastInsertId($name = null) { |
|
| 18 | 18 | return $this->pdo->lastInsertId($name); |
| 19 | 19 | } |
| 20 | 20 | /** |
@@ -41,21 +41,21 @@ discard block |
||
| 41 | 41 | |
| 42 | 42 | class SelectImpl |
| 43 | 43 | { |
| 44 | - static public function select($context, $columns){ |
|
| 44 | + static public function select($context, $columns) { |
|
| 45 | 45 | $context->appendSql("SELECT $columns"); |
| 46 | 46 | } |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | class FromImpl |
| 50 | 50 | { |
| 51 | - static public function from($context, $tables,$as=null){ |
|
| 52 | - if($tables instanceof BasicRule){ |
|
| 51 | + static public function from($context, $tables, $as = null) { |
|
| 52 | + if ($tables instanceof BasicRule) { |
|
| 53 | 53 | $context->appendSql("FROM (".$tables->context->sql.')'); |
| 54 | - $context->params = array_merge($context->params,$tables->context->params); |
|
| 54 | + $context->params = array_merge($context->params, $tables->context->params); |
|
| 55 | 55 | }else { |
| 56 | 56 | $context->appendSql("FROM ".DB::wrap($tables)); |
| 57 | 57 | } |
| 58 | - if($as){ |
|
| 58 | + if ($as) { |
|
| 59 | 59 | $context->appendSql("AS ".DB::wrap($as)); |
| 60 | 60 | } |
| 61 | 61 | } |
@@ -73,9 +73,9 @@ discard block |
||
| 73 | 73 | { |
| 74 | 74 | static public function join($context, $type, $table) { |
| 75 | 75 | $table = DB::wrap($table); |
| 76 | - if($type){ |
|
| 76 | + if ($type) { |
|
| 77 | 77 | $context->appendSql("$type JOIN $table"); |
| 78 | - }else{ |
|
| 78 | + }else { |
|
| 79 | 79 | $context->appendSql("JOIN $table"); |
| 80 | 80 | } |
| 81 | 81 | } |
@@ -90,14 +90,14 @@ discard block |
||
| 90 | 90 | |
| 91 | 91 | class ForUpdateImpl |
| 92 | 92 | { |
| 93 | - static public function forUpdate($context){ |
|
| 93 | + static public function forUpdate($context) { |
|
| 94 | 94 | $context->appendSql("FOR UPDATE"); |
| 95 | 95 | } |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | class ForUpdateOfImpl |
| 99 | 99 | { |
| 100 | - static public function of($context, $column){ |
|
| 100 | + static public function of($context, $column) { |
|
| 101 | 101 | $column = DB::wrap($column); |
| 102 | 102 | $context->appendSql("OF $column"); |
| 103 | 103 | } |
@@ -119,57 +119,57 @@ discard block |
||
| 119 | 119 | } |
| 120 | 120 | class ValuesImpl |
| 121 | 121 | { |
| 122 | - static public function values(Context $context, array $values){ |
|
| 122 | + static public function values(Context $context, array $values) { |
|
| 123 | 123 | $params = []; |
| 124 | 124 | $stubs = []; |
| 125 | - foreach ($values as $v){ |
|
| 126 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
| 127 | - $stubs[]=$v->get(); |
|
| 128 | - }else{ |
|
| 129 | - $stubs[]='?'; |
|
| 125 | + foreach ($values as $v) { |
|
| 126 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
| 127 | + $stubs[] = $v->get(); |
|
| 128 | + }else { |
|
| 129 | + $stubs[] = '?'; |
|
| 130 | 130 | $params[] = $v; |
| 131 | 131 | } |
| 132 | 132 | } |
| 133 | 133 | $stubs = implode(',', $stubs); |
| 134 | 134 | |
| 135 | - if(array_keys($values) === range(0, count($values) - 1)){ |
|
| 135 | + if (array_keys($values) === range(0, count($values) - 1)) { |
|
| 136 | 136 | //VALUES(val0, val1, val2) |
| 137 | 137 | $context->appendSql("VALUES($stubs)"); |
| 138 | 138 | |
| 139 | - }else{ |
|
| 139 | + }else { |
|
| 140 | 140 | //(col0, col1, col2) VALUES(val0, val1, val2) |
| 141 | - $columns = implode(',', array_map(function($k){return DB::wrap($k);}, array_keys($values))); |
|
| 142 | - $context->appendSql("($columns) VALUES($stubs)",false); |
|
| 141 | + $columns = implode(',', array_map(function($k) {return DB::wrap($k); }, array_keys($values))); |
|
| 142 | + $context->appendSql("($columns) VALUES($stubs)", false); |
|
| 143 | 143 | } |
| 144 | 144 | $context->appendParams($params); |
| 145 | 145 | } |
| 146 | 146 | static public function batchValues(Context $context, array $values) |
| 147 | 147 | { |
| 148 | 148 | $count = count($values); |
| 149 | - if($count == 0){ |
|
| 149 | + if ($count == 0) { |
|
| 150 | 150 | return; |
| 151 | 151 | } |
| 152 | 152 | $keys = array_keys($values[0]); |
| 153 | 153 | $row = implode(',', self::toSql(array_values($values[0]))); |
| 154 | - if($keys === range(0, count($keys) - 1)){ |
|
| 154 | + if ($keys === range(0, count($keys) - 1)) { |
|
| 155 | 155 | //VALUES(val0, val1, val2) |
| 156 | 156 | $context->appendSql("VALUES($row)"); |
| 157 | - }else{ |
|
| 157 | + }else { |
|
| 158 | 158 | //(col0, col1, col2) VALUES(val0, val1, val2) |
| 159 | - $columns = implode(',', array_map(function($k){return DB::wrap($k);}, $keys)); |
|
| 160 | - $context->appendSql("($columns) VALUES($row)",false); |
|
| 159 | + $columns = implode(',', array_map(function($k) {return DB::wrap($k); }, $keys)); |
|
| 160 | + $context->appendSql("($columns) VALUES($row)", false); |
|
| 161 | 161 | } |
| 162 | - for($i=1; $i<$count; $i++){ |
|
| 162 | + for ($i = 1; $i<$count; $i++) { |
|
| 163 | 163 | $value = self::pick($keys, $values[$i]); |
| 164 | 164 | $row = implode(',', self::toSql($value)); |
| 165 | - $context->appendSql(", ($row)",false); |
|
| 165 | + $context->appendSql(", ($row)", false); |
|
| 166 | 166 | } |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | static protected function pick(array $keys, array $values) |
| 170 | 170 | { |
| 171 | 171 | $res = []; |
| 172 | - foreach ($keys as $key){ |
|
| 172 | + foreach ($keys as $key) { |
|
| 173 | 173 | array_key_exists($key, $values) or \PhpBoot\abort("key $key not exist from the given array"); |
| 174 | 174 | $res[$key] = $values[$key]; |
| 175 | 175 | } |
@@ -177,12 +177,12 @@ discard block |
||
| 177 | 177 | } |
| 178 | 178 | static protected function toSql(array $values) |
| 179 | 179 | { |
| 180 | - foreach ($values as &$v){ |
|
| 181 | - if($v instanceof Raw){ |
|
| 180 | + foreach ($values as &$v) { |
|
| 181 | + if ($v instanceof Raw) { |
|
| 182 | 182 | $v = $v->get(); |
| 183 | - }elseif(is_bool($v)){ |
|
| 184 | - $v = $v?'true':'false'; |
|
| 185 | - }elseif(!in_array(gettype($v), ['integer', 'boolean', 'double', 'float'])){ |
|
| 183 | + }elseif (is_bool($v)) { |
|
| 184 | + $v = $v ? 'true' : 'false'; |
|
| 185 | + }elseif (!in_array(gettype($v), ['integer', 'boolean', 'double', 'float'])) { |
|
| 186 | 186 | $v = (string)$v; |
| 187 | 187 | $v = str_replace("\\", "\\\\", $v); |
| 188 | 188 | $v = str_replace("'", "\\'", $v); |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | |
| 196 | 196 | class UpdateImpl |
| 197 | 197 | { |
| 198 | - static public function update($context, $table){ |
|
| 198 | + static public function update($context, $table) { |
|
| 199 | 199 | $table = DB::wrap($table); |
| 200 | 200 | $context->appendSql("UPDATE $table"); |
| 201 | 201 | } |
@@ -203,81 +203,81 @@ discard block |
||
| 203 | 203 | |
| 204 | 204 | class UpdateSetImpl |
| 205 | 205 | { |
| 206 | - public function set(Context $context, $expr, $args){ |
|
| 207 | - if(is_string($expr)){ |
|
| 206 | + public function set(Context $context, $expr, $args) { |
|
| 207 | + if (is_string($expr)) { |
|
| 208 | 208 | return $this->setExpr($context, $expr, $args); |
| 209 | - }else{ |
|
| 209 | + }else { |
|
| 210 | 210 | return $this->setArgs($context, $expr); |
| 211 | 211 | } |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | - public function setExpr(Context $context, $expr, $args){ |
|
| 215 | - if($this->first){ |
|
| 214 | + public function setExpr(Context $context, $expr, $args) { |
|
| 215 | + if ($this->first) { |
|
| 216 | 216 | $this->first = false; |
| 217 | 217 | $prefix = 'SET '; |
| 218 | - }else{ |
|
| 218 | + }else { |
|
| 219 | 219 | $prefix = ','; |
| 220 | 220 | } |
| 221 | 221 | |
| 222 | - $context->appendSql("$prefix$expr",$prefix == 'SET '); |
|
| 222 | + $context->appendSql("$prefix$expr", $prefix == 'SET '); |
|
| 223 | 223 | $context->appendParams($args); |
| 224 | 224 | |
| 225 | 225 | } |
| 226 | - public function setArgs(Context $context, $values){ |
|
| 226 | + public function setArgs(Context $context, $values) { |
|
| 227 | 227 | $set = []; |
| 228 | 228 | $params = []; |
| 229 | - foreach ($values as $k=>$v){ |
|
| 229 | + foreach ($values as $k=>$v) { |
|
| 230 | 230 | $k = DB::wrap($k); |
| 231 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
| 232 | - $set[]= "$k=".$v->get(); |
|
| 233 | - }else{ |
|
| 234 | - $set[]= "$k=?"; |
|
| 235 | - $params[]=$v; |
|
| 231 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
| 232 | + $set[] = "$k=".$v->get(); |
|
| 233 | + }else { |
|
| 234 | + $set[] = "$k=?"; |
|
| 235 | + $params[] = $v; |
|
| 236 | 236 | } |
| 237 | 237 | } |
| 238 | - if($this->first){ |
|
| 238 | + if ($this->first) { |
|
| 239 | 239 | $this->first = false; |
| 240 | 240 | $context->appendSql('SET '.implode(',', $set)); |
| 241 | 241 | $context->appendParams($params); |
| 242 | - }else{ |
|
| 243 | - $context->appendSql(','.implode(',', $set),false); |
|
| 242 | + }else { |
|
| 243 | + $context->appendSql(','.implode(',', $set), false); |
|
| 244 | 244 | $context->appendParams($params); |
| 245 | 245 | } |
| 246 | 246 | } |
| 247 | - private $first=true; |
|
| 247 | + private $first = true; |
|
| 248 | 248 | } |
| 249 | 249 | class OrderByImpl |
| 250 | 250 | { |
| 251 | - public function orderByArgs(Context $context, $orders){ |
|
| 252 | - if(empty($orders)){ |
|
| 251 | + public function orderByArgs(Context $context, $orders) { |
|
| 252 | + if (empty($orders)) { |
|
| 253 | 253 | return $this; |
| 254 | 254 | } |
| 255 | 255 | $params = array(); |
| 256 | - foreach ($orders as $k=>$v){ |
|
| 257 | - if(is_integer($k)){ |
|
| 256 | + foreach ($orders as $k=>$v) { |
|
| 257 | + if (is_integer($k)) { |
|
| 258 | 258 | $params[] = DB::wrap($v); |
| 259 | - }else{ |
|
| 259 | + }else { |
|
| 260 | 260 | $k = DB::wrap($k); |
| 261 | 261 | |
| 262 | 262 | $v = strtoupper($v); |
| 263 | - ($v =='DESC' || $v =='ASC') or \PhpBoot\abort( new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
|
| 263 | + ($v == 'DESC' || $v == 'ASC') or \PhpBoot\abort(new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
|
| 264 | 264 | |
| 265 | 265 | $params[] = "$k $v"; |
| 266 | 266 | } |
| 267 | 267 | } |
| 268 | - if($this->first){ |
|
| 268 | + if ($this->first) { |
|
| 269 | 269 | $this->first = false; |
| 270 | 270 | $context->appendSql('ORDER BY '.implode(',', $params)); |
| 271 | - }else{ |
|
| 272 | - $context->appendSql(','.implode(',', $params),false); |
|
| 271 | + }else { |
|
| 272 | + $context->appendSql(','.implode(',', $params), false); |
|
| 273 | 273 | } |
| 274 | 274 | return $this; |
| 275 | 275 | } |
| 276 | - public function orderBy(Context $context, $column, $order=null){ |
|
| 277 | - if(is_string($column)){ |
|
| 278 | - if($order === null){ |
|
| 276 | + public function orderBy(Context $context, $column, $order = null) { |
|
| 277 | + if (is_string($column)) { |
|
| 278 | + if ($order === null) { |
|
| 279 | 279 | $column = [$column]; |
| 280 | - }else{ |
|
| 280 | + }else { |
|
| 281 | 281 | $column = [$column=>$order]; |
| 282 | 282 | } |
| 283 | 283 | } |
@@ -285,18 +285,18 @@ discard block |
||
| 285 | 285 | |
| 286 | 286 | |
| 287 | 287 | } |
| 288 | - private $first=true; |
|
| 288 | + private $first = true; |
|
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | class LimitImpl |
| 292 | 292 | { |
| 293 | - static public function limit(Context $context, $size){ |
|
| 293 | + static public function limit(Context $context, $size) { |
|
| 294 | 294 | $intSize = intval($size); |
| 295 | 295 | strval($intSize) == $size or \PhpBoot\abort( |
| 296 | 296 | new \InvalidArgumentException("invalid params for limit($size)")); |
| 297 | 297 | $context->appendSql("LIMIT $size"); |
| 298 | 298 | } |
| 299 | - static public function limitWithOffset(Context $context, $start, $size){ |
|
| 299 | + static public function limitWithOffset(Context $context, $start, $size) { |
|
| 300 | 300 | $intStart = intval($start); |
| 301 | 301 | $intSize = intval($size); |
| 302 | 302 | strval($intStart) == $start && strval($intSize) == $size or \PhpBoot\abort( |
@@ -305,31 +305,31 @@ discard block |
||
| 305 | 305 | } |
| 306 | 306 | } |
| 307 | 307 | |
| 308 | -class WhereImpl{ |
|
| 308 | +class WhereImpl { |
|
| 309 | 309 | |
| 310 | - static private function findQ($str,$offset = 0,$no=0){ |
|
| 310 | + static private function findQ($str, $offset = 0, $no = 0) { |
|
| 311 | 311 | $found = strpos($str, '?', $offset); |
| 312 | - if($no == 0 || $found === false){ |
|
| 312 | + if ($no == 0 || $found === false) { |
|
| 313 | 313 | return $found; |
| 314 | 314 | } |
| 315 | - return self::findQ($str, $found+1, $no-1); |
|
| 315 | + return self::findQ($str, $found + 1, $no - 1); |
|
| 316 | 316 | } |
| 317 | 317 | |
| 318 | - static public function where(Context $context, $prefix, $expr, $args){ |
|
| 319 | - if(empty($expr)){ |
|
| 318 | + static public function where(Context $context, $prefix, $expr, $args) { |
|
| 319 | + if (empty($expr)) { |
|
| 320 | 320 | return; |
| 321 | 321 | } |
| 322 | - if(is_callable($expr)){ |
|
| 323 | - self::conditionClosure($context,$prefix, $expr); |
|
| 324 | - }elseif (is_string($expr)){ |
|
| 322 | + if (is_callable($expr)) { |
|
| 323 | + self::conditionClosure($context, $prefix, $expr); |
|
| 324 | + }elseif (is_string($expr)) { |
|
| 325 | 325 | self::condition($context, $prefix, $expr, $args); |
| 326 | - }else{ |
|
| 326 | + }else { |
|
| 327 | 327 | self::conditionArgs($context, $prefix, $expr); |
| 328 | 328 | } |
| 329 | 329 | |
| 330 | 330 | } |
| 331 | 331 | |
| 332 | - static public function conditionClosure(Context $context, $prefix, callable $callback){ |
|
| 332 | + static public function conditionClosure(Context $context, $prefix, callable $callback) { |
|
| 333 | 333 | $context->appendSql($prefix.' ('); |
| 334 | 334 | $callback($context); |
| 335 | 335 | $context->appendSql(')'); |
@@ -355,15 +355,15 @@ discard block |
||
| 355 | 355 | * NOT IN 'id'=>['NOT IN' => [1,2,3]] |
| 356 | 356 | * @return void |
| 357 | 357 | */ |
| 358 | - static public function conditionArgs(Context $context, $prefix, $args=[]){ |
|
| 359 | - if($args ===null){ |
|
| 360 | - return ; |
|
| 358 | + static public function conditionArgs(Context $context, $prefix, $args = []) { |
|
| 359 | + if ($args === null) { |
|
| 360 | + return; |
|
| 361 | 361 | } |
| 362 | 362 | $exprs = array(); |
| 363 | 363 | $params = array(); |
| 364 | - foreach ($args as $k => $v){ |
|
| 364 | + foreach ($args as $k => $v) { |
|
| 365 | 365 | $k = DB::wrap($k); |
| 366 | - if(!is_array($v)){ |
|
| 366 | + if (!is_array($v)) { |
|
| 367 | 367 | $v = ['='=>$v]; |
| 368 | 368 | } |
| 369 | 369 | |
@@ -375,56 +375,56 @@ discard block |
||
| 375 | 375 | new \InvalidArgumentException("invalid param $op for whereArgs")); |
| 376 | 376 | |
| 377 | 377 | $var = array_values($v)[0]; |
| 378 | - if($op == 'IN' || $op == 'NOT IN'){ |
|
| 378 | + if ($op == 'IN' || $op == 'NOT IN') { |
|
| 379 | 379 | $stubs = []; |
| 380 | 380 | |
| 381 | - if($var instanceof BasicRule){ |
|
| 381 | + if ($var instanceof BasicRule) { |
|
| 382 | 382 | $stubs = "({$var->context->sql})"; |
| 383 | 383 | $params = array_merge($params, $var->context->params); |
| 384 | 384 | $exprs[] = "$k $op $stubs"; |
| 385 | - }else{ |
|
| 386 | - foreach ($var as $i){ |
|
| 387 | - if(is_a($i, Raw::class)){ |
|
| 388 | - $stubs[]=strval($i); |
|
| 389 | - }elseif($i instanceof BasicRule){ |
|
| 385 | + }else { |
|
| 386 | + foreach ($var as $i) { |
|
| 387 | + if (is_a($i, Raw::class)) { |
|
| 388 | + $stubs[] = strval($i); |
|
| 389 | + }elseif ($i instanceof BasicRule) { |
|
| 390 | 390 | $stubs = "({$i->context->sql})"; |
| 391 | 391 | $params = array_merge($params, $i->context->params); |
| 392 | - }else{ |
|
| 393 | - $stubs[]='?'; |
|
| 392 | + }else { |
|
| 393 | + $stubs[] = '?'; |
|
| 394 | 394 | $params[] = $i; |
| 395 | 395 | } |
| 396 | 396 | } |
| 397 | 397 | $stubs = implode(',', $stubs); |
| 398 | 398 | $exprs[] = "$k $op ($stubs)"; |
| 399 | 399 | } |
| 400 | - }else if($op == 'BETWEEN'){ |
|
| 400 | + }else if ($op == 'BETWEEN') { |
|
| 401 | 401 | $cond = "$k BETWEEN"; |
| 402 | - if(is_a($var[0], Raw::class)){ |
|
| 402 | + if (is_a($var[0], Raw::class)) { |
|
| 403 | 403 | $cond = "$cond ".strval($var[0]); |
| 404 | - }elseif($var[0] instanceof BasicRule){ |
|
| 404 | + }elseif ($var[0] instanceof BasicRule) { |
|
| 405 | 405 | $cond = "$cond ({$var[0]->context->sql})"; |
| 406 | 406 | $params = array_merge($params, $var[0]->context->params); |
| 407 | - }else{ |
|
| 407 | + }else { |
|
| 408 | 408 | $cond = "$cond ?"; |
| 409 | 409 | $params[] = $var[0]; |
| 410 | 410 | } |
| 411 | - if(is_a($var[1], Raw::class)){ |
|
| 411 | + if (is_a($var[1], Raw::class)) { |
|
| 412 | 412 | $cond = "$cond AND ".strval($var[1]); |
| 413 | - }elseif($var[1] instanceof BasicRule){ |
|
| 413 | + }elseif ($var[1] instanceof BasicRule) { |
|
| 414 | 414 | $cond = "$cond AND ({$var[1]->context->sql})"; |
| 415 | 415 | $params = array_merge($params, $var[1]->context->params); |
| 416 | - }else{ |
|
| 416 | + }else { |
|
| 417 | 417 | $cond = "$cond AND ?"; |
| 418 | 418 | $params[] = $var[1]; |
| 419 | 419 | } |
| 420 | 420 | $exprs[] = $cond; |
| 421 | - }else{ |
|
| 422 | - if(is_a($var, Raw::class)){ |
|
| 421 | + }else { |
|
| 422 | + if (is_a($var, Raw::class)) { |
|
| 423 | 423 | $exprs[] = "$k $op ".strval($var); |
| 424 | - }elseif($var instanceof BasicRule){ |
|
| 424 | + }elseif ($var instanceof BasicRule) { |
|
| 425 | 425 | $exprs[] = "$k $op {$var->context->sql}"; |
| 426 | 426 | $params = array_merge($params, $var->context->params); |
| 427 | - }else{ |
|
| 427 | + }else { |
|
| 428 | 428 | $exprs[] = "$k $op ?"; |
| 429 | 429 | $params[] = $var; |
| 430 | 430 | } |
@@ -433,22 +433,22 @@ discard block |
||
| 433 | 433 | |
| 434 | 434 | self::condition($context, $prefix, implode(' AND ', $exprs), $params); |
| 435 | 435 | } |
| 436 | - static public function condition(Context $context, $prefix, $expr, $args){ |
|
| 437 | - if(!empty($expr)){ |
|
| 436 | + static public function condition(Context $context, $prefix, $expr, $args) { |
|
| 437 | + if (!empty($expr)) { |
|
| 438 | 438 | $expr = "($expr)"; |
| 439 | - if($args){ |
|
| 439 | + if ($args) { |
|
| 440 | 440 | //因为PDO不支持绑定数组变量, 这里需要手动展开数组 |
| 441 | 441 | //也就是说把 where("id IN(?)", [1,2]) 展开成 where("id IN(?,?)", 1,2) |
| 442 | 442 | $cutted = null; |
| 443 | 443 | $cut = null; |
| 444 | 444 | $toReplace = array(); |
| 445 | 445 | |
| 446 | - $newArgs=array(); |
|
| 446 | + $newArgs = array(); |
|
| 447 | 447 | //找到所有数组对应的?符位置 |
| 448 | - foreach ($args as $k =>$arg){ |
|
| 449 | - if(is_array($arg) || is_a($arg, Raw::class) || is_a($arg, BasicRule::class)){ |
|
| 448 | + foreach ($args as $k =>$arg) { |
|
| 449 | + if (is_array($arg) || is_a($arg, Raw::class) || is_a($arg, BasicRule::class)) { |
|
| 450 | 450 | |
| 451 | - if(!$cutted){ |
|
| 451 | + if (!$cutted) { |
|
| 452 | 452 | $cut = new NestedStringCut($expr); |
| 453 | 453 | $cutted = $cut->getText(); |
| 454 | 454 | } |
@@ -458,54 +458,54 @@ discard block |
||
| 458 | 458 | $pos !== false or \PhpBoot\abort( |
| 459 | 459 | new \InvalidArgumentException("unmatched params and ? @ $expr")); |
| 460 | 460 | |
| 461 | - if(is_array($arg)){ |
|
| 461 | + if (is_array($arg)) { |
|
| 462 | 462 | $stubs = []; |
| 463 | - foreach ($arg as $i){ |
|
| 464 | - if(is_a($i, Raw::class)){ |
|
| 463 | + foreach ($arg as $i) { |
|
| 464 | + if (is_a($i, Raw::class)) { |
|
| 465 | 465 | $stubs[] = strval($i); |
| 466 | - }else{ |
|
| 466 | + }else { |
|
| 467 | 467 | $stubs[] = '?'; |
| 468 | 468 | $newArgs[] = $i; |
| 469 | 469 | } |
| 470 | 470 | } |
| 471 | 471 | $stubs = implode(',', $stubs); |
| 472 | - }elseif($arg instanceof BasicRule){ |
|
| 472 | + }elseif ($arg instanceof BasicRule) { |
|
| 473 | 473 | $stubs = "({$arg->context->sql})"; |
| 474 | 474 | $newArgs = array_merge($newArgs, $arg->context->params); |
| 475 | - }else{ |
|
| 475 | + }else { |
|
| 476 | 476 | $stubs = strval($arg); |
| 477 | 477 | } |
| 478 | 478 | $toReplace[] = [$pos, $stubs]; |
| 479 | 479 | |
| 480 | - }else{ |
|
| 481 | - $newArgs[]=$arg; |
|
| 480 | + }else { |
|
| 481 | + $newArgs[] = $arg; |
|
| 482 | 482 | } |
| 483 | 483 | } |
| 484 | 484 | |
| 485 | - if(count($toReplace)){ |
|
| 485 | + if (count($toReplace)) { |
|
| 486 | 486 | $toReplace = array_reverse($toReplace); |
| 487 | - foreach ($toReplace as $i){ |
|
| 487 | + foreach ($toReplace as $i) { |
|
| 488 | 488 | list($pos, $v) = $i; |
| 489 | - $expr = substr($expr, 0, $pos).$v.substr($expr, $pos+1); |
|
| 489 | + $expr = substr($expr, 0, $pos).$v.substr($expr, $pos + 1); |
|
| 490 | 490 | } |
| 491 | 491 | $args = $newArgs; |
| 492 | 492 | } |
| 493 | 493 | } |
| 494 | - if($prefix){ |
|
| 494 | + if ($prefix) { |
|
| 495 | 495 | $context->appendSql($prefix.' '.$expr); |
| 496 | - }else{ |
|
| 496 | + }else { |
|
| 497 | 497 | $context->appendSql($expr); |
| 498 | 498 | } |
| 499 | 499 | |
| 500 | - if($args){ |
|
| 500 | + if ($args) { |
|
| 501 | 501 | $context->appendParams($args); |
| 502 | 502 | } |
| 503 | 503 | } |
| 504 | 504 | } |
| 505 | 505 | } |
| 506 | 506 | |
| 507 | -class GroupByImpl{ |
|
| 508 | - static public function groupBy(Context $context, $column){ |
|
| 507 | +class GroupByImpl { |
|
| 508 | + static public function groupBy(Context $context, $column) { |
|
| 509 | 509 | $column = DB::wrap($column); |
| 510 | 510 | $context->appendSql("GROUP BY $column"); |
| 511 | 511 | } |
@@ -530,20 +530,20 @@ discard block |
||
| 530 | 530 | * @param string|false $asDict return as dict or array |
| 531 | 531 | * @return false|array |
| 532 | 532 | */ |
| 533 | - static public function get($context, $dictAs=false){ |
|
| 533 | + static public function get($context, $dictAs = false) { |
|
| 534 | 534 | |
| 535 | 535 | $st = $context->connection->prepare($context->sql); |
| 536 | - if($st->execute($context->params)){ |
|
| 536 | + if ($st->execute($context->params)) { |
|
| 537 | 537 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
| 538 | - if ($dictAs){ |
|
| 539 | - $dict= []; |
|
| 540 | - foreach ($res as $i){ |
|
| 541 | - $dict[$i[$dictAs]]=$i; |
|
| 538 | + if ($dictAs) { |
|
| 539 | + $dict = []; |
|
| 540 | + foreach ($res as $i) { |
|
| 541 | + $dict[$i[$dictAs]] = $i; |
|
| 542 | 542 | } |
| 543 | 543 | return $context->handleResult($dict); |
| 544 | 544 | } |
| 545 | 545 | return $context->handleResult($res); |
| 546 | - }else{ |
|
| 546 | + }else { |
|
| 547 | 547 | return false; |
| 548 | 548 | } |
| 549 | 549 | } |
@@ -552,22 +552,22 @@ discard block |
||
| 552 | 552 | * @param Context $context |
| 553 | 553 | * @return int|false |
| 554 | 554 | */ |
| 555 | - static public function count($context){ |
|
| 555 | + static public function count($context) { |
|
| 556 | 556 | |
| 557 | 557 | $found = []; |
| 558 | - if(!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 559 | - count($found)==0){ |
|
| 558 | + if (!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 559 | + count($found) == 0) { |
|
| 560 | 560 | \PhpBoot\abort(new \PDOException("can not use count(*) without select")); |
| 561 | 561 | } |
| 562 | 562 | list($chars, $columnBegin) = $found[0]; |
| 563 | - $columnBegin = $columnBegin + strlen('select')+1; |
|
| 563 | + $columnBegin = $columnBegin + strlen('select') + 1; |
|
| 564 | 564 | |
| 565 | 565 | $columnEnd = 0; |
| 566 | 566 | $found = []; |
| 567 | - if(!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 568 | - count($found)==0){ |
|
| 567 | + if (!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 568 | + count($found) == 0) { |
|
| 569 | 569 | $columnEnd = strlen($context->sql); |
| 570 | - }else{ |
|
| 570 | + }else { |
|
| 571 | 571 | list($chars, $columnEnd) = $found[0]; |
| 572 | 572 | } |
| 573 | 573 | $sql = substr($context->sql, 0, $columnBegin); |
@@ -575,10 +575,10 @@ discard block |
||
| 575 | 575 | $sql .= substr($context->sql, $columnEnd); |
| 576 | 576 | |
| 577 | 577 | $st = $context->connection->prepare($sql); |
| 578 | - if($st->execute($context->params)){ |
|
| 578 | + if ($st->execute($context->params)) { |
|
| 579 | 579 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
| 580 | 580 | return intval($res[0]['count']); |
| 581 | - }else{ |
|
| 581 | + }else { |
|
| 582 | 582 | return false; |
| 583 | 583 | } |
| 584 | 584 | |
@@ -586,47 +586,47 @@ discard block |
||
| 586 | 586 | } |
| 587 | 587 | class OnDuplicateKeyUpdateImpl |
| 588 | 588 | { |
| 589 | - public function set($context, $column, $value){ |
|
| 590 | - if(is_string($column)){ |
|
| 589 | + public function set($context, $column, $value) { |
|
| 590 | + if (is_string($column)) { |
|
| 591 | 591 | $this->setExpr($context, $column, $value); |
| 592 | - }else{ |
|
| 592 | + }else { |
|
| 593 | 593 | $this->setArgs($context, $column); |
| 594 | 594 | } |
| 595 | 595 | } |
| 596 | 596 | |
| 597 | - public function setExpr($context, $expr, $args){ |
|
| 597 | + public function setExpr($context, $expr, $args) { |
|
| 598 | 598 | $prefix = ''; |
| 599 | - if($this->first){ |
|
| 599 | + if ($this->first) { |
|
| 600 | 600 | $this->first = false; |
| 601 | 601 | $prefix = 'ON DUPLICATE KEY UPDATE '; |
| 602 | - }else{ |
|
| 602 | + }else { |
|
| 603 | 603 | $prefix = ','; |
| 604 | 604 | } |
| 605 | 605 | |
| 606 | - $context->appendSql("$prefix$expr",$prefix == 'ON DUPLICATE KEY UPDATE '); |
|
| 606 | + $context->appendSql("$prefix$expr", $prefix == 'ON DUPLICATE KEY UPDATE '); |
|
| 607 | 607 | $context->appendParams($args); |
| 608 | 608 | |
| 609 | 609 | } |
| 610 | - public function setArgs($context, $values){ |
|
| 610 | + public function setArgs($context, $values) { |
|
| 611 | 611 | $set = []; |
| 612 | 612 | $params = []; |
| 613 | - foreach ($values as $k=>$v){ |
|
| 613 | + foreach ($values as $k=>$v) { |
|
| 614 | 614 | $k = DB::wrap($k); |
| 615 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
| 616 | - $set[]= "$k=".$v->get(); |
|
| 617 | - }else{ |
|
| 618 | - $set[]= "$k=?"; |
|
| 619 | - $params[]=$v; |
|
| 615 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
| 616 | + $set[] = "$k=".$v->get(); |
|
| 617 | + }else { |
|
| 618 | + $set[] = "$k=?"; |
|
| 619 | + $params[] = $v; |
|
| 620 | 620 | } |
| 621 | 621 | } |
| 622 | - if($this->first){ |
|
| 622 | + if ($this->first) { |
|
| 623 | 623 | $this->first = false; |
| 624 | 624 | $context->appendSql('ON DUPLICATE KEY UPDATE '.implode(',', $set)); |
| 625 | 625 | $context->appendParams($params); |
| 626 | - }else{ |
|
| 627 | - $context->appendSql(','.implode(',', $set),false); |
|
| 626 | + }else { |
|
| 627 | + $context->appendSql(','.implode(',', $set), false); |
|
| 628 | 628 | $context->appendParams($params); |
| 629 | 629 | } |
| 630 | 630 | } |
| 631 | - private $first=true; |
|
| 631 | + private $first = true; |
|
| 632 | 632 | } |