Skip to content

Commit

Permalink
Swap static variable for shared reference to object
Browse files Browse the repository at this point in the history
  • Loading branch information
ssigwart committed Oct 24, 2023
1 parent df109d8 commit 91eec42
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 9 deletions.
75 changes: 75 additions & 0 deletions include/tcpdf_page_cache_reference_counts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
//============================================================+
// File name : tcpdf_page_cache_reference_counts.php
// Version : 1.0.000
// Begin : 2023-08-04
// Last Update : 2023-08-04
// License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
// -------------------------------------------------------------------
// Copyright (C) 2002-2013 Nicola Asuni - Tecnick.com LTD
//
// This file is part of TCPDF software library.
//
// TCPDF is free software: you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// TCPDF is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with TCPDF. If not, see <http://www.gnu.org/licenses/>.
//
// See LICENSE.TXT file for more information.
// -------------------------------------------------------------------
//
// Description : Class to keep track of page cache references
//
//============================================================+

/**
* @file
* PHP page cache reference counts class for TCPDF
* @package com.tecnick.tcpdf
*/

/**
* @class TCPDF_PAGE_CACHE_REFERENCE_COUNTS
* PHP page cache reference counts class for TCPDF
* @package com.tecnick.tcpdf
* @version 1.0.000
*/
class TCPDF_PAGE_CACHE_REFERENCE_COUNTS
{
/** @var int Number of references */
private $numReferences = 0;

/**
* Increment reference count
*/
public function incrementReferenceCount()
{
$this->numReferences++;
}

/**
* Decrement reference count
*/
public function decrementReferenceCount()
{
$this->numReferences--;
}

/**
* Get reference count
*
* @return int Reference count
*/
public function getReferenceCount()
{
return $this->numReferences;
}
}
28 changes: 19 additions & 9 deletions tcpdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@
require_once(dirname(__FILE__).'/include/tcpdf_images.php');
// TCPDF static methods and data
require_once(dirname(__FILE__).'/include/tcpdf_static.php');
// TCPDF page cache reference counts
require_once(dirname(__FILE__).'/include/tcpdf_page_cache_reference_counts.php');

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Expand Down Expand Up @@ -195,6 +197,13 @@ class TCPDF {
*/
protected $pageCacheIndex = array();

/**
* Page cache reference counts
* @protected
* @var TCPDF_PAGE_CACHE_REFERENCE_COUNTS|null
*/
protected $pageCacheRefCnts = null;

/**
* Current document state.
* @protected
Expand Down Expand Up @@ -7912,9 +7921,6 @@ public function _destroy($destroyall=false, $preserve_objcopy=false) {
}
}

/** Page cache reference counts */
protected static $pageCacheRefCnts = array();

/**
* Update page cache file
*
Expand All @@ -7927,7 +7933,9 @@ public function usePageCacheFile($memSizeInBytes)
if ($this->pageCacheFile === false) {
$this->pageCacheFile = null;
} else {
self::$pageCacheRefCnts[(int)$this->pageCacheFile] = 1;
if ($this->pageCacheRefCnts == null)
$this->pageCacheRefCnts = new TCPDF_PAGE_CACHE_REFERENCE_COUNTS();
$this->pageCacheRefCnts->incrementReferenceCount();
}
}

Expand All @@ -7938,8 +7946,8 @@ private function _closePageCacheFile()
{
if ($this->pageCacheFile !== null)
{
self::$pageCacheRefCnts[(int)$this->pageCacheFile]--;
if (self::$pageCacheRefCnts[(int)$this->pageCacheFile] == 0) {
$this->pageCacheRefCnts->decrementReferenceCount();
if ($this->pageCacheRefCnts->getReferenceCount() == 0) {
// This might be the end of the PHP script and the temporary file may be
// removed before this class, so check if it's still a valid resource.
if (is_resource($this->pageCacheFile)) {
Expand All @@ -7961,7 +7969,7 @@ public function __clone()
// Update ref count
if ($this->pageCacheFile !== null)
{
self::$pageCacheRefCnts[(int)$this->pageCacheFile]++;
$this->pageCacheRefCnts->incrementReferenceCount();
$this->roPageCacheFile = true;
}
}
Expand Down Expand Up @@ -21044,13 +21052,15 @@ protected function getPageBuffer($page) {
{
$origPageCacheFile = $this->pageCacheFile;
// Remove reference count to file and open a new page cache file
self::$pageCacheRefCnts[(int)$this->pageCacheFile]--;
$this->pageCacheRefCnts->decrementReferenceCount();
$this->pageCacheFile = fopen('php://temp', 'w+');
if ($this->pageCacheFile === false) {
$this->pageCacheFile = null;
$this->pageCacheRefCnts = null;
} else {
// Add reference to new page cache file
self::$pageCacheRefCnts[(int)$this->pageCacheFile] = 1;
$this->pageCacheRefCnts = new TCPDF_PAGE_CACHE_REFERENCE_COUNTS();
$this->pageCacheRefCnts->incrementReferenceCount();
// Copy data from old page cache file to new one
fseek($origPageCacheFile, 0, SEEK_SET);
while (($copyContent = fread($origPageCacheFile, 8192)) != false) {
Expand Down

0 comments on commit 91eec42

Please sign in to comment.