From a4843dd1a338de0cad0b36b22ea34bd9ae749c71 Mon Sep 17 00:00:00 2001 From: shenfakuan Date: Fri, 29 Apr 2016 14:39:11 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9C=A8=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E5=9F=9F=E5=90=8D=E8=B7=AF=E7=94=B1=E6=97=B6=EF=BC=8C?= =?UTF-8?q?url=E6=96=B9=E6=B3=95=E7=BB=93=E6=9E=9C=E4=B8=8D=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Url.php | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/library/think/Url.php b/library/think/Url.php index d4738bc5..be09ab08 100644 --- a/library/think/Url.php +++ b/library/think/Url.php @@ -150,22 +150,26 @@ class Url if (Config::get('url_domain_deploy')) { // 根域名 $urlDomainRoot = Config::get('url_domain_root'); - foreach (Route::domain() as $key => $rule) { - $rule = is_array($rule) ? $rule[0] : $rule; - if (false === strpos($key, '*') && 0 === strpos($url, $rule)) { - $url = ltrim($url, $rule); - $domain = $key; - // 生成对应子域名 - if(!empty($urlDomainRoot)){ - $domain .= $urlDomainRoot; + $route_domain = array_keys(Route::domain()); + foreach($route_domain as $domain_prefix) { + if(strpos($domain, trim($domain_prefix, '*.')) !== false) { + foreach (Route::domain() as $key => $rule) { + $rule = is_array($rule) ? $rule[0] : $rule; + if (false === strpos($key, '*') && 0 === strpos($url, $rule)) { + $url = ltrim($url, $rule); + $domain = $key; + // 生成对应子域名 + if(!empty($urlDomainRoot)){ + $domain .= $urlDomainRoot; + } + break; + }else if(false !== strpos($key, '*')){ + if(!empty($urlDomainRoot)){ + $domain .= $urlDomainRoot; + } + break; + } } - break; - }else if(false !== strpos($key, '*')){ - $domain = str_replace('*',strstr($domain,'.',true),$key); - if(!empty($urlDomainRoot)){ - $domain .= $urlDomainRoot; - } - break; } } } From 6ca3dad8506aba62f9873b739440b48cc3759302 Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Sun, 8 May 2016 12:36:08 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E8=A7=A3=E6=9E=90=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E5=87=A0=E4=B8=AA=E5=B8=B8=E7=94=A8=E7=9A=84=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E5=A4=84=E7=90=86=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Collection.php | 132 ++++++++++++++++++++++++++- library/think/template/taglib/Cx.php | 24 ++--- 2 files changed, 142 insertions(+), 14 deletions(-) diff --git a/library/think/Collection.php b/library/think/Collection.php index 537be258..9561e28f 100644 --- a/library/think/Collection.php +++ b/library/think/Collection.php @@ -121,7 +121,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria * 通过使用用户自定义函数,以字符串返回数组 * * @param callable $callback - * @param mixed $initial + * @param mixed $initial * @return mixed */ public function reduce(callable $callback, $initial = null) @@ -149,6 +149,132 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria return array_shift($this->items); } + /** + * 把一个数组分割为新的数组块. + * + * @param int $size + * @param bool $preserveKeys + * @return static + */ + public function chunk($size, $preserveKeys = false) + { + $chunks = []; + + foreach (array_chunk($this->items, $size, $preserveKeys) as $chunk) { + $chunks[] = new static($chunk); + } + + return new static($chunks); + } + + /** + * 在数组开头插入一个元素 + * @param mixed $value + * @param null $key + * @return int + */ + public function unshift($value, $key = null) + { + if (is_null($key)) { + array_unshift($this->items, $value); + } else { + $this->items = [$key => $value] + $this->items; + } + } + + /** + * 给每个元素执行个回调 + * + * @param callable $callback + * @return $this + */ + public function each(callable $callback) + { + foreach ($this->items as $key => $item) { + if ($callback($item, $key) === false) { + break; + } + } + + return $this; + } + + + /** + * 用回调函数过滤数组中的元素 + * @param callable|null $callback + * @return static + */ + public function filter(callable $callback = null) + { + if ($callback) { + return new static(array_filter($this->items, $callback)); + } + + return new static(array_filter($this->items)); + } + + /** + * 返回数组中指定的一列 + * @param $column_key + * @param null $index_key + * @return array + */ + public function column($column_key, $index_key = null) + { + if (function_exists('array_column')) { + return array_column($this->items, $column_key, $index_key); + } + + $result = []; + foreach ($this->items as $row) { + $key = $value = null; + $keySet = $valueSet = false; + if ($index_key !== null && array_key_exists($index_key, $row)) { + $keySet = true; + $key = (string)$row[$index_key]; + } + if ($column_key === null) { + $valueSet = true; + $value = $row; + } elseif (is_array($row) && array_key_exists($column_key, $row)) { + $valueSet = true; + $value = $row[$column_key]; + } + if ($valueSet) { + if ($keySet) { + $result[$key] = $value; + } else { + $result[] = $value; + } + } + } + return $result; + } + + + /** + * 对数组排序 + * + * @param callable|null $callback + * @return static + */ + public function sort(callable $callback = null) + { + $items = $this->items; + + $callback ? uasort($items, $callback) : uasort($items, function ($a, $b) { + + if ($a == $b) { + return 0; + } + + return ($a < $b) ? -1 : 1; + }); + + return new static($items); + } + /** * 将数组打乱 @@ -167,8 +293,8 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria /** * 截取数组 * - * @param int $offset - * @param int $length + * @param int $offset + * @param int $length * @param bool $preserveKeys * @return static */ diff --git a/library/think/template/taglib/Cx.php b/library/think/template/taglib/Cx.php index 017c6d6c..a3fe83cf 100644 --- a/library/think/template/taglib/Cx.php +++ b/library/think/template/taglib/Cx.php @@ -98,10 +98,11 @@ class Cx extends Taglib } else { $name = $this->autoBuildVar($name); } - $parseStr .= 'if(is_array(' . $name . ')): $' . $key . ' = 0;'; + + $parseStr .= 'if(is_array(' . $name . ') || ' . $name . ' instanceof \think\Collection): $' . $key . ' = 0;'; // 设置了输出数组长度 if (0 != $offset || 'null' != $length) { - $parseStr .= '$__LIST__ = array_slice(' . $name . ',' . $offset . ',' . $length . ', true); '; + $parseStr .= '$__LIST__ = is_array(' . $name . ') ? array_slice(' . $name . ',' . $offset . ',' . $length . ', true) : ' . $name . '->slice(' . $offset . ',' . $length . ', true); '; } else { $parseStr .= ' $__LIST__ = ' . $name . ';'; } @@ -144,6 +145,7 @@ class Cx extends Taglib $name = $tag['name']; $key = !empty($tag['key']) ? $tag['key'] : 'key'; $item = !empty($tag['id']) ? $tag['id'] : $tag['item']; + $empty = isset($tag['empty']) ? $tag['empty'] : ''; $offset = !empty($tag['offset']) && is_numeric($tag['offset']) ? intval($tag['offset']) : 0; $length = !empty($tag['length']) && is_numeric($tag['length']) ? intval($tag['length']) : 'null'; @@ -157,16 +159,20 @@ class Cx extends Taglib } else { $name = $this->autoBuildVar($name); } - $parseStr .= 'if(is_array(' . $name . ')): '; + $parseStr .= 'if(is_array(' . $name . ') || ' . $name . ' instanceof \think\Collection): '; // 设置了输出数组长度 if (0 != $offset || 'null' != $length) { if (!isset($var)) { $var = '$_' . uniqid(); } - $parseStr .= $var . ' = array_slice(' . $name . ',' . $offset . ',' . $length . ', true); '; + $parseStr .= $var . ' = is_array(' . $name . ') ? array_slice(' . $name . ',' . $offset . ',' . $length . ', true) : ' . $name . '->slice(' . $offset . ',' . $length . ', true); '; } else { $var = &$name; } + + $parseStr .= 'if( count(' . $var . ')==0 ) : echo "' . $empty . '" ;'; + $parseStr .= 'else: '; + // 设置了索引项 if (isset($tag['index'])) { $index = $tag['index']; @@ -185,11 +191,7 @@ class Cx extends Taglib $parseStr .= '?>'; // 循环体中的内容 $parseStr .= $content; - $parseStr .= ''; - // 设置了数组为空时的显示内容 - if (isset($tag['empty'])) { - $parseStr .= ''; - } + $parseStr .= ''; if (!empty($parseStr)) { return $parseStr; @@ -430,7 +432,7 @@ class Cx extends Taglib { $name = $tag['name']; $name = $this->autoBuildVar($name); - $parseStr = '' . $content . ''; + $parseStr = 'isEmpty())): ?>' . $content . ''; return $parseStr; } @@ -447,7 +449,7 @@ class Cx extends Taglib { $name = $tag['name']; $name = $this->autoBuildVar($name); - $parseStr = '' . $content . ''; + $parseStr = 'isEmpty()))): ?>' . $content . ''; return $parseStr; }