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:
16: /**
17: * Yaml dump and parse methods
18: *
19: * YAML is a serialization format most useful when human readability
20: * is a consideration. It can be useful for configuration files, as
21: * well as import and export functions.
22: *
23: * This file is a front end for a separate YAML package present in the
24: * vendor directory. The intent is to provide a consistent interface
25: * no mater what underlying library is actually used.
26: *
27: * At present, this class expects the symfony/yaml package.
28: *
29: * @category Xoops\Core\Yaml
30: * @package Yaml
31: * @author Richard Griffith <richard@geekwright.com>
32: * @copyright 2013-2014 XOOPS Project (http://xoops.org)
33: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
34: * @version Release: 1.0
35: * @link http://xoops.org
36: * @see http://www.yaml.org/
37: * @since 1.0
38: */
39: class Yaml
40: {
41:
42: /**
43: * Dump an PHP array as a YAML string
44: *
45: * @param mixed $var Variable which will be dumped
46: * @param integer $inline Nesting level where you switch to inline YAML
47: * @param integer $indent Number of spaces to indent for nested nodes
48: *
49: * @return string|bool YAML string or false on error
50: */
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:
62: /**
63: * Load a YAML string into a PHP array
64: *
65: * @param string $yamlString YAML dump string
66: *
67: * @return array|boolean PHP array or false on error
68: */
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:
80: /**
81: * Read a file containing YAML into a PHP array
82: *
83: * @param string $yamlFile filename of YAML file
84: *
85: * @return array|boolean PHP array or false on error
86: */
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:
99: /**
100: * Save a PHP array as a YAML file
101: *
102: * @param array $var variable which will be dumped
103: * @param string $yamlFile filename of YAML file
104: * @param integer $inline Nesting level where you switch to inline YAML
105: * @param integer $indent Number of spaces to indent for nested nodes
106: *
107: * @return integer|boolean number of bytes written, or false on error
108: */
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:
121: /**
122: * Dump an PHP array as a YAML string with a php wrapper
123: *
124: * The wrap is a php header that surrounds the yaml with section markers,
125: * '---' and '...' along with php comment markers. The php wrapper keeps the
126: * yaml file contents from being revealed by serving the file directly from
127: * a poorly configured server.
128: *
129: * @param mixed $var Variable which will be dumped
130: * @param integer $inline Nesting level where you switch to inline YAML
131: * @param integer $indent Number of spaces to indent for nested nodes
132: *
133: * @return string|boolean YAML string or false on error
134: */
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:
147: /**
148: * Load a YAML string with a php wrapper into a PHP array
149: *
150: * The wrap is a php header that surrounds the yaml with section markers,
151: * '---' and '...' along with php comment markers. The php wrapper keeps the
152: * yaml file contents from being revealed by serving the file directly from
153: * a poorly configured server.
154: *
155: * @param string $yamlString YAML dump string
156: *
157: * @return array|boolean PHP array or false on error
158: */
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:
173: /**
174: * Read a file containing YAML with a php wrapper into a PHP array
175: *
176: * The wrap is a php header that surrounds the yaml with section markers,
177: * '---' and '...' along with php comment markers. The php wrapper keeps the
178: * yaml file contents from being revealed by serving the file directly from
179: * a poorly configured server.
180: *
181: * @param string $yamlFile filename of YAML file
182: *
183: * @return array|boolean PHP array or false on error
184: */
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:
197: /**
198: * Save a PHP array as a YAML file with a php wrapper
199: *
200: * The wrap is a php header that surrounds the yaml with section markers,
201: * '---' and '...' along with php comment markers. The php wrapper keeps the
202: * yaml file contents from being revealed by serving the file directly from
203: * a poorly configured server.
204: *
205: * @param array $var variable which will be dumped
206: * @param string $yamlFile filename of YAML file
207: * @param integer $inline Nesting level where you switch to inline YAML
208: * @param integer $indent Number of spaces to indent for nested nodes
209: *
210: * @return integer|boolean number of bytes written, or false on error
211: */
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: }
224: