@@ -73,7 +73,7 @@ |
||
| 73 | 73 | } |
| 74 | 74 | } |
| 75 | 75 | /** |
| 76 | - * @param $className |
|
| 76 | + * @param string $className |
|
| 77 | 77 | * @return self |
| 78 | 78 | */ |
| 79 | 79 | static function readWithoutCache($className) |
@@ -32,9 +32,9 @@ discard block |
||
| 32 | 32 | } |
| 33 | 33 | class AnnotationReader implements \ArrayAccess |
| 34 | 34 | { |
| 35 | - static public function createDocBlockFactory(){ |
|
| 35 | + static public function createDocBlockFactory() { |
|
| 36 | 36 | $fqsenResolver = new FqsenResolver(); |
| 37 | - $tagFactory = new StandardTagFactory($fqsenResolver,[]); |
|
| 37 | + $tagFactory = new StandardTagFactory($fqsenResolver, []); |
|
| 38 | 38 | $descriptionFactory = new DescriptionFactory($tagFactory); |
| 39 | 39 | $tagFactory->addService($descriptionFactory); |
| 40 | 40 | $tagFactory->addService(new TypeResolver($fqsenResolver)); |
@@ -53,22 +53,22 @@ discard block |
||
| 53 | 53 | { |
| 54 | 54 | $rfl = new \ReflectionClass($className) or \PhpBoot\abort("load class $className failed"); |
| 55 | 55 | $fileName = $rfl->getFileName(); |
| 56 | - $key = str_replace('\\','.',self::class).md5($fileName.$className); |
|
| 56 | + $key = str_replace('\\', '.', self::class).md5($fileName.$className); |
|
| 57 | 57 | $oldData = null; |
| 58 | - $cache = new CheckableCache($localCache?:new ApcCache()); |
|
| 58 | + $cache = new CheckableCache($localCache ?: new ApcCache()); |
|
| 59 | 59 | $res = $cache->get('ann:'.$key, null, $oldData, false); |
| 60 | - if($res === null){ |
|
| 61 | - try{ |
|
| 60 | + if ($res === null) { |
|
| 61 | + try { |
|
| 62 | 62 | $meta = self::readWithoutCache($className); |
| 63 | - $cache->set($key, $meta, 0, $fileName?new ClassModifiedChecker($className):null); |
|
| 63 | + $cache->set($key, $meta, 0, $fileName ? new ClassModifiedChecker($className) : null); |
|
| 64 | 64 | return $meta; |
| 65 | - }catch (\Exception $e){ |
|
| 66 | - $cache->set($key, $e->getMessage(), 0, $fileName?new ClassModifiedChecker($className):null); |
|
| 65 | + }catch (\Exception $e) { |
|
| 66 | + $cache->set($key, $e->getMessage(), 0, $fileName ? new ClassModifiedChecker($className) : null); |
|
| 67 | 67 | throw $e; |
| 68 | 68 | } |
| 69 | - }elseif(is_string($res)){ |
|
| 69 | + }elseif (is_string($res)) { |
|
| 70 | 70 | \PhpBoot\abort($res); |
| 71 | - }else{ |
|
| 71 | + }else { |
|
| 72 | 72 | return $res; |
| 73 | 73 | } |
| 74 | 74 | } |
@@ -85,19 +85,19 @@ discard block |
||
| 85 | 85 | $reader->class->name = $className; |
| 86 | 86 | |
| 87 | 87 | //method annotations |
| 88 | - foreach ($rfl->getMethods() as $i){ |
|
| 88 | + foreach ($rfl->getMethods() as $i) { |
|
| 89 | 89 | $block = self::readAnnotationBlock($i->getDocComment()); |
| 90 | 90 | $block->name = $i->getName(); |
| 91 | - $reader->methods[$i->getName()]=$block; |
|
| 91 | + $reader->methods[$i->getName()] = $block; |
|
| 92 | 92 | } |
| 93 | 93 | //property annotations |
| 94 | - foreach ($rfl->getProperties() as $i){ |
|
| 94 | + foreach ($rfl->getProperties() as $i) { |
|
| 95 | 95 | if ($i->isStatic()) { |
| 96 | 96 | continue; |
| 97 | 97 | } |
| 98 | 98 | $block = self::readAnnotationBlock($i->getDocComment()); |
| 99 | 99 | $block->name = $i->getName(); |
| 100 | - $reader->properties[$i->getName()]=$block; |
|
| 100 | + $reader->properties[$i->getName()] = $block; |
|
| 101 | 101 | } |
| 102 | 102 | while ($rfl = $rfl->getParentClass()) { |
| 103 | 103 | foreach ($rfl->getProperties(\ReflectionProperty::IS_PRIVATE) as $i) { |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | } |
| 107 | 107 | $block = self::readAnnotationBlock($i->getDocComment()); |
| 108 | 108 | $block->name = $i->getName(); |
| 109 | - $reader->properties[$i->getName()]=$block; |
|
| 109 | + $reader->properties[$i->getName()] = $block; |
|
| 110 | 110 | } |
| 111 | 111 | } |
| 112 | 112 | return $reader; |
@@ -115,7 +115,7 @@ discard block |
||
| 115 | 115 | static private function readAnnotationBlock($doc) |
| 116 | 116 | { |
| 117 | 117 | $annBlock = new AnnotationBlock(); |
| 118 | - if(empty($doc)){ |
|
| 118 | + if (empty($doc)) { |
|
| 119 | 119 | return $annBlock; |
| 120 | 120 | } |
| 121 | 121 | $docFactory = AnnotationReader::createDocBlockFactory(); |
@@ -130,8 +130,8 @@ discard block |
||
| 130 | 130 | $annTag->parent = $annBlock; |
| 131 | 131 | $annTag->description = strval($desc); |
| 132 | 132 | $annTag->name = $tag->getName(); |
| 133 | - $annTag->children=[]; |
|
| 134 | - if($desc){ |
|
| 133 | + $annTag->children = []; |
|
| 134 | + if ($desc) { |
|
| 135 | 135 | $output = new AnnotationTagsOutput(); |
| 136 | 136 | $desc->render($output); |
| 137 | 137 | foreach ($output->tags as $child) { |
@@ -155,11 +155,11 @@ discard block |
||
| 155 | 155 | * @var AnnotationBlock[] |
| 156 | 156 | */ |
| 157 | 157 | |
| 158 | - public $methods=[]; |
|
| 158 | + public $methods = []; |
|
| 159 | 159 | /** |
| 160 | 160 | * @var AnnotationBlock[] |
| 161 | 161 | */ |
| 162 | - public $properties=[]; |
|
| 162 | + public $properties = []; |
|
| 163 | 163 | |
| 164 | 164 | public function offsetExists($offset) |
| 165 | 165 | { |
@@ -62,13 +62,13 @@ |
||
| 62 | 62 | $meta = self::readWithoutCache($className); |
| 63 | 63 | $cache->set($key, $meta, 0, $fileName?new ClassModifiedChecker($className):null); |
| 64 | 64 | return $meta; |
| 65 | - }catch (\Exception $e){ |
|
| 65 | + } catch (\Exception $e){ |
|
| 66 | 66 | $cache->set($key, $e->getMessage(), 0, $fileName?new ClassModifiedChecker($className):null); |
| 67 | 67 | throw $e; |
| 68 | 68 | } |
| 69 | - }elseif(is_string($res)){ |
|
| 69 | + } elseif(is_string($res)){ |
|
| 70 | 70 | \PhpBoot\abort($res); |
| 71 | - }else{ |
|
| 71 | + } else{ |
|
| 72 | 72 | return $res; |
| 73 | 73 | } |
| 74 | 74 | } |
@@ -8,7 +8,6 @@ |
||
| 8 | 8 | /** |
| 9 | 9 | * AnnotationTag constructor. |
| 10 | 10 | * @param string $name |
| 11 | - * @param string $summary |
|
| 12 | 11 | * @param string $description |
| 13 | 12 | * @param array $children |
| 14 | 13 | * @param AnnotationBlock|AnnotationTag|null $parent |
@@ -13,10 +13,10 @@ discard block |
||
| 13 | 13 | * @param array $children |
| 14 | 14 | * @param AnnotationBlock|AnnotationTag|null $parent |
| 15 | 15 | */ |
| 16 | - public function __construct($name='', |
|
| 17 | - $description='', |
|
| 18 | - $children=[], |
|
| 19 | - $parent=null) |
|
| 16 | + public function __construct($name = '', |
|
| 17 | + $description = '', |
|
| 18 | + $children = [], |
|
| 19 | + $parent = null) |
|
| 20 | 20 | { |
| 21 | 21 | $this->name = $name; |
| 22 | 22 | $this->description = $description; |
@@ -31,11 +31,11 @@ discard block |
||
| 31 | 31 | /** |
| 32 | 32 | * @var string |
| 33 | 33 | */ |
| 34 | - public $description=''; |
|
| 34 | + public $description = ''; |
|
| 35 | 35 | /** |
| 36 | 36 | * @var AnnotationBlock[] |
| 37 | 37 | */ |
| 38 | - public $children=[]; |
|
| 38 | + public $children = []; |
|
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | 41 | * @var AnnotationBlock|null |
@@ -375,7 +375,7 @@ |
||
| 375 | 375 | } |
| 376 | 376 | |
| 377 | 377 | /** |
| 378 | - * @return \string[] |
|
| 378 | + * @return string[] |
|
| 379 | 379 | */ |
| 380 | 380 | public function getGlobalHooks() |
| 381 | 381 | { |
@@ -24,7 +24,6 @@ |
||
| 24 | 24 | use PhpBoot\DB\DB; |
| 25 | 25 | use PhpBoot\DI\DIContainerBuilder; |
| 26 | 26 | use PhpBoot\DI\Traits\EnableDIAnnotations; |
| 27 | -use PhpBoot\Lock\LocalAutoLock; |
|
| 28 | 27 | use PhpBoot\Utils\Logger; |
| 29 | 28 | use Psr\Container\ContainerExceptionInterface; |
| 30 | 29 | use Psr\Container\ContainerInterface; |
@@ -83,11 +83,11 @@ discard block |
||
| 83 | 83 | |
| 84 | 84 | Request::class => \DI\factory([Application::class, 'createRequestFromGlobals']), |
| 85 | 85 | ]; |
| 86 | - if(function_exists('apc_fetch')){ |
|
| 86 | + if (function_exists('apc_fetch')) { |
|
| 87 | 87 | $default += [ |
| 88 | 88 | Cache::class => \DI\object(ApcCache::class) |
| 89 | 89 | ]; |
| 90 | - }else{ |
|
| 90 | + }else { |
|
| 91 | 91 | $default += [ |
| 92 | 92 | Cache::class => \DI\object(FilesystemCache::class) |
| 93 | 93 | ->constructorParameter('directory', sys_get_temp_dir()) |
@@ -129,11 +129,11 @@ discard block |
||
| 129 | 129 | * @param string[] $hooks hook class names |
| 130 | 130 | * @return void |
| 131 | 131 | */ |
| 132 | - public function loadRoutesFromClass($className, $hooks=[]) |
|
| 132 | + public function loadRoutesFromClass($className, $hooks = []) |
|
| 133 | 133 | { |
| 134 | 134 | $cache = new CheckableCache($this->cache); |
| 135 | 135 | |
| 136 | - $key = 'loadRoutesFromClass:' . md5(__CLASS__ . ':' . $className); |
|
| 136 | + $key = 'loadRoutesFromClass:'.md5(__CLASS__.':'.$className); |
|
| 137 | 137 | $routes = $cache->get($key, $this); |
| 138 | 138 | |
| 139 | 139 | $controller = null; |
@@ -150,9 +150,9 @@ discard block |
||
| 150 | 150 | $this->routes[] = [ |
| 151 | 151 | $method, |
| 152 | 152 | $uri, |
| 153 | - function (Application $app, Request $request) use ($cache, $className, $actionName, $controller) { |
|
| 153 | + function(Application $app, Request $request) use ($cache, $className, $actionName, $controller) { |
|
| 154 | 154 | |
| 155 | - $key = 'loadRoutesFromClass:route:' . md5(__CLASS__ . ':' . $className . ':' . $actionName); |
|
| 155 | + $key = 'loadRoutesFromClass:route:'.md5(__CLASS__.':'.$className.':'.$actionName); |
|
| 156 | 156 | |
| 157 | 157 | $routeInstance = $cache->get($key, $this); |
| 158 | 158 | if ($routeInstance == $this) { |
@@ -180,11 +180,11 @@ discard block |
||
| 180 | 180 | * @param string[] $hooks |
| 181 | 181 | * @return void |
| 182 | 182 | */ |
| 183 | - public function loadRoutesFromPath($fromPath, $namespace = '', $hooks=[]) |
|
| 183 | + public function loadRoutesFromPath($fromPath, $namespace = '', $hooks = []) |
|
| 184 | 184 | { |
| 185 | 185 | $dir = @dir($fromPath); |
| 186 | 186 | |
| 187 | - $getEach = function () use ($dir) { |
|
| 187 | + $getEach = function() use ($dir) { |
|
| 188 | 188 | $name = $dir->read(); |
| 189 | 189 | if (!$name) { |
| 190 | 190 | return $name; |
@@ -196,11 +196,11 @@ discard block |
||
| 196 | 196 | if ($entry == '.' || $entry == '..') { |
| 197 | 197 | continue; |
| 198 | 198 | } |
| 199 | - $path = $fromPath . '/' . str_replace('\\', '/', $entry); |
|
| 199 | + $path = $fromPath.'/'.str_replace('\\', '/', $entry); |
|
| 200 | 200 | if (is_file($path) && substr_compare($entry, '.php', strlen($entry) - 4, 4, true) == 0) { |
| 201 | - $class_name = $namespace . '\\' . substr($entry, 0, strlen($entry) - 4); |
|
| 201 | + $class_name = $namespace.'\\'.substr($entry, 0, strlen($entry) - 4); |
|
| 202 | 202 | $this->loadRoutesFromClass($class_name, $hooks); |
| 203 | - } else { |
|
| 203 | + }else { |
|
| 204 | 204 | //\Log::debug($path.' ignored'); |
| 205 | 205 | } |
| 206 | 206 | } |
@@ -213,7 +213,7 @@ discard block |
||
| 213 | 213 | * @param callable $handler function(Application $app, Request $request):Response |
| 214 | 214 | * @param string[] $hooks |
| 215 | 215 | */ |
| 216 | - public function addRoute($method, $uri, callable $handler, $hooks=[]) |
|
| 216 | + public function addRoute($method, $uri, callable $handler, $hooks = []) |
|
| 217 | 217 | { |
| 218 | 218 | $this->routes[] = [$method, $uri, $handler, $hooks]; |
| 219 | 219 | } |
@@ -239,7 +239,7 @@ discard block |
||
| 239 | 239 | { |
| 240 | 240 | // TODO 把 Route里的异常处理 ExceptionRenderer 移到这里更妥? |
| 241 | 241 | $renderer = $this->get(ExceptionRenderer::class); |
| 242 | - try{ |
|
| 242 | + try { |
|
| 243 | 243 | if ($request == null) { |
| 244 | 244 | $request = $this->make(Request::class); |
| 245 | 245 | } |
@@ -249,7 +249,7 @@ discard block |
||
| 249 | 249 | } |
| 250 | 250 | $uri = rawurldecode($uri); |
| 251 | 251 | |
| 252 | - $next = function (Request $request)use($uri){ |
|
| 252 | + $next = function(Request $request)use($uri){ |
|
| 253 | 253 | $dispatcher = $this->getDispatcher(); |
| 254 | 254 | $res = $dispatcher->dispatch($request->getMethod(), $uri); |
| 255 | 255 | if ($res[0] == Dispatcher::FOUND) { |
@@ -258,10 +258,10 @@ discard block |
||
| 258 | 258 | $request->attributes->add($res[2]); |
| 259 | 259 | } |
| 260 | 260 | list($handler, $hooks) = $res[1]; |
| 261 | - $next = function (Request $request)use($handler){ |
|
| 261 | + $next = function(Request $request)use($handler){ |
|
| 262 | 262 | return $handler($this, $request); |
| 263 | 263 | }; |
| 264 | - foreach (array_reverse($hooks) as $hookName){ |
|
| 264 | + foreach (array_reverse($hooks) as $hookName) { |
|
| 265 | 265 | $next = function($request)use($hookName, $next){ |
| 266 | 266 | $hook = $this->get($hookName); |
| 267 | 267 | /**@var $hook HookInterface*/ |
@@ -274,12 +274,12 @@ discard block |
||
| 274 | 274 | \PhpBoot\abort(new NotFoundHttpException(), [$request->getMethod(), $uri]); |
| 275 | 275 | } elseif ($res[0] == Dispatcher::METHOD_NOT_ALLOWED) { |
| 276 | 276 | \PhpBoot\abort(new MethodNotAllowedHttpException($res[1]), [$request->getMethod(), $uri]); |
| 277 | - } else { |
|
| 277 | + }else { |
|
| 278 | 278 | \PhpBoot\abort("unknown dispatch return {$res[0]}"); |
| 279 | 279 | } |
| 280 | 280 | }; |
| 281 | 281 | |
| 282 | - foreach (array_reverse($this->getGlobalHooks()) as $hookName){ |
|
| 282 | + foreach (array_reverse($this->getGlobalHooks()) as $hookName) { |
|
| 283 | 283 | $next = function($request)use($hookName, $next){ |
| 284 | 284 | $hook = $this->get($hookName); |
| 285 | 285 | /**@var $hook HookInterface*/ |
@@ -294,7 +294,7 @@ discard block |
||
| 294 | 294 | } |
| 295 | 295 | return $response; |
| 296 | 296 | |
| 297 | - }catch (\Exception $e){ |
|
| 297 | + }catch (\Exception $e) { |
|
| 298 | 298 | $renderer->render($e); |
| 299 | 299 | } |
| 300 | 300 | |
@@ -87,7 +87,7 @@ discard block |
||
| 87 | 87 | $default += [ |
| 88 | 88 | Cache::class => \DI\object(ApcCache::class) |
| 89 | 89 | ]; |
| 90 | - }else{ |
|
| 90 | + } else{ |
|
| 91 | 91 | $default += [ |
| 92 | 92 | Cache::class => \DI\object(FilesystemCache::class) |
| 93 | 93 | ->constructorParameter('directory', sys_get_temp_dir()) |
@@ -270,7 +270,7 @@ discard block |
||
| 270 | 270 | } |
| 271 | 271 | return $next($request); |
| 272 | 272 | |
| 273 | - }elseif ($res[0] == Dispatcher::NOT_FOUND) { |
|
| 273 | + } elseif ($res[0] == Dispatcher::NOT_FOUND) { |
|
| 274 | 274 | \PhpBoot\abort(new NotFoundHttpException(), [$request->getMethod(), $uri]); |
| 275 | 275 | } elseif ($res[0] == Dispatcher::METHOD_NOT_ALLOWED) { |
| 276 | 276 | \PhpBoot\abort(new MethodNotAllowedHttpException($res[1]), [$request->getMethod(), $uri]); |
@@ -294,7 +294,7 @@ discard block |
||
| 294 | 294 | } |
| 295 | 295 | return $response; |
| 296 | 296 | |
| 297 | - }catch (\Exception $e){ |
|
| 297 | + } catch (\Exception $e){ |
|
| 298 | 298 | $renderer->render($e); |
| 299 | 299 | } |
| 300 | 300 | |
@@ -4,6 +4,9 @@ |
||
| 4 | 4 | |
| 5 | 5 | class ClassModifiedChecker extends FileModifiedChecker |
| 6 | 6 | { |
| 7 | + /** |
|
| 8 | + * @param string $className |
|
| 9 | + */ |
|
| 7 | 10 | function __construct($className){ |
| 8 | 11 | $class = new \ReflectionClass($className); |
| 9 | 12 | $files = []; |
@@ -4,11 +4,11 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | class ClassModifiedChecker extends FileModifiedChecker |
| 6 | 6 | { |
| 7 | - function __construct($className){ |
|
| 7 | + function __construct($className) { |
|
| 8 | 8 | $class = new \ReflectionClass($className); |
| 9 | 9 | $files = []; |
| 10 | 10 | |
| 11 | - if($class->getFileName()){ |
|
| 11 | + if ($class->getFileName()) { |
|
| 12 | 12 | $files[] = $class->getFileName(); |
| 13 | 13 | self::getParentFileName($class, $files); |
| 14 | 14 | } |
@@ -18,15 +18,15 @@ discard block |
||
| 18 | 18 | static public function getParentFileName(\ReflectionClass $class, array &$files) |
| 19 | 19 | { |
| 20 | 20 | $parent = $class->getParentClass(); |
| 21 | - if(!$parent){ |
|
| 21 | + if (!$parent) { |
|
| 22 | 22 | return; |
| 23 | 23 | } |
| 24 | - if($parent->getFileName()){ |
|
| 24 | + if ($parent->getFileName()) { |
|
| 25 | 25 | $files[] = $parent->getParentClass(); |
| 26 | 26 | self::getParentFileName($parent, $files); |
| 27 | 27 | } |
| 28 | - foreach ($class->getInterfaces() as $interface){ |
|
| 29 | - if($interface->getFileName()){ |
|
| 28 | + foreach ($class->getInterfaces() as $interface) { |
|
| 29 | + if ($interface->getFileName()) { |
|
| 30 | 30 | $files[] = $interface->getFileName(); |
| 31 | 31 | self::getParentFileName($interface, $files); |
| 32 | 32 | } |
@@ -5,7 +5,6 @@ |
||
| 5 | 5 | use DI\FactoryInterface; |
| 6 | 6 | use \DI\InvokerInterface as DIInvokerInterface; |
| 7 | 7 | use Doctrine\Common\Cache\Cache; |
| 8 | -use PhpBoot\Cache\LocalCacheInterface; |
|
| 9 | 8 | use PhpBoot\Controller\Annotations\BindAnnotationHandler; |
| 10 | 9 | use PhpBoot\Controller\Annotations\ClassAnnotationHandler; |
| 11 | 10 | use PhpBoot\Controller\Annotations\HookAnnotationHandler; |
@@ -19,7 +19,7 @@ discard block |
||
| 19 | 19 | |
| 20 | 20 | class ControllerContainerBuilder extends ContainerBuilder |
| 21 | 21 | { |
| 22 | - static $DEFAULT_ANNOTATIONS=[ |
|
| 22 | + static $DEFAULT_ANNOTATIONS = [ |
|
| 23 | 23 | [ClassAnnotationHandler::class, 'class'], |
| 24 | 24 | [PathAnnotationHandler::class, "class.children[?name=='path']"], |
| 25 | 25 | [RouteAnnotationHandler::class, "methods.*.children[?name=='route'][]"], |
@@ -43,9 +43,9 @@ discard block |
||
| 43 | 43 | Cache $cache, |
| 44 | 44 | array $annotations = null) |
| 45 | 45 | { |
| 46 | - if($annotations){ |
|
| 46 | + if ($annotations) { |
|
| 47 | 47 | parent::__construct($annotations, $cache); |
| 48 | - }else{ |
|
| 48 | + }else { |
|
| 49 | 49 | parent::__construct(self::$DEFAULT_ANNOTATIONS, $cache); |
| 50 | 50 | } |
| 51 | 51 | |
@@ -45,7 +45,7 @@ |
||
| 45 | 45 | { |
| 46 | 46 | if($annotations){ |
| 47 | 47 | parent::__construct($annotations, $cache); |
| 48 | - }else{ |
|
| 48 | + } else{ |
|
| 49 | 49 | parent::__construct(self::$DEFAULT_ANNOTATIONS, $cache); |
| 50 | 50 | } |
| 51 | 51 | |
@@ -91,7 +91,7 @@ |
||
| 91 | 91 | |
| 92 | 92 | /** |
| 93 | 93 | * 获取指定参数信息 |
| 94 | - * @param $name |
|
| 94 | + * @param string|null $name |
|
| 95 | 95 | * @return ParamMeta|null |
| 96 | 96 | */ |
| 97 | 97 | public function getParamMeta($name){ |
@@ -15,7 +15,7 @@ discard block |
||
| 15 | 15 | /** |
| 16 | 16 | * @param ParamMeta[] $paramMates |
| 17 | 17 | */ |
| 18 | - public function __construct(array $paramMates=[]){ |
|
| 18 | + public function __construct(array $paramMates = []) { |
|
| 19 | 19 | $this->paramMetas = $paramMates; |
| 20 | 20 | } |
| 21 | 21 | |
@@ -26,32 +26,32 @@ discard block |
||
| 26 | 26 | * @param array $reference |
| 27 | 27 | * @return void |
| 28 | 28 | */ |
| 29 | - public function handle(Application $app, Request $request, array &$params, array &$reference){ |
|
| 29 | + public function handle(Application $app, Request $request, array &$params, array &$reference) { |
|
| 30 | 30 | |
| 31 | 31 | $vld = new Validator(); |
| 32 | 32 | $req = ['request'=>$request]; |
| 33 | 33 | $requestArray = new ArrayAdaptor($req); |
| 34 | 34 | $inputs = []; |
| 35 | - foreach ($this->paramMetas as $k=>$meta){ |
|
| 36 | - if($meta->isPassedByReference){ |
|
| 35 | + foreach ($this->paramMetas as $k=>$meta) { |
|
| 36 | + if ($meta->isPassedByReference) { |
|
| 37 | 37 | // param PassedByReference is used to output |
| 38 | 38 | continue; |
| 39 | 39 | } |
| 40 | 40 | $source = \JmesPath\search($meta->source, $requestArray); |
| 41 | - if ($source !== null){ |
|
| 41 | + if ($source !== null) { |
|
| 42 | 42 | $source = ArrayAdaptor::strip($source); |
| 43 | - if($source instanceof ParameterBag){ |
|
| 43 | + if ($source instanceof ParameterBag) { |
|
| 44 | 44 | $source = $source->all(); |
| 45 | 45 | } |
| 46 | - if($meta->container){ |
|
| 46 | + if ($meta->container) { |
|
| 47 | 47 | $inputs[$meta->name] = $meta->container->make($source); |
| 48 | - }else{ |
|
| 48 | + }else { |
|
| 49 | 49 | $inputs[$meta->name] = $source; |
| 50 | 50 | } |
| 51 | - if($meta->validation){ |
|
| 51 | + if ($meta->validation) { |
|
| 52 | 52 | $vld->rule($meta->validation, $meta->name); |
| 53 | 53 | } |
| 54 | - }else{ |
|
| 54 | + }else { |
|
| 55 | 55 | $meta->isOptional or \PhpBoot\abort(new BadRequestHttpException("param $source is required")); |
| 56 | 56 | $inputs[$meta->name] = $meta->default; |
| 57 | 57 | } |
@@ -66,10 +66,10 @@ discard block |
||
| 66 | 66 | ); |
| 67 | 67 | |
| 68 | 68 | $pos = 0; |
| 69 | - foreach ($this->paramMetas as $meta){ |
|
| 70 | - if($meta->isPassedByReference){ |
|
| 69 | + foreach ($this->paramMetas as $meta) { |
|
| 70 | + if ($meta->isPassedByReference) { |
|
| 71 | 71 | $params[$pos] = &$reference[$meta->name]; |
| 72 | - }else{ |
|
| 72 | + }else { |
|
| 73 | 73 | $params[$pos] = $inputs[$meta->name]; |
| 74 | 74 | } |
| 75 | 75 | $pos++; |
@@ -77,15 +77,15 @@ discard block |
||
| 77 | 77 | } |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - public function getParamNames(){ |
|
| 81 | - return array_map(function($meta){return $meta->name;}, $this->paramMetas); |
|
| 80 | + public function getParamNames() { |
|
| 81 | + return array_map(function($meta) {return $meta->name; }, $this->paramMetas); |
|
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | /** |
| 85 | 85 | * 获取参数列表 |
| 86 | 86 | * @return ParamMeta[] |
| 87 | 87 | */ |
| 88 | - public function getParamMetas(){ |
|
| 88 | + public function getParamMetas() { |
|
| 89 | 89 | return $this->paramMetas; |
| 90 | 90 | } |
| 91 | 91 | |
@@ -94,9 +94,9 @@ discard block |
||
| 94 | 94 | * @param $name |
| 95 | 95 | * @return ParamMeta|null |
| 96 | 96 | */ |
| 97 | - public function getParamMeta($name){ |
|
| 98 | - foreach ($this->paramMetas as $meta){ |
|
| 99 | - if($meta->name == $name){ |
|
| 97 | + public function getParamMeta($name) { |
|
| 98 | + foreach ($this->paramMetas as $meta) { |
|
| 99 | + if ($meta->name == $name) { |
|
| 100 | 100 | return $meta; |
| 101 | 101 | } |
| 102 | 102 | } |
@@ -45,13 +45,13 @@ discard block |
||
| 45 | 45 | } |
| 46 | 46 | if($meta->container){ |
| 47 | 47 | $inputs[$meta->name] = $meta->container->make($source); |
| 48 | - }else{ |
|
| 48 | + } else{ |
|
| 49 | 49 | $inputs[$meta->name] = $source; |
| 50 | 50 | } |
| 51 | 51 | if($meta->validation){ |
| 52 | 52 | $vld->rule($meta->validation, $meta->name); |
| 53 | 53 | } |
| 54 | - }else{ |
|
| 54 | + } else{ |
|
| 55 | 55 | $meta->isOptional or \PhpBoot\abort(new BadRequestHttpException("param $source is required")); |
| 56 | 56 | $inputs[$meta->name] = $meta->default; |
| 57 | 57 | } |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | foreach ($this->paramMetas as $meta){ |
| 70 | 70 | if($meta->isPassedByReference){ |
| 71 | 71 | $params[$pos] = &$reference[$meta->name]; |
| 72 | - }else{ |
|
| 72 | + } else{ |
|
| 73 | 73 | $params[$pos] = $inputs[$meta->name]; |
| 74 | 74 | } |
| 75 | 75 | $pos++; |
@@ -82,6 +82,9 @@ |
||
| 82 | 82 | return new DB($app, $connection); |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | + /** |
|
| 86 | + * @param \PDO $connection |
|
| 87 | + */ |
|
| 85 | 88 | public function __construct(Application $app, $connection) |
| 86 | 89 | { |
| 87 | 90 | $this->app = $app; |
@@ -67,10 +67,10 @@ discard block |
||
| 67 | 67 | */ |
| 68 | 68 | |
| 69 | 69 | static public function connect(Application $app, |
| 70 | - $dsn, |
|
| 71 | - $username, |
|
| 72 | - $password, |
|
| 73 | - $options = []) |
|
| 70 | + $dsn, |
|
| 71 | + $username, |
|
| 72 | + $password, |
|
| 73 | + $options = []) |
|
| 74 | 74 | { |
| 75 | 75 | $options += [ |
| 76 | 76 | \PDO::ATTR_ERRMODE =>\PDO::ERRMODE_EXCEPTION, |
@@ -165,7 +165,7 @@ discard block |
||
| 165 | 165 | return $this->connection; |
| 166 | 166 | } |
| 167 | 167 | /** |
| 168 | - * Splice sql use raw string(without escaping) |
|
| 168 | + * Splice sql use raw string(without escaping) |
|
| 169 | 169 | * for example: |
| 170 | 170 | * where('time>?', 'now()') => " WHERE time > 'now()' " |
| 171 | 171 | * where('time>?', Sql::raw('now()')) => " WHERE time > now() " |
@@ -55,7 +55,7 @@ discard block |
||
| 55 | 55 | * |
| 56 | 56 | * @author caoym <[email protected]> |
| 57 | 57 | */ |
| 58 | -class DB{ |
|
| 58 | +class DB { |
|
| 59 | 59 | |
| 60 | 60 | /** |
| 61 | 61 | * DB constructor. |
@@ -96,23 +96,23 @@ discard block |
||
| 96 | 96 | * @param string $column0 |
| 97 | 97 | * @return \PhpBoot\DB\rules\select\FromRule |
| 98 | 98 | */ |
| 99 | - function select($column0=null, $_=null){ |
|
| 99 | + function select($column0 = null, $_ = null) { |
|
| 100 | 100 | $obj = new SelectRule(new Context($this->connection)); |
| 101 | - if($column0 == null){ |
|
| 101 | + if ($column0 == null) { |
|
| 102 | 102 | $args = ['*']; |
| 103 | - }elseif(is_array($column0)){ |
|
| 103 | + }elseif (is_array($column0)) { |
|
| 104 | 104 | $args = $column0; |
| 105 | - }else{ |
|
| 105 | + }else { |
|
| 106 | 106 | $args = func_get_args(); |
| 107 | 107 | } |
| 108 | - foreach ($args as &$arg){ |
|
| 108 | + foreach ($args as &$arg) { |
|
| 109 | 109 | $arg = trim($arg); |
| 110 | - if($arg == '*'){ |
|
| 110 | + if ($arg == '*') { |
|
| 111 | 111 | continue; |
| 112 | 112 | } |
| 113 | 113 | $found = []; |
| 114 | - if(preg_match('/^[A-Za-z0-9]+$/', $arg, $found, PREG_OFFSET_CAPTURE)&& |
|
| 115 | - count($found)){ |
|
| 114 | + if (preg_match('/^[A-Za-z0-9]+$/', $arg, $found, PREG_OFFSET_CAPTURE) && |
|
| 115 | + count($found)) { |
|
| 116 | 116 | $arg = "`$arg`"; |
| 117 | 117 | } |
| 118 | 118 | } |
@@ -143,8 +143,8 @@ discard block |
||
| 143 | 143 | * @param string $table |
| 144 | 144 | * @return \PhpBoot\DB\rules\basic\WhereRule |
| 145 | 145 | */ |
| 146 | - public function deleteFrom($table){ |
|
| 147 | - $obj = new DeleteRule(new Context($this->connection)); |
|
| 146 | + public function deleteFrom($table) { |
|
| 147 | + $obj = new DeleteRule(new Context($this->connection)); |
|
| 148 | 148 | return $obj->deleteFrom($table); |
| 149 | 149 | } |
| 150 | 150 | /** |
@@ -152,8 +152,8 @@ discard block |
||
| 152 | 152 | * @param string $table |
| 153 | 153 | * @return \PhpBoot\DB\rules\replace\ValuesRule |
| 154 | 154 | */ |
| 155 | - public function replaceInto($table){ |
|
| 156 | - $obj = new ReplaceIntoRule(new Context($this->connection)); |
|
| 155 | + public function replaceInto($table) { |
|
| 156 | + $obj = new ReplaceIntoRule(new Context($this->connection)); |
|
| 157 | 157 | return $obj->replaceInto($table); |
| 158 | 158 | } |
| 159 | 159 | |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | * @param string $str |
| 173 | 173 | * @return Raw |
| 174 | 174 | */ |
| 175 | - static public function raw($str){ |
|
| 175 | + static public function raw($str) { |
|
| 176 | 176 | return new Raw($str); |
| 177 | 177 | } |
| 178 | 178 | |
@@ -183,8 +183,8 @@ discard block |
||
| 183 | 183 | { |
| 184 | 184 | return $this->app; |
| 185 | 185 | } |
| 186 | - const ORDER_BY_ASC ='ASC'; |
|
| 187 | - const ORDER_BY_DESC ='DESC'; |
|
| 186 | + const ORDER_BY_ASC = 'ASC'; |
|
| 187 | + const ORDER_BY_DESC = 'DESC'; |
|
| 188 | 188 | |
| 189 | 189 | /** |
| 190 | 190 | * @var \PDO |
@@ -100,9 +100,9 @@ |
||
| 100 | 100 | $obj = new SelectRule(new Context($this->connection)); |
| 101 | 101 | if($column0 == null){ |
| 102 | 102 | $args = ['*']; |
| 103 | - }elseif(is_array($column0)){ |
|
| 103 | + } elseif(is_array($column0)){ |
|
| 104 | 104 | $args = $column0; |
| 105 | - }else{ |
|
| 105 | + } else{ |
|
| 106 | 106 | $args = func_get_args(); |
| 107 | 107 | } |
| 108 | 108 | foreach ($args as &$arg){ |
@@ -7,6 +7,11 @@ discard block |
||
| 7 | 7 | use PhpBoot\DB\Context; |
| 8 | 8 | |
| 9 | 9 | class ExecResult{ |
| 10 | + |
|
| 11 | + /** |
|
| 12 | + * @param \PDO $pdo |
|
| 13 | + * @param \PDOStatement $st |
|
| 14 | + */ |
|
| 10 | 15 | public function __construct($success, $pdo, $st){ |
| 11 | 16 | $this->pdo = $pdo; |
| 12 | 17 | $this->st = $st; |
@@ -40,6 +45,10 @@ discard block |
||
| 40 | 45 | |
| 41 | 46 | class SelectImpl |
| 42 | 47 | { |
| 48 | + /** |
|
| 49 | + * @param Context $context |
|
| 50 | + * @param string $columns |
|
| 51 | + */ |
|
| 43 | 52 | static public function select($context, $columns){ |
| 44 | 53 | $context->appendSql("SELECT $columns"); |
| 45 | 54 | } |
@@ -47,6 +56,10 @@ discard block |
||
| 47 | 56 | |
| 48 | 57 | class FromImpl |
| 49 | 58 | { |
| 59 | + /** |
|
| 60 | + * @param Context $context |
|
| 61 | + * @param string $tables |
|
| 62 | + */ |
|
| 50 | 63 | static public function from($context, $tables,$as=null){ |
| 51 | 64 | if($tables instanceof BasicRule){ |
| 52 | 65 | $context->appendSql("FROM (".$tables->context->sql.')'); |
@@ -62,6 +75,10 @@ discard block |
||
| 62 | 75 | |
| 63 | 76 | class DeleteImpl |
| 64 | 77 | { |
| 78 | + /** |
|
| 79 | + * @param Context $context |
|
| 80 | + * @param string $from |
|
| 81 | + */ |
|
| 65 | 82 | static public function deleteFrom($context, $from) |
| 66 | 83 | { |
| 67 | 84 | $context->appendSql("DELETE FROM `$from`"); |
@@ -70,6 +87,11 @@ discard block |
||
| 70 | 87 | |
| 71 | 88 | class JoinImpl |
| 72 | 89 | { |
| 90 | + /** |
|
| 91 | + * @param Context $context |
|
| 92 | + * @param null|string $type |
|
| 93 | + * @param string $table |
|
| 94 | + */ |
|
| 73 | 95 | static public function join($context, $type, $table) { |
| 74 | 96 | if($type){ |
| 75 | 97 | $context->appendSql("$type JOIN $table"); |
@@ -81,6 +103,10 @@ discard block |
||
| 81 | 103 | |
| 82 | 104 | class JoinOnImpl |
| 83 | 105 | { |
| 106 | + /** |
|
| 107 | + * @param Context $context |
|
| 108 | + * @param string $condition |
|
| 109 | + */ |
|
| 84 | 110 | static public function on($context, $condition) { |
| 85 | 111 | $context->appendSql("ON $condition"); |
| 86 | 112 | } |
@@ -88,6 +114,9 @@ discard block |
||
| 88 | 114 | |
| 89 | 115 | class ForUpdateImpl |
| 90 | 116 | { |
| 117 | + /** |
|
| 118 | + * @param Context $context |
|
| 119 | + */ |
|
| 91 | 120 | static public function forUpdate($context){ |
| 92 | 121 | $context->appendSql("FOR UPDATE"); |
| 93 | 122 | } |
@@ -95,6 +124,10 @@ discard block |
||
| 95 | 124 | |
| 96 | 125 | class ForUpdateOfImpl |
| 97 | 126 | { |
| 127 | + /** |
|
| 128 | + * @param Context $context |
|
| 129 | + * @param string $column |
|
| 130 | + */ |
|
| 98 | 131 | static public function of($context, $column){ |
| 99 | 132 | $context->appendSql("OF $column"); |
| 100 | 133 | } |
@@ -102,18 +135,29 @@ discard block |
||
| 102 | 135 | |
| 103 | 136 | class InsertImpl |
| 104 | 137 | { |
| 138 | + /** |
|
| 139 | + * @param Context $context |
|
| 140 | + * @param string $table |
|
| 141 | + */ |
|
| 105 | 142 | static public function insertInto($context, $table) { |
| 106 | 143 | $context->appendSql("INSERT INTO `$table`"); |
| 107 | 144 | } |
| 108 | 145 | } |
| 109 | 146 | class ReplaceImpl |
| 110 | 147 | { |
| 148 | + /** |
|
| 149 | + * @param Context $context |
|
| 150 | + * @param string $table |
|
| 151 | + */ |
|
| 111 | 152 | static public function replaceInto($context, $table) { |
| 112 | 153 | $context->appendSql("REPLACE INTO `$table`"); |
| 113 | 154 | } |
| 114 | 155 | } |
| 115 | 156 | class ValuesImpl |
| 116 | 157 | { |
| 158 | + /** |
|
| 159 | + * @param Context $context |
|
| 160 | + */ |
|
| 117 | 161 | static public function values($context, $values){ |
| 118 | 162 | $params = []; |
| 119 | 163 | $stubs = []; |
@@ -143,6 +187,10 @@ discard block |
||
| 143 | 187 | |
| 144 | 188 | class UpdateImpl |
| 145 | 189 | { |
| 190 | + /** |
|
| 191 | + * @param Context $context |
|
| 192 | + * @param string $table |
|
| 193 | + */ |
|
| 146 | 194 | static public function update($context, $table){ |
| 147 | 195 | $context->appendSql("UPDATE `$table`"); |
| 148 | 196 | } |
@@ -150,6 +198,10 @@ discard block |
||
| 150 | 198 | |
| 151 | 199 | class UpdateSetImpl |
| 152 | 200 | { |
| 201 | + /** |
|
| 202 | + * @param Context $context |
|
| 203 | + * @param string $column |
|
| 204 | + */ |
|
| 153 | 205 | public function set($context, $column, $value){ |
| 154 | 206 | $prefix = ''; |
| 155 | 207 | if($this->first){ |
@@ -166,6 +218,10 @@ discard block |
||
| 166 | 218 | } |
| 167 | 219 | } |
| 168 | 220 | |
| 221 | + /** |
|
| 222 | + * @param Context $context |
|
| 223 | + * @param string $expr |
|
| 224 | + */ |
|
| 169 | 225 | public function setExpr($context, $expr, $args){ |
| 170 | 226 | $prefix = ''; |
| 171 | 227 | if($this->first){ |
@@ -179,6 +235,10 @@ discard block |
||
| 179 | 235 | $context->appendParams($args); |
| 180 | 236 | |
| 181 | 237 | } |
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * @param Context $context |
|
| 241 | + */ |
|
| 182 | 242 | public function setArgs($context, $values){ |
| 183 | 243 | $set = []; |
| 184 | 244 | $params = []; |
@@ -203,6 +263,9 @@ discard block |
||
| 203 | 263 | } |
| 204 | 264 | class OrderByImpl |
| 205 | 265 | { |
| 266 | + /** |
|
| 267 | + * @param Context $context |
|
| 268 | + */ |
|
| 206 | 269 | public function orderByArgs($context, $orders){ |
| 207 | 270 | if(empty($orders)){ |
| 208 | 271 | return $this; |
@@ -230,6 +293,12 @@ discard block |
||
| 230 | 293 | } |
| 231 | 294 | return $this; |
| 232 | 295 | } |
| 296 | + |
|
| 297 | + /** |
|
| 298 | + * @param Context $context |
|
| 299 | + * @param string $column |
|
| 300 | + * @param string $order |
|
| 301 | + */ |
|
| 233 | 302 | public function orderBy($context, $column, $order=null){ |
| 234 | 303 | if($this->first){ |
| 235 | 304 | $this->first = false; |
@@ -247,12 +316,22 @@ discard block |
||
| 247 | 316 | |
| 248 | 317 | class LimitImpl |
| 249 | 318 | { |
| 319 | + /** |
|
| 320 | + * @param Context $context |
|
| 321 | + * @param integer $size |
|
| 322 | + */ |
|
| 250 | 323 | static public function limit($context, $size){ |
| 251 | 324 | $intSize = intval($size); |
| 252 | 325 | strval($intSize) == $size or \PhpBoot\abort( |
| 253 | 326 | new \InvalidArgumentException("invalid params for limit($size)")); |
| 254 | 327 | $context->appendSql("LIMIT $size"); |
| 255 | 328 | } |
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * @param Context $context |
|
| 332 | + * @param integer $start |
|
| 333 | + * @param integer $size |
|
| 334 | + */ |
|
| 256 | 335 | static public function limitWithOffset($context,$start, $size){ |
| 257 | 336 | $intStart = intval($start); |
| 258 | 337 | $intSize = intval($size); |
@@ -264,6 +343,9 @@ discard block |
||
| 264 | 343 | |
| 265 | 344 | class WhereImpl{ |
| 266 | 345 | |
| 346 | + /** |
|
| 347 | + * @param string $str |
|
| 348 | + */ |
|
| 267 | 349 | static private function findQ($str,$offset = 0,$no=0){ |
| 268 | 350 | $found = strpos($str, '?', $offset); |
| 269 | 351 | if($no == 0 || $found === false){ |
@@ -271,16 +353,33 @@ discard block |
||
| 271 | 353 | } |
| 272 | 354 | return self::findQ($str, $found+1, $no-1); |
| 273 | 355 | } |
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * @param Context $context |
|
| 359 | + * @param string $expr |
|
| 360 | + */ |
|
| 274 | 361 | static public function having($context, $expr, $args){ |
| 275 | 362 | self::condition($context, 'HAVING', $expr, $args); |
| 276 | 363 | } |
| 364 | + |
|
| 365 | + /** |
|
| 366 | + * @param Context $context |
|
| 367 | + * @param string $expr |
|
| 368 | + */ |
|
| 277 | 369 | static public function where($context, $expr, $args){ |
| 278 | 370 | self::condition($context, 'WHERE', $expr, $args); |
| 279 | 371 | } |
| 280 | 372 | |
| 373 | + /** |
|
| 374 | + * @param Context $context |
|
| 375 | + */ |
|
| 281 | 376 | static public function havingArgs($context, $args){ |
| 282 | 377 | self::conditionArgs($context, 'HAVING', $args); |
| 283 | 378 | } |
| 379 | + |
|
| 380 | + /** |
|
| 381 | + * @param Context $context |
|
| 382 | + */ |
|
| 284 | 383 | static public function whereArgs($context, $args){ |
| 285 | 384 | self::conditionArgs($context, 'WHERE', $args); |
| 286 | 385 | } |
@@ -305,6 +404,7 @@ discard block |
||
| 305 | 404 | * NOT IN 'id'=>['NOT IN' => [1,2,3]] |
| 306 | 405 | * |
| 307 | 406 | * @param array $args |
| 407 | + * @param string $prefix |
|
| 308 | 408 | */ |
| 309 | 409 | static public function conditionArgs($context, $prefix, $args=[]){ |
| 310 | 410 | if($args ===null){ |
@@ -432,6 +532,11 @@ discard block |
||
| 432 | 532 | } |
| 433 | 533 | |
| 434 | 534 | class GroupByImpl{ |
| 535 | + |
|
| 536 | + /** |
|
| 537 | + * @param Context $context |
|
| 538 | + * @param string $column |
|
| 539 | + */ |
|
| 435 | 540 | static public function groupBy($context, $column){ |
| 436 | 541 | $context->appendSql("GROUP BY $column"); |
| 437 | 542 | } |
@@ -453,7 +558,6 @@ discard block |
||
| 453 | 558 | /** |
| 454 | 559 | * |
| 455 | 560 | * @param Context $context |
| 456 | - * @param string|false $asDict return as dict or array |
|
| 457 | 561 | * @return false|array |
| 458 | 562 | */ |
| 459 | 563 | static public function get($context, $dictAs=false){ |
@@ -512,6 +616,10 @@ discard block |
||
| 512 | 616 | } |
| 513 | 617 | class OnDuplicateKeyUpdateImpl |
| 514 | 618 | { |
| 619 | + /** |
|
| 620 | + * @param Context $context |
|
| 621 | + * @param string $column |
|
| 622 | + */ |
|
| 515 | 623 | public function set($context, $column, $value){ |
| 516 | 624 | $prefix = ''; |
| 517 | 625 | if($this->first){ |
@@ -528,6 +636,9 @@ discard block |
||
| 528 | 636 | } |
| 529 | 637 | } |
| 530 | 638 | |
| 639 | + /** |
|
| 640 | + * @param Context $context |
|
| 641 | + */ |
|
| 531 | 642 | public function setExpr($context, $expr, $args){ |
| 532 | 643 | $prefix = ''; |
| 533 | 644 | if($this->first){ |
@@ -541,6 +652,10 @@ discard block |
||
| 541 | 652 | $context->appendParams($args); |
| 542 | 653 | |
| 543 | 654 | } |
| 655 | + |
|
| 656 | + /** |
|
| 657 | + * @param Context $context |
|
| 658 | + */ |
|
| 544 | 659 | public function setArgs($context, $values){ |
| 545 | 660 | $set = []; |
| 546 | 661 | $params = []; |
@@ -6,14 +6,14 @@ discard block |
||
| 6 | 6 | use PhpBoot\DB\rules\basic\BasicRule; |
| 7 | 7 | use PhpBoot\DB\Context; |
| 8 | 8 | |
| 9 | -class ExecResult{ |
|
| 10 | - public function __construct($success, $pdo, $st){ |
|
| 9 | +class ExecResult { |
|
| 10 | + public function __construct($success, $pdo, $st) { |
|
| 11 | 11 | $this->pdo = $pdo; |
| 12 | 12 | $this->st = $st; |
| 13 | 13 | $this->success = $success; |
| 14 | 14 | $this->rows = $this->st->rowCount(); |
| 15 | 15 | } |
| 16 | - public function lastInsertId($name=null){ |
|
| 16 | + public function lastInsertId($name = null) { |
|
| 17 | 17 | return $this->pdo->lastInsertId($name); |
| 18 | 18 | } |
| 19 | 19 | /** |
@@ -40,21 +40,21 @@ discard block |
||
| 40 | 40 | |
| 41 | 41 | class SelectImpl |
| 42 | 42 | { |
| 43 | - static public function select($context, $columns){ |
|
| 43 | + static public function select($context, $columns) { |
|
| 44 | 44 | $context->appendSql("SELECT $columns"); |
| 45 | 45 | } |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | class FromImpl |
| 49 | 49 | { |
| 50 | - static public function from($context, $tables,$as=null){ |
|
| 51 | - if($tables instanceof BasicRule){ |
|
| 50 | + static public function from($context, $tables, $as = null) { |
|
| 51 | + if ($tables instanceof BasicRule) { |
|
| 52 | 52 | $context->appendSql("FROM (".$tables->context->sql.')'); |
| 53 | - $context->params = array_merge($context->params,$tables->context->params); |
|
| 53 | + $context->params = array_merge($context->params, $tables->context->params); |
|
| 54 | 54 | }else { |
| 55 | 55 | $context->appendSql("FROM `$tables`"); |
| 56 | 56 | } |
| 57 | - if($as){ |
|
| 57 | + if ($as) { |
|
| 58 | 58 | $context->appendSql("as `$as`"); |
| 59 | 59 | } |
| 60 | 60 | } |
@@ -71,9 +71,9 @@ discard block |
||
| 71 | 71 | class JoinImpl |
| 72 | 72 | { |
| 73 | 73 | static public function join($context, $type, $table) { |
| 74 | - if($type){ |
|
| 74 | + if ($type) { |
|
| 75 | 75 | $context->appendSql("$type JOIN $table"); |
| 76 | - }else{ |
|
| 76 | + }else { |
|
| 77 | 77 | $context->appendSql("JOIN $table"); |
| 78 | 78 | } |
| 79 | 79 | } |
@@ -88,14 +88,14 @@ discard block |
||
| 88 | 88 | |
| 89 | 89 | class ForUpdateImpl |
| 90 | 90 | { |
| 91 | - static public function forUpdate($context){ |
|
| 91 | + static public function forUpdate($context) { |
|
| 92 | 92 | $context->appendSql("FOR UPDATE"); |
| 93 | 93 | } |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | class ForUpdateOfImpl |
| 97 | 97 | { |
| 98 | - static public function of($context, $column){ |
|
| 98 | + static public function of($context, $column) { |
|
| 99 | 99 | $context->appendSql("OF $column"); |
| 100 | 100 | } |
| 101 | 101 | } |
@@ -114,27 +114,27 @@ discard block |
||
| 114 | 114 | } |
| 115 | 115 | class ValuesImpl |
| 116 | 116 | { |
| 117 | - static public function values($context, $values){ |
|
| 117 | + static public function values($context, $values) { |
|
| 118 | 118 | $params = []; |
| 119 | 119 | $stubs = []; |
| 120 | - foreach ($values as $v){ |
|
| 121 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
| 122 | - $stubs[]=$v->get(); |
|
| 123 | - }else{ |
|
| 124 | - $stubs[]='?'; |
|
| 120 | + foreach ($values as $v) { |
|
| 121 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
| 122 | + $stubs[] = $v->get(); |
|
| 123 | + }else { |
|
| 124 | + $stubs[] = '?'; |
|
| 125 | 125 | $params[] = $v; |
| 126 | 126 | } |
| 127 | 127 | } |
| 128 | 128 | $stubs = implode(',', $stubs); |
| 129 | 129 | |
| 130 | - if(array_keys($values) === range(0, count($values) - 1)){ |
|
| 130 | + if (array_keys($values) === range(0, count($values) - 1)) { |
|
| 131 | 131 | //VALUES(val0, val1, val2) |
| 132 | 132 | $context->appendSql("VALUES($stubs)"); |
| 133 | 133 | |
| 134 | - }else{ |
|
| 134 | + }else { |
|
| 135 | 135 | //(col0, col1, col2) VALUES(val0, val1, val2) |
| 136 | 136 | $columns = implode(',', array_keys($values)); |
| 137 | - $context->appendSql("($columns) VALUES($stubs)",false); |
|
| 137 | + $context->appendSql("($columns) VALUES($stubs)", false); |
|
| 138 | 138 | } |
| 139 | 139 | $context->appendParams($params); |
| 140 | 140 | } |
@@ -143,117 +143,117 @@ discard block |
||
| 143 | 143 | |
| 144 | 144 | class UpdateImpl |
| 145 | 145 | { |
| 146 | - static public function update($context, $table){ |
|
| 146 | + static public function update($context, $table) { |
|
| 147 | 147 | $context->appendSql("UPDATE `$table`"); |
| 148 | 148 | } |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | class UpdateSetImpl |
| 152 | 152 | { |
| 153 | - public function set($context, $column, $value){ |
|
| 153 | + public function set($context, $column, $value) { |
|
| 154 | 154 | $prefix = ''; |
| 155 | - if($this->first){ |
|
| 155 | + if ($this->first) { |
|
| 156 | 156 | $this->first = false; |
| 157 | 157 | $prefix = 'SET '; |
| 158 | - }else{ |
|
| 158 | + }else { |
|
| 159 | 159 | $prefix = ','; |
| 160 | 160 | } |
| 161 | - if(is_a($value, Raw::class)){ |
|
| 162 | - $context->appendSql("$prefix$column=$value",$prefix == 'SET '); |
|
| 163 | - }else{ |
|
| 164 | - $context->appendSql("$prefix$column=?",$prefix == 'SET '); |
|
| 161 | + if (is_a($value, Raw::class)) { |
|
| 162 | + $context->appendSql("$prefix$column=$value", $prefix == 'SET '); |
|
| 163 | + }else { |
|
| 164 | + $context->appendSql("$prefix$column=?", $prefix == 'SET '); |
|
| 165 | 165 | $context->appendParams([$value]); |
| 166 | 166 | } |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | - public function setExpr($context, $expr, $args){ |
|
| 169 | + public function setExpr($context, $expr, $args) { |
|
| 170 | 170 | $prefix = ''; |
| 171 | - if($this->first){ |
|
| 171 | + if ($this->first) { |
|
| 172 | 172 | $this->first = false; |
| 173 | 173 | $prefix = 'SET '; |
| 174 | - }else{ |
|
| 174 | + }else { |
|
| 175 | 175 | $prefix = ','; |
| 176 | 176 | } |
| 177 | 177 | |
| 178 | - $context->appendSql("$prefix$expr",$prefix == 'SET '); |
|
| 178 | + $context->appendSql("$prefix$expr", $prefix == 'SET '); |
|
| 179 | 179 | $context->appendParams($args); |
| 180 | 180 | |
| 181 | 181 | } |
| 182 | - public function setArgs($context, $values){ |
|
| 182 | + public function setArgs($context, $values) { |
|
| 183 | 183 | $set = []; |
| 184 | 184 | $params = []; |
| 185 | - foreach ($values as $k=>$v){ |
|
| 186 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
| 187 | - $set[]= "$k=".$v->get(); |
|
| 188 | - }else{ |
|
| 189 | - $set[]= "$k=?"; |
|
| 190 | - $params[]=$v; |
|
| 185 | + foreach ($values as $k=>$v) { |
|
| 186 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
| 187 | + $set[] = "$k=".$v->get(); |
|
| 188 | + }else { |
|
| 189 | + $set[] = "$k=?"; |
|
| 190 | + $params[] = $v; |
|
| 191 | 191 | } |
| 192 | 192 | } |
| 193 | - if($this->first){ |
|
| 193 | + if ($this->first) { |
|
| 194 | 194 | $this->first = false; |
| 195 | 195 | $context->appendSql('SET '.implode(',', $set)); |
| 196 | 196 | $context->appendParams($params); |
| 197 | - }else{ |
|
| 198 | - $context->appendSql(','.implode(',', $set),false); |
|
| 197 | + }else { |
|
| 198 | + $context->appendSql(','.implode(',', $set), false); |
|
| 199 | 199 | $context->appendParams($params); |
| 200 | 200 | } |
| 201 | 201 | } |
| 202 | - private $first=true; |
|
| 202 | + private $first = true; |
|
| 203 | 203 | } |
| 204 | 204 | class OrderByImpl |
| 205 | 205 | { |
| 206 | - public function orderByArgs($context, $orders){ |
|
| 207 | - if(empty($orders)){ |
|
| 206 | + public function orderByArgs($context, $orders) { |
|
| 207 | + if (empty($orders)) { |
|
| 208 | 208 | return $this; |
| 209 | 209 | } |
| 210 | 210 | $params = array(); |
| 211 | - foreach ($orders as $k=>$v){ |
|
| 212 | - if(is_integer($k)){ |
|
| 211 | + foreach ($orders as $k=>$v) { |
|
| 212 | + if (is_integer($k)) { |
|
| 213 | 213 | preg_match('/^[a-zA-Z0-9_.]+$/', $v) or \PhpBoot\abort( |
| 214 | 214 | new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
| 215 | 215 | |
| 216 | 216 | $params[] = $v; |
| 217 | - }else{ |
|
| 217 | + }else { |
|
| 218 | 218 | $v = strtoupper($v); |
| 219 | 219 | preg_match('/^[a-zA-Z0-9_.]+$/', $k) && |
| 220 | - ($v =='DESC' || $v =='ASC') or \PhpBoot\abort( new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
|
| 220 | + ($v == 'DESC' || $v == 'ASC') or \PhpBoot\abort(new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
|
| 221 | 221 | |
| 222 | 222 | $params[] = "$k $v"; |
| 223 | 223 | } |
| 224 | 224 | } |
| 225 | - if($this->first){ |
|
| 225 | + if ($this->first) { |
|
| 226 | 226 | $this->first = false; |
| 227 | 227 | $context->appendSql('ORDER BY '.implode(',', $params)); |
| 228 | - }else{ |
|
| 229 | - $context->appendSql(','.implode(',', $params),false); |
|
| 228 | + }else { |
|
| 229 | + $context->appendSql(','.implode(',', $params), false); |
|
| 230 | 230 | } |
| 231 | 231 | return $this; |
| 232 | 232 | } |
| 233 | - public function orderBy($context, $column, $order=null){ |
|
| 234 | - if($this->first){ |
|
| 233 | + public function orderBy($context, $column, $order = null) { |
|
| 234 | + if ($this->first) { |
|
| 235 | 235 | $this->first = false; |
| 236 | 236 | $context->appendSql("ORDER BY $column"); |
| 237 | - }else{ |
|
| 237 | + }else { |
|
| 238 | 238 | $context->appendSql(",$column", false); |
| 239 | 239 | } |
| 240 | - if($order){ |
|
| 240 | + if ($order) { |
|
| 241 | 241 | $context->appendSql($order); |
| 242 | 242 | } |
| 243 | 243 | return $this; |
| 244 | 244 | } |
| 245 | - private $first=true; |
|
| 245 | + private $first = true; |
|
| 246 | 246 | } |
| 247 | 247 | |
| 248 | 248 | class LimitImpl |
| 249 | 249 | { |
| 250 | - static public function limit($context, $size){ |
|
| 250 | + static public function limit($context, $size) { |
|
| 251 | 251 | $intSize = intval($size); |
| 252 | 252 | strval($intSize) == $size or \PhpBoot\abort( |
| 253 | 253 | new \InvalidArgumentException("invalid params for limit($size)")); |
| 254 | 254 | $context->appendSql("LIMIT $size"); |
| 255 | 255 | } |
| 256 | - static public function limitWithOffset($context,$start, $size){ |
|
| 256 | + static public function limitWithOffset($context, $start, $size) { |
|
| 257 | 257 | $intStart = intval($start); |
| 258 | 258 | $intSize = intval($size); |
| 259 | 259 | strval($intStart) == $start && strval($intSize) == $size or \PhpBoot\abort( |
@@ -262,26 +262,26 @@ discard block |
||
| 262 | 262 | } |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | -class WhereImpl{ |
|
| 265 | +class WhereImpl { |
|
| 266 | 266 | |
| 267 | - static private function findQ($str,$offset = 0,$no=0){ |
|
| 267 | + static private function findQ($str, $offset = 0, $no = 0) { |
|
| 268 | 268 | $found = strpos($str, '?', $offset); |
| 269 | - if($no == 0 || $found === false){ |
|
| 269 | + if ($no == 0 || $found === false) { |
|
| 270 | 270 | return $found; |
| 271 | 271 | } |
| 272 | - return self::findQ($str, $found+1, $no-1); |
|
| 272 | + return self::findQ($str, $found + 1, $no - 1); |
|
| 273 | 273 | } |
| 274 | - static public function having($context, $expr, $args){ |
|
| 274 | + static public function having($context, $expr, $args) { |
|
| 275 | 275 | self::condition($context, 'HAVING', $expr, $args); |
| 276 | 276 | } |
| 277 | - static public function where($context, $expr, $args){ |
|
| 277 | + static public function where($context, $expr, $args) { |
|
| 278 | 278 | self::condition($context, 'WHERE', $expr, $args); |
| 279 | 279 | } |
| 280 | 280 | |
| 281 | - static public function havingArgs($context, $args){ |
|
| 281 | + static public function havingArgs($context, $args) { |
|
| 282 | 282 | self::conditionArgs($context, 'HAVING', $args); |
| 283 | 283 | } |
| 284 | - static public function whereArgs($context, $args){ |
|
| 284 | + static public function whereArgs($context, $args) { |
|
| 285 | 285 | self::conditionArgs($context, 'WHERE', $args); |
| 286 | 286 | } |
| 287 | 287 | /** |
@@ -306,14 +306,14 @@ discard block |
||
| 306 | 306 | * |
| 307 | 307 | * @param array $args |
| 308 | 308 | */ |
| 309 | - static public function conditionArgs($context, $prefix, $args=[]){ |
|
| 310 | - if($args ===null){ |
|
| 311 | - return ; |
|
| 309 | + static public function conditionArgs($context, $prefix, $args = []) { |
|
| 310 | + if ($args === null) { |
|
| 311 | + return; |
|
| 312 | 312 | } |
| 313 | 313 | $exprs = array(); |
| 314 | 314 | $params = array(); |
| 315 | - foreach ($args as $k => $v){ |
|
| 316 | - if(is_array($v)){ |
|
| 315 | + foreach ($args as $k => $v) { |
|
| 316 | + if (is_array($v)) { |
|
| 317 | 317 | $ops = ['=', '>', '<', '<>', '>=', '<=', 'IN', 'NOT IN', 'BETWEEN', 'LIKE']; |
| 318 | 318 | $op = array_keys($v)[0]; |
| 319 | 319 | $op = strtoupper($op); |
@@ -322,46 +322,46 @@ discard block |
||
| 322 | 322 | new \InvalidArgumentException("invalid param $op for whereArgs")); |
| 323 | 323 | |
| 324 | 324 | $var = array_values($v)[0]; |
| 325 | - if($op == 'IN' || $op == 'NOT IN'){ |
|
| 325 | + if ($op == 'IN' || $op == 'NOT IN') { |
|
| 326 | 326 | $stubs = []; |
| 327 | - foreach ($var as $i){ |
|
| 328 | - if(is_a($i, Raw::class)){ |
|
| 329 | - $stubs[]=strval($i); |
|
| 330 | - }else{ |
|
| 331 | - $stubs[]='?'; |
|
| 327 | + foreach ($var as $i) { |
|
| 328 | + if (is_a($i, Raw::class)) { |
|
| 329 | + $stubs[] = strval($i); |
|
| 330 | + }else { |
|
| 331 | + $stubs[] = '?'; |
|
| 332 | 332 | $params[] = $i; |
| 333 | 333 | } |
| 334 | 334 | } |
| 335 | 335 | $stubs = implode(',', $stubs); |
| 336 | 336 | $exprs[] = "$k $op ($stubs)"; |
| 337 | - }else if($op == 'BETWEEN'){ |
|
| 337 | + }else if ($op == 'BETWEEN') { |
|
| 338 | 338 | $cond = "$k BETWEEN"; |
| 339 | - if(is_a($var[0], Raw::class)){ |
|
| 339 | + if (is_a($var[0], Raw::class)) { |
|
| 340 | 340 | $cond = "$cond ".strval($var[0]); |
| 341 | - }else{ |
|
| 341 | + }else { |
|
| 342 | 342 | $cond = "$cond ?"; |
| 343 | 343 | $params[] = $var[0]; |
| 344 | 344 | } |
| 345 | - if(is_a($var[1], Raw::class)){ |
|
| 345 | + if (is_a($var[1], Raw::class)) { |
|
| 346 | 346 | $cond = "$cond AND ".strval($var[1]); |
| 347 | - }else{ |
|
| 347 | + }else { |
|
| 348 | 348 | $cond = "$cond AND ?"; |
| 349 | 349 | $params[] = $var[1]; |
| 350 | 350 | } |
| 351 | 351 | $exprs[] = $cond; |
| 352 | - }else{ |
|
| 353 | - if(is_a($var, Raw::class)){ |
|
| 352 | + }else { |
|
| 353 | + if (is_a($var, Raw::class)) { |
|
| 354 | 354 | $exprs[] = "$k $op ".strval($var); |
| 355 | - }else{ |
|
| 355 | + }else { |
|
| 356 | 356 | $exprs[] = "$k $op ?"; |
| 357 | 357 | $params[] = $var; |
| 358 | 358 | } |
| 359 | 359 | } |
| 360 | - }else{ |
|
| 361 | - if(is_a($v, Raw::class)){ |
|
| 360 | + }else { |
|
| 361 | + if (is_a($v, Raw::class)) { |
|
| 362 | 362 | $exprs[] = "$k = ".strval($v); |
| 363 | 363 | |
| 364 | - }else{ |
|
| 364 | + }else { |
|
| 365 | 365 | $exprs[] = "$k = ?"; |
| 366 | 366 | $params[] = $v; |
| 367 | 367 | } |
@@ -370,20 +370,20 @@ discard block |
||
| 370 | 370 | |
| 371 | 371 | return self::condition($context, $prefix, implode(' AND ', $exprs), $params); |
| 372 | 372 | } |
| 373 | - static public function condition($context, $prefix, $expr, $args){ |
|
| 374 | - if(!empty($expr)){ |
|
| 375 | - if($args){ |
|
| 373 | + static public function condition($context, $prefix, $expr, $args) { |
|
| 374 | + if (!empty($expr)) { |
|
| 375 | + if ($args) { |
|
| 376 | 376 | //因为PDO不支持绑定数组变量, 这里需要手动展开数组 |
| 377 | 377 | //也就是说把 where("id IN(?)", [1,2]) 展开成 where("id IN(?,?)", 1,2) |
| 378 | 378 | $cutted = null; |
| 379 | 379 | $cut = null; |
| 380 | 380 | $toReplace = array(); |
| 381 | 381 | |
| 382 | - $newArgs=array(); |
|
| 382 | + $newArgs = array(); |
|
| 383 | 383 | //找到所有数组对应的?符位置 |
| 384 | - foreach ($args as $k =>$arg){ |
|
| 385 | - if(is_array($arg) || is_a($arg, Raw::class)){ |
|
| 386 | - if(!$cutted){ |
|
| 384 | + foreach ($args as $k =>$arg) { |
|
| 385 | + if (is_array($arg) || is_a($arg, Raw::class)) { |
|
| 386 | + if (!$cutted) { |
|
| 387 | 387 | $cut = new NestedStringCut($expr); |
| 388 | 388 | $cutted = $cut->getText(); |
| 389 | 389 | } |
@@ -393,46 +393,46 @@ discard block |
||
| 393 | 393 | $pos !== false or \PhpBoot\abort( |
| 394 | 394 | new \InvalidArgumentException("unmatched params and ? @ $expr")); |
| 395 | 395 | |
| 396 | - if(is_array($arg)){ |
|
| 396 | + if (is_array($arg)) { |
|
| 397 | 397 | $stubs = []; |
| 398 | - foreach ($arg as $i){ |
|
| 399 | - if(is_a($i, Raw::class)){ |
|
| 398 | + foreach ($arg as $i) { |
|
| 399 | + if (is_a($i, Raw::class)) { |
|
| 400 | 400 | $stubs[] = strval($i); |
| 401 | - }else{ |
|
| 401 | + }else { |
|
| 402 | 402 | $stubs[] = '?'; |
| 403 | 403 | $newArgs[] = $i; |
| 404 | 404 | } |
| 405 | 405 | } |
| 406 | 406 | $stubs = implode(',', $stubs); |
| 407 | - }else{ |
|
| 407 | + }else { |
|
| 408 | 408 | $stubs = strval($arg); |
| 409 | 409 | } |
| 410 | 410 | $toReplace[] = [$pos, $stubs]; |
| 411 | 411 | |
| 412 | - }else{ |
|
| 413 | - $newArgs[]=$arg; |
|
| 412 | + }else { |
|
| 413 | + $newArgs[] = $arg; |
|
| 414 | 414 | } |
| 415 | 415 | } |
| 416 | 416 | |
| 417 | - if(count($toReplace)){ |
|
| 417 | + if (count($toReplace)) { |
|
| 418 | 418 | $toReplace = array_reverse($toReplace); |
| 419 | - foreach ($toReplace as $i){ |
|
| 419 | + foreach ($toReplace as $i) { |
|
| 420 | 420 | list($pos, $v) = $i; |
| 421 | - $expr = substr($expr, 0, $pos).$v. substr($expr, $pos+1); |
|
| 421 | + $expr = substr($expr, 0, $pos).$v.substr($expr, $pos + 1); |
|
| 422 | 422 | } |
| 423 | 423 | $args = $newArgs; |
| 424 | 424 | } |
| 425 | 425 | } |
| 426 | 426 | $context->appendSql($prefix.' '.$expr); |
| 427 | - if($args){ |
|
| 427 | + if ($args) { |
|
| 428 | 428 | $context->appendParams($args); |
| 429 | 429 | } |
| 430 | 430 | } |
| 431 | 431 | } |
| 432 | 432 | } |
| 433 | 433 | |
| 434 | -class GroupByImpl{ |
|
| 435 | - static public function groupBy($context, $column){ |
|
| 434 | +class GroupByImpl { |
|
| 435 | + static public function groupBy($context, $column) { |
|
| 436 | 436 | $context->appendSql("GROUP BY $column"); |
| 437 | 437 | } |
| 438 | 438 | } |
@@ -456,20 +456,20 @@ discard block |
||
| 456 | 456 | * @param string|false $asDict return as dict or array |
| 457 | 457 | * @return false|array |
| 458 | 458 | */ |
| 459 | - static public function get($context, $dictAs=false){ |
|
| 459 | + static public function get($context, $dictAs = false) { |
|
| 460 | 460 | |
| 461 | 461 | $st = $context->connection->prepare($context->sql); |
| 462 | - if($st->execute($context->params)){ |
|
| 462 | + if ($st->execute($context->params)) { |
|
| 463 | 463 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
| 464 | - if ($dictAs){ |
|
| 465 | - $dict= []; |
|
| 466 | - foreach ($res as $i){ |
|
| 467 | - $dict[$i[$dictAs]]=$i; |
|
| 464 | + if ($dictAs) { |
|
| 465 | + $dict = []; |
|
| 466 | + foreach ($res as $i) { |
|
| 467 | + $dict[$i[$dictAs]] = $i; |
|
| 468 | 468 | } |
| 469 | 469 | return $context->handleResult($dict); |
| 470 | 470 | } |
| 471 | 471 | return $context->handleResult($res); |
| 472 | - }else{ |
|
| 472 | + }else { |
|
| 473 | 473 | return false; |
| 474 | 474 | } |
| 475 | 475 | } |
@@ -478,22 +478,22 @@ discard block |
||
| 478 | 478 | * @param Context $context |
| 479 | 479 | * @return int|false |
| 480 | 480 | */ |
| 481 | - static public function count($context){ |
|
| 481 | + static public function count($context) { |
|
| 482 | 482 | |
| 483 | 483 | $found = []; |
| 484 | - if(!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 485 | - count($found)==0){ |
|
| 484 | + if (!preg_match('/\bselect\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 485 | + count($found) == 0) { |
|
| 486 | 486 | \PhpBoot\abort(new \PDOException("can not use count(*) without select")); |
| 487 | 487 | } |
| 488 | 488 | list($chars, $columnBegin) = $found[0]; |
| 489 | - $columnBegin = $columnBegin + strlen('select')+1; |
|
| 489 | + $columnBegin = $columnBegin + strlen('select') + 1; |
|
| 490 | 490 | |
| 491 | 491 | $columnEnd = 0; |
| 492 | 492 | $found = []; |
| 493 | - if(!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 494 | - count($found)==0){ |
|
| 493 | + if (!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
|
| 494 | + count($found) == 0) { |
|
| 495 | 495 | $columnEnd = strlen($context->sql); |
| 496 | - }else{ |
|
| 496 | + }else { |
|
| 497 | 497 | list($chars, $columnEnd) = $found[0]; |
| 498 | 498 | } |
| 499 | 499 | $sql = substr($context->sql, 0, $columnBegin); |
@@ -501,10 +501,10 @@ discard block |
||
| 501 | 501 | $sql .= substr($context->sql, $columnEnd); |
| 502 | 502 | |
| 503 | 503 | $st = $context->connection->prepare($sql); |
| 504 | - if($st->execute($context->params)){ |
|
| 504 | + if ($st->execute($context->params)) { |
|
| 505 | 505 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
| 506 | 506 | return $res[0]['count']; |
| 507 | - }else{ |
|
| 507 | + }else { |
|
| 508 | 508 | return false; |
| 509 | 509 | } |
| 510 | 510 | |
@@ -512,54 +512,54 @@ discard block |
||
| 512 | 512 | } |
| 513 | 513 | class OnDuplicateKeyUpdateImpl |
| 514 | 514 | { |
| 515 | - public function set($context, $column, $value){ |
|
| 515 | + public function set($context, $column, $value) { |
|
| 516 | 516 | $prefix = ''; |
| 517 | - if($this->first){ |
|
| 517 | + if ($this->first) { |
|
| 518 | 518 | $this->first = false; |
| 519 | 519 | $prefix = 'ON DUPLICATE KEY UPDATE '; |
| 520 | - }else{ |
|
| 520 | + }else { |
|
| 521 | 521 | $prefix = ','; |
| 522 | 522 | } |
| 523 | - if(is_a($value, Raw::class)){ |
|
| 524 | - $context->appendSql("$prefix$column=$value",$prefix == 'ON DUPLICATE KEY UPDATE '); |
|
| 525 | - }else{ |
|
| 526 | - $context->appendSql("$prefix$column=?",$prefix == 'ON DUPLICATE KEY UPDATE '); |
|
| 523 | + if (is_a($value, Raw::class)) { |
|
| 524 | + $context->appendSql("$prefix$column=$value", $prefix == 'ON DUPLICATE KEY UPDATE '); |
|
| 525 | + }else { |
|
| 526 | + $context->appendSql("$prefix$column=?", $prefix == 'ON DUPLICATE KEY UPDATE '); |
|
| 527 | 527 | $context->appendParams([$value]); |
| 528 | 528 | } |
| 529 | 529 | } |
| 530 | 530 | |
| 531 | - public function setExpr($context, $expr, $args){ |
|
| 531 | + public function setExpr($context, $expr, $args) { |
|
| 532 | 532 | $prefix = ''; |
| 533 | - if($this->first){ |
|
| 533 | + if ($this->first) { |
|
| 534 | 534 | $this->first = false; |
| 535 | 535 | $prefix = 'ON DUPLICATE KEY UPDATE '; |
| 536 | - }else{ |
|
| 536 | + }else { |
|
| 537 | 537 | $prefix = ','; |
| 538 | 538 | } |
| 539 | 539 | |
| 540 | - $context->appendSql("$prefix$expr",$prefix == 'ON DUPLICATE KEY UPDATE '); |
|
| 540 | + $context->appendSql("$prefix$expr", $prefix == 'ON DUPLICATE KEY UPDATE '); |
|
| 541 | 541 | $context->appendParams($args); |
| 542 | 542 | |
| 543 | 543 | } |
| 544 | - public function setArgs($context, $values){ |
|
| 544 | + public function setArgs($context, $values) { |
|
| 545 | 545 | $set = []; |
| 546 | 546 | $params = []; |
| 547 | - foreach ($values as $k=>$v){ |
|
| 548 | - if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
|
| 549 | - $set[]= "$k=".$v->get(); |
|
| 550 | - }else{ |
|
| 551 | - $set[]= "$k=?"; |
|
| 552 | - $params[]=$v; |
|
| 547 | + foreach ($values as $k=>$v) { |
|
| 548 | + if (is_a($v, Raw::class)) {//直接拼接sql,不需要转义 |
|
| 549 | + $set[] = "$k=".$v->get(); |
|
| 550 | + }else { |
|
| 551 | + $set[] = "$k=?"; |
|
| 552 | + $params[] = $v; |
|
| 553 | 553 | } |
| 554 | 554 | } |
| 555 | - if($this->first){ |
|
| 555 | + if ($this->first) { |
|
| 556 | 556 | $this->first = false; |
| 557 | 557 | $context->appendSql('ON DUPLICATE KEY UPDATE '.implode(',', $set)); |
| 558 | 558 | $context->appendParams($params); |
| 559 | - }else{ |
|
| 560 | - $context->appendSql(','.implode(',', $set),false); |
|
| 559 | + }else { |
|
| 560 | + $context->appendSql(','.implode(',', $set), false); |
|
| 561 | 561 | $context->appendParams($params); |
| 562 | 562 | } |
| 563 | 563 | } |
| 564 | - private $first=true; |
|
| 564 | + private $first = true; |
|
| 565 | 565 | } |
@@ -51,7 +51,7 @@ discard block |
||
| 51 | 51 | if($tables instanceof BasicRule){ |
| 52 | 52 | $context->appendSql("FROM (".$tables->context->sql.')'); |
| 53 | 53 | $context->params = array_merge($context->params,$tables->context->params); |
| 54 | - }else { |
|
| 54 | + } else { |
|
| 55 | 55 | $context->appendSql("FROM `$tables`"); |
| 56 | 56 | } |
| 57 | 57 | if($as){ |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | static public function join($context, $type, $table) { |
| 74 | 74 | if($type){ |
| 75 | 75 | $context->appendSql("$type JOIN $table"); |
| 76 | - }else{ |
|
| 76 | + } else{ |
|
| 77 | 77 | $context->appendSql("JOIN $table"); |
| 78 | 78 | } |
| 79 | 79 | } |
@@ -120,7 +120,7 @@ discard block |
||
| 120 | 120 | foreach ($values as $v){ |
| 121 | 121 | if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
| 122 | 122 | $stubs[]=$v->get(); |
| 123 | - }else{ |
|
| 123 | + } else{ |
|
| 124 | 124 | $stubs[]='?'; |
| 125 | 125 | $params[] = $v; |
| 126 | 126 | } |
@@ -131,7 +131,7 @@ discard block |
||
| 131 | 131 | //VALUES(val0, val1, val2) |
| 132 | 132 | $context->appendSql("VALUES($stubs)"); |
| 133 | 133 | |
| 134 | - }else{ |
|
| 134 | + } else{ |
|
| 135 | 135 | //(col0, col1, col2) VALUES(val0, val1, val2) |
| 136 | 136 | $columns = implode(',', array_keys($values)); |
| 137 | 137 | $context->appendSql("($columns) VALUES($stubs)",false); |
@@ -155,12 +155,12 @@ discard block |
||
| 155 | 155 | if($this->first){ |
| 156 | 156 | $this->first = false; |
| 157 | 157 | $prefix = 'SET '; |
| 158 | - }else{ |
|
| 158 | + } else{ |
|
| 159 | 159 | $prefix = ','; |
| 160 | 160 | } |
| 161 | 161 | if(is_a($value, Raw::class)){ |
| 162 | 162 | $context->appendSql("$prefix$column=$value",$prefix == 'SET '); |
| 163 | - }else{ |
|
| 163 | + } else{ |
|
| 164 | 164 | $context->appendSql("$prefix$column=?",$prefix == 'SET '); |
| 165 | 165 | $context->appendParams([$value]); |
| 166 | 166 | } |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | if($this->first){ |
| 172 | 172 | $this->first = false; |
| 173 | 173 | $prefix = 'SET '; |
| 174 | - }else{ |
|
| 174 | + } else{ |
|
| 175 | 175 | $prefix = ','; |
| 176 | 176 | } |
| 177 | 177 | |
@@ -185,7 +185,7 @@ discard block |
||
| 185 | 185 | foreach ($values as $k=>$v){ |
| 186 | 186 | if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
| 187 | 187 | $set[]= "$k=".$v->get(); |
| 188 | - }else{ |
|
| 188 | + } else{ |
|
| 189 | 189 | $set[]= "$k=?"; |
| 190 | 190 | $params[]=$v; |
| 191 | 191 | } |
@@ -194,7 +194,7 @@ discard block |
||
| 194 | 194 | $this->first = false; |
| 195 | 195 | $context->appendSql('SET '.implode(',', $set)); |
| 196 | 196 | $context->appendParams($params); |
| 197 | - }else{ |
|
| 197 | + } else{ |
|
| 198 | 198 | $context->appendSql(','.implode(',', $set),false); |
| 199 | 199 | $context->appendParams($params); |
| 200 | 200 | } |
@@ -214,7 +214,7 @@ discard block |
||
| 214 | 214 | new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
| 215 | 215 | |
| 216 | 216 | $params[] = $v; |
| 217 | - }else{ |
|
| 217 | + } else{ |
|
| 218 | 218 | $v = strtoupper($v); |
| 219 | 219 | preg_match('/^[a-zA-Z0-9_.]+$/', $k) && |
| 220 | 220 | ($v =='DESC' || $v =='ASC') or \PhpBoot\abort( new \InvalidArgumentException("invalid params for orderBy(".json_encode($orders).")")); |
@@ -225,7 +225,7 @@ discard block |
||
| 225 | 225 | if($this->first){ |
| 226 | 226 | $this->first = false; |
| 227 | 227 | $context->appendSql('ORDER BY '.implode(',', $params)); |
| 228 | - }else{ |
|
| 228 | + } else{ |
|
| 229 | 229 | $context->appendSql(','.implode(',', $params),false); |
| 230 | 230 | } |
| 231 | 231 | return $this; |
@@ -234,7 +234,7 @@ discard block |
||
| 234 | 234 | if($this->first){ |
| 235 | 235 | $this->first = false; |
| 236 | 236 | $context->appendSql("ORDER BY $column"); |
| 237 | - }else{ |
|
| 237 | + } else{ |
|
| 238 | 238 | $context->appendSql(",$column", false); |
| 239 | 239 | } |
| 240 | 240 | if($order){ |
@@ -327,41 +327,41 @@ discard block |
||
| 327 | 327 | foreach ($var as $i){ |
| 328 | 328 | if(is_a($i, Raw::class)){ |
| 329 | 329 | $stubs[]=strval($i); |
| 330 | - }else{ |
|
| 330 | + } else{ |
|
| 331 | 331 | $stubs[]='?'; |
| 332 | 332 | $params[] = $i; |
| 333 | 333 | } |
| 334 | 334 | } |
| 335 | 335 | $stubs = implode(',', $stubs); |
| 336 | 336 | $exprs[] = "$k $op ($stubs)"; |
| 337 | - }else if($op == 'BETWEEN'){ |
|
| 337 | + } else if($op == 'BETWEEN'){ |
|
| 338 | 338 | $cond = "$k BETWEEN"; |
| 339 | 339 | if(is_a($var[0], Raw::class)){ |
| 340 | 340 | $cond = "$cond ".strval($var[0]); |
| 341 | - }else{ |
|
| 341 | + } else{ |
|
| 342 | 342 | $cond = "$cond ?"; |
| 343 | 343 | $params[] = $var[0]; |
| 344 | 344 | } |
| 345 | 345 | if(is_a($var[1], Raw::class)){ |
| 346 | 346 | $cond = "$cond AND ".strval($var[1]); |
| 347 | - }else{ |
|
| 347 | + } else{ |
|
| 348 | 348 | $cond = "$cond AND ?"; |
| 349 | 349 | $params[] = $var[1]; |
| 350 | 350 | } |
| 351 | 351 | $exprs[] = $cond; |
| 352 | - }else{ |
|
| 352 | + } else{ |
|
| 353 | 353 | if(is_a($var, Raw::class)){ |
| 354 | 354 | $exprs[] = "$k $op ".strval($var); |
| 355 | - }else{ |
|
| 355 | + } else{ |
|
| 356 | 356 | $exprs[] = "$k $op ?"; |
| 357 | 357 | $params[] = $var; |
| 358 | 358 | } |
| 359 | 359 | } |
| 360 | - }else{ |
|
| 360 | + } else{ |
|
| 361 | 361 | if(is_a($v, Raw::class)){ |
| 362 | 362 | $exprs[] = "$k = ".strval($v); |
| 363 | 363 | |
| 364 | - }else{ |
|
| 364 | + } else{ |
|
| 365 | 365 | $exprs[] = "$k = ?"; |
| 366 | 366 | $params[] = $v; |
| 367 | 367 | } |
@@ -398,18 +398,18 @@ discard block |
||
| 398 | 398 | foreach ($arg as $i){ |
| 399 | 399 | if(is_a($i, Raw::class)){ |
| 400 | 400 | $stubs[] = strval($i); |
| 401 | - }else{ |
|
| 401 | + } else{ |
|
| 402 | 402 | $stubs[] = '?'; |
| 403 | 403 | $newArgs[] = $i; |
| 404 | 404 | } |
| 405 | 405 | } |
| 406 | 406 | $stubs = implode(',', $stubs); |
| 407 | - }else{ |
|
| 407 | + } else{ |
|
| 408 | 408 | $stubs = strval($arg); |
| 409 | 409 | } |
| 410 | 410 | $toReplace[] = [$pos, $stubs]; |
| 411 | 411 | |
| 412 | - }else{ |
|
| 412 | + } else{ |
|
| 413 | 413 | $newArgs[]=$arg; |
| 414 | 414 | } |
| 415 | 415 | } |
@@ -469,7 +469,7 @@ discard block |
||
| 469 | 469 | return $context->handleResult($dict); |
| 470 | 470 | } |
| 471 | 471 | return $context->handleResult($res); |
| 472 | - }else{ |
|
| 472 | + } else{ |
|
| 473 | 473 | return false; |
| 474 | 474 | } |
| 475 | 475 | } |
@@ -493,7 +493,7 @@ discard block |
||
| 493 | 493 | if(!preg_match('/\bfrom\b/i', $context->sql, $found, PREG_OFFSET_CAPTURE) || |
| 494 | 494 | count($found)==0){ |
| 495 | 495 | $columnEnd = strlen($context->sql); |
| 496 | - }else{ |
|
| 496 | + } else{ |
|
| 497 | 497 | list($chars, $columnEnd) = $found[0]; |
| 498 | 498 | } |
| 499 | 499 | $sql = substr($context->sql, 0, $columnBegin); |
@@ -504,7 +504,7 @@ discard block |
||
| 504 | 504 | if($st->execute($context->params)){ |
| 505 | 505 | $res = $st->fetchAll(\PDO::FETCH_ASSOC); |
| 506 | 506 | return $res[0]['count']; |
| 507 | - }else{ |
|
| 507 | + } else{ |
|
| 508 | 508 | return false; |
| 509 | 509 | } |
| 510 | 510 | |
@@ -517,12 +517,12 @@ discard block |
||
| 517 | 517 | if($this->first){ |
| 518 | 518 | $this->first = false; |
| 519 | 519 | $prefix = 'ON DUPLICATE KEY UPDATE '; |
| 520 | - }else{ |
|
| 520 | + } else{ |
|
| 521 | 521 | $prefix = ','; |
| 522 | 522 | } |
| 523 | 523 | if(is_a($value, Raw::class)){ |
| 524 | 524 | $context->appendSql("$prefix$column=$value",$prefix == 'ON DUPLICATE KEY UPDATE '); |
| 525 | - }else{ |
|
| 525 | + } else{ |
|
| 526 | 526 | $context->appendSql("$prefix$column=?",$prefix == 'ON DUPLICATE KEY UPDATE '); |
| 527 | 527 | $context->appendParams([$value]); |
| 528 | 528 | } |
@@ -533,7 +533,7 @@ discard block |
||
| 533 | 533 | if($this->first){ |
| 534 | 534 | $this->first = false; |
| 535 | 535 | $prefix = 'ON DUPLICATE KEY UPDATE '; |
| 536 | - }else{ |
|
| 536 | + } else{ |
|
| 537 | 537 | $prefix = ','; |
| 538 | 538 | } |
| 539 | 539 | |
@@ -547,7 +547,7 @@ discard block |
||
| 547 | 547 | foreach ($values as $k=>$v){ |
| 548 | 548 | if(is_a($v, Raw::class)){//直接拼接sql,不需要转义 |
| 549 | 549 | $set[]= "$k=".$v->get(); |
| 550 | - }else{ |
|
| 550 | + } else{ |
|
| 551 | 551 | $set[]= "$k=?"; |
| 552 | 552 | $params[]=$v; |
| 553 | 553 | } |
@@ -556,7 +556,7 @@ discard block |
||
| 556 | 556 | $this->first = false; |
| 557 | 557 | $context->appendSql('ON DUPLICATE KEY UPDATE '.implode(',', $set)); |
| 558 | 558 | $context->appendParams($params); |
| 559 | - }else{ |
|
| 559 | + } else{ |
|
| 560 | 560 | $context->appendSql(','.implode(',', $set),false); |
| 561 | 561 | $context->appendParams($params); |
| 562 | 562 | } |
@@ -40,6 +40,9 @@ discard block |
||
| 40 | 40 | |
| 41 | 41 | class OnDuplicateKeyUpdateRule extends ExecRule |
| 42 | 42 | { |
| 43 | + /** |
|
| 44 | + * @param \PhpBoot\DB\Context $context |
|
| 45 | + */ |
|
| 43 | 46 | public function __construct($context) |
| 44 | 47 | { |
| 45 | 48 | parent::__construct($context); |
@@ -69,8 +72,7 @@ discard block |
||
| 69 | 72 | * ->onDuplicateKeyUpdateArgs(['a'=>Sql::raw('a+1')]) |
| 70 | 73 | * => "INSERT INTO table(a,b) VALUES(1,now()) ON DUPLICATE KEY UPDATE a=a+1" |
| 71 | 74 | * |
| 72 | - * @param string $column |
|
| 73 | - * @param mixed $value |
|
| 75 | + * @param mixed $values |
|
| 74 | 76 | * @return \PhpBoot\DB\rules\basic\ExecRule |
| 75 | 77 | */ |
| 76 | 78 | public function onDuplicateKeyUpdateArgs($values) { |
@@ -85,8 +87,6 @@ discard block |
||
| 85 | 87 | * ->onDuplicateKeyUpdateExpr('a=a+1') |
| 86 | 88 | * => "INSERT INTO table(a,b) VALUES(1,now()) ON DUPLICATE KEY UPDATE a=a+1" |
| 87 | 89 | * |
| 88 | - * @param string $column |
|
| 89 | - * @param mixed $value |
|
| 90 | 90 | * @return \PhpBoot\DB\rules\basic\ExecRule |
| 91 | 91 | */ |
| 92 | 92 | public function onDuplicateKeyUpdateExpr($expr, $_=null) { |
@@ -89,7 +89,7 @@ |
||
| 89 | 89 | * @param mixed $value |
| 90 | 90 | * @return \PhpBoot\DB\rules\basic\ExecRule |
| 91 | 91 | */ |
| 92 | - public function onDuplicateKeyUpdateExpr($expr, $_=null) { |
|
| 92 | + public function onDuplicateKeyUpdateExpr($expr, $_ = null) { |
|
| 93 | 93 | $this->impl->setExpr($this->context, $expr, array_slice(func_get_args(), 1)); |
| 94 | 94 | return new ExecRule($this->context); |
| 95 | 95 | } |