Fixes #13254: Made yii\helpers\StringHelper and yii\validators\StringValidator independent of Yii::$app instance

This commit is contained in:
Carsten Brandt
2017-09-07 21:32:07 +02:00
committed by Alexander Makarov
parent 61996979db
commit 36b535c26b
5 changed files with 22 additions and 10 deletions

View File

@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.13 under development 2.0.13 under development
------------------------ ------------------------
- Enh #13254: Made `yii\helpers\StringHelper` and `yii\validators\StringValidator` independent of `Yii::$app` instance (cebe)
- Enh #14273: `yii\log\Target::$enabled` now supports callable value (dmirogin) - Enh #14273: `yii\log\Target::$enabled` now supports callable value (dmirogin)
- Bug #14773: Fixed `yii\widgets\ActiveField::$options` does not support 'class' option in array format (klimov-paul) - Bug #14773: Fixed `yii\widgets\ActiveField::$options` does not support 'class' option in array format (klimov-paul)
- Bug #14723: Fixed serialization of `yii\db\Connection` instance closes database connection (klimov-paul) - Bug #14723: Fixed serialization of `yii\db\Connection` instance closes database connection (klimov-paul)

View File

@ -104,12 +104,15 @@ class BaseStringHelper
*/ */
public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false) public static function truncate($string, $length, $suffix = '...', $encoding = null, $asHtml = false)
{ {
if ($encoding === null) {
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
}
if ($asHtml) { if ($asHtml) {
return static::truncateHtml($string, $length, $suffix, $encoding ?: Yii::$app->charset); return static::truncateHtml($string, $length, $suffix, $encoding);
} }
if (mb_strlen($string, $encoding ?: Yii::$app->charset) > $length) { if (mb_strlen($string, $encoding) > $length) {
return rtrim(mb_substr($string, 0, $length, $encoding ?: Yii::$app->charset)) . $suffix; return rtrim(mb_substr($string, 0, $length, $encoding)) . $suffix;
} }
return $string; return $string;
@ -152,7 +155,9 @@ class BaseStringHelper
protected static function truncateHtml($string, $count, $suffix, $encoding = false) protected static function truncateHtml($string, $count, $suffix, $encoding = false)
{ {
$config = \HTMLPurifier_Config::create(null); $config = \HTMLPurifier_Config::create(null);
$config->set('Cache.SerializerPath', \Yii::$app->getRuntimePath()); if (Yii::$app !== null) {
$config->set('Cache.SerializerPath', Yii::$app->getRuntimePath());
}
$lexer = \HTMLPurifier_Lexer::create($config); $lexer = \HTMLPurifier_Lexer::create($config);
$tokens = $lexer->tokenizeHTML($string, $config, new \HTMLPurifier_Context()); $tokens = $lexer->tokenizeHTML($string, $config, new \HTMLPurifier_Context());
$openTokens = []; $openTokens = [];
@ -215,9 +220,10 @@ class BaseStringHelper
} }
if ($caseSensitive) { if ($caseSensitive) {
return strncmp($string, $with, $bytes) === 0; return strncmp($string, $with, $bytes) === 0;
}
return mb_strtolower(mb_substr($string, 0, $bytes, '8bit'), Yii::$app->charset) === mb_strtolower($with, Yii::$app->charset); }
$encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
return mb_strtolower(mb_substr($string, 0, $bytes, '8bit'), $encoding) === mb_strtolower($with, $encoding);
} }
/** /**
@ -243,7 +249,8 @@ class BaseStringHelper
return substr_compare($string, $with, -$bytes, $bytes) === 0; return substr_compare($string, $with, -$bytes, $bytes) === 0;
} }
return mb_strtolower(mb_substr($string, -$bytes, mb_strlen($string, '8bit'), '8bit'), Yii::$app->charset) === mb_strtolower($with, Yii::$app->charset); $encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
return mb_strtolower(mb_substr($string, -$bytes, mb_strlen($string, '8bit'), '8bit'), $encoding) === mb_strtolower($with, $encoding);
} }
/** /**

View File

@ -82,7 +82,7 @@ class StringValidator extends Validator
$this->length = null; $this->length = null;
} }
if ($this->encoding === null) { if ($this->encoding === null) {
$this->encoding = Yii::$app->charset; $this->encoding = Yii::$app ? Yii::$app->charset : 'UTF-8';
} }
if ($this->message === null) { if ($this->message === null) {
$this->message = Yii::t('yii', '{attribute} must be a string.'); $this->message = Yii::t('yii', '{attribute} must be a string.');

View File

@ -19,7 +19,9 @@ class StringHelperTest extends TestCase
protected function setUp() protected function setUp()
{ {
parent::setUp(); parent::setUp();
$this->mockApplication();
// destroy application, Helper must work without Yii::$app
$this->destroyApplication();
} }
public function testStrlen() public function testStrlen()

View File

@ -19,7 +19,9 @@ class StringValidatorTest extends TestCase
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$this->mockApplication();
// destroy application, Validator must work without Yii::$app
$this->destroyApplication();
} }
public function testValidateValue() public function testValidateValue()