Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
fixes some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
prisis committed Apr 26, 2016
1 parent 7a32ad6 commit b79bac1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 118 deletions.
5 changes: 3 additions & 2 deletions src/Viserio/Contracts/Filesystem/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
interface Loader
{
/**
* Load the given configuration group.
* Load the given file path.
*
* @param string $file
* @param string $tag
*
* @return array
*/
public function load($file);
public function load($file, $tag = null);

/**
* Determine if the given file exists.
Expand Down
87 changes: 20 additions & 67 deletions src/Viserio/Filesystem/FileLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use Narrowspark\Arr\StaticArr as Arr;
use Viserio\Contracts\Filesystem\Loader as LoaderContract;
use Viserio\Contracts\Parsers\Parser as ParserContract;
use Viserio\Contracts\Parsers\TaggableParser as TaggableParserContract;
use Viserio\Parsers\IniParser;
use Viserio\Parsers\JsonParser;
use Viserio\Parsers\PHPParser;
Expand All @@ -19,7 +19,7 @@ class FileLoader implements LoaderContract
/**
* The parser instance.
*
* @var ParserContract
* @var TaggableParserContract
*/
protected $parser;

Expand All @@ -30,13 +30,6 @@ class FileLoader implements LoaderContract
*/
protected $directories;

/**
* All of the named path hints.
*
* @var array
*/
protected $hints = [];

/**
* A cache of whether namespaces and groups exists.
*
Expand All @@ -47,11 +40,11 @@ class FileLoader implements LoaderContract
/**
* Create a new fileloader.
*
* @param FilesystemContract $files
* @param ParserContract $parser
* @param array $directories
* @param FilesystemContract $files
* @param TaggableParserContract $parser
* @param array $directories
*/
public function __construct(ParserContract $parser, array $directories)
public function __construct(TaggableParserContract $parser, array $directories)
{
$this->parser = $parser;
$this->directories = $directories;
Expand Down Expand Up @@ -108,43 +101,34 @@ public function addDirectory($directory)
}

/**
* Load the given file path.
*
* @param string $file
*
* @return array
* {@inheritdoc}
*/
public function load($file)
public function load($file, $tag = null)
{
// Determine if the given file exists.
$path = $this->exists($file);

$parser = $this->parser;

if ($tag !== null) {
$parser->setTag($tag);
}

// Set the right Parser for data and return data array
return $this->parser->parse($path);
return $parser->parse($path);
}

/**
* Determine if the given file exists.
*
* @param string $file
*
* @return bool|string
* {@inheritdoc}
*/
public function exists($file)
{
$key = str_replace('/', '', $namespace . $file);

// We'll first check to see if we have determined if this namespace
// combination have been checked before. If they have, we will
// just return the cached result so we don't have to hit the disk.
if (isset($this->exists[$envKey])) {
return $this->exists[$envKey];
}
$key = str_replace('/', '', $file);

// Finally, we can simply check if this file exists. We will also cache
// the value in an array so we don't have to go through this process
// again on subsequent checks for the existing of the data file.
$path = $this->getPath($namespace, $file);
$path = $this->getPath($file);
$file = $this->normalizeDirectorySeparator($path . $file);

if ($this->parser->getFilesystem()->has($file)) {
Expand All @@ -158,45 +142,14 @@ public function exists($file)
}

/**
* Add a new namespace to the loader.
*
* @param string $namespace
* @param string $hint
*
* @return self
*/
public function addNamespace($namespace, $hint)
{
$this->hints[$namespace] = $hint;

return $this;
}

/**
* Returns all registered namespaces with the data
* loader.
* Get the data path for a file.
*
* @return array
*/
public function getNamespaces()
{
return $this->hints;
}

/**
* Get the data path for a namespace.
*
* @param string $namespace
* @param string $file
*
* @return string
*/
protected function getPath($namespace, $file)
protected function getPath($file)
{
if (isset($this->hints[$namespace])) {
return $this->normalizeDirectorySeparator($this->hints[$namespace] . '/');
}

foreach ($this->directories as $directory) {
$file = $this->normalizeDirectorySeparator($directory . '/' . $file);

Expand Down
71 changes: 31 additions & 40 deletions src/Viserio/Filesystem/Tests/FileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use org\bovigo\vfs\vfsStream;
use Viserio\Filesystem\FileLoader;
use Viserio\Filesystem\Filesystem;
use Viserio\Parsers\Parser;
use Viserio\Parsers\TaggableParser;
use Viserio\Support\Traits\NormalizePathAndDirectorySeparatorTrait;

class FileLoaderTest extends \PHPUnit_Framework_TestCase
Expand All @@ -24,7 +24,7 @@ class FileLoaderTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
$this->root = vfsStream::setup();
$this->fileloader = new FileLoader(new Parser(new Filesystem()), []);
$this->fileloader = new FileLoader(new TaggableParser(new Filesystem()), []);
}

public function testLoad()
Expand All @@ -46,57 +46,48 @@ public function testLoad()
$this->assertSame(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5], $data);
}

/**
* @expectedException Viserio\Contracts\Filesystem\Exception\UnsupportedFormatException
* @expectedExceptionMessage Unable to find the right Parser for [inia].
*/
public function testLoadToThrowException()
{
$this->fileloader->load('test.inia');
}

public function testLoadwithGroup()
{
$data = $this->fileloader->load('test.ini', 'Test');
$file = vfsStream::newFile('temp.json')->withContent(
'
{
"a":1,
"b":2,
"c":3,
"d":4,
"e":5
}
'
)->at($this->root);

$this->assertSame(['Test::one' => '1', 'Test::five' => '5', 'Test::animal' => 'BIRD'], $data);
}
$data = $this->fileloader->load($file->url(), 'Test');

public function testExistswithEnvironment()
{
$exist = $this->fileloader->exists('test.ini', null, 'production', null);
$this->assertSame($this->normalizeDirectorySeparator(__DIR__ . '/Fixture/production/test.ini'), $exist);
$this->assertSame(['Test::a' => 1, 'Test::b' => 2, 'Test::c' => 3, 'Test::d' => 4, 'Test::e' => 5], $data);
}

public function testExistsWithCache()
{
$exist = $this->fileloader->exists('test.json');
$this->assertSame($this->normalizeDirectorySeparator(__DIR__ . '/Fixture/test.json'), $exist);

$exist2 = $this->fileloader->exists('test.json');
$this->assertSame($this->normalizeDirectorySeparator(__DIR__ . '/Fixture/test.json'), $exist2);

$envExist1 = $this->fileloader->exists('test.ini', null, 'production', null);
$this->assertSame($this->normalizeDirectorySeparator(__DIR__ . '/Fixture/production/test.ini'), $envExist1);

$envExist2 = $this->fileloader->exists('test.ini', null, 'production', null);
$this->assertSame($this->normalizeDirectorySeparator(__DIR__ . '/Fixture/production/test.ini'), $envExist2);
}

public function testCascadePackage()
{
# code...
}
$file = vfsStream::newFile('temp.json')->withContent(
'
{
"a":1,
"b":2,
"c":3,
"d":4,
"e":5
}
'
)->at($this->root);

public function testNamespace()
{
$this->fileloader->addNamespace('foo', 'barr');
$exist = $this->fileloader->exists($file->url());
$this->assertSame($this->normalizeDirectorySeparator($file->url()), $exist);

$this->assertContains('barr', $this->fileloader->getNamespaces());
$exist2 = $this->fileloader->exists($file->url());
$this->assertSame($this->normalizeDirectorySeparator($file->url()), $exist2);
}

public function testGetParser()
{
$this->assertInstanceOf(Parser::class, $this->fileloader->getParser());
$this->assertInstanceOf(TaggableParser::class, $this->fileloader->getParser());
}
}
2 changes: 1 addition & 1 deletion src/Viserio/Parsers/TaggableParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function setTag($key)
public function parse($payload)
{
if (!$this->taggedKey) {
throw new Exception('You need to use setTag() first');
return parent::parse($payload);
}

return $this->group($this->taggedKey, parent::parse($payload));
Expand Down
8 changes: 0 additions & 8 deletions src/Viserio/Parsers/Tests/TaggableParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,4 @@ public function testParseGroup()
$this->assertTrue(is_array($parsed));
$this->assertSame(['foo::a' => 1, 'foo::e' => 5], $parsed);
}

/**
* @expectedException Exception
*/
public function testParseGroupToThrowException()
{
$this->parser->parse('nonexistfile');
}
}

0 comments on commit b79bac1

Please sign in to comment.