Source of file LoadReplacementData.php
Size: 6,605 Bytes - Last Modified: 2020-07-03T12:08:09+00:00
/var/www/docs.ssmods.com/process/src/src/Api/LoadReplacementData.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 | <?php namespace Sunnysideup\UpgradeToSilverstripe4\Api; use SilverStripe\Upgrader\Util\ConfigFile; /** * loads yml data if strings to replace in * code. * * The replacements should be in the same folder as this class. * * Alternatively, you can specify another folderContainingReplacementData in the * construct method. * * It will also search the root folders for any packages / projects being upgraded. * replacement data can be found in any path provided like this: * * PATH / toFolder / '.upgrade.replacements.yml' * * toFolders have names like: SS37 or SS4. */ use Sunnysideup\UpgradeToSilverstripe4\ModuleUpgrader; class LoadReplacementData { /** * Standard file name */ protected $ymlFileName = '.upgrade.replacements.yml'; /** * Module Object * @var ModuleUpgrader */ protected $myMu = null; /** * folder containing the replacement file * * @var string */ protected $folderContainingReplacementData = ''; /** * name of the sub-folder for the replacement data. * @var string */ protected $toFolders = 'SS4'; /** * array of replacements * @var array */ protected $fullArray = []; /** * @var array */ protected $languages = []; /** * @var array */ protected $flatFindArray = []; /** * @var array */ protected $flatReplacedArray = []; /** * path where to look for data. * @var array */ protected $paths = []; /** * @param ModuleUpgrader $mu * @param string $alternativeReplacementDataFolder * @param string $toFolder - the subfolder used for the specific replace` */ public function __construct($mu, $alternativeReplacementDataFolder = '', $toFolder = 'SS4') { $this->myMu = $mu; $this->folderContainingReplacementData = $alternativeReplacementDataFolder ?: $this->defaultLocation(); $this->toFolder = $toFolder; $this->compileFlatArray(); } public function setToFolder(string $s) { $this->toFolder = $s; return $this; } public function getReplacementArrays(): array { return $this->fullArray; } public function getLanguages() { return $this->languages; } public function getFlatFindArray(): array { return $this->flatFindArray; } public function getFlatReplacedArray(): array { return $this->flatReplacedArray; } protected function mu() { return $this->myMu; } protected function defaultLocation() { return $this->mu()->getLocationOfThisUpgrader() . DIRECTORY_SEPARATOR . 'ReplacementData'; } protected function compileFlatArray() { $this->fullArray = $this->getData(); $count = 0; foreach ($this->fullArray as $path => $pathArray) { foreach ($pathArray as $language => $languageArray) { $this->languages[$language] = $language; foreach ($languageArray as $findKey => $findKeyArray) { if (! isset($findKeyArray['R'])) { user_error('replacement key not set: ' . print_r($findKeyArray, true)); } $replaceKey = $findKeyArray['R']; $key = strtolower($language . '_' . $path . '_' . $count); $this->flatFindArray[$key] = $findKey; $this->flatReplacedArray[$key] = $replaceKey; $count++; } } } } /** * retrieve all replacements * * @return array */ protected function getData(): array { $this->getPaths(); // Merge with any other upgrade spec in the top level $config = []; foreach ($this->paths as $path) { $file = $path . DIRECTORY_SEPARATOR . $this->toFolder . DIRECTORY_SEPARATOR . $this->ymlFileName; if (file_exists($file)) { $nextConfig = ConfigFile::loadConfig($file); // Merge $config = $this->mergeConfig($config, $nextConfig); $this->mu()->colourPrint('loaded replacement file: ' . $file); } else { $this->mu()->colourPrint('could not find: ' . $file); } } ksort($config); return $config; } /** * returns a list of paths to be checked for replacement data. * * @return array */ protected function getPaths(): array { $array = []; foreach ($this->mu()->getExistingModuleDirLocations() as $moduleDir) { $array[$moduleDir] = $moduleDir; } $globalFixes = $this->mu()->checkIfPathExistsAndCleanItUp($this->folderContainingReplacementData); if ($globalFixes) { $array[$globalFixes] = $globalFixes; } $this->paths = $array; return $this->paths; } /** * merge config of two files ... * @param array $left * @param array $right * * @return array */ protected static function mergeConfig(array $left, array $right): array { //see ConfigFile for original $merged = $left; foreach ($right as $key => $value) { // if non-associative, just merge in unique items if (is_numeric($key)) { if (! in_array($value, $merged, true)) { $merged[] = $value; } continue; } // If not merged into left hand side, then simply assign if (! isset($merged[$key])) { $merged[$key] = $value; continue; } // Make sure both sides are the same type if (is_array($merged[$key]) !== is_array($value)) { user_error( "Config option ${key} cannot merge non-array with array value." ); } // If array type, then merge if (is_array($value)) { $merged[$key] = self::mergeConfig($merged[$key], $value); continue; } // If non array types, don't merge, but instead assert both values are set if ($merged[$key] !== $value) { user_error( "Config option ${key} is defined with different values in multiple files." ); } } return $merged; } } |