Skip to content
This repository has been archived by the owner on Aug 22, 2018. It is now read-only.

Commit

Permalink
Add file loader for JSON
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
mloberg committed Dec 31, 2015
1 parent f1941c6 commit 6d0d060
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public function load($fileName, $refresh = false)

$loaderResolver = new LoaderResolver(array(
new YamlFileLoader($locator),
new JsonFileLoader($locator),
));

$delegatingLoader = new DelegatingLoader($loaderResolver);
Expand Down
33 changes: 33 additions & 0 deletions src/JsonFileLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/*
* Copyright (c) 2015 Matthew Loberg
* Distributed under the MIT License (http://opensource.org/licenses/MIT)
*/

namespace Mlo\FileLoader;

use Symfony\Component\Config\Loader\FileLoader as BaseFileLoader;

/**
* JsonFileLoader
*
* @author Matthew Loberg <loberg.matt@gmail.com>
*/
class JsonFileLoader extends BaseFileLoader
{
/**
* @inheritDoc
*/
public function load($resource, $type = null)
{
return json_decode(file_get_contents($resource), true);
}

/**
* @inheritDoc
*/
public function supports($resource, $type = null)
{
return is_string($resource) && 'json' === pathinfo($resource, PATHINFO_EXTENSION);
}
}
18 changes: 17 additions & 1 deletion tests/FileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function testSetGetCacheDirectory()
/**
* @covers ::load
*/
public function testLoad()
public function testLoadYaml()
{
$this->loader->addDirectory($this->dataDirectory);

Expand All @@ -108,6 +108,22 @@ public function testLoad()
$this->assertTrue(file_exists($this->cacheDirectory . '/test.yml.php'));
}

/**
* @covers ::load
*/
public function testLoadJson()
{
$this->loader->addDirectory($this->dataDirectory);

$this->assertFalse(file_exists($this->cacheDirectory . '/data.json.php'));

$values = $this->loader->load('data.json');

$this->assertEquals('bar', $values['foo']);

$this->assertTrue(file_exists($this->cacheDirectory . '/data.json.php'));
}

/**
* @covers ::load
*/
Expand Down
52 changes: 52 additions & 0 deletions tests/JsonFileLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/*
* Copyright (c) 2015 Matthew Loberg
* Distributed under the MIT License (http://opensource.org/licenses/MIT)
*/

namespace Mlo\FileLoader\Tests;

use Mlo\FileLoader\JsonFileLoader;
use Symfony\Component\Config\FileLocator;

/**
* @coversDefaultClass \Mlo\FileLoader\JsonFileLoader
*/
class JsonFileLoaderTest extends \PHPUnit_Framework_TestCase
{
/**
* @var JsonFileLoader
*/
private $jsonFileLoader;

/**
* @inheritDoc
*/
protected function setUp()
{
$this->jsonFileLoader = new JsonFileLoader(new FileLocator());
}

/**
* @covers ::supports
*/
public function testSupports()
{
$this->assertFalse($this->jsonFileLoader->supports(new \stdClass()));
$this->assertTrue($this->jsonFileLoader->supports('data.json'));
$this->assertFalse($this->jsonFileLoader->supports('data.yml'));
}

/**
* @covers ::load
*/
public function testLoad()
{
$values = $this->jsonFileLoader->load(__DIR__ . '/data/data.json');

$this->assertInternalType('array', $values);
$this->assertEquals('bar', $values['foo']);
$this->assertEquals('world', $values['name']);
$this->assertEquals('hello', $values['greeting']);
}
}
5 changes: 5 additions & 0 deletions tests/data/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"foo": "bar",
"name": "world",
"greeting": "hello"
}

0 comments on commit 6d0d060

Please sign in to comment.