-
-
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
8ea7fda
commit 08a9459
Showing
8 changed files
with
181 additions
and
54 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
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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace yii1tech\config; | ||
|
||
use CDbCriteria; | ||
use Yii; | ||
|
||
/** | ||
|
@@ -24,7 +25,8 @@ | |
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
*/ | ||
class StorageDb extends Storage { | ||
class StorageDb extends Storage | ||
{ | ||
/** | ||
* @var string id of the database connection application component. | ||
*/ | ||
|
@@ -33,6 +35,14 @@ class StorageDb extends Storage { | |
* @var string name of the table, which should store values. | ||
*/ | ||
public $table = 'app_config'; | ||
/** | ||
* @var string name of the column, which should store config item key. | ||
*/ | ||
public $keyColumn = 'id'; | ||
/** | ||
* @var string name of the column, which should store config item value. | ||
*/ | ||
public $valueColumn = 'value'; | ||
|
||
/** | ||
* @return \CDbConnection database connection application component. | ||
|
@@ -47,22 +57,36 @@ public function getDbConnection() | |
*/ | ||
public function save(array $values): bool | ||
{ | ||
$this->clear(); | ||
|
||
$data = []; | ||
foreach ($values as $id => $value) { | ||
$data[] = array( | ||
'id' => $id, | ||
'value' => $value, | ||
); | ||
} | ||
$existingValues = $this->get(); | ||
|
||
$insertedRowsCount = $this->getDbConnection() | ||
->getCommandBuilder() | ||
->createMultipleInsertCommand($this->table, $data) | ||
->execute(); | ||
foreach ($values as $key => $value) { | ||
if (array_key_exists($key, $existingValues)) { | ||
if ($value === $existingValues[$key]) { | ||
continue; | ||
} | ||
|
||
$criteria = new CDbCriteria(); | ||
$criteria->addColumnCondition([$this->keyColumn => $key]); | ||
$data = [$this->valueColumn => $value]; | ||
|
||
$this->getDbConnection() | ||
->getCommandBuilder() | ||
->createUpdateCommand($this->table, $data, $criteria) | ||
->execute(); | ||
} else { | ||
$data = [ | ||
$this->keyColumn => $key, | ||
$this->valueColumn => $value, | ||
]; | ||
|
||
$this->getDbConnection() | ||
->getCommandBuilder() | ||
->createInsertCommand($this->table, $data) | ||
->execute(); | ||
} | ||
} | ||
|
||
return (count($values) == $insertedRowsCount); | ||
return true; | ||
} | ||
|
||
/** | ||
|
@@ -76,7 +100,7 @@ public function get(): array | |
|
||
$values = []; | ||
foreach ($rows as $row) { | ||
$values[$row['id']] = $row['value']; | ||
$values[$row[$this->keyColumn]] = $row[$this->valueColumn]; | ||
} | ||
|
||
return $values; | ||
|
@@ -87,7 +111,24 @@ public function get(): array | |
*/ | ||
public function clear(): bool | ||
{ | ||
$this->getDbConnection()->createCommand()->delete($this->table); | ||
$this->getDbConnection() | ||
->createCommand() | ||
->delete($this->table); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function clearValue($key): bool | ||
{ | ||
$criteria = new CDbCriteria(); | ||
$criteria->addColumnCondition([$this->keyColumn => $key]); | ||
|
||
$this->getDbConnection() | ||
->createCommand() | ||
->delete($this->table, $criteria->condition, $criteria->params); | ||
|
||
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 |
---|---|---|
|
@@ -12,7 +12,8 @@ | |
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
*/ | ||
class StoragePhp extends Storage { | ||
class StoragePhp extends Storage | ||
{ | ||
/** | ||
* @var string name of the file, which should be used to store values. | ||
*/ | ||
|
@@ -55,17 +56,9 @@ protected function defaultFileName(): string | |
*/ | ||
public function save(array $values): bool | ||
{ | ||
$this->clear(); | ||
$values = array_merge($this->get(), $values); | ||
|
||
$fileName = $this->getFileName(); | ||
|
||
$dirName = dirname($fileName); | ||
if (!file_exists($dirName)) { | ||
mkdir($dirName, 0777, true); | ||
} | ||
|
||
$bytesWritten = file_put_contents($fileName, $this->composeFileContent($values)); | ||
$this->invalidateScriptCache($fileName); | ||
$bytesWritten = $this->saveToFile($values); | ||
|
||
return ($bytesWritten > 0); | ||
} | ||
|
@@ -98,6 +91,43 @@ public function clear(): bool | |
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clearValue($key): bool | ||
{ | ||
$values = $this->get(); | ||
if (empty($values)) { | ||
return true; | ||
} | ||
|
||
unset($values[$key]); | ||
|
||
$bytesWritten = $this->saveToFile($values); | ||
|
||
return ($bytesWritten > 0); | ||
} | ||
|
||
/** | ||
* Saves given values to the file. | ||
* @param array $values values to be saved. | ||
* @return int number of bytes written. | ||
*/ | ||
protected function saveToFile(array $values) | ||
{ | ||
$fileName = $this->getFileName(); | ||
|
||
$dirName = dirname($fileName); | ||
if (!file_exists($dirName)) { | ||
mkdir($dirName, 0777, true); | ||
} | ||
|
||
$bytesWritten = file_put_contents($fileName, $this->composeFileContent($values)); | ||
$this->invalidateScriptCache($fileName); | ||
|
||
return $bytesWritten; | ||
} | ||
|
||
/** | ||
* Composes file content for the given values. | ||
* @param array $values values to be saved. | ||
|
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
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
Oops, something went wrong.