Permalink
Browse files

dev release

  • Loading branch information...
0 parents commit 82e0047e73b217d464e632d9257acce6dd8d84af heartsentwined committed Sep 17, 2012
Showing with 183 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +10 −0 .travis.yml
  3. +37 −0 README.md
  4. +24 −0 composer.json
  5. +32 −0 src/Heartsentwined/Yaml/Yaml.php
  6. +67 −0 test/Heartsentwined/Test/Yaml/YamlTest.php
  7. +3 −0 test/bootstrap.php
  8. +7 −0 test/phpunit.xml
@@ -0,0 +1,3 @@
+.git
+composer.lock
+vendor/
@@ -0,0 +1,10 @@
+language: php
+php:
+ - "5.3"
+ - "5.4"
+before_script: composer install
+script: phpunit --configuration test/phpunit.xml
+notifications:
+ email:
+ on_success: always
+ on_failure: always
@@ -0,0 +1,37 @@
+# Heartsentwined\Yaml
+
+[![Build Status](https://secure.travis-ci.org/heartsentwined/yaml.png)](http://travis-ci.org/heartsentwined/yaml)
+
+Wrapper around Symfony's Yaml parser - added `__DIR__` support.
+
+# Installation
+
+[Composer](http://getcomposer.org/):
+
+```json
+{
+ "require": {
+ "heartsentwined/yaml": "1.*"
+ }
+}
+```
+
+# Usage
+
+Two constants added:
+- `__DIR__`: behave as expected
+- `___DIR___` (an extra underscore around): literal `__DIR__`
+
+Parse a Yaml file `foo/bar.yml`:
+
+```php
+use Heartsentwined\Yaml\Yaml;
+$parsedArray = Yaml::parse('foo/bar.yml');
+```
+
+Parse a Yaml string `foo: bar` (of course, neither `__DIR__` nor `___DIR___` would be available)
+
+```php
+use Heartsentwined\Yaml\Yaml;
+$parsedArray = Yaml::parse('foo: bar');
+```
@@ -0,0 +1,24 @@
+{
+ "name": "heartsentwined/yaml",
+ "description": "Wrapper around Symfony's Yaml parser - added __DIR__ support.",
+ "license": "GPL-3.0",
+ "keywords": ["yaml", "yml"],
+ "homepage": "https://github.com/heartsentwined/yaml",
+ "authors": [
+ {
+ "name": "heartsentwined",
+ "email": "heartsentwined@cogito-lab.com",
+ "role": "Developer"
+ }
+ ],
+ "autoload": {
+ "psr-0": {
+ "Heartsentwined\\Yaml": "src/"
+ }
+ },
+ "require": {
+ "php": ">=5.3.3",
+ "heartsentwined/arg-validator": "1.*",
+ "symfony/yaml": "2.0.*"
+ }
+}
@@ -0,0 +1,32 @@
+<?php
+namespace Heartsentwined\Yaml;
+
+use Heartsentwined\ArgValidator\ArgValidator;
+use Symfony\Component\Yaml\Yaml as SymfonyYaml;
+
+class Yaml
+{
+ /**
+ * Yaml parser
+ *
+ * __DIR__ support: (only available for Yaml files, of course!)
+ * - '__DIR__' will behave as expected
+ * - '___DIR___' (an extra underscore around) will be translated as a literal '__DIR__'
+ *
+ * @param mixed $yaml
+ * @return self
+ */
+ public static function parse($yaml)
+ {
+ ArgValidator::assert($yaml, 'string');
+ if (is_file($yaml) && is_readable($yaml)) {
+ $dir = realpath($yaml);
+ $yaml = file_get_contents($yaml);
+ $yaml = strtr($yaml, array(
+ '___DIR___' => '__DIR__',
+ '__DIR__' => $dir,
+ ));
+ }
+ return SymfonyYaml::parse($yaml);
+ }
+}
@@ -0,0 +1,67 @@
+<?php
+namespace Heartsentwined\Test\Yaml;
+
+use Heartsentwined\Yaml\Yaml;
+
+class YamlTest extends \PHPUnit_Framework_TestCase
+{
+ public function setUp()
+ {
+ $this->tmpFile = '.yaml';
+ $this->tmpPath = realpath($this->tmpFile);
+ }
+
+ public function testParse()
+ {
+ // yaml stream
+ $yaml = 'foo: bar';
+ $this->assertSame(
+ array('foo' => 'bar'),
+ Yaml::parse($yaml));
+
+ // yaml file
+ file_put_contents($this->tmpFile, 'foo: bar');
+ $this->assertSame(
+ array('foo' => 'bar'),
+ Yaml::parse($this->tmpFile));
+
+ // __DIR__
+ file_put_contents($this->tmpFile, 'foo: __DIR__/bar');
+ $this->assertSame(
+ array('foo' => "{$this->tmpPath}/bar"),
+ Yaml::parse($this->tmpFile));
+
+ // ___DIR___
+ file_put_contents($this->tmpFile, 'foo: ___DIR___/bar');
+ $this->assertSame(
+ array('foo' => '__DIR__/bar'),
+ Yaml::parse($this->tmpFile));
+
+ // multiple __DIR__
+ file_put_contents($this->tmpFile, '__DIR__/foo: __DIR__/bar');
+ $this->assertSame(
+ array("{$this->tmpPath}/foo" => "{$this->tmpPath}/bar"),
+ Yaml::parse($this->tmpFile));
+
+ // multiple ___DIR___
+ file_put_contents($this->tmpFile, '___DIR___/foo: ___DIR___/bar');
+ $this->assertSame(
+ array('__DIR__/foo' => '__DIR__/bar'),
+ Yaml::parse($this->tmpFile));
+
+ // altogether
+ file_put_contents($this->tmpFile, "__DIR__/foo: ___DIR___/bar\n___DIR___bar: __DIR__/foo");
+ $this->assertSame(
+ array(
+ "{$this->tmpPath}/foo" => '__DIR__/bar',
+ '__DIR__/bar' => "{$this->tmpPath}/foo",
+ ),
+ Yaml::parse($this->tmpFile));
+
+ // neither available when passed in a string
+ $yaml = '__DIR__/foo: ___DIR___/bar';
+ $this->assertSame(
+ array('__DIR__/foo' => '___DIR___/bar'),
+ Yaml::parse($yaml));
+ }
+}
@@ -0,0 +1,3 @@
+<?php
+$loader = require_once __DIR__ . '/../vendor/autoload.php';
+$loader->add('Heartsentwined\Test', __DIR__);
@@ -0,0 +1,7 @@
+<phpunit bootstrap="./bootstrap.php" colors="true">
+ <testsuites>
+ <testsuite>
+ <directory>./</directory>
+ </testsuite>
+ </testsuites>
+</phpunit>

0 comments on commit 82e0047

Please sign in to comment.