Source of file ConfigYML.php
Size: 3,135 Bytes - Last Modified: 2021-12-23T10:42:13+00:00
/var/www/docs.ssmods.com/process/src/src/Api/ConfigYML.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 | <?php namespace Sunnysideup\ModuleChecks\Api; use Exception; use Sunnysideup\ModuleChecks\BaseObject; use Sunnysideup\ModuleChecks\Model\ModuleCheck; use Sunnysideup\ModuleChecks\Tasks\UpdateModules; use Yaml; use Sunnysideup\Flush\FlushNow; class ConfigYML extends BaseObject { protected $moduleName = ''; protected $repo = null; protected $ymlData = null; protected $filename = ''; public function __construct($repo, $fileToCheck = 'config.yml') { $this->repo = $repo; $this->filename = $this->repo->Directory() . '/_config/' . $fileToCheck; } public function reWrite() { if (! $this->readYMLFromFile()) { return false; } if (! $this->writeYAMLToFile()) { return false; } return true; } public function readYMLFromFile() { FlushNow::do_flush('reading config yml ... ', 'updating'); if (! file_exists($this->filename)) { //UpdateModules::$unsolvedItems[$this->repo->ModuleName] = "Unable to load " . $this->filename; ModuleCheck::log_error('Unable to load: ' . $this->filename); return false; } try { $this->ymlData = Yaml::parse(file_get_contents($this->filename)); } catch (Exception $e) { ModuleCheck::log_error('Unable to parse the YAML string: ' . $e->getMessage()); //trigger_error ("Error in YML file"); $this->replaceFaultyYML(); return false; } return $this->ymlData; } public function replaceFaultyYML() { return false; /**function broken do not use**/ if (file_exists($this->filename)) { $rawYML = file_get_contents($this->filename); $lines = explode("\n", $rawYML); $replacment = ''; foreach ($lines as $index => $line) { if (strpos($line, 'After:') !== false) { $replacment = 'After:'; $listitems = explode(',', $line); //print_r ($listitems); foreach ($listitems as $item) { if (! trim($item)) { continue; } $item = str_replace('After: ', '', $item); $replacment .= ' - ' . trim($item) . ''; } $lines[$index] = $replacment; } } $newYML = implode('', $lines); FlushNow::do_flush('Updating config.YML to correct syntax ... ', 'updating'); // $file = fopen($this->filename, 'w'); file_put_contents($this->filename, $newYML); } else { return false; } } public function writeYAMLToFile() { FlushNow::do_flush('Writing config yml ... ', 'updating'); if (! $this->ymlData) { return false; } $yaml = Yaml::dump($this->ymlData); file_put_contents($this->filename, $yaml); return true; } } |