Skip to content

Commit 601abf5

Browse files
author
zzy
committed
add request listener
1 parent 3071a67 commit 601abf5

File tree

25 files changed

+442
-90
lines changed

25 files changed

+442
-90
lines changed

app/config/config.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ services:
9696
tags:
9797
- { name: twig.extension }
9898

99+
kernel.listener.kernel_request_listener:
100+
class: Redwood\WebBundle\Listener\KernelRequestListener
101+
arguments: ['@service_container']
102+
tags:
103+
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority:255 }
104+
105+
99106
# Swiftmailer Configuration
100107
swiftmailer:
101108
transport: %mailer_transport%

app/config/parameters_default.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ parameters:
33
site_name: Redwood
44
site_url: www.111dev.com
55
weibo_key: testdata
6-
qq_key: testdata
6+
qq_key: testdata
7+
8+
route_white_list: ['/uploader/upload_callback', '/uploader/process_callback', '/coin/pay/return/alipay','/coin/pay/notify/alipay','/login', '/login_check']

src/Redwood/Common/ArrayToolkit.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ public static function group(array $array, $key)
6767
return $grouped;
6868
}
6969

70+
// 把原数组$array的 name 字段, 作为 新数组的键名 index($array, 'id')
71+
// $array = {
72+
// [0]:array(
73+
// "id" => 9,
74+
// "name" => 'aaaa'
75+
// ),
76+
// [1]:array(
77+
// "id" => 2,
78+
// "name" => 'bbbb'
79+
// )
80+
// }
81+
// 处理后:
82+
// $new = {
83+
// [9]:array(
84+
// "name" => 'aaaa'
85+
// ),
86+
// [2]:array(
87+
// "name" => 'bbbb'
88+
// )
89+
// }
7090
public static function index (array $array, $name)
7191
{
7292
$indexedArray = array();

src/Redwood/Service/Common/DynamicQueryBuilder.php

Lines changed: 90 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,111 @@ public function __construct(Connection $connection, $conditions)
1616

1717
public function where($where)
1818
{
19-
if (!$this->isWhereInConditions($where)) {
20-
return $this;
21-
}
22-
return parent::where($where);
19+
if (!$this->isWhereInConditions($where)) {
20+
return $this;
21+
}
22+
23+
return parent::where($where);
2324
}
2425

2526
public function andWhere($where)
2627
{
27-
if (!$this->isWhereInConditions($where)) {
28-
return $this;
29-
}
28+
if (!$this->isWhereInConditions($where)) {
29+
return $this;
30+
}
31+
32+
if ($this->isInCondition($where)) {
33+
$where = $this->whereIn($where);
34+
if(!$where) {
35+
return $this;
36+
}
37+
}
38+
3039
return parent::andWhere($where);
3140
}
3241

42+
public function orWhere($where)
43+
{
44+
if (!$this->isWhereInConditions($where)) {
45+
return $this;
46+
}
47+
48+
if ($this->isInCondition($where)) {
49+
$where = $this->whereIn($where);
50+
if(!$where) {
51+
return $this;
52+
}
53+
}
54+
55+
return parent::orWhere($where);
56+
}
57+
3358
public function andStaticWhere($where)
3459
{
35-
return parent::andWhere($where);
60+
return parent::andWhere($where);
61+
}
62+
63+
protected function whereIn($where)
64+
{
65+
$conditionName = $this->getConditionName($where);
66+
67+
if (empty($this->conditions[$conditionName]) || !is_array($this->conditions[$conditionName])) {
68+
return false;
69+
}
70+
71+
$this->conditions[$conditionName] = array_unique($this->conditions[$conditionName]);
72+
73+
$marks = array();
74+
75+
foreach (array_values($this->conditions[$conditionName]) as $index => $value) {
76+
$marks[] = ":{$conditionName}_{$index}";
77+
$this->conditions["{$conditionName}_{$index}"] = $value;
78+
}
79+
80+
$where = str_replace(":{$conditionName}", join(',', $marks), $where);
81+
82+
return $where;
3683
}
3784

3885
public function execute()
3986
{
40-
foreach ($this->conditions as $field => $value) {
41-
$this->setParameter(":{$field}", $value);
42-
}
43-
return parent::execute();
87+
foreach ($this->conditions as $field => $value) {
88+
$this->setParameter(":{$field}", $value);
89+
}
90+
91+
return parent::execute();
92+
}
93+
94+
protected function isInCondition($where)
95+
{
96+
$matched = preg_match('/\s+(IN)\s+/', $where, $matches);
97+
98+
if (empty($matched)) {
99+
return false;
100+
} else {
101+
return true;
102+
}
44103
}
45104

46-
private function isWhereInConditions($where)
105+
protected function getConditionName($where)
47106
{
48-
$matched = preg_match('/:([a-zA-z0-9_]+)/', $where, $matches);
49-
if (empty($matched)) {
50-
return false;
51-
}
107+
$matched = preg_match('/:([a-zA-z0-9_]+)/', $where, $matches);
108+
109+
if (empty($matched)) {
110+
return false;
111+
}
112+
113+
return $matches[1];
114+
}
115+
116+
protected function isWhereInConditions($where)
117+
{
118+
$conditionName = $this->getConditionName($where);
119+
120+
if (!$conditionName) {
121+
return false;
122+
}
52123

53-
return array_key_exists($matches[1], $this->conditions) && !is_null($this->conditions[$matches[1]]);
124+
return array_key_exists($conditionName, $this->conditions) && !is_null($this->conditions[$conditionName]);
54125
}
55-
}
126+
}

