-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core logic for file system classes added
- Loading branch information
1 parent
e013c6e
commit c376d58
Showing
9 changed files
with
308 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,21 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: cyberinferno | ||
* Date: 21-Sep-19 | ||
* Time: 1:14 PM | ||
*/ | ||
|
||
namespace cyberinferno\Cabal\Helpers\Exceptions; | ||
|
||
|
||
class FileBadFormatException | ||
/** | ||
* Class FileBadFormatException | ||
* | ||
* Exception thrown when a specified file is not in expected format | ||
* | ||
* @package cyberinferno\Cabal\Helpers\Exceptions | ||
* @author Karthik P | ||
*/ | ||
class FileBadFormatException extends \Exception | ||
{ | ||
|
||
public function __construct($message = '', $code = 0, \Exception $previous = null) { | ||
if (empty($message)) { | ||
$message = 'Format of the specified file was bad and hence could not be read'; | ||
} | ||
parent::__construct($message, $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,21 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: cyberinferno | ||
* Date: 21-Sep-19 | ||
* Time: 1:06 PM | ||
*/ | ||
|
||
namespace cyberinferno\Cabal\Helpers\Exceptions; | ||
|
||
|
||
class FileNotFoundException | ||
/** | ||
* Class FileNotFoundException | ||
* | ||
* Exception thrown when a specified file was not found or could not be accessed | ||
* | ||
* @package cyberinferno\Cabal\Helpers\Exceptions | ||
* @author Karthik P | ||
*/ | ||
class FileNotFoundException extends \Exception | ||
{ | ||
|
||
public function __construct($message = '', $code = 0, \Exception $previous = null) { | ||
if (empty($message)) { | ||
$message = 'File not found'; | ||
} | ||
parent::__construct($message, $code, $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,71 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: cyberinferno | ||
* Date: 21-Sep-19 | ||
* Time: 1:29 PM | ||
*/ | ||
|
||
namespace cyberinferno\Cabal\Helpers\FileSystem\Client; | ||
|
||
use cyberinferno\Cabal\Helpers\Exceptions\FileBadFormatException; | ||
use cyberinferno\Cabal\Helpers\FileSystem\File; | ||
|
||
class CabalMsg | ||
/** | ||
* Class CabalMsg | ||
* | ||
* Helper class to load contents from cabal_msg.dec file | ||
* | ||
* @package cyberinferno\Cabal\Helpers\FileSystem\Client | ||
* @author Karthik P | ||
*/ | ||
class CabalMsg extends File | ||
{ | ||
/** | ||
* @var \SimpleXMLElement | ||
*/ | ||
protected $_xmlObject; | ||
|
||
/** | ||
* Returns the XML object of the specified cabal_msg.dec file | ||
* | ||
* @return \SimpleXMLElement | ||
*/ | ||
public function getXmlObject() | ||
{ | ||
if (isset($this->_xmlObject) === false) { | ||
libxml_use_internal_errors(true); | ||
$domDocument = new \DOMDocument(); | ||
$domDocument->recover = true; | ||
$domDocument->loadXML($this->getFileContents()); | ||
$this->_xmlObject = simplexml_load_string($domDocument->saveXML()); | ||
} | ||
return $this->_xmlObject; | ||
} | ||
|
||
/** | ||
* Returns array representation of attributes of the XML tree node attribute | ||
* If $topLevelKey is sent only that node's attributes are returned | ||
* | ||
* WARNING: This method might take lot of time to complete when XML tree is large | ||
* | ||
* @param string $topLevelKey | ||
* @return array | ||
*/ | ||
public function getArray($topLevelKey = '') | ||
{ | ||
$xmlObject = $this->getXmlObject(); | ||
if (!empty($topLevelKey)) { | ||
return get_object_vars($xmlObject->$topLevelKey); | ||
} | ||
return get_object_vars($xmlObject); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function validateFormat() | ||
{ | ||
if (filesize($this->_filePath) == 0 || $this->getFileContents() === null) { | ||
throw new FileBadFormatException(); | ||
} | ||
$fileContents = $this->getFileContents(); | ||
if (substr($fileContents, 0, strlen('<cabal_message>')) !== '<cabal_message>') { | ||
throw new FileBadFormatException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,70 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: cyberinferno | ||
* Date: 21-Sep-19 | ||
* Time: 6:35 PM | ||
*/ | ||
|
||
namespace cyberinferno\Cabal\Helpers\FileSystem; | ||
|
||
use cyberinferno\Cabal\Helpers\Exceptions\FileBadFormatException; | ||
use cyberinferno\Cabal\Helpers\Exceptions\FileNotFoundException; | ||
|
||
class File | ||
/** | ||
* Class File | ||
* | ||
* Base class for all file system classes. Has basic file loading and validating methods | ||
* | ||
* @package cyberinferno\Cabal\Helpers\FileSystem | ||
* @author Karthik P | ||
*/ | ||
abstract class File | ||
{ | ||
protected $_filePath; | ||
protected $_fileContents; | ||
|
||
/** | ||
* File constructor. | ||
* @param string $filePath | ||
* @throws FileBadFormatException | ||
* @throws FileNotFoundException | ||
*/ | ||
public function __construct($filePath) | ||
{ | ||
$this->_filePath = $filePath; | ||
$this->validateFileExists(); | ||
$this->validateFormat(); | ||
} | ||
|
||
/** | ||
* Checks whether the specified file exists | ||
* | ||
* @throws FileNotFoundException | ||
*/ | ||
protected function validateFileExists() | ||
{ | ||
if (file_exists($this->_filePath) === false) { | ||
throw new FileNotFoundException(); | ||
} | ||
} | ||
|
||
/** | ||
* Checks whether the specified file isn't empty | ||
* | ||
* @throws FileBadFormatException | ||
*/ | ||
protected function validateFormat() | ||
{ | ||
if (filesize($this->_filePath) == 0 || $this->getFileContents() === null) { | ||
throw new FileBadFormatException(); | ||
} | ||
} | ||
|
||
/** | ||
* Loads the file contents if not already loaded and returns the same | ||
* | ||
* @return null|string | ||
*/ | ||
public function getFileContents() | ||
{ | ||
if (isset($this->_fileContents) === false) { | ||
$this->_fileContents = file_get_contents($this->_filePath); | ||
} | ||
return $this->_fileContents; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,41 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: cyberinferno | ||
* Date: 22-Sep-19 | ||
* Time: 12:32 AM | ||
*/ | ||
|
||
namespace cyberinferno\Cabal\Helpers\FileSystem\Server; | ||
|
||
use cyberinferno\Cabal\Helpers\Exceptions\FileBadFormatException; | ||
use cyberinferno\Cabal\Helpers\FileSystem\File; | ||
|
||
class WorldDrop | ||
/** | ||
* Class WorldDrop | ||
* | ||
* Helper class to load contents from World_drop.scp file | ||
* | ||
* @package cyberinferno\Cabal\Helpers\FileSystem\Server | ||
*/ | ||
class WorldDrop extends File | ||
{ | ||
/** | ||
* Returns array representation of the World_dop.scp file | ||
* | ||
* @return array | ||
*/ | ||
public function getArray() | ||
{ | ||
$fileContents = $this->getFileContents(); | ||
return explode("\n", $fileContents); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function validateFormat() | ||
{ | ||
if (filesize($this->_filePath) == 0 || $this->getFileContents() === null) { | ||
throw new FileBadFormatException(); | ||
} | ||
$fileContents = $this->getFileContents(); | ||
if (substr($fileContents, 0, strlen('[WorldDrop]')) !== '[WorldDrop]') { | ||
throw new FileBadFormatException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,66 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: cyberinferno | ||
* Date: 21-Sep-19 | ||
* Time: 8:20 PM | ||
*/ | ||
|
||
class CabalMsgTest | ||
|
||
class CabalMsgTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
public $fileSystem; | ||
|
||
public function setUp() | ||
{ | ||
$directory = [ | ||
'client' => [ | ||
'cabal_msg.dec' => '<cabal_message><version index="3" /></cabal_message>', | ||
'cabal_msg_invalid.dec' => '<cabal_message' | ||
] | ||
]; | ||
$this->fileSystem = \org\bovigo\vfs\vfsStream::setup('root', 444, $directory); | ||
} | ||
|
||
public function testIsThereAnySyntaxError() | ||
{ | ||
$myClass = new \cyberinferno\Cabal\Helpers\FileSystem\Client\CabalMsg($this->fileSystem->url() . '/client/cabal_msg.dec'); | ||
$this->assertTrue(is_object($myClass)); | ||
unset($myClass); | ||
} | ||
|
||
/** | ||
* @expectedException \cyberinferno\Cabal\Helpers\Exceptions\FileNotFoundException | ||
*/ | ||
public function testFileNotFoundExceptionIsThrownWhenNoFile() | ||
{ | ||
$myClass = new \cyberinferno\Cabal\Helpers\FileSystem\Client\CabalMsg( | ||
$this->fileSystem->url() . '/unknown.dec' | ||
); | ||
unset($myClass); | ||
} | ||
|
||
/** | ||
* @expectedException \cyberinferno\Cabal\Helpers\Exceptions\FileBadFormatException | ||
*/ | ||
public function testFileBadFormatExceptionIsThrownWhenNoFile() | ||
{ | ||
$myClass = new \cyberinferno\Cabal\Helpers\FileSystem\Client\CabalMsg( | ||
$this->fileSystem->url() . '/client/cabal_msg_invalid.dec' | ||
); | ||
unset($myClass); | ||
} | ||
|
||
public function testGetXmlObject() | ||
{ | ||
$myClass = new \cyberinferno\Cabal\Helpers\FileSystem\Client\CabalMsg( | ||
$this->fileSystem->url() . '/client/cabal_msg.dec' | ||
); | ||
$result = $myClass->getXmlObject(); | ||
$this->assertTrue($result instanceof \SimpleXMLElement); | ||
} | ||
|
||
public function testGetArray() | ||
{ | ||
$myClass = new \cyberinferno\Cabal\Helpers\FileSystem\Client\CabalMsg( | ||
$this->fileSystem->url() . '/client/cabal_msg.dec' | ||
); | ||
$result = $myClass->getArray(); | ||
$this->assertTrue(is_array($result)); | ||
$result = $myClass->getArray('version'); | ||
$this->assertTrue(!empty($result)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.