From f964b16f3db95d655420dfae2012ee9fbb98a1a8 Mon Sep 17 00:00:00 2001 From: Andrey Andreev Date: Tue, 12 Nov 2013 17:04:55 +0200 Subject: Deprecate CI_Input::is_cli_request() and add common function is_cli() to replace it Calls to this function are often needed before the Input library is available --- system/core/Common.php | 18 ++++++++++++++++ system/core/Input.php | 7 ++++--- system/core/URI.php | 19 +---------------- system/libraries/Session/Session.php | 8 ++++---- tests/mocks/core/common.php | 9 ++++++++ tests/mocks/core/uri.php | 5 ----- user_guide_src/source/changelog.rst | 2 ++ user_guide_src/source/general/cli.rst | 2 +- user_guide_src/source/general/common_functions.rst | 13 ++++++++++++ user_guide_src/source/helpers/date_helper.rst | 2 +- user_guide_src/source/installation/upgrade_300.rst | 24 ++++++++++++++++++++++ user_guide_src/source/libraries/input.rst | 3 +++ 12 files changed, 80 insertions(+), 32 deletions(-) diff --git a/system/core/Common.php b/system/core/Common.php index 56008efe8..c008bd571 100644 --- a/system/core/Common.php +++ b/system/core/Common.php @@ -355,6 +355,24 @@ if ( ! function_exists('is_https')) // ------------------------------------------------------------------------ +if ( ! function_exists('is_cli')) +{ + + /** + * Is CLI? + * + * Test to see if a request was made from the command line. + * + * @return bool + */ + function is_cli() + { + return (PHP_SAPI === 'cli' OR defined('STDIN')); + } +} + +// ------------------------------------------------------------------------ + if ( ! function_exists('show_error')) { /** diff --git a/system/core/Input.php b/system/core/Input.php index 8c32e459e..384e30d43 100644 --- a/system/core/Input.php +++ b/system/core/Input.php @@ -708,7 +708,7 @@ class CI_Input { $_SERVER['PHP_SELF'] = strip_tags($_SERVER['PHP_SELF']); // CSRF Protection check - if ($this->_enable_csrf === TRUE && ! $this->is_cli_request()) + if ($this->_enable_csrf === TRUE && ! is_cli()) { $this->security->csrf_verify(); } @@ -901,11 +901,12 @@ class CI_Input { * * Test to see if a request was made from the command line. * - * @return bool + * @deprecated 3.0.0 Use is_cli() instead + * @return bool */ public function is_cli_request() { - return (PHP_SAPI === 'cli' OR defined('STDIN')); + return is_cli(); } // -------------------------------------------------------------------- diff --git a/system/core/URI.php b/system/core/URI.php index bc086d223..bad9985d7 100644 --- a/system/core/URI.php +++ b/system/core/URI.php @@ -99,7 +99,7 @@ class CI_URI { if ($protocol === 'AUTO') { // Is the request coming from the command line? - if ($this->_is_cli_request()) + if (is_cli()) { $this->_set_uri_string($this->_parse_argv()); return; @@ -279,23 +279,6 @@ class CI_URI { // -------------------------------------------------------------------- - /** - * Is CLI Request? - * - * Duplicate of method from the Input class to test to see if - * a request was made from the command line. - * - * @see CI_Input::is_cli_request() - * @used-by CI_URI::_fetch_uri_string() - * @return bool - */ - protected function _is_cli_request() - { - return (PHP_SAPI === 'cli') OR defined('STDIN'); - } - - // -------------------------------------------------------------------- - /** * Parse CLI arguments * diff --git a/system/libraries/Session/Session.php b/system/libraries/Session/Session.php index 659a0269e..19de97994 100644 --- a/system/libraries/Session/Session.php +++ b/system/libraries/Session/Session.php @@ -102,10 +102,10 @@ class CI_Session extends CI_Driver_Library { */ public function __construct(array $params = array()) { - $CI =& get_instance(); + $_config =& get_instance()->config; // No sessions under CLI - if ($CI->input->is_cli_request()) + if (is_cli()) { return; } @@ -113,7 +113,7 @@ class CI_Session extends CI_Driver_Library { log_message('debug', 'CI_Session Class Initialized'); // Add possible extra entries to our valid drivers list - $drivers = isset($params['sess_valid_drivers']) ? $params['sess_valid_drivers'] : $CI->config->item('sess_valid_drivers'); + $drivers = isset($params['sess_valid_drivers']) ? $params['sess_valid_drivers'] : $_config->item('sess_valid_drivers'); if ( ! empty($drivers)) { $drivers = array_map('strtolower', (array) $drivers); @@ -121,7 +121,7 @@ class CI_Session extends CI_Driver_Library { } // Get driver to load - $driver = isset($params['sess_driver']) ? $params['sess_driver'] : $CI->config->item('sess_driver'); + $driver = isset($params['sess_driver']) ? $params['sess_driver'] : $_config->item('sess_driver'); if ( ! $driver) { log_message('debug', "Session: No driver name is configured, defaulting to 'cookie'."); diff --git a/tests/mocks/core/common.php b/tests/mocks/core/common.php index e5dc29c86..b073f230d 100644 --- a/tests/mocks/core/common.php +++ b/tests/mocks/core/common.php @@ -190,4 +190,13 @@ if ( ! function_exists('set_status_header')) { return TRUE; } +} + +if ( ! function_exists('is_cli')) +{ + // In order to test HTTP functionality, we need to lie about this + function is_cli() + { + return FALSE; + } } \ No newline at end of file diff --git a/tests/mocks/core/uri.php b/tests/mocks/core/uri.php index 94f75df64..11078587b 100644 --- a/tests/mocks/core/uri.php +++ b/tests/mocks/core/uri.php @@ -18,9 +18,4 @@ class Mock_Core_URI extends CI_URI { } - protected function _is_cli_request() - { - return FALSE; - } - } \ No newline at end of file diff --git a/user_guide_src/source/changelog.rst b/user_guide_src/source/changelog.rst index 5fc86b1b5..23fd95893 100644 --- a/user_guide_src/source/changelog.rst +++ b/user_guide_src/source/changelog.rst @@ -396,6 +396,7 @@ Release Date: Not Released - Added method ``post_get()`` and changed ``get_post()`` to search in GET data first. Both methods' names now properly match their GET/POST data search priorities. - Changed method ``_fetch_from_array()`` to parse array notation in field name. - Added an option for ``_clean_input_keys()`` to return FALSE instead of terminating the whole script. + - Deprecated the ``is_cli_request()`` method, it is now an alias for the new :php:func:`is_cli()` common function. - :doc:`Common functions ` changes include: @@ -404,6 +405,7 @@ Release Date: Not Released - Removed redundant conditional to determine HTTP server protocol in :php:func:`set_status_header()`. - Changed ``_exception_handler()`` to respect php.ini *display_errors* setting. - Added function :php:func:`is_https()` to check if a secure connection is used. + - Added function :php:func:`is_cli()` to replace the ``CI_Input::is_cli_request()`` method. - Added function :php:func:`function_usable()` to check if a function exists and is not disabled by `Suhosin `. - Removed the third (`$php_error`) from function :php:func:`log_message()`. diff --git a/user_guide_src/source/general/cli.rst b/user_guide_src/source/general/cli.rst index 4145d5ccc..4f3b07d9e 100644 --- a/user_guide_src/source/general/cli.rst +++ b/user_guide_src/source/general/cli.rst @@ -23,7 +23,7 @@ but they are not always obvious. - Run your cron-jobs without needing to use *wget* or *curl* - Make your cron-jobs inaccessible from being loaded in the URL by - checking for ``$this->input->is_cli_request()`` + checking the return value of :func:`is_cli()`. - Make interactive "tasks" that can do things like set permissions, prune cache folders, run backups, etc. - Integrate with other applications in other languages. For example, a diff --git a/user_guide_src/source/general/common_functions.rst b/user_guide_src/source/general/common_functions.rst index e085ef808..2dfec9cc0 100644 --- a/user_guide_src/source/general/common_functions.rst +++ b/user_guide_src/source/general/common_functions.rst @@ -173,6 +173,19 @@ is_https() Returns TRUE if a secure (HTTPS) connection is used and FALSE in any other case (including non-HTTP requests). +is_cli() +======== + +.. php:function:: is_cli() + + :returns: bool + +Returns TRUE if the application is run through the command line +and FALSE if not. + +.. note:: This function checks both if the ``PHP_SAPI`` value is 'cli' + or if the ``STDIN`` constant is defined. + function_usable() ================= diff --git a/user_guide_src/source/helpers/date_helper.rst b/user_guide_src/source/helpers/date_helper.rst index 5dfee8b48..8126eba6c 100644 --- a/user_guide_src/source/helpers/date_helper.rst +++ b/user_guide_src/source/helpers/date_helper.rst @@ -80,7 +80,7 @@ Example:: $time = time(); echo standard_date($format, $time); -.. note:: This function is DEPRECATED.Use the native ``date()`` combined with +.. note:: This function is DEPRECATED. Use the native ``date()`` combined with `DateTime's format constants `_ instead: diff --git a/user_guide_src/source/installation/upgrade_300.rst b/user_guide_src/source/installation/upgrade_300.rst index e8fdd0b15..8f9cc1bdc 100644 --- a/user_guide_src/source/installation/upgrade_300.rst +++ b/user_guide_src/source/installation/upgrade_300.rst @@ -457,4 +457,28 @@ then you can now just access the properties instead:: $this->router->method; .. note:: Those methods are still available, but you're strongly encouraged to remove their usage + sooner rather than later. + +Input library method is_cli_request() +===================================== + +Calls to the ``CI_Input::is_cli_request()`` method are necessary at many places +in the CodeIgniter internals and this is often before the :doc:`Input Library +<../libraries/input>` is loaded. Because of that, it is being replaced by a common +function named :php:func:`is_cli()` and this method is now just an alias. + +The new function is both available at all times for you to use and shorter to type. + +:: + + // Old + $this->input->is_cli_request(); + + // New + is_cli(); + +``CI_Input::is_cli_request()`` is now now deprecated and scheduled for removal in +CodeIgniter 3.1+. + +.. note:: This method is still available, but you're strongly encouraged to remove its usage sooner rather than later. \ No newline at end of file diff --git a/user_guide_src/source/libraries/input.rst b/user_guide_src/source/libraries/input.rst index fb245d7cd..b58ed2f0d 100644 --- a/user_guide_src/source/libraries/input.rst +++ b/user_guide_src/source/libraries/input.rst @@ -315,6 +315,9 @@ see if PHP is being run on the command line. $this->input->is_cli_request() +.. note:: This method is DEPRECATED and is now just an alias for the + :php:func:`is_cli()` function. + $this->input->method() ====================== -- cgit v1.2.3-24-g4f1b