* @version 2.0.0 */ /* * ChangeLog: * ---------- * 2012-09-09 v1.0.0 Class Creation. PEAR style based on com\simplethingframework\util\YAML v2.0.0 */ class STF_Util_YAML { private $implimentations; private $yamlDecode = null; private $yamlEncode = null; public function __construct() { $this->implimentations = (object) array(); $this->implimentations->decoders = array(); $this->implimentations->encoders = array(); // PECL PHP-YAML // if(function_exists('yaml_parse')) { // Parses a YAML stream to PHP Data $this->implimentations->decoders[] = 'yaml_parse'; // yaml_parse($input, $position=0, &$numberOfDocs, callbacks);// callbacks is a funcion or array of YAML tag => callback mappings // $position -1 returns an array of documents $this->yamlDecode = 'phpYamlDecode'; } if(function_exists('yaml_parse_file')) { // Parses a YAML stream to PHP Data $this->implimentations->decoders[] = 'yaml_parse_file'; // yaml_parse_file($path, $position=0, &$numberOfDocs, callbacks); // $position -1 returns an array of documents } if(function_exists('yaml_parse_url')) { // Parses a YAML stream to PHP Data $this->implimentations->decoders[] = 'yaml_parse_url'; // yaml_parse_url($url, $position=0, &$numberOfDocs, callbacks); // $position -1 returns an array of documents } if(function_exists('yaml_emit')) { // Converts PHP data to YAML $this->implimentations->encoders[] = 'yaml_emit'; // yaml_emit($data, YAML_UTF8_ENCODING, YAML_LN_BREAK);// Defaults to YAML_ANY_ENCODING, YAML_ANY_BREAK $this->yamlEncode = $this->phpYamlEncode; } if(function_exists('yaml_emit_file')) { // Converts PHP data to YAML $this->implimentations->encoders[] = 'yaml_emit_file'; // yaml_emit_file($path, $data, YAML_UTF8_ENCODING, YAML_LN_BREAK);// Defaults to YAML_ANY_ENCODING, YAML_ANY_BREAK } // PECL Syck Library // if(function_exists('syck_load')) { // Parses a YAML stream $this->implimentations->decoders[] = 'syck_load'; if($this->yamlDecode === null) $this->yamlDecode = $this->syckDecode; } if(function_exists('syck_dump')) { // Converts arrays to YAML $this->implimentations->encoders[] = 'syck_dump'; if($this->yamlEncode === null) $this->yamlEncode = $this->syckEncode; } if(count($this->implimentations->decoders) == 0 || count($this->implimentations->encoders) == 0) { $includePaths = explode(PATH_SEPARATOR, get_include_path()); $foundSymphonyYamlEncoder = false; $foundSymphonyYamlDecoder = false; $foundSpycCodex = false; foreach($includePaths as $path) { $path .= '/'; // Test for sfYaml // if(file_exists($path.'sfYamlParser.php')) { if(file_exists($path.'sfYaml.php')) { // use sfYaml; // include 'sfYaml.php'; $this->implimentations->decoders[] = 'sfYaml'; $foundSymphonyYamlEncoder = true; } if($this->yamlDecode === null) $this->yamlDecode = $this->sfYamlDecode; } if(file_exists($path.'sfYamlDumper.php')) { if(file_exists($path.'sfYaml.php')) { $this->implimentations->encoders[] = 'sfYaml'; $foundSymphonyYamlDecoder = true; } } if(!$foundSymphonyYamlEncoder || !$foundSymphonyYamlDecoder) { // Test for spyc.php // if(file_exists($path.'spyc.php')) { // use Spyc; $this->implimentations->decoders[] = 'Spyc::YAMLLoad'; $this->implimentations->encoders[] = 'Spyc::YAMLDump'; $foundSpycCodex = true; // $data = Spyc::YAMLLoad($fileOrStream); // $yaml_str = Spyc::YAMLDump($array, $indent=2, $wordWrap=40); if($this->yamlDecode === null) $this->yamlDecode = $this->spycDecode; } } if(($foundSymphonyYamlEncoder && $foundSymphonyYamlDecoder) || $foundSpycCodex) break; } } }// end function __construct() public function decode($src) { // return $this->{$this->yamlDecode}($src); // return $this->phpYamlDecode($src); // return print_r(array('decoders'=>$this->implimentations->decoders, 'encoders'=>$this->implimentations->encoders), true); return $this->sfYamlDecode($src); } public function encode($data) { return $this->yamlEncode($src); } private function phpYamlDecode($src) { if(preg_match('/^https?:\/\//', $src)) { if(in_array('yaml_parse_url', $this->implimentations->decoders)) $yaml = yaml_parse_url($src);// yaml_parse_url($url, $position=0, &$numberOfDocs, callbacks); else { $yaml = file_get_contents($src); $yaml = yaml_parse($yaml); } } else if(file_exists($src)) { if(is_readable($src)) { if(in_array('yaml_parse_file', $this->implimentations->decoders)) $yaml = yaml_parse_file($src);// yaml_parse_file($path, $position=0, &$numberOfDocs, callbacks); else { $yaml = file_get_contents($src); $yaml = yaml_parse($yaml); } } else { // ERROR } } else { $yaml = yaml_parse($src); } return $yaml; } private function sfYamlDecode($src) { return sfYaml::load($src); } /** * The Syck extension is YAML 1.0 compliant. * */ private function syckDecode($src) { if(preg_match('/^https?:\/\//', $src)) { $yaml = file_get_contents($src); $yaml = syck_load($yaml); } else if(file_exists($src)) { if(is_readable($src)) { $yaml = file_get_contents($src); $yaml = syck_load($yaml); } else { // ERROR } } else { $yaml = syck_load($src); } return $yaml; } } ?>