Skip to content

Commit

Permalink
FDB: OOP - Read
Browse files Browse the repository at this point in the history
  • Loading branch information
PeratX committed Sep 3, 2019
1 parent df8dc37 commit 59094a4
Show file tree
Hide file tree
Showing 21 changed files with 424 additions and 120 deletions.
3 changes: 1 addition & 2 deletions FDBGen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ Flash Database Generator for FlashDetector
## Usage

```powershell
PS X:\Scripts>php fdbgen.php -f -v fdb-version -d path-to-fdbs -o fdb.json
PS X:\Scripts>php fdbgen.php -i -d fdb.json -o iddb.json
PS X:\Scripts>php fdbgen.php
```

## License
Expand Down
8 changes: 7 additions & 1 deletion FDBGen/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,11 @@
"author": "iTX Technologies",
"main": "iTXTech\\FlashDetector\\FDBGen\\Loader",
"order": 0,
"website": "https://github.com/iTXTech/FlashDetector"
"website": "https://github.com/iTXTech/FlashDetector",
"dependencies": [
{
"name": "iTXTech/FlashDetector",
"version": "1.0.0"
}
]
}
2 changes: 1 addition & 1 deletion FDWebServer/CGI/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
"result" => true,
"time" => time(),
"server" => "FDWebServer-WorkerManEE",
"fdb_info" => FlashDetector::getFdb()["info"]
"fdb_info" => FlashDetector::getFdb()->getInfo()->getVersion()
]);
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static function onRequest(){
"status" => true,
"time" => time(),
"server" => "FDWebServer-WorkerManEE",
"fdb_info" => FlashDetector::getFdb()["info"]
"fdb_info" => FlashDetector::getFdb()->getInfo()->getVersion()
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function process(Request $request, Response $response, Server $ser
"result" => true,
"time" => time(),
"server" => "FDWebServer-Swoole",
"fdb_info" => FlashDetector::getFdb()["info"]
"fdb_info" => FlashDetector::getFdb()->getInfo()->getVersion()
]);
}
}
27 changes: 25 additions & 2 deletions FlashDetector/src/iTXTech/FlashDetector/Arrayable.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,29 @@

namespace iTXTech\FlashDetector;

interface Arrayable{
public function toArray() : array;
abstract class Arrayable{

public function __construct(array $arr = null){
if($arr != null){
foreach($arr as $k => $v){
$this->{$k} = $v;
}
}
}

public function toArray() : array{
$reflectionClass = new \ReflectionClass($this);
$properties = $reflectionClass->getProperties();
$info = [];
foreach($properties as $property){
if(is_object($this->{$property->getName()})){
/** @var Arrayable $prop */
$prop = $this->{$property->getName()};
$info[$property->getName()] = $prop->toArray();
}else{
$info[$property->getName()] = $this->{$property->getName()};
}
}
return $info;
}
}
5 changes: 3 additions & 2 deletions FlashDetector/src/iTXTech/FlashDetector/Decoder/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace iTXTech\FlashDetector\Decoder;

use iTXTech\FlashDetector\Constants;
use iTXTech\FlashDetector\Fdb\PartNumber;
use iTXTech\FlashDetector\FlashDetector;
use iTXTech\FlashDetector\FlashInfo;
use iTXTech\SimpleFramework\Util\StringUtil;
Expand Down Expand Up @@ -74,7 +75,7 @@ public static function matchFromStart(string &$str, array $info, $default = Cons
return $default;
}

public static function getFlashInfoFromFdb(FlashInfo $info) : ?array{
return FlashDetector::getFdb()[strtolower($info->getManufacturer())][$info->getPartNumber()] ?? null;
public static function getFlashInfoFromFdb(FlashInfo $info) : ?PartNumber{
return FlashDetector::getFdb()->getPartNumber($info->getManufacturer(), $info->getPartNumber());
}
}
5 changes: 3 additions & 2 deletions FlashDetector/src/iTXTech/FlashDetector/Decoder/Micron.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace iTXTech\FlashDetector\Decoder;

use iTXTech\FlashDetector\Constants;
use iTXTech\FlashDetector\Fdb\PartNumber;
use iTXTech\FlashDetector\FlashDetector;
use iTXTech\FlashDetector\FlashInfo;
use iTXTech\FlashDetector\Property\Classification;
Expand Down Expand Up @@ -211,8 +212,8 @@ protected static function setInterface(string $interface, FlashInfo $info) : Fla
->setSync($i[0])->setAsync($i[1])->setSpi($i[2]));
}

