Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
Added Exception `UnableToParseFormatException`
Improved PHP Adapter, support for non extension path
Improved JSON Adapter, just more clear
Fixed bug with AbstractAdapter, support for non extension path
  • Loading branch information
VeeeneX committed Jul 6, 2015
1 parent 8e2a8f6 commit 221d657
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 21 deletions.
13 changes: 6 additions & 7 deletions src/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
class AbstractAdapter implements AdapterInterface
{
public $basePath;
public $extension;

public function setBasePath($basePath)
{
Expand All @@ -45,18 +46,16 @@ public function load($data)

public function loadFile($file)
{
$data = $this->getContent($file);

if ($data) {
$this->parse($data);
if ($data = $this->getContent($file)) {
return $this->parse($data);
}

return null;
}

private function getValidPath($path)
{
if (file_exists($basePath.$path)) {
if (file_exists($path)) {
return true;
}

Expand All @@ -66,8 +65,8 @@ private function getValidPath($path)

protected function getContent($path)
{
if ($this->getValidPath($this->basePath.$path)) {
return file_get_contents($this->basePath.$path);
if ($this->getValidPath($this->basePath.$path.$this->extension)) {
return file_get_contents($this->basePath.$path.$this->extension);
}

return null;
Expand Down
6 changes: 3 additions & 3 deletions src/Adapter/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

class JSON extends AbstractAdapter
{
public $extension = ".php";

public function parse($data)
{
$data = json_decode($data, true);

return $data;
return json_decode($data, true);
}
}
26 changes: 16 additions & 10 deletions src/Adapter/PHP.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,32 @@
namespace Aurora\Adapter;

use Aurora\AbstractAdapter;
use Aurora\Exception\UnableToParseFormatException;

class PHP extends AbstractAdapter
{
public $extension = ".php";

public function parse($path){
public function parse($data)
{

try {
$temp = require $path;
} catch (\Exception $e) {
// To do throw errors
if (is_callable($data)) {
$data = call_user_func($data);
}

if (is_callable($temp)) {
$temp = call_user_func($temp);
if (!$data || !is_array($data)) {
throw new UnableToParseFormatException;
}

if (!$temp || !is_array($temp)) {
// To do throw error
return $data;
}

protected function getContent($path)
{
if ($this->getValidPath($this->basePath.$path.$this->extension)) {
return require_once $this->basePath.$path.$this->extension;
}

return $temp;
return null;
}
}
9 changes: 9 additions & 0 deletions src/Exception/UnableToParseFormatException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Aurora\Exception;

use Exception;

class UnableToParseFormatException extends Exception
{
}
1 change: 0 additions & 1 deletion src/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public static function extension($file)
public static function mime($file, $guess = true)
{
if (function_exists('finfo_open')) {
// Get mime using the file information functions
$info = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($info, $file);
finfo_close($info);
Expand Down

0 comments on commit 221d657

Please sign in to comment.