[enh] diagnostic:formats adds factory & resolver + tweaks
This commit is contained in:
parent
a9452725d3
commit
4c8d880272
|
@ -83,6 +83,32 @@ class Alternc_Diagnostic_Format_Abstract {
|
||||||
return $this->directory;
|
return $this->directory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks a file reference is ok
|
||||||
|
*
|
||||||
|
* @param string $file_name
|
||||||
|
* @return boolean
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
function checkIsFileReadable($file_name){
|
||||||
|
|
||||||
|
$file_path = $this->directory->getFile_path() .DIRECTORY_SEPARATOR.$file_name;
|
||||||
|
if( ! is_file( $file_path)){
|
||||||
|
throw new \Exception("Invalid file: $file_path does not exist.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if( !is_readable( $file_path)){
|
||||||
|
throw new \Exception("Invalid file: $file_path cannot be read.");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getFileContent( $file_reference ){
|
||||||
|
|
||||||
|
$file_path = $this->directory->getFile_path() .DIRECTORY_SEPARATOR.$file_reference;
|
||||||
|
return file_get_contents($file_path);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Used to unserialize files
|
||||||
|
*/
|
||||||
|
class Alternc_Diagnostic_Format_Factory{
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $file_name;
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var Alternc_Diagnostic_Directory
|
||||||
|
*/
|
||||||
|
protected $directory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param string $file_name
|
||||||
|
* @param Alternc_Diagnostic_Directory $directory
|
||||||
|
*/
|
||||||
|
public function __construct( $file_name, Alternc_Diagnostic_Directory $directory ) {
|
||||||
|
|
||||||
|
$this->file_name = $file_name;
|
||||||
|
$this->directory = $directory;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads file and converts into data
|
||||||
|
*
|
||||||
|
* @return Alternc_Diagnostic_Data
|
||||||
|
*/
|
||||||
|
function build(){
|
||||||
|
|
||||||
|
$extension = pathinfo($this->file_name, PATHINFO_EXTENSION);
|
||||||
|
$class_name = "Alternc_Diagnostic_Format_".ucfirst(strtolower($extension));
|
||||||
|
$format = new $class_name($this->directory);
|
||||||
|
$dataInstance = $format->read($this->file_name);
|
||||||
|
return $dataInstance;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -14,7 +14,7 @@ interface Alternc_Diagnostic_Format_Interface{
|
||||||
/**
|
/**
|
||||||
* Writes a Data object to file
|
* Writes a Data object to file
|
||||||
*
|
*
|
||||||
* @return boolean
|
* @return string file_name
|
||||||
*/
|
*/
|
||||||
function write();
|
function write();
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,22 @@ class Alternc_Diagnostic_Format_Json
|
||||||
*/
|
*/
|
||||||
function read( $file_reference ){
|
function read( $file_reference ){
|
||||||
|
|
||||||
|
// Attempts to check file is ok
|
||||||
|
$this->checkIsFileReadable($file_reference);
|
||||||
|
|
||||||
|
// Attempts to retrieve file content
|
||||||
|
$file_content = $this->getFileContent($file_reference);
|
||||||
|
|
||||||
|
// Attempts to convert string to json
|
||||||
|
$arrayData = json_decode($file_content,true);
|
||||||
|
|
||||||
|
// Exits if error
|
||||||
|
if(json_last_error()){
|
||||||
|
throw new \Exception("Failed to convert file $file_reference from JSON with PHP JSON_ERROR #". json_last_error());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns data object
|
||||||
|
return $this->convertJsonToData( $arrayData );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -40,8 +56,28 @@ class Alternc_Diagnostic_Format_Json
|
||||||
if( ! file_put_contents($filename, $file_content) ){
|
if( ! file_put_contents($filename, $file_content) ){
|
||||||
throw new \Exception("Failed to write in json format to file $filename for data".serialize($this->getData()));
|
throw new \Exception("Failed to write in json format to file $filename for data".serialize($this->getData()));
|
||||||
}
|
}
|
||||||
return true;
|
return $filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Operates the conversion recursively
|
||||||
|
*
|
||||||
|
* @param array $arrayData
|
||||||
|
* @return \Alternc_Diagnostic_Data
|
||||||
|
*/
|
||||||
|
function convertJsonToData( $arrayData ){
|
||||||
|
|
||||||
|
$dataInstance = new Alternc_Diagnostic_Data($arrayData["type"]);
|
||||||
|
$dataInstance->setMetadata($arrayData["metadata"]);
|
||||||
|
if( Alternc_Diagnostic_Data::TYPE_SECTION === $arrayData["type"] ){
|
||||||
|
$dataInstance->setData($arrayData["data"]);
|
||||||
|
return $dataInstance;
|
||||||
|
}
|
||||||
|
foreach($arrayData["data"] as $key => $value){
|
||||||
|
$dataInstance->addData($key, $this->convertJsonToData($value));
|
||||||
|
}
|
||||||
|
return $dataInstance;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,60 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
class Alternc_Diagnostic_Format_Resolver {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Alternc_Diagnostic_Directory
|
||||||
|
*/
|
||||||
|
public $directoryInstance;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param Alternc_Diagnostic_Directory $directoryInstance
|
||||||
|
*/
|
||||||
|
public function __construct( Alternc_Diagnostic_Directory $directoryInstance ) {
|
||||||
|
|
||||||
|
$this->directoryInstance = $directoryInstance;
|
||||||
|
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Attempts to convert an int or a string to a file reference
|
||||||
|
*
|
||||||
|
* @param int|string $file_reference
|
||||||
|
* @return string
|
||||||
|
* @throws \Exception
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function resolve( $file_reference ){
|
||||||
|
if(is_int($file_reference) or preg_match("/\d+/",$file_reference)){
|
||||||
|
$fileList = $this->directoryInstance->getList();
|
||||||
|
if(array_key_exists($file_reference, $fileList)){
|
||||||
|
$file_reference = $fileList[$file_reference];
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
throw new \Exception("Invalid file reference $file_reference");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $file_reference;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds a file by reference or name
|
||||||
|
*
|
||||||
|
* @param mixed $file_reference
|
||||||
|
* @throws \Exception
|
||||||
|
* @return Alternc_Diagnostic_Data Resulting data
|
||||||
|
*/
|
||||||
|
function getDataInstance( $file_reference){
|
||||||
|
|
||||||
|
// Attempts to retrieve the file name
|
||||||
|
$file_reference = $this->resolve($file_reference);
|
||||||
|
// Uses factory to resolve the format
|
||||||
|
$factoryInstance = new Alternc_Diagnostic_Format_Factory( $file_reference, $this->directoryInstance);
|
||||||
|
return $factoryInstance->build();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue