Skip to content

Commit 3384c31

Browse files
committed
Merge branch 'master' of github:JBZoo/Utils
2 parents 4daea3f + c8df69e commit 3384c31

30 files changed

+1189
-98
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
[*]
7+
end_of_line = lf
8+
charset = utf-8
9+
trim_trailing_whitespace = true
10+
insert_final_newline = true
11+
indent_style = space
12+
indent_size = 4

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ build/*
22
vendor/*
33
phpunit.xml
44
composer.lock
5+
6+
# phpstorm
7+
.idea

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ php:
66
- 5.5
77
- 5.6
88
- 7.0
9+
- hhvm
910

1011
matrix:
1112
fast_finish: true
13+
allow_failures:
14+
- php: hhvm
1215
include:
1316
- php: 5.6
1417
env: XDEBUG_OFF="yes"

README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ Cli::exec($command, $args = array(), $cwd = null, $verbose = false)
101101

102102
// Build params for cli
103103
Cli::build($command, $args = array())
104+
105+
// Returns true if STDOUT supports colorization.
106+
Cli::hasColorSupport()
107+
108+
// Returns the number of columns of the terminal.
109+
Cli::getNumberOfColumns()
110+
111+
// Returns if the file descriptor is an interactive terminal or not.
112+
Cli::isInteractive($fileDescriptor = self::STDOUT)
104113
```
105114

106115

@@ -529,6 +538,9 @@ Str::esc($string)
529538

530539
// Convert camel case to human readable format
531540
Str::splitCamelCase($input, $separator = '_', $toLower = true)
541+
542+
// Generates a universally unique identifier (UUID v4) according to RFC 4122
543+
Str::uuid()
532544
```
533545

534546

@@ -660,6 +672,64 @@ Email::getDomain(array('[email protected]', '[email protected]'));
660672
Email::getDomainSorted(array('[email protected]', '[email protected]'));
661673
```
662674

675+
## Timer
676+
```php
677+
use JBZoo\Utils\Timer;
678+
679+
// Formats the elapsed time as a string.
680+
Timer::format($time);
681+
Timer::formatMS($time); // Only ms format
682+
683+
// Formats the elapsed time since the start of the request as a string.
684+
Timer::timeSinceStart();
685+
686+
// Get request time
687+
Timer::getRequestTime();
688+
```
689+
690+
691+
## Enviroment
692+
```php
693+
use JBZoo\Utils\Env;
694+
695+
// Returns an environment variable.
696+
Env::get($name, $options = self::VAR_STRING);
697+
698+
// Converts the type of values like "true", "false", "null" or "123".
699+
Env::convert($value, $options = self::VAR_STRING);
700+
701+
// Returns true when Xdebug is supported or the runtime used is PHPDBG (PHP >= 7.0).
702+
Env::canCollectCodeCoverage();
703+
704+
// Returns the path to the binary of the current runtime. Appends ' --php' to the path when the runtime is HHVM.
705+
Env::getBinary();
706+
707+
// PHP Name and version
708+
Env::getNameWithVersion();
709+
710+
// Get PHP Name
711+
Env::getName();
712+
713+
// Get PHP Version
714+
Env::getVersion();
715+
716+
// Returns true when the runtime used is PHP and Xdebug is loaded.
717+
Env::hasXdebug();
718+
719+
// Returns true when the runtime used is HHVM.
720+
Env::isHHVM();
721+
722+
// Returns true when the runtime used is PHP without the PHPDBG SAPI.
723+
Env::isPHP();
724+
725+
// Returns true when the runtime used is PHP with the PHPDBG SAPI.
726+
Env::isPHPDBG();
727+
728+
// Returns true when the runtime used is PHP with the PHPDBG SAPI and the phpdbg_*_oplog() functions are available (PHP >= 7.0).
729+
Env::hasPHPDBGCodeCoverage();
730+
```
731+
732+
663733
## Links (ideas and some functions)
664734
* utilphp - https://github.com/brandonwamboldt/utilphp
665735
* PHPBinString - https://github.com/Grandt/PHPBinString
@@ -671,6 +741,9 @@ Email::getDomainSorted(array('[email protected]', '[email protected]'));
671741
* http://shiflett.org/blog/2006/mar/server-name-versus-http-host
672742
* https://github.com/joomla-framework/string
673743
* Askar (ARACOOL) https://github.com/ARACOOOL
744+
* Sebastian Bergmann https://github.com/sebastianbergmann/php-timer
745+
* Sebastian Bergmann https://github.com/sebastianbergmann/environment
746+
* Oscar Otero https://github.com/oscarotero/env
674747

675748

676749
## Unit tests and check code style

composer.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
"description" : "A collection of useful PHP functions, mini classes and snippets that you need every day.",
55
"license" : "MIT",
66
"minimum-stability" : "stable",
7-
"keywords" : ["php", "helpers", "helper", "utility", "utils", "collection"],
7+
"keywords" : [
8+
"helpers", "helper", "utility", "utils", "collection", "environment", "image", "filesystem",
9+
"array", "cli", "command line", "dates", "email", "env", "filter", "http", "url", "serialize",
10+
"slugif", "string", "mbstring", "timer", "misc"
11+
],
812
"authors" : [
913
{
1014
"name" : "SmetDenis",
@@ -24,14 +28,17 @@
2428
"php" : ">=5.3.10"
2529
},
2630
"require-dev" : {
27-
"jbzoo/phpunit" : "^1.2"
31+
"jbzoo/phpunit" : "^1.5"
2832
},
2933
"suggest" : {
3034
"ext-mbstring" : "Provides multibyte specific string functions",
3135
"symfony/process" : "For Cli::exec() method only",
3236
"symfony/polyfill-mbstring" : "For UTF-8 if ext-mbstring disabled"
3337
},
3438
"autoload" : {
39+
"files" : [
40+
"src/defines.php"
41+
],
3542
"psr-4" : {
3643
"JBZoo\\Utils\\" : "src"
3744
}

phpunit.xml.dist

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
JBZoo Utils
4+
5+
This file is part of the JBZoo CCK package.
6+
For the full copyright and license information, please view the LICENSE
7+
file that was distributed with this source code.
8+
9+
@package Utils
10+
@license MIT
11+
@copyright Copyright (C) JBZoo.com, All rights reserved.
12+
@link https://github.com/JBZoo/Utils
13+
-->
214
<phpunit bootstrap="tests/autoload.php"
315
colors="true"
416
convertErrorsToExceptions="true"
@@ -29,7 +41,7 @@
2941
<logging>
3042
<log type="coverage-clover" target="build/logs/clover.xml"/>
3143
<log type="coverage-html" target="build/phpunit" lowUpperBound="80" highLowerBound="95"/>
32-
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false" showOnlySummary="true"/>
44+
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true" showOnlySummary="false"/>
3345
</logging>
3446

3547
</phpunit>

src/Cli.php

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* @license MIT
1111
* @copyright Copyright (C) JBZoo.com, All rights reserved.
1212
* @link https://github.com/JBZoo/Utils
13+
* @author Sebastian Bergmann <[email protected]>
1314
* @author Denis Smetannikov <[email protected]>
1415
*/
1516

