public function save($alias, array $data = [], array $segments = []) { // page data $content = '---' . PHP_EOL; $content .= Yaml::dump($data); $content .= '---' . PHP_EOL; // page segments if (array_key_exists(0, $segments)) { $content .= $segments[0]; $content .= PHP_EOL; unset($segments[0]); } if (array_key_exists('', $segments)) { $content .= $segments['']; $content .= PHP_EOL; unset($segments['']); } foreach ($segments as $key => $value) { $content .= '--- ' . $key . ' ---' . PHP_EOL; $content .= $value; $content .= PHP_EOL; } $path = $this->alias->get($alias); return file_put_contents($path, $content); }
/** * @param string $path * @return array */ public function load($path) { if (!defined('UTF8_BOM')) { define('UTF8_BOM', chr(0xef) . chr(0xbb) . chr(0xbf)); } $yaml = ''; $fileObject = new \SplFileObject($path); $i = 0; foreach ($fileObject as $line) { // strip BOM from the beginning and \n and \r from end of line $line = rtrim(ltrim($line, UTF8_BOM), "\n\r"); if (preg_match('/^---$/', $line)) { $i++; continue; } if ($i > 1) { break; } if ($i == 1) { // add PHP_EOL to end of line $yaml .= $line . PHP_EOL; } } unset($fileObject); return (array) Yaml::parse($yaml); }
public static function updateData($filepath, array $data) { $content = file_get_contents($filepath); $matches = self::pregMatch($content); if (count($matches) == 3) { $newContent = ''; $newContent .= '---' . PHP_EOL; $newContent .= Yaml::dump($data); $newContent .= '---' . PHP_EOL; $newContent .= $matches[2]; return file_put_contents($filepath, $newContent); } return false; }
public function editAction($request) { $path = $request->getQuery('path', null); $absPath = $this->alias->get($path); // Config $name = pathinfo($absPath, PATHINFO_FILENAME); $config = $this->config->get('plugins.config.adminpanel.data.' . $name . '.config'); if (is_null($config)) { return $this->editAsString($request); } $saved = false; if ($this->request->getMethod() == 'POST') { $data = $request->getPost('data', []); $content = Yaml::dump(array_values($data)); $saved = file_put_contents($absPath, $content); } return $this->render('data/edit.twig', ['config' => $config, 'data' => Yaml::parseFile($absPath), 'saved' => $saved]); }
public function reformatFileAction($request) { $name = $request->getPost('name'); $files = $this->getYamlFiles(); if (empty($name) || !array_key_exists($name, $files)) { $this->sendErrorHeader($this->t('Invalid parameter!')); } if (!is_file($files[$name]['path'])) { $this->sendErrorHeader($this->t('{name} does not exist.', ['{name}' => $files[$name]['label']])); } if (!FilesystemHelper::createBackupFile($files[$name]['path'])) { $this->sendErrorHeader($this->t('Backup file can not be created.')); } $parsed = Yaml::parse($files[$name]['path']); $content = Yaml::dump($parsed); if (!file_put_contents($files[$name]['path'], $content)) { $this->sendErrorHeader($this->t('File can not be created.')); } echo $this->t('File was formatted and saved.'); exit; }
/** * @param string $path * @return array */ public function load($path) { $data = []; // dir does not exist or is not readable if (!is_readable($path)) { return $data; } $files = scandir($path); foreach ($files as $file) { if (substr($file, 0, 1) === '.') { continue; } $info = pathinfo($file); if (!in_array($info['extension'], $this->extensions)) { continue; } $key = $info['filename']; $yaml = file_get_contents($path . '/' . $file); $data[$key] = Yaml::parse($yaml); } return $data; }
/** * @param string $path * @return array */ public function load($path) { $yaml = ''; $fileObject = new \SplFileObject($path); $i = 0; foreach ($fileObject as $line) { // strip \n and \r from end of line $line = rtrim($line, "\n\r"); if (preg_match('/^---$/', $line)) { $i++; continue; } if ($i > 1) { break; } if ($i == 1) { // add PHP_EOL to end of line $yaml .= $line . PHP_EOL; } } unset($fileObject); return (array) Yaml::parse($yaml); }