Skip to content

Commit 9c978b5

Browse files
authored
Merge pull request #6884 from paulbalandan/is-windows
Add `is_windows()` global function
2 parents 4efe890 + 30a6bb1 commit 9c978b5

File tree

8 files changed

+75
-12
lines changed

8 files changed

+75
-12
lines changed

system/CLI/CLI.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,12 @@ public static function wait(int $seconds, bool $countdown = false)
518518

519519
/**
520520
* if operating system === windows
521+
*
522+
* @deprecated v4.3 Use `is_windows()` instead
521523
*/
522524
public static function isWindows(): bool
523525
{
524-
return PHP_OS_FAMILY === 'Windows';
526+
return is_windows();
525527
}
526528

527529
/**
@@ -544,7 +546,7 @@ public static function clearScreen()
544546
{
545547
// Unix systems, and Windows with VT100 Terminal support (i.e. Win10)
546548
// can handle CSI sequences. For lower than Win10 we just shove in 40 new lines.
547-
static::isWindows() && ! static::streamSupports('sapi_windows_vt100_support', STDOUT)
549+
is_windows() && ! static::streamSupports('sapi_windows_vt100_support', STDOUT)
548550
? static::newLine(40)
549551
: static::fwrite(STDOUT, "\033[H\033[2J");
550552
}
@@ -691,7 +693,7 @@ public static function hasColorSupport($resource): bool
691693
return true;
692694
}
693695

694-
if (static::isWindows()) {
696+
if (is_windows()) {
695697
// @codeCoverageIgnoreStart
696698
return static::streamSupports('sapi_windows_vt100_support', $resource)
697699
|| isset($_SERVER['ANSICON'])
@@ -736,7 +738,7 @@ public static function getHeight(int $default = 32): int
736738
public static function generateDimensions()
737739
{
738740
try {
739-
if (static::isWindows()) {
741+
if (is_windows()) {
740742
// Shells such as `Cygwin` and `Git bash` returns incorrect values
741743
// when executing `mode CON`, so we use `tput` instead
742744
if (getenv('TERM') || (($shell = getenv('SHELL')) && preg_match('/(?:bash|zsh)(?:\.exe)?$/', $shell))) {

system/Common.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ function is_cli(): bool
691691
function is_really_writable(string $file): bool
692692
{
693693
// If we're on a Unix server we call is_writable
694-
if (DIRECTORY_SEPARATOR === '/') {
694+
if (! is_windows()) {
695695
return is_writable($file);
696696
}
697697

@@ -721,6 +721,26 @@ function is_really_writable(string $file): bool
721721
}
722722
}
723723

724+
if (! function_exists('is_windows')) {
725+
/**
726+
* Detect if platform is running in Windows.
727+
*/
728+
function is_windows(?bool $mock = null): bool
729+
{
730+
static $mocked;
731+
732+
if (func_num_args() === 1) {
733+
$mocked = $mock;
734+
}
735+
736+
if (isset($mocked)) {
737+
return $mocked;
738+
}
739+
740+
return DIRECTORY_SEPARATOR === '\\';
741+
}
742+
}
743+
724744
if (! function_exists('lang')) {
725745
/**
726746
* A convenience method to translate a string or array of them and format

tests/system/CLI/CLITest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ public function testPromptByMultipleKeys()
112112
PhpStreamWrapper::restore();
113113
}
114114

115-
public function testIsWindows()
116-
{
117-
$this->assertSame(('\\' === DIRECTORY_SEPARATOR), CLI::isWindows());
118-
$this->assertSame(defined('PHP_WINDOWS_VERSION_MAJOR'), CLI::isWindows());
119-
}
120-
121115
public function testNewLine()
122116
{
123117
$this->expectOutputString('');

tests/system/Commands/GeneratorsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testGenerateFileOverwritten()
6161

6262
public function testGenerateFileFailsOnUnwritableDirectory()
6363
{
64-
if ('\\' === DIRECTORY_SEPARATOR) {
64+
if (is_windows()) {
6565
$this->markTestSkipped('chmod does not work as expected on Windows');
6666
}
6767

tests/system/CommonFunctionsTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,4 +700,25 @@ public function testLangOnCLI()
700700

701701
$this->resetServices();
702702
}
703+
704+
public function testIsWindows()
705+
{
706+
$this->assertSame(strpos(php_uname(), 'Windows') !== false, is_windows());
707+
$this->assertSame(defined('PHP_WINDOWS_VERSION_MAJOR'), is_windows());
708+
}
709+
710+
public function testIsWindowsUsingMock()
711+
{
712+
is_windows(true);
713+
$this->assertTrue(is_windows());
714+
$this->assertNotFalse(is_windows());
715+
716+
is_windows(false);
717+
$this->assertFalse(is_windows());
718+
$this->assertNotTrue(is_windows());
719+
720+
is_windows(null);
721+
$this->assertSame(strpos(php_uname(), 'Windows') !== false, is_windows());
722+
$this->assertSame(defined('PHP_WINDOWS_VERSION_MAJOR'), is_windows());
723+
}
703724
}

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ Helpers and Functions
237237
- You can set the locale to :php:func:`route_to()` if you pass a locale value as the last parameter.
238238
- Added :php:func:`request()` and :php:func:`response()` functions.
239239
- Added :php:func:`decamelize()` function to convert camelCase to snake_case.
240+
- Added :php:func:`is_windows()` global function to detect Windows platforms.
240241

241242
Error Handling
242243
==============
@@ -298,6 +299,7 @@ Deprecations
298299
- ``CodeIgniter::$path`` and ``CodeIgniter::setPath()`` are deprecated. No longer used.
299300
- The public property ``IncomingRequest::$uri`` is deprecated. It will be protected. Use ``IncomingRequest::getUri()`` instead.
300301
- The public property ``IncomingRequest::$config`` is deprecated. It will be protected.
302+
- The method ``CLI::isWindows()`` is deprecated. Use ``is_windows()`` instead.
301303

302304
Bugs Fixed
303305
**********

user_guide_src/source/general/common_functions.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,19 @@ Miscellaneous Functions
281281
:returns: true if you can write to the file, false otherwise.
282282
:rtype: bool
283283

284+
.. php:function:: is_windows([$mock = null])
285+
286+
:param bool|null $mock: If given and is a boolean then it will be used as the return value.
287+
:rtype: bool
288+
289+
Detect if platform is running in Windows.
290+
291+
.. note:: The boolean value provided to $mock will persist in subsequent calls. To reset this
292+
mock value, the user must pass an explicit ``null`` to the function call. This will
293+
refresh the function to use auto-detection.
294+
295+
.. literalinclude:: common_functions/012.php
296+
284297
.. php:function:: log_message($level, $message [, $context])
285298
286299
:param string $level: The level of severity
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
is_windows(true);
4+
5+
// some code ...
6+
7+
if (is_windows()) {
8+
// do something ..
9+
}
10+
11+
is_windows(null); // reset

0 commit comments

Comments
 (0)