Author: Fabien Potencier (fabien.potencier@symfony-project.com)
Example #1
0
 /**
  * Load YAML into a PHP array statically
  *
  * The load method, when supplied with a YAML stream (string or file),
  * will do its best to convert YAML in a file into a PHP array.
  *
  *  Usage:
  *  <code>
  *   $array = sfYAML::Load('config.yml');
  *   print_r($array);
  *  </code>
  *
  * @param string $input Path of YAML file or string containing YAML
  *
  * @return array
  */
 public static function load($input)
 {
     $file = '';
     // if input is a file, process it
     if (strpos($input, "\n") === false && is_file($input)) {
         $file = $input;
         ob_start();
         $retval = (include $input);
         $content = ob_get_clean();
         // if an array is returned by the config file assume it's in plain php form else in yaml
         $input = is_array($retval) ? $retval : $content;
     }
     // if an array is returned by the config file assume it's in plain php form else in yaml
     if (is_array($input)) {
         return $input;
     }
     require_once dirname(__FILE__) . '/Yaml/YamlParser.php';
     $yaml = new YamlParser();
     try {
         $ret = $yaml->parse($input);
     } catch (Exception $e) {
         throw new InvalidArgumentException(sprintf('Unable to parse %s: %s', $file ? sprintf('file "%s"', $file) : 'string', $e->getMessage()));
     }
     return $ret;
 }
Example #2
0
 public function testParse()
 {
     $projectFile = realpath(__DIR__ . '/fixtures/') . '/yaml_parser_test_file.yml';
     $parser = new YamlParser();
     $parsedConfigs = $parser->parse($projectFile);
     $expected = array('suite1' => array('image' => 'pozitim-ci/centos-php54', 'environments' => array('APPLICATION_ENV' => 'pozitim-ci'), 'commands' => array('echo 1')), 'suite2' => array('image' => 'pozitim-ci/centos-php55', 'environments' => array('APPLICATION_ENV' => 'pozitim-ci'), 'commands' => array('echo 2')), 'suite3' => array('image' => 'pozitim-ci/centos-php56', 'environments' => array('APPLICATION_ENV' => 'pozitim-ci'), 'commands' => array('echo 3')));
     $this->assertEquals($expected, $parsedConfigs);
 }
Example #3
0
 public function testYamlParser()
 {
     $fixtureFile = self::TESTS_FIXTURES_DIRECTORY . 'country.yml';
     $fixture = $this->parserInstance->parse($fixtureFile);
     $this->assertCount(247, $fixture);
     $this->assertEquals('country', $fixture->getName());
     $this->assertInstanceOf('TheIconic\\Fixtures\\Fixture\\Fixture', $fixture);
 }
 public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false)
 {
     $this->currentLineNb = -1;
     $this->currentLine = '';
     $this->lines = explode("\n", $value);
     $data = array();
     $context = null;
     while (true) {
         $isRef = $isInPlace = $isProcessed = false;
         if ($x) {
             $c = $this->getRealCurrentLineNb() + 1;
             $parser = new YamlParser($c);
             $parser->refs =& $this->refs;
             $data[] = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport);
         } elseif ($y) {
             if ($isProcessed) {
                 // Merge keys
                 $data = $isProcessed;
                 // hash
             } elseif (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
                 // if next line is less indented or equal, then it means that the current value is null
                 if ($this->isNextLineIndented() && !$this->isNextLineUnIndentedCollection()) {
                     $data[$key] = null;
                 } else {
                     $c = $this->getRealCurrentLineNb() + 1;
                     $parser = new Parser($c);
                     $parser->refs =& $this->refs;
                     $data[$key] = $parser->parse($this->getNextEmbedBlock(), $exceptionOnInvalidType, $objectSupport);
                 }
             } else {
                 if ($isInPlace) {
                     $data = $this->refs[$isInPlace];
                 } else {
                     $data[$key] = $this->parseValue($values['value'], $exceptionOnInvalidType, $objectSupport);
                 }
             }
         } else {
             if ($x) {
                 if (is_array($value)) {
                     $first = reset($value);
                     if (is_string($first) && 0 === strpos($first, '*')) {
                         $data = array();
                         foreach ($value as $alias) {
                             $data[] = $this->refs[substr($alias, 1)];
                         }
                         $value = $data;
                     }
                 }
                 return $value;
             }
             throw new ParseException($error, $this->getRealCurrentLineNb() + 1, $this->currentLine);
         }
         if ($isRef) {
             $this->refs[$isRef] = end($data);
         }
     }
     return empty($data) ? null : $data;
 }