src/Redwood/Service/Note/Impl/JswidgetServiceImpl.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,28 @@
44
use Redwood\Service\Common\BaseService;
55
use Redwood\Service\Note\JswidgetService;
66

7+
use Redwood\Common\ArrayToolkit;
8+
79
class JswidgetServiceImpl extends BaseService implements JswidgetService
810
{
911
public function createJswidget($jswidget)
1012
{
1113
$jswidget['userId'] = $this->getCurrentUser()->id;
1214
$jswidget['createTime'] = time();
15+
$this->filterJswidgetFields($jswidget);
1316
return $this->getJswidgetDao()->addJswidget($jswidget);
1417
}
1518

16-
public function updateJswidget($jswidget)
19+
public function updateJswidget($id, $jswidget)
1720
{
1821
$jswidget['updateTime'] = time();
19-
return $this->getJswidgetDao()->updateJswidget($jswidget['id'], $jswidget);
22+
return $this->getJswidgetDao()->updateJswidget($id, $jswidget);
2023
}
2124

2225
public function getJswidget($id)
2326
{
2427
$jswidget = $this->getJswidgetDao()->getJswidget($id);
28+
2529
if(!$jswidget){
2630
return null;
2731
} else {
@@ -30,6 +34,29 @@ public function getJswidget($id)
3034
}
3135

3236

37+
private function filterJswidgetFields(&$fields)
38+
{
39+
40+
if (!empty($fields['tags'])) {
41+
$tempTags = explode(',', $fields['tags']);
42+
$fields['tags'] = implode("|", $tempTags);
43+
}
44+
45+
return $fields;
46+
}
47+
48+
private function filterJswidgetTags(&$fields)
49+
{
50+
51+
if (!empty($fields['tags'])) {
52+
$tempTags = explode('|', $fields['tags']);
53+
$fields['tags'] = implode(",", $tempTags);
54+
}
55+
56+
return $fields;
57+
}
58+
59+
3360

3461

3562
public function deleteJswidget($id){

src/Redwood/Service/Note/JswidgetService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ interface JswidgetService
55
{
66
public function createJswidget($jswidget);
77

8-
public function updateJswidget($jswidget);
8+
public function updateJswidget($id, $jswidget);
99

1010
public function deleteJswidget($id);
1111

src/Redwood/Service/Taxonomy/Dao/Impl/TagDaoImpl.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,29 @@ public function getTagByName($name)
2121
return $this->getConnection()->fetchAssoc($sql, array($name));
2222
}
2323

24-
public function addTag($tag)
24+
public function getTagByLikeName($name)
25+
{
26+
$name = "%{$name}%";
27+
$sql = "SELECT * FROM {$this->table} WHERE name LIKE ?";
28+
return $this->getConnection()->fetchAll($sql, array($name));
29+
}
30+
31+
public function getTagsByIds(array $ids)
32+
{
33+
$conditions = array();
34+
if (empty($ids)) {
35+
return array();
36+
}else{
37+
$conditions['tagIds'] = $ids;
38+
}
39+
40+
$builder = $this->createTagQueryBuilder($conditions)
41+
->select('*');
42+
43+
return $builder->execute()->fetchAll() ? : array();
44+
}
45+
46+
public function addTag($tag)
2547
{
2648
$affected = $this->getConnection()->insert($this->table, $tag);
2749
if ($affected <= 0) {
@@ -74,6 +96,7 @@ private function createTagQueryBuilder($conditions)
7496
return $this->createDynamicQueryBuilder($conditions)
7597
->from($this->table, 'tag')
7698
->andWhere('name LIKE :name')
99+
->andWhere('id IN (:tagIds)')
77100
->andWhere('id = :id');
78101
}
79102

src/Redwood/Service/Taxonomy/Dao/TagDao.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public function getTag($id);
1414

1515
public function getTagByName($name);
1616

17+
public function getTagByLikeName($name);
18+
19+
public function getTagsByIds(array $ids);
20+
1721
public function searchTagCount(array $conditions);
1822

1923
public function searchTags($conditions, $orderBy, $start, $limit);

src/Redwood/Service/Taxonomy/Impl/TagServiceImpl.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ public function getTag($id)
6262
}
6363
}
6464

65+
public function getTagsByIds($ids)
66+
{
67+
$idsArray = explode('|', $ids);
68+
$tags = $this->getTagDao()->getTagsByIds($idsArray);
69+
return $tags;
70+
// return ArrayToolkit::index($tags, 'id');
71+
}
72+
73+
public function getTagByLikeName($name)
74+
{
75+
return $this->getTagDao()->getTagByLikeName($name);
76+
}
77+
78+
6579
public function getTagByName($name)
6680
{
6781
return $this->getTagDao()->getTagByName($name);

src/Redwood/Service/Taxonomy/TagService.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ public function getTag($id);
1414

1515
public function getTagByName($name);
1616

17+
public function getTagByLikeName($name);
18+
19+
public function getTagsByIds($ids);
20+
1721
public function isTagNameAvalieable($name);
1822

1923
public function findTagById($id);

0 commit comments

Comments
 (0)