@@ -24,6 +25,10 @@
2425
*/
2526
class Cli
2627
{
28+
const STDIN = 0;
29+
const STDOUT = 1;
30+
const STDERR = 2;
31+
2732
/**
2833
* Is command line
2934
*
@@ -88,7 +93,7 @@ public static function err($message, $addEol = true)
8893
public static function exec($command, $args = array(), $cwd = null, $verbose = false)
8994
{
9095
if (!class_exists('\Symfony\Component\Process\Process')) {
91-
throw new \Exception("Symfony/Process package required for Cli::exec() method");
96+
throw new \Exception("Symfony/Process package required for Cli::exec() method"); // @codeCoverageIgnore
9297
}
9398

9499
$cmd = self::build($command, $args);
@@ -147,7 +152,7 @@ public static function build($command, $args = array())
147152
}
148153
}
149154

150-
if ($value) {
155+
if (strlen($value) > 0) {
151156
$stringArgs[] = $key . '="' . addcslashes($value, '"') . '"';
152157
} else {
153158
$stringArgs[] = $key;
@@ -161,4 +166,102 @@ public static function build($command, $args = array())
161166

162167
return $realCommand;
163168
}
169+
170+
/**
171+
* Returns true if STDOUT supports colorization.
172+
*
173+
* This code has been copied and adapted from
174+
* Symfony\Component\Console\Output\OutputStream.
175+
*
176+
* @return bool
177+
* @codeCoverageIgnore
178+
*/
179+
public static function hasColorSupport()
180+
{
181+
if (DIRECTORY_SEPARATOR == '\\') {
182+
183+
$winColor = Env::get('ANSICON', Env::VAR_BOOL)
184+
|| 'ON' === Env::get('ConEmuANSI')
185+
|| 'xterm' === Env::get('TERM');
186+
187+
return $winColor;
188+
}
189+
190+
if (!defined('STDOUT')) {
191+
return false;
192+
}
193+
194+
return self::isInteractive(STDOUT);
195+
}
196+
197+
/**
198+
* Returns the number of columns of the terminal.
199+
*
200+
* @return int
201+
* @codeCoverageIgnore
202+
*/
203+
public static function getNumberOfColumns()
204+
{
205+
if (DIRECTORY_SEPARATOR == '\\') {
206+
$columns = 80;
207+
208+
if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) {
209+
$columns = $matches[1];
210+
211+
} elseif (function_exists('proc_open')) {
212+
$process = proc_open(
213+
'mode CON',
214+
array(
215+
1 => array('pipe', 'w'),
216+
2 => array('pipe', 'w'),
217+
),
218+
$pipes,
219+
null,
220+
null,
221+
array('suppress_errors' => true)
222+
);
223+
224+
if (is_resource($process)) {
225+
$info = stream_get_contents($pipes[1]);
226+
fclose($pipes[1]);
227+
fclose($pipes[2]);
228+
proc_close($process);
229+
if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
230+
$columns = $matches[2];
231+
}
232+
}
233+
}
234+
235+
return $columns - 1;
236+
}
237+
238+
if (!self::isInteractive(self::STDIN)) {
239+
return 80;
240+
}
241+
242+
if (preg_match('#\d+ (\d+)#', shell_exec('stty size'), $match) === 1) {
243+
if ((int)$match[1] > 0) {
244+
return (int)$match[1];
245+
}
246+
}
247+
248+
if (preg_match('#columns = (\d+);#', shell_exec('stty'), $match) === 1) {
249+
if ((int)$match[1] > 0) {
250+
return (int)$match[1];
251+
}
252+
}
253+
254+
return 80;
255+
}
256+
257+
/**
258+
* Returns if the file descriptor is an interactive terminal or not.
259+
*
260+
* @param int|resource $fileDescriptor
261+
* @return bool
262+
*/
263+
public static function isInteractive($fileDescriptor = self::STDOUT)
264+
{
265+
return function_exists('posix_isatty') && @posix_isatty($fileDescriptor);
266+
}
164267
}

src/Dates.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use \DateTimeZone;
2020

2121
/**
22-
* Class Time
22+
* Class Dates
2323
* @package JBZoo\Utils
2424
*/
2525
class Dates

0 commit comments

Comments
 (0)