/** * @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); }
/** * @param string $alias * @param bool $addDefFields * @return array * @throws \Exception */ public function load($alias, $addDefFields = true) { $content = $this->readFile($alias); list($yaml, $segments) = $this->parseContent($content); $data = (array) Yaml::parse($yaml); if ($addDefFields) { $data['format'] = isset($data['format']) ? $data['format'] : pathinfo($alias, PATHINFO_EXTENSION); $data['date'] = isset($data['date']) ? $data['date'] : PathHelper::extractDateFromPath($alias); $data['path'] = isset($data['path']) ? $data['path'] : $alias; } return ['data' => $data, 'segments' => $segments]; }
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); }