XOOPS  2.6.0
Yaml.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  */
11 
12 namespace Xoops\Core;
13 
14 use Symfony\Component\Yaml\Yaml as VendorYaml;
15 
39 class Yaml
40 {
41 
51  public static function dump($var, $inline = 4, $indent = 4)
52  {
53  try {
54  $ret = VendorYaml::dump($var, $inline, $indent);
55  } catch (\Exception $e) {
56  \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
57  $ret = false;
58  }
59  return $ret;
60  }
61 
69  public static function load($yamlString)
70  {
71  try {
72  $ret = VendorYaml::parse($yamlString);
73  } catch (\Exception $e) {
74  \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
75  $ret = false;
76  }
77  return $ret;
78  }
79 
87  public static function read($yamlFile)
88  {
89  try {
90  $yamlString = file_get_contents($yamlFile);
91  $ret = VendorYaml::parse($yamlString);
92  } catch (\Exception $e) {
93  \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
94  $ret = false;
95  }
96  return $ret;
97  }
98 
109  public static function save($var, $yamlFile, $inline = 4, $indent = 4)
110  {
111  try {
112  $yamlString = VendorYaml::dump($var, $inline, $indent);
113  $ret = file_put_contents($yamlFile, $yamlString);
114  } catch (\Exception $e) {
115  \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
116  $ret = false;
117  }
118  return $ret;
119  }
120 
135  public static function dumpWrapped($var, $inline = 4, $indent = 4)
136  {
137  try {
138  $yamlString = VendorYaml::dump($var, $inline, $indent);
139  $ret = empty($yamlString) ? false : "<?php\n/*\n---\n" . $yamlString . "\n...\n*/\n";
140  } catch (\Exception $e) {
141  \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
142  $ret = false;
143  }
144  return $ret;
145  }
146 
159  public static function loadWrapped($yamlString)
160  {
161  try {
162  $match = array();
163  $matched = preg_match('/(---.*\.\.\.)/s', $yamlString, $match);
164  $unwrapped = $matched ? $match[1] : $yamlString;
165  $ret = VendorYaml::parse($unwrapped);
166  } catch (\Exception $e) {
167  \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
168  $ret = false;
169  }
170  return $ret;
171  }
172 
185  public static function readWrapped($yamlFile)
186  {
187  try {
188  $yamlString = file_get_contents($yamlFile);
189  $ret = self::loadWrapped($yamlString);
190  } catch (\Exception $e) {
191  \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
192  $ret = false;
193  }
194  return $ret;
195  }
196 
212  public static function saveWrapped($var, $yamlFile, $inline = 4, $indent = 4)
213  {
214  try {
215  $yamlString = self::dumpWrapped($var, $inline, $indent);
216  $ret = file_put_contents($yamlFile, $yamlString);
217  } catch (\Exception $e) {
218  \Xoops::getInstance()->events()->triggerEvent('core.exception', $e);
219  $ret = false;
220  }
221  return $ret;
222  }
223 }
static dump($var, $inline=4, $indent=4)
Definition: Yaml.php:51
static getInstance()
Definition: Xoops.php:160
static loadWrapped($yamlString)
Definition: Yaml.php:159
static dumpWrapped($var, $inline=4, $indent=4)
Definition: Yaml.php:135
static readWrapped($yamlFile)
Definition: Yaml.php:185
static load($yamlString)
Definition: Yaml.php:69
static save($var, $yamlFile, $inline=4, $indent=4)
Definition: Yaml.php:109
static read($yamlFile)
Definition: Yaml.php:87
$var
Definition: userinfo.php:125
static saveWrapped($var, $yamlFile, $inline=4, $indent=4)
Definition: Yaml.php:212