Skip to content

Commit

Permalink
[Yaml] moved exceptions to their own sub-namespace (added a specific …
Browse files Browse the repository at this point in the history
…exception for the dump)
  • Loading branch information
fabpot committed Jun 14, 2011
1 parent 252ddda commit c3e1d03
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 42 deletions.
8 changes: 5 additions & 3 deletions ParserException.php → Exception/DumpException.php
Expand Up @@ -9,13 +9,15 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\Yaml;
namespace Symfony\Component\Yaml\Exception;

/**
* Exception class used by all exceptions thrown by the component.
* Exception class thrown when an error occurs during dumping.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @api
*/
class ParserException extends Exception
class DumpException extends \RuntimeException implements ExceptionInterface
{
}
8 changes: 5 additions & 3 deletions Exception.php → Exception/ExceptionInterface.php
Expand Up @@ -9,13 +9,15 @@
* file that was distributed with this source code.
*/

namespace Symfony\Component\Yaml;
namespace Symfony\Component\Yaml\Exception;

/**
* Exception class used by all exceptions thrown by the component.
* Exception interface for all exceptions thrown by the component.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @api
*/
class Exception extends \Exception
interface ExceptionInterface
{
}
143 changes: 143 additions & 0 deletions Exception/ParseException.php
@@ -0,0 +1,143 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Yaml\Exception;

/**
* Exception class thrown when an error occurs during parsing.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @api
*/
class ParseException extends \RuntimeException implements ExceptionInterface
{
private $parsedFile;
private $parsedLine;
private $snippet;
private $rawMessage;

/**
* Constructor.
*
* @param string $message The error message
* @param integer $lineno The line where the error occurred
* @param integer $snippet The snippet of code near the problem
* @param string $filename The file name where the error occurred
* @param Exception $previous The previous exception
*/
public function __construct($message, $parsedLine = -1, $snippet = null, $parsedFile = null, Exception $previous = null)
{
$this->parsedFile = $parsedFile;
$this->parsedLine = $parsedLine;
$this->snippet = $snippet;
$this->rawMessage = $message;

$this->updateRepr();

parent::__construct($this->message, 0, $previous);
}

/**
* Gets the snippet of code near the error.
*
* @return string The snippet of code
*/
public function getSnippet()
{
return $this->snippet;
}

/**
* Sets the snippet of code near the error.
*
* @param string $parsedFile The filename
*/
public function setSnippet($snippet)
{
$this->snippet = $snippet;

$this->updateRepr();
}

/**
* Gets the filename where the error occurred.
*
* This method returns null if a string is parsed.
*
* @return string The filename
*/
public function getParsedFile()
{
return $this->parsedFile;
}

/**
* Sets the filename where the error occurred.
*
* @param string $parsedFile The filename
*/
public function setParsedFile($parsedFile)
{
$this->parsedFile = $parsedFile;

$this->updateRepr();
}

/**
* Gets the line where the error occurred.
*
* @return integer The file line
*/
public function getParsedLine()
{
return $this->parsedLine;
}

/**
* Sets the line where the error occurred.
*
* @param integer $parsedLine The file line
*/
public function setParsedLine($parsedLine)
{
$this->parsedLine = $parsedLine;

$this->updateRepr();
}

private function updateRepr()
{
$this->message = $this->rawMessage;

$dot = false;
if ('.' === substr($this->message, -1)) {
$this->message = substr($this->message, 0, -1);
$dot = true;
}

if (null !== $this->parsedFile) {
$this->message .= sprintf(' in %s', json_encode($this->parsedFile));
}

if ($this->parsedLine >= 0) {
$this->message .= sprintf(' at line %d', $this->parsedLine);
}

if ($this->snippet) {
$this->message .= sprintf(' (near "%s")', $this->snippet);
}

if ($dot) {
$this->message .= '.';
}
}
}
23 changes: 13 additions & 10 deletions Inline.php
Expand Up @@ -10,6 +10,9 @@

namespace Symfony\Component\Yaml;

use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Exception\DumpException;

/**
* Inline implements a YAML parser/dumper for the YAML inline syntax.
*
Expand Down Expand Up @@ -64,13 +67,13 @@ static public function parse($value)
*
* @return string The YAML string representing the PHP array
*
* @throws Exception When trying to dump PHP resource
* @throws DumpException When trying to dump PHP resource
*/
static public function dump($value)
{
switch (true) {
case is_resource($value):
throw new Exception('Unable to dump PHP resources in a YAML file.');
throw new DumpException('Unable to dump PHP resources in a YAML file.');
case is_object($value):
return '!!php/object:'.serialize($value);
case is_array($value):
Expand Down Expand Up @@ -141,7 +144,7 @@ static private function dumpArray($value)
*
* @return string A YAML string
*
* @throws ParserException When malformed inline YAML string is parsed
* @throws ParseException When malformed inline YAML string is parsed
*/
static public function parseScalar($scalar, $delimiters = null, $stringDelimiters = array('"', "'"), &$i = 0, $evaluate = true)
{
Expand All @@ -162,7 +165,7 @@ static public function parseScalar($scalar, $delimiters = null, $stringDelimiter
$output = $match[1];
$i += strlen($output);
} else {
throw new ParserException(sprintf('Malformed inline YAML string (%s).', $scalar));
throw new ParseException(sprintf('Malformed inline YAML string (%s).', $scalar));
}

$output = $evaluate ? self::evaluateScalar($output) : $output;
Expand All @@ -179,12 +182,12 @@ static public function parseScalar($scalar, $delimiters = null, $stringDelimiter
*
* @return string A YAML string
*
* @throws ParserException When malformed inline YAML string is parsed
* @throws ParseException When malformed inline YAML string is parsed
*/
static private function parseQuotedScalar($scalar, &$i)
{
if (!preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
throw new ParserException(sprintf('Malformed inline YAML string (%s).', substr($scalar, $i)));
throw new ParseException(sprintf('Malformed inline YAML string (%s).', substr($scalar, $i)));
}

$output = substr($match[0], 1, strlen($match[0]) - 2);
Expand All @@ -209,7 +212,7 @@ static private function parseQuotedScalar($scalar, &$i)
*
* @return string A YAML string
*
* @throws ParserException When malformed inline YAML string is parsed
* @throws ParseException When malformed inline YAML string is parsed
*/
static private function parseSequence($sequence, &$i = 0)
{
Expand Down Expand Up @@ -254,7 +257,7 @@ static private function parseSequence($sequence, &$i = 0)
++$i;
}

throw new ParserException(sprintf('Malformed inline YAML string %s', $sequence));
throw new ParseException(sprintf('Malformed inline YAML string %s', $sequence));
}

/**
Expand All @@ -265,7 +268,7 @@ static private function parseSequence($sequence, &$i = 0)
*
* @return string A YAML string
*
* @throws ParserException When malformed inline YAML string is parsed
* @throws ParseException When malformed inline YAML string is parsed
*/
static private function parseMapping($mapping, &$i = 0)
{
Expand Down Expand Up @@ -318,7 +321,7 @@ static private function parseMapping($mapping, &$i = 0)
}
}

throw new ParserException(sprintf('Malformed inline YAML string %s', $mapping));
throw new ParseException(sprintf('Malformed inline YAML string %s', $mapping));
}

/**
Expand Down

0 comments on commit c3e1d03

Please sign in to comment.