/** * @param string $name * * @return array|string|bool|int|float|null */ public function getParameter($name) { if (!isset($this->parameterMap[$name])) { return $this->container->getParameter($name); } $varName = $this->parameterMap[$name]['variable']; $var = getenv($varName); if (false === $var) { return $this->container->getParameter($name); } if ($this->parameterMap[$name]['yaml']) { return Inline::parse($var); } return $var; }
public function testDump() { $testsForDump = $this->getTestsForDump(); foreach ($testsForDump as $yaml => $value) { $this->assertEquals($yaml, Inline::dump($value), sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml)); } foreach ($this->getTestsForParse() as $yaml => $value) { if ($value == 1230) { continue; } $this->assertEquals($value, Inline::parse(Inline::dump($value)), 'check consistency'); } foreach ($testsForDump as $yaml => $value) { if ($value == 1230) { continue; } $this->assertEquals($value, Inline::parse(Inline::dump($value)), 'check consistency'); } }
public function getFunctions() { return array(new ExpressionFunction('dynamic_parameter', function ($paramName, $envVar) { return sprintf('(false === getenv(%s) ? $this->getParameter(%s) : getenv(%s))', $envVar, $paramName, $envVar); }, function (array $variables, $paramName, $envVar) { $envParam = getenv($envVar); if (false !== $envParam) { return $envParam; } return $variables['container']->getParameter($paramName); }), new ExpressionFunction('dynamic_yaml_parameter', function ($paramName, $envVar) { return sprintf('(false === getenv(%s) ? $this->getParameter(%s) : \\Symfony\\Component\\Yaml\\Inline::parse(getenv(%s)))', $envVar, $paramName, $envVar); }, function (array $variables, $paramName, $envVar) { $envParam = getenv($envVar); if (false !== $envParam) { return Inline::parse($envParam); } return $variables['container']->getParameter($paramName); })); }
/** * Parses a YAML value. * * @param string $value A YAML value * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior * @param string $context The parser context (either sequence or mapping) * * @return mixed A PHP value * * @throws ParseException When reference does not exist */ private function parseValue($value, $flags, $context) { if (0 === strpos($value, '*')) { if (false !== ($pos = strpos($value, '#'))) { $value = substr($value, 1, $pos - 2); } else { $value = substr($value, 1); } if (!array_key_exists($value, $this->refs)) { throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLineNb + 1, $this->currentLine); } return $this->refs[$value]; } if (preg_match('/^' . self::TAG_PATTERN . self::BLOCK_SCALAR_HEADER_PATTERN . '$/', $value, $matches)) { $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : ''; $data = $this->parseBlockScalar($matches['separator'], preg_replace('#\\d+#', '', $modifiers), (int) abs($modifiers)); if (isset($matches['tag']) && '!!binary' === $matches['tag']) { return Inline::evaluateBinaryScalar($data); } return $data; } try { Inline::$parsedLineNumber = $this->getRealCurrentLineNb(); $parsedValue = Inline::parse($value, $flags, $this->refs); if ('mapping' === $context && is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) { throw new ParseException('A colon cannot be used in an unquoted mapping value.'); } return $parsedValue; } catch (ParseException $e) { $e->setParsedLine($this->getRealCurrentLineNb() + 1); $e->setSnippet($this->currentLine); throw $e; } }
private function getParams(array $expectedParams, array $actualParams) { // Simply use the expectedParams value as default for the missing params. if (!$this->io->isInteractive()) { return array_replace($expectedParams, $actualParams); } $isStarted = false; foreach ($expectedParams as $key => $message) { if (array_key_exists($key, $actualParams)) { continue; } if (!$isStarted) { $isStarted = true; $this->io->write('<comment>Some parameters are missing. Please provide them.</comment>'); } $default = Inline::dump($message); $value = $this->io->ask(sprintf('<question>%s</question> (<comment>%s</comment>): ', $key, $default), $default); $actualParams[$key] = Inline::parse($value); } return $actualParams; }
/** * @expectedException \Symfony\Component\Yaml\Exception\ParseException * @expectedExceptionMessage Malformed inline YAML string: {this, is not, supported}. */ public function testNotSupportedMissingValue() { Inline::parse('{this, is not, supported}'); }
/** * @dataProvider getScalarIndicators * @expectedException Symfony\Component\Yaml\Exception\ParseException * @expectedExceptionMessage cannot start a plain scalar; you need to quote the scalar. */ public function testParseUnquotedScalarStartingWithScalarIndicator($indicator) { Inline::parse(sprintf('{ foo: %sfoo }', $indicator)); }
/** * Parses a YAML value. * * @param string $value A YAML value * @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise * @param bool $objectSupport True if object support is enabled, false otherwise * * @return mixed A PHP value * * @throws ParseException When reference does not exist */ private function parseValue($value, $exceptionOnInvalidType, $objectSupport) { if (0 === strpos($value, '*')) { if (false !== ($pos = strpos($value, '#'))) { $value = substr($value, 1, $pos - 2); } else { $value = substr($value, 1); } if (!array_key_exists($value, $this->refs)) { throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLine); } return $this->refs[$value]; } if (preg_match('/^' . self::FOLDED_SCALAR_PATTERN . '$/', $value, $matches)) { $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : ''; return $this->parseFoldedScalar($matches['separator'], preg_replace('#\\d+#', '', $modifiers), intval(abs($modifiers))); } try { return Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $this->refs); } catch (ParseException $e) { $e->setParsedLine($this->getRealCurrentLineNb() + 1); $e->setSnippet($this->currentLine); throw $e; } }
/** * @dataProvider getInvalidBinaryData */ public function testParseInvalidBinaryData($data, $expectedMessage) { $this->setExpectedExceptionRegExp('\\Symfony\\Component\\Yaml\\Exception\\ParseException', $expectedMessage); Inline::parse($data); }
/** * Parses a YAML value. * * @param string $value A YAML value * @param int $flags A bit field of PARSE_* constants to customize the YAML parser behavior * @param string $context The parser context (either sequence or mapping) * * @return mixed A PHP value * * @throws ParseException When reference does not exist */ private function parseValue($value, $flags, $context) { if (0 === strpos($value, '*')) { if (false !== ($pos = strpos($value, '#'))) { $value = substr($value, 1, $pos - 2); } else { $value = substr($value, 1); } if (!array_key_exists($value, $this->refs)) { throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLineNb + 1, $this->currentLine); } return $this->refs[$value]; } if (preg_match('/^' . self::TAG_PATTERN . self::BLOCK_SCALAR_HEADER_PATTERN . '$/', $value, $matches)) { $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : ''; $data = $this->parseBlockScalar($matches['separator'], preg_replace('#\\d+#', '', $modifiers), (int) abs($modifiers)); if (isset($matches['tag']) && '!!binary' === $matches['tag']) { return Inline::evaluateBinaryScalar($data); } return $data; } try { $quotation = '' !== $value && ('"' === $value[0] || "'" === $value[0]) ? $value[0] : null; // do not take following lines into account when the current line is a quoted single line value if (null !== $quotation && preg_match('/^' . $quotation . '.*' . $quotation . '(\\s*#.*)?$/', $value)) { return Inline::parse($value, $flags, $this->refs); } while ($this->moveToNextLine()) { // unquoted strings end before the first unindented line if (null === $quotation && $this->getCurrentLineIndentation() === 0) { $this->moveToPreviousLine(); break; } $value .= ' ' . trim($this->currentLine); // quoted string values end with a line that is terminated with the quotation character if ('' !== $this->currentLine && substr($this->currentLine, -1) === $quotation) { break; } } Inline::$parsedLineNumber = $this->getRealCurrentLineNb(); $parsedValue = Inline::parse($value, $flags, $this->refs); if ('mapping' === $context && is_string($parsedValue) && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) { throw new ParseException('A colon cannot be used in an unquoted mapping value.'); } return $parsedValue; } catch (ParseException $e) { $e->setParsedLine($this->getRealCurrentLineNb() + 1); $e->setSnippet($this->currentLine); throw $e; } }
public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF() { $value = '686e444'; $this->assertSame($value, Inline::parse(Inline::dump($value))); }
/** * Present a node question to the user * * @param NodeInterface $node The node in question * @param string $name The name of the node * @return mixed The new value of the node */ private function writeNodeQuestion($node, $name) { if (!$this->questionInfoShown) { $this->io->write(array("<comment> ! [NOTE] You can change all the configuration options later", " ! on the config.yml file in the app folder</>\n")); $this->questionInfoShown = true; } if (!$node->getAttribute('asked')) { return $node->getDefaultValue(); } $this->writeWarning($node); if ($info = $node->getInfo()) { $this->io->write(" {$info}"); } if ($example = $node->getExample()) { // We use Inline::dump() to convert the value to the YAML // format so that it is readable by the user (as an example, // false values are converted to 'false' instead of an empty // string) $example = Inline::dump($example); $this->io->write(" Example: <comment>{$example}</comment>"); } $question = " <fg=green>{$name}"; if ($node instanceof EnumNode) { // Create list of possible values for enum configuration parameters $values = $node->getValues(); foreach ($values as &$value) { $value = Inline::dump($value); } $question .= ' (' . implode(', ', $values) . ')'; } $question .= "</fg=green>"; if ($node->hasDefaultValue()) { // Show the default value of the parameter $default = Inline::dump($node->getDefaultValue()); $question .= " [<comment>{$default}</comment>]"; } else { $default = null; } // Show a user-friendly prompt $question .= ":\n > "; $value = $this->io->askAndValidate($question, function ($value) use($node) { $value = Inline::parse($value); // Make sure that there are no errors $node->finalize($value); return $value; }, null, $default); $this->io->write(""); return $value; }
/** * Parses a YAML value. * * @param string $value A YAML value * @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise * @param bool $objectSupport True if object support is enabled, false otherwise * @param bool $objectForMap true if maps should return a stdClass instead of array() * @param string $context The parser context (either sequence or mapping) * * @return mixed A PHP value * * @throws ParseException When reference does not exist */ private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $context) { if (0 === strpos($value, '*')) { if (false !== ($pos = strpos($value, '#'))) { $value = substr($value, 1, $pos - 2); } else { $value = substr($value, 1); } if (!array_key_exists($value, $this->refs)) { throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLine); } return $this->refs[$value]; } if (preg_match('/^' . Inline::REGEX_TAG_PATTERN . self::BLOCK_SCALAR_HEADER_PATTERN . '$/', $value, $matches)) { $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : ''; $output = $this->parseBlockScalar($matches['separator'], preg_replace('#\\d+#', '', $modifiers), (int) abs($modifiers)); if (isset($matches['tag']) && $matches['tag'] == '!!binary') { $output = base64_decode($output); } return $output; } try { $parsedValue = Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs); if ('mapping' === $context && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($parsedValue, ': ')) { throw new ParseException('A colon cannot be used in an unquoted mapping value.'); } return $parsedValue; } catch (ParseException $e) { $e->setParsedLine($this->getRealCurrentLineNb() + 1); $e->setSnippet($this->currentLine); throw $e; } }
private function checkPermissionToInstall() { $value = $this->io->ask("<question>Would you like connect OrangeGate with solr ?</question> [<comment>yes</comment>] ", 'yes'); return Inline::parse($value) === "yes" ? true : false; }
/** * @expectedException \Symfony\Component\Yaml\Exception\ParseException * @expectedExceptionMessage A reference must contain at least one character. */ public function testParseUnquotedAsteriskFollowedByAComment() { Inline::parse('{ foo: * #foo }'); }
/** * Parses a YAML value. * * @param string $value A YAML value * @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise * @param bool $objectSupport True if object support is enabled, false otherwise * @param bool $objectForMap true if maps should return a stdClass instead of array() * @param string $context The parser context (either sequence or mapping) * * @return mixed A PHP value * * @throws ParseException When reference does not exist */ private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $context) { if (0 === strpos($value, '*')) { if (false !== ($pos = strpos($value, '#'))) { $value = substr($value, 1, $pos - 2); } else { $value = substr($value, 1); } if (!array_key_exists($value, $this->refs)) { throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLine); } return $this->refs[$value]; } if (preg_match('/^' . self::BLOCK_SCALAR_HEADER_PATTERN . '$/', $value, $matches)) { $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : ''; return $this->parseBlockScalar($matches['separator'], preg_replace('#\\d+#', '', $modifiers), (int) abs($modifiers)); } if ('mapping' === $context && '"' !== $value[0] && "'" !== $value[0] && '[' !== $value[0] && '{' !== $value[0] && '!' !== $value[0] && false !== strpos($value, ': ')) { @trigger_error(sprintf('Using a colon in an unquoted mapping value in line %d is deprecated since Symfony 2.8 and will throw a ParseException in 3.0.', $this->getRealCurrentLineNb() + 1), E_USER_DEPRECATED); // to be thrown in 3.0 // throw new ParseException('A colon cannot be used in an unquoted mapping value.'); } try { return Inline::parse($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $this->refs); } catch (ParseException $e) { $e->setParsedLine($this->getRealCurrentLineNb() + 1); $e->setSnippet($this->currentLine); throw $e; } }
/** * @expectedException \Symfony\Component\Yaml\Exception\ParseException */ public function testParseInvalidMappingKeyShouldThrowException() { $value = '{ "foo " bar": "bar" }'; Inline::parse($value); }
/** * @expectedException \Symfony\Component\Yaml\Exception\ParseException */ public function testParseInvalidSequenceShouldThrowException() { Inline::parse('{ foo: bar } bar'); }
public function testParseMapReferenceInSequence() { $foo = array('a' => 'Steve', 'b' => 'Clark', 'c' => 'Brian'); $this->assertSame(array($foo), Inline::parse('[*foo]', false, false, false, array('foo' => $foo))); }
/** * Parses a YAML value. * * @param string $value A YAML value * * @return mixed A PHP value * * @throws ParseException When reference does not exist */ private function parseValue($value) { if (0 === strpos($value, '*')) { if (false !== ($pos = strpos($value, '#'))) { $value = substr($value, 1, $pos - 2); } else { $value = substr($value, 1); } if (!array_key_exists($value, $this->refs)) { throw new ParseException(sprintf('Reference "%s" does not exist.', $value), $this->currentLine); } return $this->refs[$value]; } if (preg_match('/^(?P<separator>\\||>)(?P<modifiers>\\+|\\-|\\d+|\\+\\d+|\\-\\d+|\\d+\\+|\\d+\\-)?(?P<comments> +#.*)?$/', $value, $matches)) { $modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : ''; return $this->parseFoldedScalar($matches['separator'], preg_replace('#\\d+#', '', $modifiers), intval(abs($modifiers))); } try { return Inline::parse($value); } catch (ParseException $e) { $e->setParsedLine($this->getRealCurrentLineNb() + 1); $e->setSnippet($this->currentLine); throw $e; } }
/** * @dataProvider getTimestampTests */ public function testParseTimestampAsDateTimeObject($yaml, $year, $month, $day, $hour, $minute, $second) { $expected = new \DateTime('now', new \DateTimeZone('UTC')); $expected->setDate($year, $month, $day); $expected->setTime($hour, $minute, $second); $this->assertEquals($expected, Inline::parse($yaml, Yaml::PARSE_DATETIME)); }