Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PSR-0 autoloading of sfYaml classes #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor
tests/DoctrineTest/doctrine_tests/*
*TestCase.php
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"ext-pdo": "*"
},
"autoload": {
"psr-0": { "Doctrine_": "lib/" }
"psr-0": {
"Doctrine_": "lib/",
"sfYaml": "lib/Doctrine/Parser/sfYaml"
}
}
}
60 changes: 46 additions & 14 deletions lib/Doctrine/Parser/sfYaml/sfYaml.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <[email protected]>
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand Down Expand Up @@ -64,7 +64,7 @@ static public function getSpecVersion()
*
* @throws InvalidArgumentException If the YAML is not valid
*/
public static function load($input)
public static function load($input, $encoding = 'UTF-8')
{
$file = '';

Expand All @@ -87,7 +87,13 @@ public static function load($input)
return $input;
}

require_once dirname(__FILE__).'/sfYamlParser.php';
$mbConvertEncoding = false;
$encoding = strtoupper($encoding);
if ('UTF-8' != $encoding && function_exists('mb_convert_encoding'))
{
$input = mb_convert_encoding($input, 'UTF-8', $encoding);
$mbConvertEncoding = true;
}

$yaml = new sfYamlParser();

Expand All @@ -100,6 +106,11 @@ public static function load($input)
throw new InvalidArgumentException(sprintf('Unable to parse %s: %s', $file ? sprintf('file "%s"', $file) : 'string', $e->getMessage()));
}

if ($ret && $mbConvertEncoding)
{
$ret = self::arrayConvertEncoding($ret, $encoding);
}

return $ret;
}

Expand All @@ -116,20 +127,41 @@ public static function load($input)
*/
public static function dump($array, $inline = 2)
{
require_once dirname(__FILE__).'/sfYamlDumper.php';

$yaml = new sfYamlDumper();

return $yaml->dump($array, $inline);
}
}

/**
* Wraps echo to automatically provide a newline.
*
* @param string $string The string to echo with new line
*/
function echoln($string)
{
echo $string."\n";
/**
* Converts all kayes and values from UTF-8 to given encoding
*
* @param array $result Original result
* @param string $encoding The expected encoding
* @return array
*/
protected static function arrayConvertEncoding(array $result, $encoding)
{
$convertedResult = array();
foreach ($result as $key => $value)
{
if (is_string($key))
{
$key = mb_convert_encoding($key, $encoding, 'UTF-8');
}
if (is_array($value))
{
$convertedResult[$key] = self::arrayConvertEncoding($value, $encoding);
}
else if (is_string($value))
{
$convertedResult[$key] = mb_convert_encoding($value, $encoding, 'UTF-8');
}
else
{
$convertedResult[$key] = $value;
}
}

return $convertedResult;
}
}
2 changes: 0 additions & 2 deletions lib/Doctrine/Parser/sfYaml/sfYamlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
* file that was distributed with this source code.
*/

require_once(dirname(__FILE__).'/sfYamlInline.php');

/**
* sfYamlDumper dumps PHP variables to YAML strings.
*
Expand Down
31 changes: 18 additions & 13 deletions lib/Doctrine/Parser/sfYaml/sfYamlInline.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
* file that was distributed with this source code.
*/

require_once dirname(__FILE__).'/sfYaml.php';

