From 5635e75c6f981062241bd4e0c3c008a00fff78e9 Mon Sep 17 00:00:00 2001 From: Mark Croxton Date: Wed, 7 Sep 2022 11:44:25 +0100 Subject: [PATCH] Fix deprecation notices for PHP 8.1+, and housekeeping. --- README.md | 2 +- composer.json | 4 +- system/user/addons/stash/mod.stash.php | 10 +- .../user/addons/stash/models/stash_model.php | 579 +++++++++--------- 4 files changed, 299 insertions(+), 296 deletions(-) diff --git a/README.md b/README.md index 7ed8062..40d5ca7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ ## Stash -![Release](https://img.shields.io/badge/release-3.1.0-brightgreen.svg) +![Release](https://img.shields.io/badge/release-3.1.1-brightgreen.svg) Stash for ExpressionEngine 3.0 to 6.0+. diff --git a/composer.json b/composer.json index 052e11c..57c9bc5 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "croxton/stash", "description": "Extended templating for ExpressionEngine", - "version": "3.1.0", + "version": "3.1.1", "homepage": "https://github.com/croxton/Stash", "support": { "docs": "https://github.com/croxton/Stash/blob/master/README.md", @@ -15,6 +15,6 @@ } ], "require": { - "php": ">=7.0.0" + "php": ">=7.4" } } diff --git a/system/user/addons/stash/mod.stash.php b/system/user/addons/stash/mod.stash.php index d451726..f1480fa 100755 --- a/system/user/addons/stash/mod.stash.php +++ b/system/user/addons/stash/mod.stash.php @@ -233,7 +233,7 @@ public function init($calling_from_hook = FALSE) } else { - $this->parse_vars = (bool) preg_match('/1|on|yes|y/i', $this->parse_vars); + $this->parse_vars = (bool) preg_match('/1|on|yes|y/i', (string) $this->parse_vars); } // parsing: how many passes of the template should we make? (more passes = more overhead). Default = 1 @@ -1952,13 +1952,13 @@ public function get_list($params=array(), $value='', $type='variable', $scope='u // does limit contain a fraction? if ($limit) { - $limit = $this->_parse_fraction($limit, $offset, $absolute_results); + $limit = $this->_parse_fraction($limit, $absolute_results, $offset); } // does offset contain a fraction, e.g. '1/3' ? if ($offset) { - $offset = $this->_parse_fraction($offset, 0, $absolute_results); + $offset = $this->_parse_fraction($offset, $absolute_results); } // pagination @@ -3956,7 +3956,7 @@ protected function sort_by_integer($a, $b) * @param integer * @return integer */ - private function _parse_fraction($fraction, $offset=0, $total) + private function _parse_fraction($fraction, $total, $offset=0) { if (strstr($fraction, '/')) { @@ -4964,7 +4964,7 @@ private function _api_call($method, $params, $type='variable', $scope='user', $v ee()->TMPL->tagdata = $value; } - $this->init(); // re-initilize Stash + $this->init(); // re-initialize Stash $result = $this->{$method}(); // restore original template params and tagdata diff --git a/system/user/addons/stash/models/stash_model.php b/system/user/addons/stash/models/stash_model.php index c81ace6..0d49294 100755 --- a/system/user/addons/stash/models/stash_model.php +++ b/system/user/addons/stash/models/stash_model.php @@ -10,25 +10,25 @@ */ class Stash_model extends CI_Model { - - protected static $keys = array(); - protected static $inserted_keys = array(); - protected static $queue; + + protected static array $keys = array(); + protected static array $inserted_keys = array(); + protected static stdClass $queue; // default bundle types - protected static $bundle_ids = array( - 'default' => 1, - 'template' => 2, - 'static' => 3 + protected static array $bundle_ids = array( + 'default' => 1, + 'template' => 2, + 'static' => 3 ); // name of cache files - private $_static_file = 'index.html'; + private string $_static_file = 'index.html'; // placeholder for indexes - private $_index_key = '[index]'; + private string $_index_key = '[index]'; - function __construct() + public function __construct() { parent::__construct(); @@ -40,17 +40,17 @@ function __construct() // Process the queue on shutdown. // Note that as PHP-FPM does not support this function, // we call process_queue() in Stash_ext::template_post_parse() as well. - // But, you ask, so why do this at all? + // But, you ask, so why do this at all? // Because if you SET/UPDATE a Stash variable outside of an EE - // template, you can't rely on extension hooks being triggered + // template, you can't rely on extension hooks being triggered register_shutdown_function(array($this, "process_queue")); } - + /** * Insert key for user session * * @param string $key - * @param string $session_id + * @param string|null $session_id * @param integer $site_id * @param integer $expire * @param string $parameters @@ -58,59 +58,58 @@ function __construct() * @param integer $bundle_id * @return boolean */ - function insert_key($key, $bundle_id = 1, $session_id, $site_id = 1, $expire = 0, $parameters = '', $label = '') + public function insert_key(string $key, int $bundle_id = 1, string $session_id = NULL, int $site_id = 1, int $expire = 0, string $parameters = '', string $label = ''): bool { $cache_key = $key . '_'. $bundle_id .'_' .$site_id . '_' . $session_id; - $data = array( - 'key_name' => $key, - 'bundle_id' => $bundle_id, - 'session_id' => $session_id, - 'site_id' => $site_id, - 'created' => ee()->localize->now, - 'expire' => $expire, - 'parameters' => $parameters, - 'key_label' => $label + $data = array( + 'key_name' => $key, + 'bundle_id' => $bundle_id, + 'session_id' => $session_id, + 'site_id' => $site_id, + 'created' => ee()->localize->now, + 'expire' => $expire, + 'parameters' => $parameters, + 'key_label' => $label ); if ($result = $this->queue_insert('stash', $cache_key, $data)) { // store a record of the key self::$inserted_keys[] = $cache_key; - + // cache result to eliminate need for a query in future gets self::$keys[$cache_key] = $parameters; - + return TRUE; } - else - { - return FALSE; - } + + return FALSE; } - + /** * Check to see if a key has been inserted within this page load * * @param string $cache_key * @return bool */ - function is_inserted_key($cache_key) + public function is_inserted_key($cache_key): bool { - return in_array($cache_key, self::$inserted_keys) ? TRUE : FALSE; + return in_array($cache_key, self::$inserted_keys); } - + /** * Update key for user session * * @param string $key + * @param int $bundle_id * @param string $session_id * @param integer $site_id * @param integer $expire - * @param string $parameters + * @param string|null $parameters * @return boolean */ - function update_key($key, $bundle_id = 1, $session_id = '', $site_id = 1, $expire = 0, $parameters = NULL) + public function update_key(string $key, int $bundle_id = 1, string $session_id = '', int $site_id = 1, int $expire = 0, string $parameters = NULL): bool { $cache_key = $key . '_'. $bundle_id .'_' .$site_id . '_' . $session_id; @@ -121,99 +120,99 @@ function update_key($key, $bundle_id = 1, $session_id = '', $site_id = 1, $expir 'created' => ee()->localize->now, 'expire' => $expire, ); - + if ($parameters !== NULL) { $data += array('parameters' => $parameters); - } - + } + if ( ! empty($session_id)) { $data += array('session_id' => $session_id); - } - - if ($result = $this->queue_update('stash', $cache_key, $data)) - { + } + + if ($result = $this->queue_update('stash', $cache_key, $data)) + { // update cache self::$keys[$cache_key] = $parameters; return TRUE; } - else - { - return FALSE; - } + + return FALSE; } - + /** * Refresh key for user session * * @param string $key + * @param int $bundle_id * @param string $session_id * @param integer $site_id * @param integer $refresh Seconds to expiry date * @return boolean */ - function refresh_key($key, $bundle_id = 1, $session_id = '', $site_id = 1, $refresh) - { + public function refresh_key(string $key, int $bundle_id = 1, string $session_id = '', int $site_id = 1, int $refresh = 0): bool + { $expire = ee()->localize->now + $refresh; return $this->update_key($key, $bundle_id, $session_id, $site_id, $expire); } - + /** * Get key value * * @param string $key + * @param int $bundle_id * @param string $session_id * @param integer $site_id * @param string $col - * @return string + * @return bool|string */ - function get_key($key, $bundle_id = 1, $session_id = '', $site_id = 1, $col = 'parameters') + public function get_key(string $key, int $bundle_id = 1, string $session_id = '', int $site_id = 1, string $col = 'parameters'): bool|string { $cache_key = $key . '_' . $bundle_id .'_' . $session_id . $site_id . '_' . $col; - + if ( ! isset(self::$keys[$cache_key])) { $this->db->select($col .', created, expire') - ->from('stash') - ->where('key_name', $key) - ->where('bundle_id', $bundle_id) - ->where('site_id', $site_id) - ->limit(1); + ->from('stash') + ->where('key_name', $key) + ->where('bundle_id', $bundle_id) + ->where('site_id', $site_id) + ->limit(1); if ( ! empty($session_id)) { $this->db->where('session_id', $session_id); } - + $result = $this->db->get(); - - if ($result->num_rows() == 1) + + if ($result->num_rows() === 1) { $key_created = $result->row('created'); $key_expire = $result->row('expire'); // if this key expires soon and is scoped to user session, refresh it - if ($key_expire > 0 && $session_id != '_global' && ! empty($session_id)) + if ($key_expire > 0 && $session_id !== '_global' && ! empty($session_id)) { $refresh = $key_expire - $key_created; // refresh period (seconds) $expire = $key_expire - ee()->localize->now; // time to expiry (seconds) - - if ( ($refresh / $expire) > 2 ) + + if ( ($refresh / $expire) > 2 ) { // more than half the refresh time has passed since the last time key was accessed // so let's refresh the key expiry - $this->refresh_key($key, $bundle_id, $session_id, $site_id, $refresh); + $this->refresh_key($key, $bundle_id, $session_id, $site_id, $refresh); } } - + // cache result self::$keys[$cache_key] = $result->row($col); } else { // Cache empty result, this will get overwritten if the key is inserted later on - // Otherwise it will prevent repeated unecessary queries for empty variables + // Otherwise it will prevent repeated unnecessary queries for empty variables self::$keys[$cache_key] = ''; } } @@ -221,25 +220,23 @@ function get_key($key, $bundle_id = 1, $session_id = '', $site_id = 1, $col = 'p { return FALSE; } - else - { - return self::$keys[$cache_key]; - } + + return self::$keys[$cache_key]; } /** * Get key expiry date * * @param string $key + * @param int $bundle_id * @param string $session_id * @param integer $site_id - * @param string $col - * @return timestamp / boolean + * @return int | boolean */ - function get_key_expiry($key, $bundle_id = 1, $session_id = '', $site_id = 1) + public function get_key_expiry(string $key, int $bundle_id = 1, string $session_id = '', int $site_id = 1): bool|int { $cache_key = $key . '_'. $bundle_id .'_' .$site_id . '_' . $session_id; - + if ( isset(self::$keys[$cache_key])) { return self::$keys[$cache_key]['expire']; @@ -247,49 +244,49 @@ function get_key_expiry($key, $bundle_id = 1, $session_id = '', $site_id = 1) return FALSE; } - + /** * Delete key(s) by name, optionally limited to keys registered with the user session * * @param string $key - * @param integer/boolean $bundle_id - * @param string $session_id + * @param bool|integer $bundle_id + * @param string|null $session_id * @param integer $site_id * @param integer $invalidate Delay until cached item expires (seconds) * @return boolean */ - function delete_key($key, $bundle_id = FALSE, $session_id = NULL, $site_id = 1, $invalidate=0) + public function delete_key(string $key, bool|int $bundle_id = FALSE, string $session_id = NULL, int $site_id = 1, int $invalidate=0) : bool { $this->db->where('key_name', $key) - ->where('site_id', $site_id); + ->where('site_id', $site_id); // match a specific bundle - if ($bundle_id) + if ($bundle_id) { $this->db->where('bundle_id', $bundle_id); - } - + } + if ( ! is_null($session_id)) { $this->db->where('session_id', $session_id); - + if ($session_id !== '_global') { // make sure we only delete the user's variable $this->db->limit(1); } } - + // get matching key(s) $query = $this->db->select('id, site_id, key_name, key_label, session_id, bundle_id')->get('stash'); if ($query->num_rows() > 0) - { + { if ( $this->delete_cache($query->result(), TRUE, $invalidate)) { // deleted, now remove the key from the internal key cache $cache_key = $key . '_'. $bundle_id .'_' .$site_id . '_' . $session_id; - + if ( isset(self::$keys[$cache_key])) { unset(self::$keys[$cache_key]); @@ -305,29 +302,29 @@ function delete_key($key, $bundle_id = FALSE, $session_id = NULL, $site_id = 1, /** * Delete key(s) matching a regular expression, and/or scope, and/or bundle * - * @param integer/boolean $bundle_id - * @param string $session_id + * @param int|bool $bundle_id + * @param string|null $session_id * @param integer $site_id - * @param string $regex a regular expression + * @param string|null $regex a regular expression * @param integer $invalidate Delay until cached item expires (seconds) * @return boolean */ - function delete_matching_keys($bundle_id = FALSE, $session_id=NULL, $site_id = 1, $regex=NULL, $invalidate=0) + public function delete_matching_keys(int|bool $bundle_id = FALSE, string $session_id=NULL, int $site_id = 1, string $regex=NULL, int $invalidate=0) : bool { $deleted = FALSE; // have keys been deleted from the database? $clear_static = TRUE; // attempt to delete corresponding *individual* static cache files? // Can we clear the entire static cache in one go (to minimize disk access)? - if (ee()->config->item('stash_static_cache_enabled') - && $regex == NULL - && $invalidate == 0) + if (ee()->config->item('stash_static_cache_enabled') + && $regex === NULL + && $invalidate === 0) { if ( ! $bundle_id || $this->_can_static_cache($bundle_id) ) { if ( is_null($session_id) || $session_id === 'site' || $session_id === 'all') { $this->_delete_dir('/', $site_id); - $clear_static = FALSE; + $clear_static = FALSE; } } } @@ -336,14 +333,14 @@ function delete_matching_keys($bundle_id = FALSE, $session_id=NULL, $site_id = 1 $this->db->where('site_id', $site_id); // match a specific bundle - if ($bundle_id) + if ($bundle_id) { $this->db->where('bundle_id', $bundle_id); } - // match session_id - if ( ! is_null($session_id)) - { + // match session_id + if ( ! is_null($session_id)) + { // scope supplied? if ($session_id === 'user') { @@ -362,9 +359,9 @@ function delete_matching_keys($bundle_id = FALSE, $session_id=NULL, $site_id = 1 } } - // match key_name regex - if ( ! is_null($regex)) - { + // match key_name regex + if ( ! is_null($regex)) + { $this->db->where('key_name RLIKE ', $this->db->escape($regex), FALSE); } @@ -378,11 +375,11 @@ function delete_matching_keys($bundle_id = FALSE, $session_id=NULL, $site_id = 1 if ($deleted) { - // deleted sucessfully, reset the static key cache + // deleted successfully, reset the static key cache self::$keys = array(); } - return $deleted; + return $deleted; } /** @@ -391,9 +388,10 @@ function delete_matching_keys($bundle_id = FALSE, $session_id=NULL, $site_id = 1 * @param array $vars An array of objects * @param boolean $clear_static Clear the static cache too, for variables in the static bundle? * @param integer $invalidate Delay until cached item expires (seconds) + * @param bool $call_hook * @return boolean */ - protected function delete_cache($vars, $clear_static = TRUE, $invalidate = 0, $call_hook = TRUE) + protected function delete_cache(array $vars, bool $clear_static = TRUE, int $invalidate = 0, bool $call_hook = TRUE): bool { // get a list of variable ids $ids = array(); @@ -407,9 +405,9 @@ protected function delete_cache($vars, $clear_static = TRUE, $invalidate = 0, $c if ($invalidate > 0) { - // "Soft delete" - update variables to expire at random intervals within + // "Soft delete" - update variables to expire at random intervals within // the invalidation period and so help prevent cache stampedes. - // When the variables are subsequently pruned, this function is triggered + // When the variables are subsequently pruned, this function is triggered // again and any corresponding caches will be deleted $result = $this->_invalidate($ids, $invalidate); } @@ -430,20 +428,20 @@ protected function delete_cache($vars, $clear_static = TRUE, $invalidate = 0, $c // ------------------------------------- // 'stash_delete' hook // ------------------------------------- - if (ee()->extensions->active_hook('stash_delete') === TRUE + if (ee()->extensions->active_hook('stash_delete') === TRUE && $call_hook === TRUE) { ee()->extensions->call('stash_delete', array( 'key_name' => $row->key_name, - 'key_label' => $row->key_label, - 'bundle_id' => $row->bundle_id, - 'session_id' => $row->session_id, + 'key_label' => $row->key_label, + 'bundle_id' => $row->bundle_id, + 'session_id' => $row->session_id, 'site_id' => $row->site_id )); } } } - + return $result; } @@ -453,7 +451,7 @@ protected function delete_cache($vars, $clear_static = TRUE, $invalidate = 0, $c * @param integer $site_id * @return boolean */ - function flush_cache($site_id = 1) + public function flush_cache(int $site_id = 1): bool { // delete all variables saved in the db if ($result = ee()->db->where('site_id', $site_id)->delete('stash')) @@ -486,7 +484,7 @@ function flush_cache($site_id = 1) * @param integer $period the invalidation period in seconds * @return boolean */ - private function _invalidate($ids, $period=0) + private function _invalidate(array $ids, int $period=0): bool { $now = ee()->localize->now; @@ -500,30 +498,30 @@ private function _invalidate($ids, $period=0) $id_count = count($ids)-1; // what we're doing here is approximately dividing the expiry delay across the target ids, - // increasing the delay according the original id value, so that variables - // generated later in the original template, get regenereated later too + // increasing the delay according the original id value, so that variables + // generated later in the original template, get regenerated later too ee()->db->where_in('id', $ids); $this->db->set('expire', 'FLOOR (' . ee()->localize->now . ' + '.$period.' - ( ('.$id_end.' - id) / '.$id_count.' * ' . $period . ' ))', false); return $this->db->update('stash'); - } + } else - { + { // invalidating a single item ee()->db->where('id', $ids[0]); $this->db->set('expire', ee()->localize->now); - return $this->db->update('stash'); + return $this->db->update('stash'); } } - + /** * Prune expired keys * * @return boolean */ - function prune_keys() + public function prune_keys(): bool { $query = $this->db->get_where('stash', array( - 'expire <' => ee()->localize->now, + 'expire <' => ee()->localize->now, 'expire !=' => '0' )); @@ -535,7 +533,7 @@ function prune_keys() ee()->extensions->call('stash_prune', $query->result_array()); } - if ($query->num_rows() > 0) + if ($query->num_rows() > 0) { if ($deleted = $this->delete_cache($query->result(), TRUE, 0, FALSE)) { @@ -545,26 +543,26 @@ function prune_keys() return FALSE; } - + /** * Get a bundle id from the name * * @param string $bundle - * @return integer + * @return bool|int */ - function get_bundle_by_name($bundle) + public function get_bundle_by_name(string $bundle): bool|int { $cache_key = $bundle; - + if ( ! isset(self::$bundle_ids[$cache_key])) { $result = $this->db->select('id') - ->from('stash_bundles') - ->where('bundle_name', $bundle) - ->limit(1) - ->get(); - - if ($result->num_rows() == 1) + ->from('stash_bundles') + ->where('bundle_name', $bundle) + ->limit(1) + ->get(); + + if ($result->num_rows() === 1) { self::$bundle_ids[$cache_key] = $result->row('id'); } @@ -572,7 +570,7 @@ function get_bundle_by_name($bundle) { self::$bundle_ids[$cache_key] = FALSE; } - } + } return self::$bundle_ids[$cache_key]; } @@ -581,19 +579,19 @@ function get_bundle_by_name($bundle) * Get a bundle name from the id * * @param integer $bundle_id - * @return integer + * @return bool|string */ - function get_bundle_by_id($bundle_id) + public function get_bundle_by_id(int $bundle_id): bool|string { if ( ! $bundle_name = array_search($bundle_id, self::$bundle_ids) ) { $result = $this->db->select('bundle_name') - ->from('stash_bundles') - ->where('id', $bundle_id) - ->limit(1) - ->get(); - - if ($result->num_rows() == 1) + ->from('stash_bundles') + ->where('id', $bundle_id) + ->limit(1) + ->get(); + + if ($result->num_rows() === 1) { $bundle_name = $result->row('bundle_name'); self::$bundle_ids[$bundle_name] = $bundle_id; @@ -606,76 +604,74 @@ function get_bundle_by_id($bundle_id) return $bundle_name; } - + /** * Insert a bundle * * @param string $bundle - * @param integer $site_id - * @param integer $bundle_label + * @param string $bundle_label + * @return bool|int */ - function insert_bundle($bundle, $bundle_label = '') + public function insert_bundle(string $bundle, string $bundle_label = ''): bool|int { - $data = array( - 'bundle_name' => $bundle, - 'bundle_label' => $bundle_label + $data = array( + 'bundle_name' => $bundle, + 'bundle_label' => $bundle_label ); - + if ( $result = $this->db->insert('stash_bundles', $data) ) { return $this->db->insert_id(); } - else - { - return FALSE; - } + + return FALSE; } - + /** * Check if there is at least one entry for the bundle in this site * - * @return integer + * @param int $bundle_id + * @param int $site_id + * @return bool */ - function bundle_entry_exists($bundle_id, $site_id = 1) + public function bundle_entry_exists(int $bundle_id, int $site_id = 1): bool { $result = $this->db->select('id') - ->from('stash') - ->where('bundle_id', $bundle_id) - ->where('site_id', $site_id) - ->limit(1) - ->get(); - - if ($result->num_rows() == 1) + ->from('stash') + ->where('bundle_id', $bundle_id) + ->where('site_id', $site_id) + ->limit(1) + ->get(); + + if ($result->num_rows() === 1) { return TRUE; } - else - { - return FALSE; - } + + return FALSE; } - + /** * Count the number of entries for a given bundle * - * @return boolean + * @param int $bundle_id + * @param int $site_id + * @return int */ - function bundle_entry_count($bundle_id, $site_id = 1) + public function bundle_entry_count(int $bundle_id, int $site_id = 1): int { $result = $this->db->select('COUNT(id) AS row_count') - ->from('stash') - ->where('bundle_id', $bundle_id) - ->where('site_id', $site_id) - ->get(); - - if ($result->num_rows() == 1) + ->from('stash') + ->where('bundle_id', $bundle_id) + ->where('site_id', $site_id) + ->get(); + + if ($result->num_rows() === 1) { return $result->row('row_count'); } - else - { - return 0; - } + + return 0; } @@ -686,14 +682,16 @@ function bundle_entry_count($bundle_id, $site_id = 1) */ /** - * Check if the variable can be cached as a static file and write it + * Check if the variable can be cached as a static file and write it * - * @param array $data + * @param string|array $data * @return boolean */ - protected function write_static_cache($data) + protected function write_static_cache(string|array $data): bool { - if ( ! isset($data['bundle_id'])) return FALSE; + if ( ! isset($data['bundle_id'])) { + return FALSE; + } // write to static file cache? if ($this->_can_static_cache($data['bundle_id'])) @@ -709,13 +707,14 @@ protected function write_static_cache($data) } /** - * Delete the associated static file for a given variable + * Delete the associated static file for a given variable * - * @param integer $key - * @param string $bundle_id + * @param string $key + * @param integer $bundle_id + * @param int $site_id * @return boolean */ - protected function delete_static_cache($key, $bundle_id, $site_id) + protected function delete_static_cache(string $key, int $bundle_id, int $site_id): bool { // write to static file cache? if ($this->_can_static_cache($bundle_id)) @@ -734,10 +733,11 @@ protected function delete_static_cache($key, $bundle_id, $site_id) * Write a static file * * @param string $uri - * @param string $parameters + * @param int $site_id + * @param string|null $parameters * @return boolean */ - private function _write_file($uri, $site_id, $parameters = NULL) + private function _write_file(string $uri, int $site_id, string $parameters = NULL): bool { ee()->load->helper('file'); @@ -758,14 +758,15 @@ private function _write_file($uri, $site_id, $parameters = NULL) } /** - * Delete a static file - * - * @param string $uri - * @return boolean - */ - private function _delete_file($uri = '/', $site_id) + * Delete a static file + * + * @param string $uri + * @param int $site_id + * @return boolean + */ + private function _delete_file(string $uri = '/', int $site_id = 1): bool { - if ($path = $this->_path($uri, $site_id) && @unlink($this->_path($uri, $site_id).$this->_static_file)) + if ($path = ($this->_path($uri, $site_id) && @unlink($this->_path($uri, $site_id) . $this->_static_file))) { return TRUE; } @@ -773,29 +774,31 @@ private function _delete_file($uri = '/', $site_id) } /** - * Recursively delete directories and files in a static cache directory - * - * @param string $uri - * @return boolean - */ - private function _delete_dir($uri, $site_id) + * Recursively delete directories and files in a static cache directory + * + * @param string $uri + * @param int $site_id + * @return boolean + */ + private function _delete_dir(string $uri, int $site_id): bool { ee()->load->helper('file'); - if ( $path = $this->_path($uri, $site_id) && delete_files($this->_path($uri, $site_id), TRUE) ) + if ( $path = ($this->_path($uri, $site_id) && delete_files($this->_path($uri, $site_id), true))) { return TRUE; } return FALSE; } - /** - * Returns the path to the cache directory, if it exists - * - * @param string $uri - * @return string/boolean - */ - private function _path($uri = '/', $site_id) + /** + * Returns the path to the cache directory, if it exists + * + * @param string $uri + * @param int $site_id + * @return bool|string + */ + private function _path(string $uri = '/', int $site_id = 1): bool|string { // Get parts $path = ee()->config->item('stash_static_basepath'); @@ -807,12 +810,12 @@ private function _path($uri = '/', $site_id) } // Blacklist of characters we don't want to allow as directory names in the cache - $bad = ee()->config->item('stash_static_character_blacklist') - ? (array) ee()->config->item('stash_static_character_blacklist') - : array(LD, RD, '<', '>', ':', '"', '\\', '|', '*', '.'); + $bad = ee()->config->item('stash_static_character_blacklist') + ? (array) ee()->config->item('stash_static_character_blacklist') + : array(LD, RD, '<', '>', ':', '"', '\\', '|', '*', '.'); $new_uri = str_replace($bad, '', $uri); - if ($uri != $new_uri) + if ($uri !== $new_uri) { return FALSE; } @@ -821,13 +824,13 @@ private function _path($uri = '/', $site_id) return trim($path.'/'.$site_id.'/'.trim($uri, '/')).'/'; } - /** - * Check that static cache is enabled, and the bundle is static - * - * @param integer $bundle_id - * @return boolean - */ - private function _can_static_cache($bundle_id) + /** + * Check that static cache is enabled, and the bundle is static + * + * @param integer $bundle_id + * @return boolean + */ + private function _can_static_cache(int $bundle_id): bool { if ( ee()->config->item('stash_static_cache_enabled')) { @@ -847,36 +850,36 @@ private function _can_static_cache($bundle_id) ================================================================ */ - /** - * Parse a URI from a stash variable key (typically, the uri of a cached page) - * - * @param string $key - * @return string - */ - public function parse_uri_from_key($key) + /** + * Parse a URI from a stash variable key (typically, the uri of a cached page) + * + * @param string $key + * @return string + */ + public function parse_uri_from_key(string $key): string { $uri = explode(':', $key); - return $uri[0] == $this->_index_key ? '' : $uri[0]; + return $uri[0] === $this->_index_key ? '' : $uri[0]; } - /** - * Getter: returns the index key placeholder - * - * @return string - */ - public function get_index_key() + /** + * Getter: returns the index key placeholder + * + * @return string + */ + public function get_index_key(): string { return $this->_index_key; } - /** - * Prepare INSERT IGNORE BATCH SQL query - * - * @param string $table The table to insert into - * @param Array $data Array in form of "Column" => "Value", ... - * @return Null - */ - protected function insert_ignore_batch($table, array $data) + /** + * Prepare INSERT IGNORE BATCH SQL query + * + * @param string $table The table to insert into + * @param Array $data Array in form of "Column" => "Value", ... + * @return void + */ + protected function insert_ignore_batch(string $table, array $data): void { $_keys = array(); $_prepared = array(); @@ -886,7 +889,7 @@ protected function insert_ignore_batch($table, array $data) $_values = array(); // static caching - save an index of the variable only? - if (ee()->config->item('stash_static_cache_index') + if (ee()->config->item('stash_static_cache_index') && $this->_can_static_cache($row['bundle_id'])) { $row['parameters'] = ''; // remove variable content @@ -895,29 +898,29 @@ protected function insert_ignore_batch($table, array $data) foreach ($row as $col => $val) { // add key - if ( ! in_array($col, $_keys) ) + if ( ! in_array($col, $_keys) ) { $_keys[] = $col; } // add values - $_values[] = $this->db->escape($val); + $_values[] = $this->db->escape($val); } $_prepared[] = '(' . implode(',', $_values) . ')'; } - + $this->db->query('INSERT IGNORE INTO '.$this->db->dbprefix.$table.' ('.implode(',',$_keys).') VALUES '.implode(',', array_values($_prepared)).';'); } - /** - * Queue an insert for batch processing later - * - * @param string $table The table to insert into - * @param string $cache_key Unique key identifying this variable - * @param Array $data Array in form of "Column" => "Value", ... - * @return boolean - */ - protected function queue_insert($table, $cache_key, $data) + /** + * Queue an insert for batch processing later + * + * @param string $table The table to insert into + * @param string $cache_key Unique key identifying this variable + * @param Array $data Array in form of "Column" => "Value", ... + * @return boolean + */ + protected function queue_insert(string $table, string $cache_key, array $data): bool { if ( ! isset(self::$queue->inserts[$table])) { @@ -933,15 +936,15 @@ protected function queue_insert($table, $cache_key, $data) return TRUE; } - /** - * Queue an update for batch processing later - * - * @param string $table The table to insert into - * @param string $cache_key Unique key identifying this variable - * @param Array $data Array in form of "Column" => "Value", ... - * @return boolean - */ - protected function queue_update($table, $cache_key, $data) + /** + * Queue an update for batch processing later + * + * @param string $table The table to insert into + * @param string $cache_key Unique key identifying this variable + * @param array $data Array in form of "Column" => "Value", ... + * @return boolean + */ + protected function queue_update(string $table, string $cache_key, array $data): bool { if ( ! isset(self::$queue->updates[$table])) { @@ -953,16 +956,16 @@ protected function queue_update($table, $cache_key, $data) return TRUE; } - /** - * Process queued queries - * - * @return boolean - */ - public function process_queue() + /** + * Process queued queries + * + * @return void + */ + public function process_queue(): void { // batch inserts - must run first foreach(self::$queue->inserts as $table => $data) - { + { $this->insert_ignore_batch($table, $data); // write to static file cache @@ -974,12 +977,12 @@ public function process_queue() // run each queued update in order if (count(self::$queue->updates) > 0) - { - // update keys + { + // update keys foreach(self::$queue->updates as $table => $data) - { + { foreach($data as $query) - { + { // required columns $where = array( 'key_name' => $query['key_name'], @@ -989,8 +992,8 @@ public function process_queue() if ( isset($query['session_id'])) { - $where += array('session_id' => $query['session_id']); - unset($query['session_id']); + $where += array('session_id' => $query['session_id']); + unset($query['session_id']); } // update db @@ -1008,15 +1011,15 @@ public function process_queue() } /** - * Get the Queue object by reference - * - * @return object - */ - public function &get_queue() + * Get the Queue object by reference + * + * @return stdClass + */ + public function &get_queue(): stdClass { return self::$queue; } - + } /* End of file stash_model.php */