From a0b016905a3540c05be5d0c480ca83cdd58ad850 Mon Sep 17 00:00:00 2001 From: Christian Mohr Date: Wed, 3 Jan 2018 14:56:09 +0100 Subject: prevent _ci_vars from being cached in _ci_cached_vars Signed-off-by: Christian Mohr --- system/core/Loader.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'system') diff --git a/system/core/Loader.php b/system/core/Loader.php index 5b051e1a8..98db8d4f7 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -938,11 +938,10 @@ class CI_Loader { * * You can either set variables using the dedicated $this->load->vars() * function or via the second parameter of this function. We'll merge - * the two types and cache them so that views that are embedded within - * other views can have access to these variables. + * the two types. */ - empty($_ci_vars) OR $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars); - extract($this->_ci_cached_vars); + empty($this->_ci_cached_vars) OR $_ci_vars = array_merge($this->_ci_cached_vars, $_ci_vars); + extract($_ci_vars); /** * Buffer the output -- cgit v1.2.3-24-g4f1b From 7d591a3c36cd58b430125eee214a9dba36290d00 Mon Sep 17 00:00:00 2001 From: Christian Mohr Date: Wed, 3 Jan 2018 16:37:55 +0100 Subject: reproduce caching behaviour for nested view() calls Signed-off-by: Christian Mohr --- system/core/Loader.php | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'system') diff --git a/system/core/Loader.php b/system/core/Loader.php index 98db8d4f7..1e5eeecd5 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -93,6 +93,13 @@ class CI_Loader { */ protected $_ci_cached_vars = array(); + /** + * stack of variables for nested _ci_load calls + * + * @var array + */ + protected $_ci_vars_stack = array(); + /** * List of loaded classes * @@ -934,14 +941,30 @@ class CI_Loader { } /* - * Extract and cache variables + * Extract and stack variables * * You can either set variables using the dedicated $this->load->vars() * function or via the second parameter of this function. We'll merge - * the two types. + * the two types. Additionally we merge them with the last variables + * from the nested call stack so that views that are embedded within + * other views can have access to these variables. */ - empty($this->_ci_cached_vars) OR $_ci_vars = array_merge($this->_ci_cached_vars, $_ci_vars); - extract($_ci_vars); + + // merge with global cached vars (first call) or last state from nested + // call stack (subsequent nested calls) + // if (!empty($this->_ci_vars_stack)) { + // $_ci_vars = array_merge(end($this->_ci_vars_stack), $_ci_vars); + // } else if (!empty($this->_ci_cached_vars)) { + // // merge with cached vars + // $_ci_vars = array_merge($this->_ci_cached_vars, $_ci_vars); + // } + + empty($_ci_vars) OR $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars); + + + // push current _ci_vars state to stack and extract it + // array_push($this->_ci_vars_stack, $_ci_vars); + extract($this->_ci_cached_vars); /** * Buffer the output @@ -959,6 +982,9 @@ class CI_Loader { include($_ci_path); // include() vs include_once() allows for multiple views with the same name log_message('info', 'File loaded: '.$_ci_path); + // remove current _ci_vars state from stack + // array_pop($this->_ci_vars_stack); + // Return the file data if requested if ($_ci_return === TRUE) { -- cgit v1.2.3-24-g4f1b From 5d7e8c0ef3f324ead7cb97a9738dc790e0b0b196 Mon Sep 17 00:00:00 2001 From: Christian Mohr Date: Wed, 3 Jan 2018 16:42:35 +0100 Subject: reproduce caching behaviour for nested view() calls Signed-off-by: Christian Mohr --- system/core/Loader.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) (limited to 'system') diff --git a/system/core/Loader.php b/system/core/Loader.php index 1e5eeecd5..3506a0ef1 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -952,18 +952,14 @@ class CI_Loader { // merge with global cached vars (first call) or last state from nested // call stack (subsequent nested calls) - // if (!empty($this->_ci_vars_stack)) { - // $_ci_vars = array_merge(end($this->_ci_vars_stack), $_ci_vars); - // } else if (!empty($this->_ci_cached_vars)) { - // // merge with cached vars - // $_ci_vars = array_merge($this->_ci_cached_vars, $_ci_vars); - // } - - empty($_ci_vars) OR $this->_ci_cached_vars = array_merge($this->_ci_cached_vars, $_ci_vars); - + if (!empty($this->_ci_vars_stack)) { + $_ci_vars = array_merge(end($this->_ci_vars_stack), $_ci_vars); + } else if (!empty($this->_ci_cached_vars)) { + $_ci_vars = array_merge($this->_ci_cached_vars, $_ci_vars); + } // push current _ci_vars state to stack and extract it - // array_push($this->_ci_vars_stack, $_ci_vars); + array_push($this->_ci_vars_stack, $_ci_vars); extract($this->_ci_cached_vars); /** @@ -983,7 +979,7 @@ class CI_Loader { log_message('info', 'File loaded: '.$_ci_path); // remove current _ci_vars state from stack - // array_pop($this->_ci_vars_stack); + array_pop($this->_ci_vars_stack); // Return the file data if requested if ($_ci_return === TRUE) -- cgit v1.2.3-24-g4f1b From 88c7763b4832ad14b4ff1381d0f158204eb0bb6f Mon Sep 17 00:00:00 2001 From: Christian Mohr Date: Wed, 3 Jan 2018 18:46:18 +0100 Subject: reproduce caching behaviour for nested view() calls Signed-off-by: Christian Mohr --- system/core/Loader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'system') diff --git a/system/core/Loader.php b/system/core/Loader.php index 3506a0ef1..e75578ae8 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -960,7 +960,7 @@ class CI_Loader { // push current _ci_vars state to stack and extract it array_push($this->_ci_vars_stack, $_ci_vars); - extract($this->_ci_cached_vars); + extract($_ci_vars); /** * Buffer the output -- cgit v1.2.3-24-g4f1b From 79561aef771e6ccc323aa328f12826294a48724d Mon Sep 17 00:00:00 2001 From: Christian Mohr Date: Wed, 3 Jan 2018 19:43:02 +0100 Subject: fixed case without parameter Signed-off-by: Christian Mohr --- system/core/Loader.php | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'system') diff --git a/system/core/Loader.php b/system/core/Loader.php index e75578ae8..07e572950 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -950,6 +950,11 @@ class CI_Loader { * other views can have access to these variables. */ + // init current _ci_vars + if (!is_array($_ci_vars)) { + $_ci_vars = []; + } + // merge with global cached vars (first call) or last state from nested // call stack (subsequent nested calls) if (!empty($this->_ci_vars_stack)) { -- cgit v1.2.3-24-g4f1b From bbb3d55f6e0315faf03dcf894709cc04d6109179 Mon Sep 17 00:00:00 2001 From: Christian Mohr Date: Wed, 7 Mar 2018 19:05:09 +0100 Subject: improved code structure and comments --- system/core/Loader.php | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) (limited to 'system') diff --git a/system/core/Loader.php b/system/core/Loader.php index 07e572950..18fba2b72 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -94,11 +94,11 @@ class CI_Loader { protected $_ci_cached_vars = array(); /** - * stack of variables for nested _ci_load calls + * Stack of variable arrays to provide nested _ci_load calls all variables from parent calls * * @var array */ - protected $_ci_vars_stack = array(); + protected $_ci_load_vars_stack = array(); /** * List of loaded classes @@ -945,26 +945,38 @@ class CI_Loader { * * You can either set variables using the dedicated $this->load->vars() * function or via the second parameter of this function. We'll merge - * the two types. Additionally we merge them with the last variables - * from the nested call stack so that views that are embedded within - * other views can have access to these variables. + * the two types so that loaded views and files have access to these + * variables. + * Additionally we want all subsequent nested _ci_load() calls embedded + * within the current file to 'inherit' all variables that are + * accessible to the current file. For this purpose we push the current + * variable configuration (_ci_vars) to the stack and remove it again + * after the file or view is completely loaded. Nested _ci_load() calls + * within the current file extend the stack with their variable + * configuration. */ - // init current _ci_vars + // Init current _ci_vars as current variable configuration if (!is_array($_ci_vars)) { $_ci_vars = []; } - // merge with global cached vars (first call) or last state from nested - // call stack (subsequent nested calls) - if (!empty($this->_ci_vars_stack)) { - $_ci_vars = array_merge(end($this->_ci_vars_stack), $_ci_vars); - } else if (!empty($this->_ci_cached_vars)) { + // Include the global cached vars into the current _ci_vars if needed + if (!empty($this->_ci_cached_vars)) { $_ci_vars = array_merge($this->_ci_cached_vars, $_ci_vars); } - // push current _ci_vars state to stack and extract it - array_push($this->_ci_vars_stack, $_ci_vars); + // Merge the last variable configuration from a parent _ci_load() + // call into the current _ci_vars + if (!empty($this->_ci_load_vars_stack)) { + $previous_variable_configuration = end($this->_ci_load_vars_stack); + $_ci_vars = array_merge($previous_variable_configuration, $_ci_vars); + } + + // Push the current _ci_vars to the stack + array_push($this->_ci_load_vars_stack, $_ci_vars); + + // Extract the current _ci_vars extract($_ci_vars); /** @@ -983,8 +995,8 @@ class CI_Loader { include($_ci_path); // include() vs include_once() allows for multiple views with the same name log_message('info', 'File loaded: '.$_ci_path); - // remove current _ci_vars state from stack - array_pop($this->_ci_vars_stack); + // Remove current _ci_vars from stack again + array_pop($this->_ci_load_vars_stack); // Return the file data if requested if ($_ci_return === TRUE) -- cgit v1.2.3-24-g4f1b From f5255269f21e66481166518db2653dc3e8c46202 Mon Sep 17 00:00:00 2001 From: Christian Mohr Date: Thu, 15 Mar 2018 16:21:49 +0100 Subject: fixed styleguide violations --- system/core/Loader.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'system') diff --git a/system/core/Loader.php b/system/core/Loader.php index 18fba2b72..9a56b3857 100644 --- a/system/core/Loader.php +++ b/system/core/Loader.php @@ -957,18 +957,21 @@ class CI_Loader { */ // Init current _ci_vars as current variable configuration - if (!is_array($_ci_vars)) { + if ( ! is_array($_ci_vars)) + { $_ci_vars = []; } // Include the global cached vars into the current _ci_vars if needed - if (!empty($this->_ci_cached_vars)) { + if ( ! empty($this->_ci_cached_vars)) + { $_ci_vars = array_merge($this->_ci_cached_vars, $_ci_vars); } // Merge the last variable configuration from a parent _ci_load() // call into the current _ci_vars - if (!empty($this->_ci_load_vars_stack)) { + if ( ! empty($this->_ci_load_vars_stack)) + { $previous_variable_configuration = end($this->_ci_load_vars_stack); $_ci_vars = array_merge($previous_variable_configuration, $_ci_vars); } -- cgit v1.2.3-24-g4f1b