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

Add PSR-0 autoloading of sfYaml classes #34

wants to merge 2 commits into from

Conversation

endelwar
Copy link

This PR should fix #25

@zploskey
Copy link

I long ago added something similar to our project to autoload sfyaml from doctrine1:

    "autoload": {
        "psr-0": {
            "sfYaml": "vendor/lexpress/doctrine1/lib/Doctrine/Parser/sfYaml"
        }
    }

Without it we had problems with some of the doctrine CLI features that deal with yaml. It would be nice not to have to add this manually.

One suggestion for the PR would be to not change the indentation for the whole file, keeping the 4 space indentation, minimizing whitespace changes. It looks like you just added one line to composer.json and gitignored vendor/.

@endelwar
Copy link
Author

@zploskey I'm so used to 2 space indentation that I didn't though about it: I've fixed identation, but I like that the psr-0 section is on 2 separated lines, I thimnk that is more clear

@zploskey
Copy link

Agreed. This looks good to me, but you'll have to track down a committer.

@GromNaN
Copy link
Collaborator

GromNaN commented Oct 1, 2017

The sfYaml component is duplicated in doctrine1 and symfony1 libraries. If anything diverged between both copies, this change might lead to complex issues.

@GromNaN
Copy link
Collaborator

GromNaN commented Oct 1, 2017

$ diff symfony1/lib/yaml/sfYaml.class.php doctrine1/lib/Doctrine/Parser/sfYaml/sfYaml.php
5c5
<  * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
---
>  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
67c67
<   public static function load($input, $encoding = 'UTF-8')
---
>   public static function load($input)
90,96c90
<     $mbConvertEncoding = false;
<     $encoding = strtoupper($encoding);
<     if ('UTF-8' != $encoding && function_exists('mb_convert_encoding'))
<     {
<       $input = mb_convert_encoding($input, 'UTF-8', $encoding);
<       $mbConvertEncoding = true;
<     }
---
>     require_once dirname(__FILE__).'/sfYamlParser.php';
109,113d102
<     if ($ret && $mbConvertEncoding)
<     {
<       $ret = self::arrayConvertEncoding($ret, $encoding);
<     }
<
129a119,120
>     require_once dirname(__FILE__).'/sfYamlDumper.php';
>
133a125
> }
135,166c127,134
<   /**
<    * 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;
<   }
---
> /**
>  * Wraps echo to automatically provide a newline.
>  *
>  * @param string $string The string to echo with new line
>  */
> function echoln($string)
> {
>   echo $string."\n";
$ diff symfony1/lib/yaml/sfYamlDumper.class.php doctrine1/lib/Doctrine/Parser/sfYaml/sfYamlDumper.php
10a11,12
> require_once(dirname(__FILE__).'/sfYamlInline.php');
>
$ diff symfony1/lib/yaml/sfYamlInline.class.php doctrine1/lib/Doctrine/Parser/sfYaml/sfYamlInline.php
10a11,12
> require_once dirname(__FILE__).'/sfYaml.php';
>
34c36
<     if ('' === $value)
---
>     if (0 == strlen($value))
88,89c90
<         return stream_get_contents($value);
<         // throw new InvalidArgumentException('Unable to dump PHP resources in a YAML file.');
---
>         throw new InvalidArgumentException('Unable to dump PHP resources in a YAML file.');
103c104
<         return is_infinite($value) ? str_ireplace('INF', '.Inf', (string) $value) : (is_string($value) ? "'$value'" : $value);
---
>         return is_infinite($value) ? str_ireplace('INF', '.Inf', strval($value)) : (is_string($value) ? "'$value'" : $value);
137c138
<       (count($keys) > 1 && array_sum(array_map('intval', $keys)) == count($keys) * (count($keys) - 1) / 2))
---
>       (count($keys) > 1 && array_reduce($keys, create_function('$v,$w', 'return (integer) $v + $w;'), 0) == count($keys) * (count($keys) - 1) / 2))
251c252
<     ++$i;
---
>     $i += 1;
311c312
<     ++$i;
---
>     $i += 1;
397c398
<         return (int) self::parseScalar(substr($scalar, 2));
---
>         return intval(self::parseScalar(substr($scalar, 2)));
402c403
<         $cast = (int) $scalar;
---
>         $cast = intval($scalar);
408,409d408
<       case 0 === strpos($scalar, '0x'):
<         return hexdec($scalar);
411c410
<         return floatval($scalar);
---
>         return '0x' == $scalar[0].$scalar[1] ? hexdec($scalar) : floatval($scalar);
417,422c416,417
<       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('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar):
>         return floatval(str_replace(',', '', $scalar));
$ diff symfony1/lib/yaml/sfYamlParser.class.php doctrine1/lib/Doctrine/Parser/sfYaml/sfYamlParser.php
10a11,12
> require_once(dirname(__FILE__).'/sfYamlInline.php');
>
58,62d59
<     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.');
<     }
<
174c171
<               $merged = array_merge($merged, $parsed);
---
>               $merged = array_merge($merge, $parsed);
424c421
<       return $this->parseFoldedScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
---
>       return $this->parseFoldedScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), intval(abs($modifiers)));

@endelwar
Copy link
Author

endelwar commented Oct 2, 2017

@GromNaN looks like sfYaml in symfony has been updated to autoload classes and manage encoding better. I can work on this testing the new yaml library version with doctrine1 or creating a doctrine namespace for yaml classes if this doesn't work.

This update fixes a bug in array merging, permits dumping of PHP resource in a YAML file, fixes dumpArray() for PHP 7.1, enforce UTF-8 encoding of YAML files, and remove unused echoln() function
@endelwar
Copy link
Author

endelwar commented Oct 2, 2017

The symfony1 sfYaml library is fully compatible with doctrine1

@regisnew
Copy link

regisnew commented Oct 2, 2019

$this PR should fix #35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PHP Notices showing up when trying to build schema yaml from db
4 participants