Skip to content

Commit

Permalink
Improved cacheable API
Browse files Browse the repository at this point in the history
  • Loading branch information
PeratX committed Mar 10, 2017
1 parent 6fe3fb9 commit 0ef659a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 25 deletions.
2 changes: 0 additions & 2 deletions src/PeratX/SFQRCode/QRCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class QRCode{
public $width;
public $data;


public function encodeMask(QRInput $input, $mask){
if($input->getVersion() < 0 || $input->getVersion() > SFQRCode::QRSPEC_VERSION_MAX){
throw new \Exception('wrong version');
Expand Down Expand Up @@ -92,7 +91,6 @@ public function encodeString8bit($string, $version, $level){
}

$input = new QRInput($version, $level);
if($input == NULL) return NULL;

$ret = $input->append(SFQRCode::QR_MODE_8, strlen($string), str_split($string));
if($ret < 0){
Expand Down
13 changes: 1 addition & 12 deletions src/PeratX/SFQRCode/QRMask.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ class QRMask{

public $runLength = array();


public function __construct(){
$this->runLength = array_fill(0, SFQRCode::QRSPEC_WIDTH_MAX + 1, 0);
}


public function writeFormatInformation($width, &$frame, $mask, $level){
$blacks = 0;
$format = QRSpec::getFormatInfo($mask, $level);
Expand Down Expand Up @@ -118,7 +116,6 @@ private function generateMaskNo($maskNo, $width, $frame){
return $bitMask;
}


public static function serial($bitFrame){
$codeArr = array();

Expand All @@ -128,7 +125,6 @@ public static function serial($bitFrame){
return gzcompress(join("\n", $codeArr), 9);
}


public static function unserial($code){
$codeArr = array();

Expand All @@ -139,13 +135,12 @@ public static function unserial($code){
return $codeArr;
}


public function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly = false){
$b = 0;

$fileName = SFQRCode::getInstance()->getDataFolder() . 'mask_' . $maskNo . DIRECTORY_SEPARATOR . 'mask_' . $width . '_' . $maskNo . '.dat';

if(SFQRCode::QR_CACHEABLE){
if(SFQRCode::isCacheable()){
if(file_exists($fileName)){
$bitMask = self::unserial(file_get_contents($fileName));
}else{
Expand Down Expand Up @@ -176,7 +171,6 @@ public function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly = false){
return $b;
}


public function makeMask($width, $frame, $maskNo, $level){
$masked = array_fill(0, $width, str_repeat("\0", $width));
$this->makeMaskNo($maskNo, $width, $frame, $masked);
Expand All @@ -185,7 +179,6 @@ public function makeMask($width, $frame, $maskNo, $level){
return $masked;
}


public function calcN1N3($length){
$demerit = 0;

Expand Down Expand Up @@ -214,7 +207,6 @@ public function calcN1N3($length){
return $demerit;
}


public function evaluateSymbol($width, $frame){
$demerit = 0;

Expand Down Expand Up @@ -280,7 +272,6 @@ public function evaluateSymbol($width, $frame){
return $demerit;
}


public function mask($width, $frame, $level){
$minDemerit = PHP_INT_MAX;

Expand Down Expand Up @@ -316,6 +307,4 @@ public function mask($width, $frame, $level){

return $bestMask;
}


}
13 changes: 5 additions & 8 deletions src/PeratX/SFQRCode/QRSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -435,16 +435,14 @@ public static function createFrame($version){
return $frame;
}

public static function serial($frame){
public static function serialize($frame){
return gzcompress(join("\n", $frame), 9);
}


public static function unserial($code){
public static function deserialize($code){
return explode("\n", gzuncompress($code));
}


public static function newFrame($version){
if($version < 1 || $version > SFQRCode::QRSPEC_VERSION_MAX)
return null;
Expand All @@ -453,12 +451,12 @@ public static function newFrame($version){

$fileName = SFQRCode::getInstance()->getDataFolder() . 'frame_' . $version . '.dat';

if(SFQRCode::QR_CACHEABLE){
if(SFQRCode::isCacheable()){
if(file_exists($fileName)){
self::$frames[$version] = self::unserial(file_get_contents($fileName));
self::$frames[$version] = self::deserialize(file_get_contents($fileName));
}else{
self::$frames[$version] = self::createFrame($version);
file_put_contents($fileName, self::serial(self::$frames[$version]));
file_put_contents($fileName, self::serialize(self::$frames[$version]));
}
}else{
self::$frames[$version] = self::createFrame($version);
Expand All @@ -471,7 +469,6 @@ public static function newFrame($version){
return self::$frames[$version];
}


public static function rsBlockNum($spec){
return $spec[0] + $spec[3];
}
Expand Down
2 changes: 0 additions & 2 deletions src/PeratX/SFQRCode/QRTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

class QRTools{


public static function binarize($frame){
$len = count($frame);
foreach($frame as &$frameLine){
Expand All @@ -28,7 +27,6 @@ public static function binarize($frame){
return $frame;
}


public static function tcPdfBarcodeArray($code, $mode = 'QR,L'){
$barcode_array = array();

Expand Down
11 changes: 10 additions & 1 deletion src/PeratX/SFQRCode/SFQRCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PeratX\SimpleFramework\Module\Module;

class SFQRCode extends Module{
const QR_CACHEABLE = true;
const QR_FIND_BEST_MASK = true;
const QR_FIND_FROM_RANDOM = false;
const QR_DEFAULT_MASK = 2;
Expand Down Expand Up @@ -58,13 +57,23 @@ class SFQRCode extends Module{
const QRCAP_REMINDER = 2;
const QRCAP_EC = 3;

private static $cacheable = false;

private static $obj;

public function load(){
self::$obj = $this;
@mkdir($this->getDataFolder());
}

public static function isCacheable(): bool{
return self::$cacheable;
}

public static function setCacheable(bool $cacheable){
self::$cacheable = $cacheable;
}

public static function getInstance(): SFQRCode{
return self::$obj;
}
Expand Down

0 comments on commit 0ef659a

Please sign in to comment.