/**
* sfYamlInline implements a YAML parser/dumper for the YAML inline syntax.
*
Expand All @@ -33,7 +31,7 @@ static public function load($value)
{
$value = trim($value);

if (0 == strlen($value))
if ('' === $value)
{
return '';
}
Expand Down Expand Up @@ -87,7 +85,8 @@ static public function dump($value)
switch (true)
{
case is_resource($value):
throw new InvalidArgumentException('Unable to dump PHP resources in a YAML file.');
return stream_get_contents($value);
// throw new InvalidArgumentException('Unable to dump PHP resources in a YAML file.');
case is_object($value):
return '!!php/object:'.serialize($value);
case is_array($value):
Expand All @@ -101,7 +100,7 @@ static public function dump($value)
case ctype_digit($value):
return is_string($value) ? "'$value'" : (int) $value;
case is_numeric($value):
return is_infinite($value) ? str_ireplace('INF', '.Inf', strval($value)) : (is_string($value) ? "'$value'" : $value);
return is_infinite($value) ? str_ireplace('INF', '.Inf', (string) $value) : (is_string($value) ? "'$value'" : $value);
case false !== strpos($value, "\n") || false !== strpos($value, "\r"):
return sprintf('"%s"', str_replace(array('"', "\n", "\r"), array('\\"', '\n', '\r'), $value));
case preg_match('/[ \s \' " \: \{ \} \[ \] , & \* \# \?] | \A[ - ? | < > = ! % @ ` ]/x', $value):
Expand Down Expand Up @@ -135,7 +134,7 @@ static protected function dumpArray($value)
if (
(1 == count($keys) && '0' == $keys[0])
||
(count($keys) > 1 && array_reduce($keys, create_function('$v,$w', 'return (integer) $v + $w;'), 0) == count($keys) * (count($keys) - 1) / 2))
(count($keys) > 1 && array_sum(array_map('intval', $keys)) == count($keys) * (count($keys) - 1) / 2))
{
$output = array();
foreach ($value as $val)
Expand Down Expand Up @@ -249,7 +248,7 @@ static protected function parseSequence($sequence, &$i = 0)
{
$output = array();
$len = strlen($sequence);
$i += 1;
++$i;

// [foo, bar, ...]
while ($i < $len)
Expand Down Expand Up @@ -309,7 +308,7 @@ static protected function parseMapping($mapping, &$i = 0)
{
$output = array();
$len = strlen($mapping);
$i += 1;
++$i;

// {foo: bar, bar:foo, ...}
while ($i < $len)
Expand Down Expand Up @@ -395,26 +394,32 @@ static protected function evaluateScalar($scalar)
case 0 === strpos($scalar, '!str'):
return (string) substr($scalar, 5);
case 0 === strpos($scalar, '! '):
return intval(self::parseScalar(substr($scalar, 2)));
return (int) self::parseScalar(substr($scalar, 2));
case 0 === strpos($scalar, '!!php/object:'):
return unserialize(substr($scalar, 13));
case ctype_digit($scalar):
$raw = $scalar;
$cast = intval($scalar);
$cast = (int) $scalar;
return '0' == $scalar[0] ? octdec($scalar) : (((string) $raw == (string) $cast) ? $cast : $raw);
case in_array(strtolower($scalar), $trueValues):
return true;
case in_array(strtolower($scalar), $falseValues):
return false;
case 0 === strpos($scalar, '0x'):
return hexdec($scalar);
case is_numeric($scalar):
return '0x' == $scalar[0].$scalar[1] ? hexdec($scalar) : floatval($scalar);
return floatval($scalar);
case 0 == strcasecmp($scalar, '.inf'):
case 0 == strcasecmp($scalar, '.NaN'):
return -log(0);
case 0 == strcasecmp($scalar, '-.inf'):
return log(0);
case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar):
return floatval(str_replace(',', '', $scalar));
case preg_match('/^(-|\+)?[0-9,]+(\.\d+)?$/', $scalar):
$replaced = str_replace(',', '', $scalar);
$replaced = str_replace('+', '', $replaced);
$floatval = floatval($replaced);
$intval = intval($replaced);
return $floatval == $intval ? $intval : $floatval;
case preg_match(self::getTimestampRegex(), $scalar):
return strtotime($scalar);
default:
Expand Down
11 changes: 7 additions & 4 deletions lib/Doctrine/Parser/sfYaml/sfYamlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
* file that was distributed with this source code.
*/

require_once(dirname(__FILE__).'/sfYamlInline.php');

if (!defined('PREG_BAD_UTF8_OFFSET_ERROR'))
{
define('PREG_BAD_UTF8_OFFSET_ERROR', 5);
Expand Down Expand Up @@ -57,6 +55,11 @@ public function parse($value)
$this->currentLine = '';
$this->lines = explode("\n", $this->cleanup($value));

if (function_exists('mb_detect_encoding') && false === mb_detect_encoding($value, 'UTF-8', true))
{
throw new InvalidArgumentException('The YAML value does not appear to be valid UTF-8.');
}

if (function_exists('mb_internal_encoding') && ((int) ini_get('mbstring.func_overload')) & 2)
{
$mbEncoding = mb_internal_encoding();
Expand Down Expand Up @@ -168,7 +171,7 @@ public function parse($value)
else
{
// Associative array, merge
$merged = array_merge($merge, $parsed);
$merged = array_merge($merged, $parsed);
}

$isProcessed = $merged;
Expand Down Expand Up @@ -418,7 +421,7 @@ protected function parseValue($value)
{
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';

return $this->parseFoldedScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), intval(abs($modifiers)));
return $this->parseFoldedScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
}
else
{
Expand Down