|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | + |
| 5 | +namespace PCF\MagentoMigration\Command\Phinx; |
| 6 | + |
| 7 | +use Magento\Framework\App\DeploymentConfig; |
| 8 | +use Magento\Framework\Config\ConfigOptionsListConstants; |
| 9 | +use Magento\Framework\Exception\LocalizedException; |
| 10 | +use PCF\MagentoMigration\Api\ConfigBuilderInterface; |
| 11 | +use PCF\MagentoMigration\Api\PathLocatorInterface; |
| 12 | +use Symfony\Component\Yaml\Dumper; |
| 13 | + |
| 14 | +class ConfigBuilder implements ConfigBuilderInterface |
| 15 | +{ |
| 16 | + /** @var DeploymentConfig */ |
| 17 | + protected $deploymentConfig; |
| 18 | + |
| 19 | + /** @var PathLocatorInterface */ |
| 20 | + protected $pathLocator; |
| 21 | + |
| 22 | + /** @var Dumper */ |
| 23 | + protected $yamlDumper; |
| 24 | + |
| 25 | + protected $defaultDb = 'default'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @param DeploymentConfig $deploymentConfig |
| 29 | + * @param PathLocatorInterface $pathLocator |
| 30 | + * @param Dumper $yamlDumper |
| 31 | + */ |
| 32 | + public function __construct( |
| 33 | + DeploymentConfig $deploymentConfig, |
| 34 | + PathLocatorInterface $pathLocator, |
| 35 | + Dumper $yamlDumper |
| 36 | + ) { |
| 37 | + $this->deploymentConfig = $deploymentConfig; |
| 38 | + $this->pathLocator = $pathLocator; |
| 39 | + $this->yamlDumper = $yamlDumper; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @inheritdoc |
| 44 | + */ |
| 45 | + public function createConfigPath() :string |
| 46 | + { |
| 47 | + $yml = $this->yamlDumper->dump($this->getConfigArray(), 5); |
| 48 | + $tmpFileName = tempnam(sys_get_temp_dir(), md5($yml)); |
| 49 | + file_put_contents($tmpFileName, $yml); |
| 50 | + |
| 51 | + return $tmpFileName; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @param string $defaultDb |
| 56 | + */ |
| 57 | + protected function setDefaultDb(string $defaultDb) |
| 58 | + { |
| 59 | + $this->defaultDb = $defaultDb; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @return array |
| 64 | + */ |
| 65 | + protected function getConfigArray(): array |
| 66 | + { |
| 67 | + return [ |
| 68 | + 'paths' => [ 'migrations' => $this->pathLocator->getAllMigrationDirs()], |
| 69 | + 'environments' => $this->getEnvironments() |
| 70 | + ]; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @throws LocalizedException |
| 75 | + * todo: make port and charset configurable |
| 76 | + */ |
| 77 | + protected function getDbConfig() |
| 78 | + { |
| 79 | + $magentoConfig = $this->deploymentConfig->getConfigData(ConfigOptionsListConstants::CONFIG_PATH_DB); |
| 80 | + |
| 81 | + if (empty($magentoConfig['connection'])) { |
| 82 | + throw new LocalizedException(__('no db connection given')); |
| 83 | + } |
| 84 | + |
| 85 | + $connections = []; |
| 86 | + foreach ($magentoConfig['connection'] as $envName => $config) { |
| 87 | + $connections[$envName] = [ |
| 88 | + 'adapter' => 'mysql', |
| 89 | + 'host' => $config['host'], |
| 90 | + 'name' => $config['dbname'], |
| 91 | + 'user' => $config['username'], |
| 92 | + 'pass' => $config['password'], |
| 93 | + 'port' => 3306, |
| 94 | + 'charset' => 'utf8' |
| 95 | + ]; |
| 96 | + } |
| 97 | + |
| 98 | + if (empty($magentoConfig['connection']['default'])) { |
| 99 | + $this->setDefaultDb(key($magentoConfig['connection'])); |
| 100 | + } |
| 101 | + |
| 102 | + return $connections; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return array |
| 107 | + */ |
| 108 | + protected function getEnvironments() : array |
| 109 | + { |
| 110 | + $return = [ |
| 111 | + 'default_migration_table' => 'phinxlog', |
| 112 | + 'default_database' => $this->defaultDb, |
| 113 | + ]; |
| 114 | + |
| 115 | + return array_merge($return, $this->getDbConfig()); |
| 116 | + } |
| 117 | +} |
0 commit comments