-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42d60fa
commit 8ea7fda
Showing
8 changed files
with
322 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace yii1tech\config; | ||
|
||
use CApplication; | ||
use CBehavior; | ||
use CEvent; | ||
use CLogger; | ||
use LogicException; | ||
use Yii; | ||
|
||
/** | ||
* Configures (re-configures) application using data acquired from configuration manager. | ||
* | ||
* Application configuration example: | ||
* | ||
* ```php | ||
* [ | ||
* 'behaviors' => [ | ||
* 'configFromManagerBehavior' => [ | ||
* 'class' => yii1tech\config\ConfiguresAppFromConfigManager::class, | ||
* ], | ||
* // ... | ||
* ], | ||
* 'components' => [ | ||
* 'appConfigManager' => [ | ||
* 'class' => yii1tech\config\Manager::class, | ||
* 'items' => [ | ||
* // ... | ||
* ], | ||
* ], | ||
* // ... | ||
* ], | ||
* // ... | ||
* ]; | ||
* ``` | ||
* | ||
* @see \yii1tech\config\Manager | ||
* | ||
* @mixin \CApplication | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
*/ | ||
class ConfiguresAppFromConfigManager extends CBehavior | ||
{ | ||
/** | ||
* @var \yii1tech\config\Manager|string config manager instance or its ID inside application components. | ||
*/ | ||
public $configManager = 'appConfigManager'; | ||
|
||
/** | ||
* @param \CApplication $app application instance to be configured. | ||
* @return \yii1tech\config\Manager config manager instance. | ||
* @throws \LogicException on failure | ||
*/ | ||
protected function getConfigManager(CApplication $app) | ||
{ | ||
if (is_object($this->configManager)) { | ||
return $this->configManager; | ||
} | ||
|
||
$configManager = $app->getComponent($this->configManager); | ||
if (!is_object($configManager)) { | ||
throw new LogicException('Application component "' . $this->configManager . '" is missing.'); | ||
} | ||
|
||
return $configManager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function events(): array | ||
{ | ||
return [ | ||
'onBeginRequest' => 'beginRequest', | ||
]; | ||
} | ||
|
||
/** | ||
* This event raises before {@link CApplication}. | ||
* It update {@link CApplication::params} with database data. | ||
* @param CEvent $event event object. | ||
*/ | ||
public function beginRequest(CEvent $event): void | ||
{ | ||
$this->configureApplication($event->sender); | ||
} | ||
|
||
/** | ||
* Updates application configuration from config manager. | ||
* @param \CApplication $app application instance to be configured. | ||
*/ | ||
protected function configureApplication(CApplication $app): void | ||
{ | ||
try { | ||
$app->configure($this->getConfigManager($app)->fetchConfig()); | ||
} catch (\Exception $exception) { | ||
// application can be run before the persistent storage is ready, for example: before the first DB migration applied | ||
Yii::log( | ||
'"' . get_class($this) . '" is unable to update application configuration from config manager:' . $exception->getMessage(), | ||
CLogger::LEVEL_WARNING | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace yii1tech\config; | ||
|
||
/** | ||
* StorageArray uses internal array for the config storage. | ||
* | ||
* This class can be useful in unit tests. | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
*/ | ||
class StorageArray extends Storage | ||
{ | ||
/** | ||
* @var array stored data. | ||
*/ | ||
protected $data = []; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function save(array $values): bool | ||
{ | ||
$this->data = array_merge($this->data, $values); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get(): array | ||
{ | ||
return $this->data; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clear(): bool | ||
{ | ||
$this->data = []; | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace yii1tech\config\test; | ||
|
||
use Yii; | ||
use yii1tech\config\ConfiguresAppFromConfigManager; | ||
use yii1tech\config\Manager; | ||
use yii1tech\config\StorageArray; | ||
|
||
class ConfiguresAppFromConfigManagerTest extends TestCase | ||
{ | ||
protected function createConfigManager(): Manager | ||
{ | ||
return (new Manager()) | ||
->setStorage($this->createStorage()) | ||
->setItems([ | ||
'appName' => [ | ||
'path' => 'name', | ||
], | ||
'dateFormat' => [ | ||
'path' => 'components.format.dateFormat', | ||
], | ||
'existingParam' => [ | ||
'path' => 'params.param1', | ||
], | ||
'newParam' => [ | ||
'path' => 'params.paramNew', | ||
], | ||
]); | ||
} | ||
|
||
protected function createStorage(): StorageArray | ||
{ | ||
$storage = new StorageArray(); | ||
|
||
$storage->save([ | ||
'appName' => 'app-name-override', | ||
'dateFormat' => 'date-format-override', | ||
'existingParam' => 'param1-override', | ||
'newParam' => 'param-new-value', | ||
]); | ||
|
||
return $storage; | ||
} | ||
|
||
public function testConfigureApplication(): void | ||
{ | ||
$app = Yii::app(); | ||
|
||
$app->setComponent('appConfigManager', $this->createConfigManager()); | ||
|
||
$behavior = new ConfiguresAppFromConfigManager(); | ||
|
||
$app->attachBehavior('test', $behavior); | ||
|
||
$app->run(); | ||
|
||
$this->assertSame('app-name-override', $app->name); | ||
|
||
$this->assertSame('param1-override', $app->params['param1']); | ||
$this->assertSame('param2-value', $app->params['param2']); | ||
$this->assertSame('param-new-value', $app->params['paramNew']); | ||
|
||
$this->assertSame('date-format-override', $app->getFormat()->dateFormat); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace yii1tech\config\test; | ||
|
||
class DummyApplication extends \CApplication | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function processRequest() | ||
{ | ||
// do nothing | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace yii1tech\config\test; | ||
|
||
use yii1tech\config\StorageArray; | ||
|
||
class StorageArrayTest extends TestCase | ||
{ | ||
/** | ||
* @var \Illuminatech\Config\StorageDb test storage. | ||
*/ | ||
protected $storage; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->storage = new StorageArray(); | ||
} | ||
|
||
public function testSave() | ||
{ | ||
$values = [ | ||
'test.name' => 'Test name', | ||
'test.title' => 'Test title', | ||
]; | ||
|
||
$this->storage->save($values); | ||
|
||
$returnedValues = $this->storage->get(); | ||
|
||
$this->assertEquals($values, $returnedValues); | ||
} | ||
|
||
/** | ||
* @depends testSave | ||
*/ | ||
public function testUpdate() | ||
{ | ||
$values = [ | ||
'test.name' => 'Origin name', | ||
'test.title' => 'Origin title', | ||
]; | ||
|
||
$this->storage->save($values); | ||
|
||
$this->storage->save([ | ||
'test.title' => 'Updated title' | ||
]); | ||
|
||
$returnedValues = $this->storage->get(); | ||
|
||
$this->assertSame('Updated title', $returnedValues['test.title']); | ||
$this->assertSame('Origin name', $returnedValues['test.name']); | ||
} | ||
|
||
/** | ||
* @depends testSave | ||
*/ | ||
public function testClear() | ||
{ | ||
$values = [ | ||
'test.name' => 'Test name', | ||
'test.title' => 'Test title', | ||
]; | ||
|
||
$this->storage->save($values); | ||
$this->storage->clear(); | ||
|
||
$this->assertEmpty($this->storage->get()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters