From ea919ad8b276c78207ec33d1fc34f1f0ef15bc0d Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Sat, 13 Mar 2010 11:19:28 +0100 Subject: [feature/request-class] Refactored request class and wrapper functions. The request class - now makes use of the new type cast helper (dependency injection) - has no static methods anymore. - now has a constructor argument to leave super globals turned on Brought back the set_var function in functions.php. It is now a wrapper around the type cast helper. It creates an instance on the fly. The request_var wrapper function now has an optional last argument to inject the request class instance, rather than abusing the $var_name. PHPBB3-9716 --- phpBB/includes/functions.php | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'phpBB/includes/functions.php') diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index d5132d218e..81c5c40fd1 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -18,6 +18,13 @@ if (!defined('IN_PHPBB')) // Common global functions +function set_var(&$result, $var, $type, $multibyte = false) +{ + // no need for dependency injection here, if you have the object, call the method yourself! + $type_cast_helper = new phpbb_type_cast_helper(); + $type_cast_helper->set_var($result, $var, $type, $multibyte); +} + /** * Wrapper function of phpbb_request::variable which exists for backwards compatability. * See {@link phpbb_request_interface::variable phpbb_request_interface::variable} for @@ -40,30 +47,30 @@ if (!defined('IN_PHPBB')) * @return mixed The value of $_REQUEST[$var_name] run through {@link set_var set_var} to ensure that the type is the * the same as that of $default. If the variable is not set $default is returned. */ -function request_var($var_name, $default, $multibyte = false, $cookie = false) +function request_var($var_name, $default, $multibyte = false, $cookie = false, phpbb_request_interface $request = null) { // This is all just an ugly hack to add "Dependency Injection" to a function // the only real code is the function call which maps this function to a method. - static $request = null; + static $static_request = null; - if ($var_name instanceof phpbb_request_interface) + if ($request instanceof phpbb_request_interface) { - $request = $var_name; - return; + $static_request = $request; + + if (empty($var_name)) + { + return; + } } + $tmp_request = $static_request; + // no request class set, create a temporary one ourselves to keep backwards compatability - if ($request === null) + if ($tmp_request === null) { - $tmp_request = new phpbb_request(); - // enable super globals, so the magically created request class does not + // false param: enable super globals, so the created request class does not // make super globals inaccessible everywhere outside this function. - $tmp_request->enable_super_globals(); - } - else - { - // otherwise use the static injected instance - $tmp_request = $request; + $tmp_request = new phpbb_request(new phpbb_type_cast_helper(), false); } return $tmp_request->variable($var_name, $default, $multibyte, ($cookie) ? phpbb_request_interface::COOKIE : phpbb_request_interface::REQUEST); -- cgit v1.2.1