Skip to content

Commit

Permalink
Merge pull request #4 from E96/phpredis
Browse files Browse the repository at this point in the history
Phpredis
  • Loading branch information
andruha committed Apr 7, 2016
2 parents d01d8f0 + 70d2ff0 commit 50d4d3b
Show file tree
Hide file tree
Showing 27 changed files with 444 additions and 183 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
4 changes: 3 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Ignore all test and documentation for archive
/.github export-ignore
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/docs export-ignore
/docs export-ignore
7 changes: 7 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Contributing to Yii2
====================

- [Report an issue](docs/internals/report-an-issue.md)
- [Translate documentation or messages](docs/internals/translation-workflow.md)
- [Give us feedback or start a design discussion](http://www.yiiframework.com/forum/index.php/forum/42-general-discussions-for-yii-20/)
- [Contribute to the core code or fix bugs](docs/internals/git-workflow.md)
14 changes: 14 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
### What steps will reproduce the problem?

### What's expected?

### What do you get instead?


### Additional info

| Q | A
| ---------------- | ---
| Yii vesion |
| PHP version |
| Operating system |
7 changes: 7 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
| Q | A
| ------------- | ---
| Is bugfix? | yes/no
| New feature? | yes/no
| Breaks BC? | yes/no
| Tests pass? | yes/no
| Fixed issues | comma-separated list of tickets # fixed by the PR, if any
18 changes: 7 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@ php:
- 5.6
- 7.0
- hhvm
- hhvm-nightly

services:
- redis-server

# run build against hhvm but allow them to fail
# http://docs.travis-ci.com/user/build-configuration/#Rows-That-are-Allowed-To-Fail
matrix:
fast_finish: true
allow_failures:
- php: hhvm-nightly
- php: hhvm
- php: 7.0

# faster builds on new travis setup not using sudo
sudo: false
services:
- redis-server

# cache vendor dirs
cache:
Expand All @@ -29,11 +22,14 @@ cache:

install:
- travis_retry composer self-update && composer --version
- travis_retry composer global require "fxp/composer-asset-plugin:~1.0.0"
- travis_retry composer global require "fxp/composer-asset-plugin:~1.1.1"
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- travis_retry composer install --prefer-dist --no-interaction

before_script:
# install phpredis extension.
- sh -c "git clone https://github.com/nicolasff/phpredis/ && cd phpredis && phpize && ./configure && make && sudo make install " > /dev/null
- echo "extension=redis.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
- |
if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
PHPUNIT_FLAGS="--coverage-clover=coverage.clover"
Expand Down
58 changes: 39 additions & 19 deletions ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,11 @@ public function all($db = null)
{
// TODO add support for orderBy
$data = $this->executeScript($db, 'All');
$rows = [];
foreach ($data as $dataRow) {
$row = [];
$c = count($dataRow);
for ($i = 0; $i < $c;) {
$row[$dataRow[$i++]] = $dataRow[$i++];
}

$rows[] = $row;
if (empty($data)) {
return [];
}
$rows = $this->parseList($data);

if (!empty($rows)) {
$models = $this->createModels($rows);
if (!empty($this->with)) {
Expand Down Expand Up @@ -158,11 +153,8 @@ public function one($db = null)
if (empty($data)) {
return null;
}
$row = [];
$c = count($data);
for ($i = 0; $i < $c;) {
$row[$data[$i++]] = $data[$i++];
}
$row = $this->parseRow($data);

if ($this->asArray) {
$model = $row;
} else {
Expand Down Expand Up @@ -307,7 +299,7 @@ public function scalar($attribute, $db = null)
* @param string $type the type of the script to generate
* @param string $columnName
* @throws NotSupportedException
* @return array|bool|null|string
* @return array|boolean|null|string
*/
protected function executeScript($db, $type, $columnName = null)
{
Expand Down Expand Up @@ -347,7 +339,10 @@ protected function executeScript($db, $type, $columnName = null)
}

// convert inCondition for one key
if (is_array($this->where) && isset($this->where[0]) && $this->where[0] == 'in' && count($this->where[1]) == 1) {
if (is_array($this->where) && isset($this->where[0]) && $this->where[0] == 'in' && count(
$this->where[1]
) == 1
) {
$this->where = [current($this->where[1]) => $this->where[2]];
}

Expand All @@ -359,7 +354,7 @@ protected function executeScript($db, $type, $columnName = null)
$method = 'build' . $type;
$script = $db->getLuaScriptBuilder()->$method($this, $columnName);

return $db->executeCommand('EVAL', [$script, 0]);
return $db->executeCommand('EVAL', [$script]);
}

/**
Expand All @@ -368,14 +363,14 @@ protected function executeScript($db, $type, $columnName = null)
* If this parameter is not given, the `db` application component will be used.
* @param string $type the type of the script to generate
* @param string $columnName
* @return array|bool|null|string
* @return array|boolean|null|string
* @throws \yii\base\InvalidParamException
* @throws \yii\base\NotSupportedException
*/
private function findByPk($db, $type, $columnName = null)
{
if (count($this->where) == 1) {
$pks = (array) reset($this->where);
$pks = (array)reset($this->where);
} else {
foreach ($this->where as $values) {
if (is_array($values)) {
Expand Down Expand Up @@ -488,4 +483,29 @@ private function findByPk($db, $type, $columnName = null)
}
throw new InvalidParamException('Unknown fetch type: ' . $type);
}

private function parseList($data)
{
$result = [];
foreach ($data as $list) {
$result[] = $this->parseRow($list);
}

return $result;
}

private function parseRow($row)
{
if (!isset($row[0])) {
return $row;
}

$result = [];
$c = count($row);

for ($i = 0; $i < $c;) {
$result[$row[$i++]] = $row[$i++];
}
return $result;
}
}
32 changes: 14 additions & 18 deletions ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ActiveRecord extends BaseActiveRecord
*/
public static function getDb()
{
return \Yii::$app->get('redis');
return Yii::$app->get('redis');
}

/**
Expand Down Expand Up @@ -126,20 +126,19 @@ public function insert($runValidation = true, $attributes = null)

$key = static::keyPrefix() . ':a:' . static::buildKey($pk);
// save attributes
$setArgs = [$key];
$setArgs = [];
foreach ($values as $attribute => $value) {
// only insert attributes that are not null
if ($value !== null) {
if (is_bool($value)) {
$value = (int) $value;
}
$setArgs[] = $attribute;
$setArgs[] = $value;
$setArgs[$attribute] = $value;
}
}

if (count($setArgs) > 1) {
$db->executeCommand('HMSET', $setArgs);
$db->executeCommand('HMSET', [$key, $setArgs]);
}

$changedAttributes = array_fill_keys(array_keys($values), null);
Expand Down Expand Up @@ -174,8 +173,6 @@ public static function updateAll($attributes, $condition = null)
$pk = static::buildKey($pk);
$key = static::keyPrefix() . ':a:' . $pk;
// save attributes
$delArgs = [$key];
$setArgs = [$key];
foreach ($attributes as $attribute => $value) {
if (isset($newPk[$attribute])) {
$newPk[$attribute] = $value;
Expand All @@ -184,33 +181,32 @@ public static function updateAll($attributes, $condition = null)
if (is_bool($value)) {
$value = (int) $value;
}
$setArgs[] = $attribute;
$setArgs[] = $value;
$setArgs[$attribute] = $value;
} else {
$delArgs[] = $attribute;
$delArgs = $attribute;
}
}
$newPk = static::buildKey($newPk);
$newKey = static::keyPrefix() . ':a:' . $newPk;
// rename index if pk changed
if ($newPk != $pk) {
$db->executeCommand('MULTI');
if (count($setArgs) > 1) {
$db->executeCommand('HMSET', $setArgs);
if (!empty($setArgs)) {
$db->executeCommand('HMSET', [$key, $setArgs]);
}
if (count($delArgs) > 1) {
$db->executeCommand('HDEL', $delArgs);
if (!empty($delArgs)) {
$db->executeCommand('HDEL', [$key, $delArgs]);
}
$db->executeCommand('HSET', [static::keyPrefix(), $newPk, 0]);
$db->executeCommand('HDEL', [static::keyPrefix(), $pk]);
$db->executeCommand('RENAME', [$key, $newKey]);
$db->executeCommand('EXEC');
} else {
if (count($setArgs) > 1) {
$db->executeCommand('HMSET', $setArgs);
if (!empty($setArgs)) {
$db->executeCommand('HMSET', [$key, $setArgs]);
}
if (count($delArgs) > 1) {
$db->executeCommand('HDEL', $delArgs);
if (!empty($delArgs)) {
$db->executeCommand('HDEL', [$key, $delArgs]);
}
}
$n++;
Expand Down
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
Yii Framework 2 redis extension Change Log
==========================================

2.0.5 under development
2.0.6 under development
-----------------------

- Chg #14: Added missing `BLPOP` to `$redisCommands` (samdark)
- no changes in this release.


2.0.5 March 17, 2016
--------------------

- Bug #22: Fixed string escaping issue in LuaScriptBuilder (vistart)
- Bug #37: Fixed detection of open socket (mirocow)
- Bug #46: Fixed bug to execute session_regenerate_id in PHP 7.0 (githubjeka)
- Enh #31: Added `Connection::$socketClientFlags` property for connection flags to be passed to `stream_socket_client()` (hugh-lee)
- Chg #14: Added missing `BLPOP` command to `$redisCommands` (samdark)
- Chg #61: Added missing `GEO*` commands to `$redisCommands` (leadermt)


2.0.4 May 10, 2015
Expand Down
Loading

0 comments on commit 50d4d3b

Please sign in to comment.