public static function getFlashInfoFromFdb(FlashInfo $info) : ?array{
return FlashDetector::getFdb()[strtolower(self::getName())][self::removePackage($info->getPartNumber())] ?? null;
public static function getFlashInfoFromFdb(FlashInfo $info) : ?PartNumber{
return FlashDetector::getFdb()->getPartNumber(self::getName(), self::removePackage($info->getPartNumber()));
}

public static function removePackage(string $pn) : string{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace iTXTech\FlashDetector\Decoder;

use iTXTech\FlashDetector\Constants;
use iTXTech\FlashDetector\Fdb\PartNumber;
use iTXTech\FlashDetector\FlashDetector;
use iTXTech\FlashDetector\FlashInfo;
use iTXTech\SimpleFramework\Util\StringUtil;
Expand Down Expand Up @@ -52,7 +53,7 @@ public static function decode(string $partNumber) : FlashInfo{
}
}

public static function getFlashInfoFromFdb(FlashInfo $info) : ?array{
public static function getFlashInfoFromFdb(FlashInfo $info) : ?PartNumber{
return null;
}
}
5 changes: 3 additions & 2 deletions FlashDetector/src/iTXTech/FlashDetector/Decoder/SKHynix.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace iTXTech\FlashDetector\Decoder;

use iTXTech\FlashDetector\Constants;
use iTXTech\FlashDetector\Fdb\PartNumber;
use iTXTech\FlashDetector\FlashDetector;
use iTXTech\FlashDetector\FlashInfo;
use iTXTech\FlashDetector\Property\Classification;
Expand Down Expand Up @@ -217,12 +218,12 @@ public static function decode(string $partNumber) : FlashInfo{
return $flashInfo;
}

public static function getFlashInfoFromFdb(FlashInfo $info) : ?array{
public static function getFlashInfoFromFdb(FlashInfo $info) : ?PartNumber{
$partNumber = $info->getPartNumber();
if(StringUtil::contains($partNumber, "-")){
$partNumber = explode("-", $partNumber)[0];
}
return FlashDetector::getFdb()[strtolower(self::getName())][self::removePackage($partNumber)] ?? null;
return FlashDetector::getFdb()->getPartNumber(self::getName(), self::removePackage($partNumber));
}

public static function removePackage(string $pn) : string{
Expand Down
11 changes: 6 additions & 5 deletions FlashDetector/src/iTXTech/FlashDetector/Decoder/Samsung.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace iTXTech\FlashDetector\Decoder;

use iTXTech\FlashDetector\Constants;
use iTXTech\FlashDetector\Fdb\PartNumber;
use iTXTech\FlashDetector\FlashDetector;
use iTXTech\FlashDetector\FlashInfo;
use iTXTech\FlashDetector\Property\Classification;
Expand Down Expand Up @@ -223,12 +224,12 @@ public static function decode(string $partNumber) : FlashInfo{
return $flashInfo;
}

public static function getFlashInfoFromFdb(FlashInfo $info) : ?array{
public static function getFlashInfoFromFdb(FlashInfo $info) : ?PartNumber{
$partNumber = $info->getPartNumber();
if(StringUtil::contains($partNumber, "-")){
$partNumber = explode("-", $partNumber)[0];
}
if(!isset(FlashDetector::getFdb()[strtolower(self::getName())][$partNumber]) and strlen($partNumber) === 10){//standard
if(!FlashDetector::getFdb()->hasPartNumber(self::getName(), $partNumber) and strlen($partNumber) === 10){//standard
$c = self::CLASSIFICATION[substr($partNumber, 2, 1)] ?? -1;
//convert part number to single die
if($c[1] > 1){//die
Expand All @@ -247,12 +248,12 @@ public static function getFlashInfoFromFdb(FlashInfo $info) : ?array{
}
}
$partNumber{8} = "0";
$info = FlashDetector::getFdb()[strtolower(self::getName())][$partNumber] ?? null;
$info = FlashDetector::getFdb()->getPartNumber(self::getName(), $partNumber) ?? null;
if($info !== null){
$info["m"] .= " (" . $c[1] . " x " . $partNumber . ")";
$info->setComment($info->getComment() . " (" . $c[1] . " x " . $partNumber . ")");
}
return $info;
}
return FlashDetector::getFdb()[strtolower(self::getName())][$partNumber] ?? null;
return FlashDetector::getFdb()->getPartNumber(self::getName(), $partNumber);
}
}
9 changes: 5 additions & 4 deletions FlashDetector/src/iTXTech/FlashDetector/Decoder/SanDisk.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
namespace iTXTech\FlashDetector\Decoder;

use iTXTech\FlashDetector\Constants;
use iTXTech\FlashDetector\Fdb\PartNumber;
use iTXTech\FlashDetector\FlashDetector;
use iTXTech\FlashDetector\FlashInfo;
use iTXTech\FlashDetector\Property\Classification;
Expand Down Expand Up @@ -133,11 +134,11 @@ public static function decode(string $partNumber) : FlashInfo{
return $flashInfo;
}

public static function getFlashInfoFromFdb(FlashInfo $info) : ?array{
$data = FlashDetector::getFdb()[strtolower(self::getName())][$info->getPartNumber()] ?? null;
public static function getFlashInfoFromFdb(FlashInfo $info) : ?PartNumber{
$data = FlashDetector::getFdb()->getPartNumber(self::getName(), $info->getPartNumber());
if($data != null){
$comment = "";
$parts = explode("/", $data["m"] ?? "");
$parts = explode("/", $data->getComment() ?? "");
$extraInfo = $info->getExtraInfo() ?? [];
foreach($parts as $part){
if($part == ""){
Expand All @@ -153,7 +154,7 @@ public static function getFlashInfoFromFdb(FlashInfo $info) : ?array{
if($comment != ""){
$comment = substr($comment, 0, strlen($comment) - 1);
}
$data["m"] = $comment;
$data->setComment($comment);
$info->setExtraInfo($extraInfo);
}
return $data;
Expand Down
80 changes: 80 additions & 0 deletions FlashDetector/src/iTXTech/FlashDetector/Fdb/Fdb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
* iTXTech FlashDetector
*
* Copyright (C) 2018-2019 iTX Technologies
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace iTXTech\FlashDetector\Fdb;

use iTXTech\FlashDetector\Arrayable;

class Fdb extends Arrayable{
/** @var Info */
protected $info;
/** @var Vendor[] */
protected $vendors = [];

public function __construct(array $arr = null){
if($arr != null){
$this->info = new Info($arr["info"]);
unset($arr["info"]);
foreach($arr as $vendor => $pns){
$this->vendors[$vendor] = new Vendor($vendor, $pns);
}
}
}

/**
* @return Vendor[]
*/
public function getVendors() : array{
return $this->vendors;
}

public function setVendors(array $vendors){
$this->vendors = $vendors;
}

public function getPartNumber(string $vendor, string $partNumber) : ?PartNumber{
$vendor = strtolower($vendor);
if(isset($this->vendors[$vendor])){
$pn = $this->vendors[$vendor]->getPartNumber($partNumber);
if($pn != null){
return clone $pn;
}
}
return null;
}

public function hasPartNumber(string $vendor, string $partNumber) : bool{
return $this->getPartNumber($vendor, $partNumber) != null;
}

public function getInfo() : Info{
return $this->info;
}

public function toArray() : array{
$arr = [
"info" => $this->info->toArray(),
];
foreach($this->vendors as $vendor){
$arr[$vendor->getName()] = $vendor->toArray();
}
return $arr;
}
}
72 changes: 72 additions & 0 deletions FlashDetector/src/iTXTech/FlashDetector/Fdb/Info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/*
* iTXTech FlashDetector
*
* Copyright (C) 2018-2019 iTX Technologies
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace iTXTech\FlashDetector\Fdb;

use iTXTech\FlashDetector\Arrayable;

class Info extends Arrayable{
protected $name;
protected $version;
protected $website;
protected $time;
protected $controllers;

public function getName() : ?string{
return $this->name;
}

public function setName(string $name){
$this->name = $name;
return $this;
}

public function getVersion() : ?int{
return $this->version;
}

public function setVersion(int $version){
$this->version = $version;
}

public function getWebsite() : ?string{
return $this->website;
}

public function setWebsite(string $website){
$this->website = $website;
}

public function getTime() : ?string{
return $this->time;
}

public function setTime(string $time){
$this->time = $time;
}

public function getControllers() : ?array{
return $this->controllers;
}

public function setControllers(array $controllers){
$this->controllers = $controllers;
}
}
Loading

0 comments on commit 59094a4

Please sign in to comment.