Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

core_config_data handler for Magento 2 #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Est/Handler/Magento2/AbstractDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* abstract Magento 2 database handler class
*
* @author Christoph Frenes <[email protected]>
*/
abstract class Est_Handler_Magento2_AbstractDatabase extends Est_Handler_Magento_AbstractDatabase
{
/**
* Read database connection parameters from env.php file
*
* @return array
* @throws Exception
*/
protected function _getDatabaseConnectionParameters()
{
$dbCredentialFile = 'app/etc/env.php';

if (!is_file($dbCredentialFile)) {
throw new Exception(sprintf('File "%s" not found', $dbCredentialFile));
}

$config = (include $dbCredentialFile);

if (!is_array($config) || empty($config['db'])) {
throw new Exception(sprintf('DB credentials not found in file %s', $dbCredentialFile));
}

$db = $config['db'];
$credentials = $db['connection']['default'];
$this->_tablePrefix = (string)$db['table_prefix'];

return array(
'host' => (string)$credentials['host'],
'database' => (string)$credentials['dbname'],
'username' => (string)$credentials['username'],
'password' => (string)$credentials['password']
);
}
}
153 changes: 153 additions & 0 deletions Est/Handler/Magento2/CoreConfigData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

/**
* Parameters
*
* - scope
* - scopeId
* - path
*/
class Est_Handler_Magento2_CoreConfigData extends Est_Handler_Magento2_AbstractDatabase
{
/**
* Protected method that actually applies the settings. This method is implemented in the inheriting classes and
* called from ->apply
*
* @throws Exception
* @return bool
*/
protected function _apply()
{
$this->_checkIfTableExists('core_config_data');

$scope = $this->param1;
$scopeId = $this->param2;
$path = $this->param3;

$sqlParameters = $this->_getSqlParameters($scope, $scopeId, $path);
$containsPlaceholder = $this->_containsPlaceholder($sqlParameters);
$action = self::ACTION_NO_ACTION;

if (strtolower(trim($this->value)) == '--delete--') {
$action = self::ACTION_DELETE;
} else {
$query = 'SELECT `value` FROM `' . $this->_tablePrefix . 'core_config_data` WHERE `scope` LIKE :scope AND `scope_id` LIKE :scopeId AND `path` LIKE :path';
$firstRow = $this->_getFirstRow($query, $sqlParameters);

if ($containsPlaceholder) {
// scope, scope_id or path contains '%' char - we can't build an insert query, only update is possible
if ($firstRow === false) {
$this->addMessage(
new Est_Message('Trying to update using placeholders but no rows found in the db', Est_Message::SKIPPED)
);
} else {
$action = self::ACTION_UPDATE;
}
} else {
if ($firstRow === false) {
$action = self::ACTION_INSERT;
} elseif ($firstRow['value'] == $this->value) {
$this->addMessage(
new Est_Message(sprintf('Value "%s" is already in place. Skipping.', $firstRow['value']), Est_Message::SKIPPED)
);
} else {
$action = self::ACTION_UPDATE;
}
}
}

switch ($action) {
case self::ACTION_DELETE:
$query = 'DELETE FROM `' . $this->_tablePrefix . 'core_config_data` WHERE `scope` LIKE :scope AND `scope_id` LIKE :scopeId AND `path` LIKE :path';
$this->_processDelete($query, $sqlParameters);
break;
case self::ACTION_INSERT:
$sqlParameters[':value'] = $this->value;
$query = 'INSERT INTO `' . $this->_tablePrefix . 'core_config_data` (`scope`, `scope_id`, `path`, value) VALUES (:scope, :scopeId, :path, :value)';
$this->_processInsert($query, $sqlParameters);
break;
case self::ACTION_UPDATE:
$sqlParameters[':value'] = $this->value;
$query = 'UPDATE `' . $this->_tablePrefix . 'core_config_data` SET `value` = :value WHERE `scope` LIKE :scope AND `scope_id` LIKE :scopeId AND `path` LIKE :path';
$this->_processUpdate($query, $sqlParameters, $firstRow['value']);
break;
case self::ACTION_NO_ACTION;
default:
break;
}

$this->destroyDb();

return true;
}

/**
* Protected method that actually extracts the settings. This method is implemented in the inheriting classes and
* called from ->extract and only echos constructed csv
*/
protected function _extract()
{
$this->_checkIfTableExists('core_config_data');

$scope = $this->param1;
$scopeId = $this->param2;
$path = $this->param3;

$sqlParameters = $this->_getSqlParameters($scope, $scopeId, $path);

$query = 'SELECT scope, scope_id, path, value FROM `' . $this->_tablePrefix
. 'core_config_data` WHERE `scope` LIKE :scope AND `scope_id` LIKE :scopeId AND `path` LIKE :path';

return $this->_outputQuery($query, $sqlParameters);
}

/**
* Constructs the sql parameters
*
* @param string $scope
* @param string $scopeId
* @param string $path
* @return array
* @throws Exception
*/
protected function _getSqlParameters($scope, $scopeId, $path)
{
if (empty($scope)) {
throw new Exception("No scope found");
}
if (is_null($scopeId)) {
throw new Exception("No scopeId found");
}
if (empty($path)) {
throw new Exception("No path found");
}

if (!in_array($scope, array('default', 'stores', 'websites', '%'))) {
throw new Exception("Scope must be 'default', 'stores', 'websites', or '%'");
}

if ($scope == 'default') {
$scopeId = 0;
}

if ($scope == 'stores' && !is_numeric($scopeId)) {
// do a store code lookup
$code = $scopeId;
$scopeId = $this->_getStoreIdFromCode($code);
$this->addMessage(new Est_Message("Found store id '$scopeId' for code '$code'", Est_Message::INFO));
}

if ($scope == 'websites' && !is_numeric($scopeId)) {
// do a website code lookup
$code = $scopeId;
$scopeId = $this->_getWebsiteIdFromCode($code);
$this->addMessage(new Est_Message("Found website id '$scopeId' for code '$code'", Est_Message::INFO));
}

return array(
':scope' => $scope,
':scopeId' => $scopeId,
':path' => $path
);
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Example
* Param2: XPath
* Param3: not used

* **Est_Handler_Magento_CoreConfigData**: Changes values of core_config_data table in a Magento instance. It reads its database parameters from app/etc/local.xml - therefore it needs to be placed after any adjustments of DB credentials.
* **Est_Handler_Magento_CoreConfigData** or **Est_Handler_Magento2_CoreConfigData**: Changes values of core_config_data table in a Magento 1 or 2 instance. It reads its database parameters from app/etc/local.xml or app/etc/env.php - therefore it needs to be placed after any adjustments of DB credentials.

* Param1: scope ('default', 'stores', 'websites', or '%')
* Param2: scopeid (store id, store code, website id, website code, 0 for default scope or '%')
Expand Down