Created
January 10, 2018 14:33
-
-
Save KevinMace/214d46b3505739120a908b6be13c8fd1 to your computer and use it in GitHub Desktop.
Magento 2 console command to find any configuration values missing a default/global value and sets them to null. This is a fix for issue #12097
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
<?php | |
/** | |
* @author Kevin Mace (kevin.mace@sotechnology.co.uk). All rights reserved. | |
*/ | |
namespace KevinMace\Framework\Console; | |
use Symfony\Component\Console\Command\Command; | |
use Magento\Framework\App\Filesystem\DirectoryList; | |
use Magento\Framework\Filesystem\DriverPool; | |
use Symfony\Component\Console\Input\InputArgument; | |
/** | |
* Repair Default Config Command | |
* | |
* Finds any config values missing a default/global value and sets | |
* them to null. This is a fix for issue #12097 | |
*/ | |
class RepairDefaultConfigCommand extends Command | |
{ | |
/** | |
* @var \Symfony\Component\Console\Output\OutputInterface $output | |
*/ | |
protected $output; | |
/** | |
* @var \Symfony\Component\Console\Input\InputInterface $input | |
*/ | |
protected $input; | |
/** | |
* @var \Magento\Store\Model\Store | |
*/ | |
protected $store; | |
/** | |
* @var \Magento\Store\Model\StoreManagerInterface $storeManager | |
*/ | |
protected $storeManager; | |
/** | |
* @var ConfigFactory | |
*/ | |
protected $ConfigFactory; | |
/** | |
* @var ConfigCollectionFactory | |
*/ | |
protected $ConfigCollectionFactory; | |
/** | |
* @param \Magento\Store\Model\StoreManagerInterface $storeManager | |
*/ | |
public function __construct( | |
\Magento\Store\Model\StoreManagerInterface $storeManager, | |
\Magento\Config\Model\Config\Factory $configFactory, | |
\Magento\Config\Model\ResourceModel\Config\Data\CollectionFactory $configCollectionFactory | |
) { | |
$this->storeManager = $storeManager; | |
$this->configFactory = $configFactory; | |
$this->configCollectionFactory = $configCollectionFactory; | |
parent::__construct(); | |
} | |
/** | |
* Set the command name and description | |
*/ | |
protected function configure() | |
{ | |
$this->setName('kevinmace:config:repair') | |
->setDescription( | |
'Finds any config values missing a default value and sets them to null. This is a fix for issue #12097' | |
); | |
} | |
/** | |
* Fired when the command is run | |
* | |
* @param \Symfony\Component\Console\Input\InputInterface $input, | |
* @param \Symfony\Component\Console\Output\OutputInterface $output | |
* | |
* @retun void | |
*/ | |
protected function execute( | |
\Symfony\Component\Console\Input\InputInterface $input, | |
\Symfony\Component\Console\Output\OutputInterface $output | |
) { | |
$this->output = $output; | |
$this->input = $input; | |
$paths = []; | |
$globalPaths = []; | |
$configCollection = $this->configCollectionFactory | |
->create() | |
->addFieldToFilter('scope', ['neq' => 'default']); | |
foreach ($configCollection as $config) { | |
if (!in_array($config->getPath(), $paths)) { | |
$paths[] = $config->getPath(); | |
} | |
} | |
$globalConfigCollection = $this->configCollectionFactory | |
->create() | |
->addFieldToFilter('scope', 'default') | |
->addFieldToFilter('path', ['in' => $paths]); | |
foreach ($globalConfigCollection as $config) { | |
if (!in_array($config->getPath(), $globalPaths)) { | |
$globalPaths[] = $config->getPath(); | |
} | |
} | |
$missingPaths = array_diff($paths, $globalPaths); | |
$this->output->writeln('<comment>' . count($missingPaths) . ' missing entries found</comment>'); | |
if (count($missingPaths)) { | |
$this->output->writeln('<comment>Beginning process to add missing default entries</comment>'); | |
} | |
foreach ($missingPaths as $path) { | |
$config = $this->configFactory->create(); | |
$config->setDataByPath($path, null); | |
$config->save(); | |
$this->output->writeln('<info>Default entry created for: "' . $path . '"</info>'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment