mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 06:15:19 +08:00
Added yii\di\Instance::__set_state()
method
This commit is contained in:
@ -40,6 +40,7 @@ Yii Framework 2 Change Log
|
||||
- Enh #13650: Improved `yii\base\Security::hkdf()` to take advantage of native `hash_hkdf()` implementation in PHP >= 7.1.2 (charlesportwoodii)
|
||||
- Bug #13379: Fixed `applyFilter` function in `yii.gridView.js` to work correctly when params in `filterUrl` are indexed (SilverFire)
|
||||
- Bug #13670: Fixed alias option from console when it includes `-` or `_` in option name (pana1990)
|
||||
- Enh: Added `yii\di\Instance::__set_state()` method to restore object after serialization using `var_export()` function (silvefire)
|
||||
|
||||
|
||||
2.0.11.2 February 08, 2017
|
||||
|
@ -161,4 +161,22 @@ class Instance
|
||||
return Yii::$container->get($this->id);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Restores class state after using `var_export()`
|
||||
*
|
||||
* @param array $state
|
||||
* @return Instance
|
||||
* @throws InvalidConfigException when $state property does not contain `id` parameter
|
||||
* @see var_export()
|
||||
* @since 2.0.12
|
||||
*/
|
||||
public static function __set_state($state)
|
||||
{
|
||||
if (!isset($state['id'])) {
|
||||
throw new InvalidConfigException('Failed to instantiate class "Instance". Required parameter "id" is missing');
|
||||
}
|
||||
|
||||
return new self($state['id']);
|
||||
}
|
||||
}
|
||||
|
@ -156,4 +156,31 @@ class InstanceTest extends TestCase
|
||||
$this->assertInstanceOf('yii\db\Connection', $db = $cache->db);
|
||||
$this->assertEquals('sqlite:path/to/file.db', $db->dsn);
|
||||
}
|
||||
|
||||
public function testRestoreAfterVarExport()
|
||||
{
|
||||
$instance = Instance::of('something');
|
||||
$export = var_export($instance, true);
|
||||
|
||||
$this->assertEquals(<<<'PHP'
|
||||
yii\di\Instance::__set_state(array(
|
||||
'id' => 'something',
|
||||
))
|
||||
PHP
|
||||
, $export);
|
||||
|
||||
$this->assertEquals($instance, Instance::__set_state([
|
||||
'id' => 'something',
|
||||
]));
|
||||
}
|
||||
|
||||
public function testRestoreAfterVarExportRequiresId()
|
||||
{
|
||||
$this->setExpectedException(
|
||||
'yii\base\InvalidConfigException',
|
||||
'Failed to instantiate class "Instance". Required parameter "id" is missing'
|
||||
);
|
||||
|
||||
Instance::__set_state([]);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user