Example #5
0
 /**
  * Loads a YAML/YML file as an array
  *
  * @param   string  $path
  * @return  object
  * @throws  ParseException If If there is an error parsing the YAML file
  */
 public function parse($path)
 {
     try {
         $data = YamlParser::parse(file_get_contents($path));
     } catch (Exception $exception) {
         throw new ParseException(array('message' => 'Error parsing YAML', 'exception' => $exception));
     }
     return $data;
 }
 private function setPages()
 {
     $pages = array();
     $path = project() . '/styleguide/pages.yml';
     if (YamlParser::hasYaml($path)) {
         $parser = new YamlParser($path);
         $pages = $parser->get('Page');
     }
     if (!$pages || empty($pages)) {
         $pages = array();
     }
     // add the styleguide page
     $children = $this->controller->styleguide_service->getNavigation();
     foreach ($children as $child) {
         $child->request = $this->controller->request;
         $child->setField('Template', 'StyleGuide');
     }
     $pages = array_merge(array('StyleGuide' => new \ArrayData(array('ID' => 'styleGuide', 'Title' => 'Style Guide', 'Children' => $children))), $pages);
     $this->pages = $pages;
 }
 public function getParse()
 {
     return YamlParser::parse(file_get_contents(CONFIG_PATH . 'settings.yaml'));
 }
 /**
  * Parses a YAML string to a PHP value.
  *
  * @param  string A YAML string
  *
  * @return mixed  A PHP value
  */
 public function parse($value)
 {
     $this->value = $this->cleanup($value);
     $this->currentLineNb = -1;
     $this->currentLine = '';
     $this->lines = explode("\n", $this->value);
     $data = array();
     while ($this->moveToNextLine()) {
         if ($this->isCurrentLineEmpty()) {
             continue;
         }
         // tab?
         if (preg_match('#^\\t+#', $this->currentLine)) {
             throw new InvalidArgumentException(sprintf('A YAML file cannot contain tabs as indentation at line %d (%s).', $this->getRealCurrentLineNb() + 1, $this->currentLine));
         }
         $isRef = $isInPlace = $isProcessed = false;
         if (preg_match('#^\\-(\\s+(?P<value>.+?))?\\s*$#', $this->currentLine, $values)) {
             if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#', $values['value'], $matches)) {
                 $isRef = $matches['ref'];
                 $values['value'] = $matches['value'];
             }
             // array
             if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
                 $c = $this->getRealCurrentLineNb() + 1;
                 $parser = new YamlParser($c);
                 $parser->refs =& $this->refs;
                 $data[] = $parser->parse($this->getNextEmbedBlock());
             } else {
                 if (preg_match('/^([^ ]+)\\: +({.*?)$/', $values['value'], $matches)) {
                     $data[] = array($matches[1] => YamlInline::load($matches[2]));
                 } else {
                     $data[] = $this->parseValue($values['value']);
                 }
             }
         } else {
             if (preg_match('#^(?P<key>[^ ].*?) *\\:(\\s+(?P<value>.+?))?\\s*$#', $this->currentLine, $values)) {
                 $key = YamlInline::parseScalar($values['key']);
                 if ('<<' === $key) {
                     if (isset($values['value']) && '*' === substr($values['value'], 0, 1)) {
                         $isInPlace = substr($values['value'], 1);
                         if (!array_key_exists($isInPlace, $this->refs)) {
                             throw new InvalidArgumentException(sprintf('Reference "%s" does not exist at line %s (%s).', $isInPlace, $this->getRealCurrentLineNb() + 1, $this->currentLine));
                         }
                     } else {
                         if (isset($values['value']) && $values['value'] !== '') {
                             $value = $values['value'];
                         } else {
                             $value = $this->getNextEmbedBlock();
                         }
                         $c = $this->getRealCurrentLineNb() + 1;
                         $parser = new YamlParser($c);
                         $parser->refs =& $this->refs;
                         $parsed = $parser->parse($value);
                         $merged = array();
                         if (!is_array($parsed)) {
                             throw new InvalidArgumentException(sprintf("YAML merge keys used with a scalar value instead of an array at line %s (%s)", $this->getRealCurrentLineNb() + 1, $this->currentLine));
                         } else {
                             if (isset($parsed[0])) {
                                 // Numeric array, merge individual elements
                                 foreach (array_reverse($parsed) as $parsedItem) {
                                     if (!is_array($parsedItem)) {
                                         throw new InvalidArgumentException(sprintf("Merge items must be arrays at line %s (%s).", $this->getRealCurrentLineNb() + 1, $parsedItem));
                                     }
                                     $merged = array_merge($parsedItem, $merged);
                                 }
                             } else {
                                 // Associative array, merge
                                 $merged = array_merge($merge, $parsed);
                             }
                         }
                         $isProcessed = $merged;
                     }
                 } else {
                     if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#', $values['value'], $matches)) {
                         $isRef = $matches['ref'];
                         $values['value'] = $matches['value'];
                     }
                 }
                 if ($isProcessed) {
                     // Merge keys
                     $data = $isProcessed;
                 } else {
                     if (!isset($values['value']) || '' == trim($values['value'], ' ') || 0 === strpos(ltrim($values['value'], ' '), '#')) {
                         // if next line is less indented or equal, then it means that the current value is null
                         if ($this->isNextLineIndented()) {
                             $data[$key] = null;
                         } else {
                             $c = $this->getRealCurrentLineNb() + 1;
                             $parser = new YamlParser($c);
                             $parser->refs =& $this->refs;
                             $data[$key] = $parser->parse($this->getNextEmbedBlock());
                         }
                     } else {
                         if ($isInPlace) {
                             $data = $this->refs[$isInPlace];
                         } else {
                             $data[$key] = $this->parseValue($values['value']);
                         }
                     }
                 }
             } else {
                 // one liner?
                 if (1 == count(explode("\n", rtrim($this->value, "\n")))) {
                     $value = YamlInline::load($this->lines[0]);
                     if (is_array($value)) {
                         $first = reset($value);
                         if ('*' === substr($first, 0, 1)) {
                             $data = array();
                             foreach ($value as $alias) {
                                 $data[] = $this->refs[substr($alias, 1)];
                             }
                             $value = $data;
                         }
                     }
                     return $value;
                 }
                 throw new InvalidArgumentException(sprintf('Unable to parse line %d (%s).', $this->getRealCurrentLineNb() + 1, $this->currentLine));
             }
         }
         if ($isRef) {
             $this->refs[$isRef] = end($data);
         }
     }
     return empty($data) ? null : $data;
 }